]> diplodocus.org Git - nmh/blob - uip/comp.c
6d088d872943b31649492abcc4e2b4af2b276c6d
[nmh] / uip / comp.c
1 /* comp.c -- compose a message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/showfile.h"
10 #include "sbr/smatch.h"
11 #include "sbr/refile.h"
12 #include "sbr/cpydata.h"
13 #include "sbr/m_draft.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/fdcompare.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_find.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/path.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
25 #include "h/utils.h"
26 #include "h/fmt_scan.h"
27 #include "h/done.h"
28 #include "sbr/m_maildir.h"
29 #include <fcntl.h>
30
31 #define COMP_SWITCHES \
32 X("draftfolder +folder", 0, DFOLDSW) \
33 X("draftmessage msg", 0, DMSGSW) \
34 X("nodraftfolder", 0, NDFLDSW) \
35 X("editor editor", 0, EDITRSW) \
36 X("noedit", 0, NEDITSW) \
37 X("file file", 0, FILESW) \
38 X("form formfile", 0, FORMSW) \
39 X("use", 0, USESW) \
40 X("nouse", 0, NUSESW) \
41 X("whatnowproc program", 0, WHATSW) \
42 X("nowhatnowproc", 0, NWHATSW) \
43 X("build", 5, BILDSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("to address", 0, TOSW) \
47 X("cc address", 0, CCSW) \
48 X("from address", 0, FROMSW) \
49 X("fcc mailbox", 0, FCCSW) \
50 X("width columns", 0, WIDTHSW) \
51 X("subject text", 0, SUBJECTSW) \
52
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(COMP);
55 #undef X
56
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(COMP, switches);
59 #undef X
60
61 #define DISPO_SWITCHES \
62 X("quit", 0, NOSW) \
63 X("replace", 0, YESW) \
64 X("use", 0, USELSW) \
65 X("list", 0, LISTDSW) \
66 X("refile +folder", 0, REFILSW) \
67 X("new", 0, NEWSW) \
68
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(DISPO);
71 #undef X
72
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(DISPO, aqrunl);
75 #undef X
76
77 static struct swit aqrul[] = {
78 { "quit", 0, NOSW },
79 { "replace", 0, YESW },
80 { "use", 0, USELSW },
81 { "list", 0, LISTDSW },
82 { "refile", 0, REFILSW },
83 { NULL, 0, 0 }
84 };
85
86 int
87 main (int argc, char **argv)
88 {
89 int use = NOUSE;
90 bool nedit = false;
91 bool nwhat = false;
92 bool build = false;
93 int i, in = NOTOK, isdf = 0, out, dat[5], format_len = 0;
94 int outputlinelen = OUTPUTLINELEN;
95 char *cp, *cwd, *maildir, *dfolder = NULL;
96 char *ed = NULL, *file = NULL, *form = NULL;
97 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
98 char *to = NULL, *from = NULL, *cc = NULL, *fcc = NULL, *dp;
99 char *subject = NULL;
100 char drft[BUFSIZ], **argp, **arguments;
101 struct msgs *mp = NULL;
102 struct format *fmt;
103 struct stat st;
104
105 if (nmh_init(argv[0], true, true)) { return 1; }
106
107 arguments = getarguments (invo_name, argc, argv, 1);
108 argp = arguments;
109
110 while ((cp = *argp++)) {
111 if (*cp == '-') {
112 switch (smatch (++cp, switches)) {
113 case AMBIGSW:
114 ambigsw (cp, switches);
115 done (1);
116 case UNKWNSW:
117 die("-%s unknown", cp);
118
119 case HELPSW:
120 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
121 invo_name);
122 print_help (buf, switches, 1);
123 done (0);
124 case VERSIONSW:
125 print_version(invo_name);
126 done (0);
127
128 case EDITRSW:
129 if (!(ed = *argp++) || *ed == '-')
130 die("missing argument to %s", argp[-2]);
131 nedit = false;
132 continue;
133 case NEDITSW:
134 nedit = true;
135 continue;
136
137 case WHATSW:
138 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
139 die("missing argument to %s", argp[-2]);
140 nwhat = false;
141 continue;
142
143 case BILDSW:
144 build = true;
145 /* FALLTHRU */
146 case NWHATSW:
147 nwhat = true;
148 continue;
149
150 case FORMSW:
151 if (!(form = *argp++) || *form == '-')
152 die("missing argument to %s", argp[-2]);
153 continue;
154
155 case USESW:
156 use++;
157 continue;
158 case NUSESW:
159 use = NOUSE;
160 continue;
161
162 case FILESW: /* compatibility */
163 if (file)
164 die("only one file at a time!");
165 if (!(file = *argp++) || *file == '-')
166 die("missing argument to %s", argp[-2]);
167 isdf = NOTOK;
168 continue;
169
170 case DFOLDSW:
171 if (dfolder)
172 die("only one draft folder at a time!");
173 if (!(cp = *argp++) || *cp == '-')
174 die("missing argument to %s", argp[-2]);
175 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
176 *cp != '@' ? TFOLDER : TSUBCWF);
177 continue;
178 case DMSGSW:
179 if (file)
180 die("only one draft message at a time!");
181 if (!(file = *argp++) || *file == '-')
182 die("missing argument to %s", argp[-2]);
183 continue;
184 case NDFLDSW:
185 dfolder = NULL;
186 isdf = NOTOK;
187 continue;
188
189 case TOSW:
190 if (!(cp = *argp++) || *cp == '-')
191 die("missing argument to %s", argp[-2]);
192 to = addlist(to, cp);
193 continue;
194
195 case CCSW:
196 if (!(cp = *argp++) || *cp == '-')
197 die("missing argument to %s", argp[-2]);
198 cc = addlist(cc, cp);
199 continue;
200
201 case FROMSW:
202 if (!(cp = *argp++) || *cp == '-')
203 die("missing argument to %s", argp[-2]);
204 from = addlist(from, cp);
205 continue;
206
207 case FCCSW:
208 if (!(cp = *argp++) || *cp == '-')
209 die("missing argument to %s", argp[-2]);
210 dp = NULL;
211 if (*cp == '@')
212 cp = dp = path(cp + 1, TSUBCWF);
213 fcc = addlist(fcc, cp);
214 free(dp);
215 continue;
216
217 case WIDTHSW:
218 if (!(cp = *argp++) || *cp == '-')
219 die("missing argument to %s", argp[-2]);
220 if ((outputlinelen = atoi(cp)) < 10)
221 die("impossible width %d", outputlinelen);
222 continue;
223
224 case SUBJECTSW:
225 if (!(cp = *argp++) || *cp == '-')
226 die("missing argument to %s", argp[-2]);
227 subject = cp;
228 continue;
229 }
230 }
231 if (*cp == '+' || *cp == '@') {
232 if (folder)
233 die("only one folder at a time!");
234 folder = pluspath (cp);
235 } else {
236 if (msg)
237 die("only one message at a time!");
238 msg = cp;
239 }
240 }
241
242 cwd = mh_xstrdup(pwd ());
243
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER));
246
247 /*
248 * Check if we are using a draft folder
249 * and have specified a message in it.
250 */
251 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
252 file = msg;
253 msg = NULL;
254 }
255 if (form && (folder || msg))
256 die("can't mix forms and folders/msgs");
257
258 cp = NULL;
259
260 if (folder || msg) {
261 /*
262 * Use a message as the "form" for the new message.
263 */
264 if (!msg)
265 msg = "cur";
266 if (!folder)
267 folder = getfolder (1);
268 maildir = m_maildir (folder);
269
270 if (chdir (maildir) == NOTOK)
271 adios (maildir, "unable to change directory to");
272
273 /* read folder and create message structure */
274 if (!(mp = folder_read (folder, 1)))
275 die("unable to read folder %s", folder);
276
277 /* check for empty folder */
278 if (mp->nummsg == 0)
279 die("no messages in %s", folder);
280
281 /* parse the message range/sequence/name and set SELECTED */
282 if (!m_convert (mp, msg))
283 done (1);
284 seq_setprev (mp); /* set the previous-sequence */
285 seq_save (mp);
286
287 if (mp->numsel > 1)
288 die("only one message at a time!");
289
290 if ((in = open (form = mh_xstrdup(m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
291 adios (form, "unable to open message");
292 } else {
293 struct comp *cptr;
294
295 if (! form)
296 form = components;
297
298 cp = new_fs(form, NULL, NULL);
299 format_len = strlen(cp);
300 fmt_compile(cp, &fmt, 1);
301
302 /*
303 * Set up any components that were fed to us on the command line
304 */
305
306 if (from) {
307 cptr = fmt_findcomp("from");
308 if (cptr)
309 cptr->c_text = from;
310 }
311 if (to) {
312 cptr = fmt_findcomp("to");
313 if (cptr)
314 cptr->c_text = to;
315 }
316 if (cc) {
317 cptr = fmt_findcomp("cc");
318 if (cptr)
319 cptr->c_text = cc;
320 }
321 if (fcc) {
322 cptr = fmt_findcomp("fcc");
323 if (cptr)
324 cptr->c_text = fcc;
325 }
326 if (subject) {
327 cptr = fmt_findcomp("subject");
328 if (cptr)
329 cptr->c_text = subject;
330 }
331 }
332
333 try_it_again:
334 strncpy (drft, build ? m_maildir ("draft")
335 : m_draft (dfolder, file, use, &isdf), sizeof(drft));
336
337 /*
338 * Check if we have an existing draft
339 */
340 if (!build && (out = open (drft, O_RDONLY)) != NOTOK) {
341 i = fdcompare (in, out);
342 close (out);
343
344 /*
345 * If we have given -use flag, or if the
346 * draft is just the same as the components
347 * file, then no need to ask any questions.
348 */
349 if (use || i)
350 goto edit_it;
351
352 if (stat (drft, &st) == NOTOK)
353 adios (drft, "unable to stat");
354 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
355 for (i = LISTDSW; i != YESW;) {
356 if (!(argp = read_switch_multiword ("\nDisposition? ",
357 isdf ? aqrunl : aqrul)))
358 done (1);
359 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
360 case NOSW:
361 done (0);
362 case NEWSW:
363 file = NULL;
364 use = NOUSE;
365 goto try_it_again;
366 case YESW:
367 break;
368 case USELSW:
369 use++;
370 goto edit_it;
371 case LISTDSW:
372 showfile (++argp, drft);
373 break;
374 case REFILSW:
375 if (refile (++argp, drft) == 0)
376 i = YESW;
377 break;
378 default:
379 inform("say what?");
380 break;
381 }
382 }
383 } else {
384 if (use)
385 adios (drft, "unable to open");
386 }
387
388 if ((out = creat (drft, m_gmprot ())) == NOTOK)
389 adios (drft, "unable to create");
390 if (cp) {
391 charstring_t scanl;
392
393 i = format_len + 1024;
394 scanl = charstring_create (i + 2);
395 dat[0] = 0;
396 dat[1] = 0;
397 dat[2] = 0;
398 dat[3] = outputlinelen;
399 dat[4] = 0;
400 fmt_scan(fmt, scanl, i, dat, NULL);
401 if (write(out, charstring_buffer (scanl),
402 charstring_bytes (scanl)) < 0) {
403 advise (drft, "write");
404 }
405 charstring_free(scanl);
406 } else {
407 cpydata (in, out, form, drft);
408 close (in);
409 }
410 close (out);
411
412 edit_it:
413 context_save (); /* save the context file */
414
415 if (nwhat)
416 done (0);
417 what_now (ed, nedit, use, drft, NULL, 0, NULL, NULL, 0, cwd, 0);
418 done (1);
419 return 1;
420 }