]> diplodocus.org Git - nmh/blob - uip/repl.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / repl.c
1 /* repl.c -- reply to 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 <h/mime.h>
10 #include <h/utils.h>
11 #include "sbr/m_maildir.h"
12
13 #define REPL_SWITCHES \
14 X("group", 0, GROUPSW) \
15 X("nogroup", 0, NGROUPSW) \
16 X("annotate", 0, ANNOSW) \
17 X("noannotate", 0, NANNOSW) \
18 X("cc all|to|cc|me", 0, CCSW) \
19 X("nocc all|to|cc|me", 0, NCCSW) \
20 X("draftfolder +folder", 0, DFOLDSW) \
21 X("draftmessage msg", 0, DMSGSW) \
22 X("nodraftfolder", 0, NDFLDSW) \
23 X("editor editor", 0, EDITRSW) \
24 X("noedit", 0, NEDITSW) \
25 X("convertargs type argstring", 0, CONVERTARGSW) \
26 X("fcc folder", 0, FCCSW) \
27 X("filter filterfile", 0, FILTSW) \
28 X("form formfile", 0, FORMSW) \
29 X("format", 5, FRMTSW) \
30 X("noformat", 7, NFRMTSW) \
31 X("inplace", 0, INPLSW) \
32 X("noinplace", 0, NINPLSW) \
33 X("mime", 0, MIMESW) \
34 X("nomime", 0, NMIMESW) \
35 X("query", 0, QURYSW) \
36 X("noquery", 0, NQURYSW) \
37 X("whatnowproc program", 0, WHATSW) \
38 X("nowhatnowproc", 0, NWHATSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("file file", 4, FILESW) \
43 X("build", 5, BILDSW) /* interface from mhe */ \
44 X("atfile", 0, ATFILESW) \
45 X("noatfile", 0, NOATFILESW) \
46 X("fmtproc program", 0, FMTPROCSW) \
47 X("nofmtproc", 0, NFMTPROCSW) \
48
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(REPL);
51 #undef X
52
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(REPL, switches);
55 #undef X
56
57 #define CC_SWITCHES \
58 X("to", 0, CTOSW) \
59 X("cc", 0, CCCSW) \
60 X("me", 0, CMESW) \
61 X("all", 0, CALSW) \
62
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(CC);
65 #undef X
66
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(CC, ccswitches);
69 #undef X
70
71 #define DISPO_SWITCHES \
72 X("quit", 0, NOSW) \
73 X("replace", 0, YESW) \
74 X("list", 0, LISTDSW) \
75 X("refile +folder", 0, REFILSW) \
76 X("new", 0, NEWSW) \
77
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(DISPO);
80 #undef X
81
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
84 #undef X
85
86 static struct swit aqrl[] = {
87 { "quit", 0, NOSW },
88 { "replace", 0, YESW },
89 { "list", 0, LISTDSW },
90 { "refile +folder", 0, REFILSW },
91 { NULL, 0, 0 }
92 };
93
94 short ccto = -1; /* global for replsbr */
95 short cccc = -1;
96 short ccme = -1;
97 short querysw = 0;
98
99 static short outputlinelen = OUTPUTLINELEN;
100 static short groupreply = 0; /* Is this a group reply? */
101
102 static int mime = 0; /* include original as MIME part */
103 static char *form = NULL; /* form (components) file */
104 static char *filter = NULL; /* message filter file */
105 static char *fcc = NULL; /* folders to add to Fcc: header */
106
107
108 /*
109 * prototypes
110 */
111 static void docc (char *, int);
112 static void add_convert_header (const char *, char *, char *, char *);
113
114
115 int
116 main (int argc, char **argv)
117 {
118 int i, isdf = 0;
119 int anot = 0, inplace = 1;
120 int nedit = 0, nwhat = 0;
121 int atfile = 0;
122 int fmtproc = -1;
123 char *cp, *cwd, *dp, *maildir, *file = NULL;
124 char *folder = NULL, *msg = NULL, *dfolder = NULL;
125 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
126 char **argp, **arguments;
127 svector_t convert_types = svector_create (10);
128 svector_t convert_args = svector_create (10);
129 size_t n;
130 struct msgs *mp = NULL;
131 struct stat st;
132 FILE *in;
133
134 int buildsw = 0;
135
136 if (nmh_init(argv[0], 1)) { return 1; }
137
138 arguments = getarguments (invo_name, argc, argv, 1);
139 argp = arguments;
140
141 while ((cp = *argp++)) {
142 if (*cp == '-') {
143 switch (smatch (++cp, switches)) {
144 case AMBIGSW:
145 ambigsw (cp, switches);
146 done (1);
147 case UNKWNSW:
148 adios (NULL, "-%s unknown", cp);
149
150 case HELPSW:
151 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
152 invo_name);
153 print_help (buf, switches, 1);
154 done (0);
155 case VERSIONSW:
156 print_version(invo_name);
157 done (0);
158
159 case GROUPSW:
160 groupreply++;
161 continue;
162 case NGROUPSW:
163 groupreply = 0;
164 continue;
165
166 case ANNOSW:
167 anot++;
168 continue;
169 case NANNOSW:
170 anot = 0;
171 continue;
172
173 case CCSW:
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 docc (cp, 1);
177 continue;
178 case NCCSW:
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 docc (cp, 0);
182 continue;
183
184 case EDITRSW:
185 if (!(ed = *argp++) || *ed == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 nedit = 0;
188 continue;
189 case NEDITSW:
190 nedit++;
191 continue;
192
193 case CONVERTARGSW: {
194 char *type;
195 size_t i;
196
197 if (!(type = *argp++)) {
198 adios (NULL, "missing type argument to %s", argp[-2]);
199 }
200 if (!(cp = *argp++)) {
201 adios (NULL, "missing argstring argument to %s",
202 argp[-3]);
203 }
204
205 for (i = 0; i < svector_size (convert_types); ++i) {
206 if (! strcmp (svector_at (convert_types, i), type)) {
207 /* Already saw this type, so just update
208 its args. */
209 svector_strs (convert_args)[i] = cp;
210 break;
211 }
212 }
213
214 if (i == svector_size (convert_types)) {
215 svector_push_back (convert_types, type);
216 svector_push_back (convert_args, cp);
217 }
218 continue;
219 }
220
221 case WHATSW:
222 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
223 adios (NULL, "missing argument to %s", argp[-2]);
224 nwhat = 0;
225 continue;
226 case BILDSW:
227 buildsw++;
228 /* FALLTHRU */
229 case NWHATSW:
230 nwhat++;
231 continue;
232
233 case FCCSW:
234 if (!(cp = *argp++) || *cp == '-')
235 adios (NULL, "missing argument to %s", argp[-2]);
236 dp = NULL;
237 if (*cp == '@')
238 cp = dp = path (cp + 1, TSUBCWF);
239 if (fcc)
240 fcc = add (", ", fcc);
241 fcc = add (cp, fcc);
242 free(dp);
243 continue;
244
245 case FILESW:
246 if (file)
247 adios (NULL, "only one file at a time!");
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 file = path (cp, TFILE);
251 continue;
252 case FILTSW:
253 if (!(cp = *argp++) || *cp == '-')
254 adios (NULL, "missing argument to %s", argp[-2]);
255 filter = getcpy (etcpath (cp));
256 mime = 0;
257 continue;
258 case FORMSW:
259 if (!(form = *argp++) || *form == '-')
260 adios (NULL, "missing argument to %s", argp[-2]);
261 continue;
262
263 case FRMTSW:
264 filter = getcpy (etcpath (mhlreply));
265 mime = 0;
266 continue;
267 case NFRMTSW:
268 filter = NULL;
269 continue;
270
271 case INPLSW:
272 inplace++;
273 continue;
274 case NINPLSW:
275 inplace = 0;
276 continue;
277
278 case MIMESW:
279 mime++;
280 filter = NULL;
281 continue;
282 case NMIMESW:
283 mime = 0;
284 continue;
285
286 case QURYSW:
287 querysw++;
288 continue;
289 case NQURYSW:
290 querysw = 0;
291 continue;
292
293 case WIDTHSW:
294 if (!(cp = *argp++) || *cp == '-')
295 adios (NULL, "missing argument to %s", argp[-2]);
296 if ((outputlinelen = atoi (cp)) < 10)
297 adios (NULL, "impossible width %d", outputlinelen);
298 continue;
299
300 case DFOLDSW:
301 if (dfolder)
302 adios (NULL, "only one draft folder at a time!");
303 if (!(cp = *argp++) || *cp == '-')
304 adios (NULL, "missing argument to %s", argp[-2]);
305 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
306 *cp != '@' ? TFOLDER : TSUBCWF);
307 continue;
308 case DMSGSW:
309 if (dmsg)
310 adios (NULL, "only one draft message at a time!");
311 if (!(dmsg = *argp++) || *dmsg == '-')
312 adios (NULL, "missing argument to %s", argp[-2]);
313 continue;
314 case NDFLDSW:
315 dfolder = NULL;
316 isdf = NOTOK;
317 continue;
318
319 case ATFILESW:
320 atfile++;
321 continue;
322 case NOATFILESW:
323 atfile = 0;
324 continue;
325
326 case FMTPROCSW:
327 if (!(formatproc = *argp++) || *formatproc == '-')
328 adios (NULL, "missing argument to %s", argp[-2]);
329 fmtproc = 1;
330 continue;
331 case NFMTPROCSW:
332 fmtproc = 0;
333 continue;
334 }
335 }
336 if (*cp == '+' || *cp == '@') {
337 if (folder)
338 adios (NULL, "only one folder at a time!");
339 folder = pluspath (cp);
340 } else {
341 if (msg)
342 adios (NULL, "only one message at a time!");
343 msg = cp;
344 }
345 }
346
347 if (ccto == -1)
348 ccto = groupreply;
349 if (cccc == -1)
350 cccc = groupreply;
351 if (ccme == -1)
352 ccme = groupreply;
353
354 cwd = mh_xstrdup(pwd ());
355
356 if (!context_find ("path"))
357 free (path ("./", TFOLDER));
358 if (file && (msg || folder))
359 adios (NULL, "can't mix files and folders/msgs");
360
361 try_it_again:
362
363 strncpy (drft, buildsw ? m_maildir ("reply")
364 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
365
366 /* Check if a draft exists */
367 if (!buildsw && stat (drft, &st) != NOTOK) {
368 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
369 for (i = LISTDSW; i != YESW;) {
370 if (!(argp = read_switch_multiword ("\nDisposition? ",
371 isdf ? aqrnl : aqrl)))
372 done (1);
373 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
374 case NOSW:
375 done (0);
376 case NEWSW:
377 dmsg = NULL;
378 goto try_it_again;
379 case YESW:
380 break;
381 case LISTDSW:
382 showfile (++argp, drft);
383 break;
384 case REFILSW:
385 if (refile (++argp, drft) == 0)
386 i = YESW;
387 break;
388 default:
389 inform("say what?");
390 break;
391 }
392 }
393 }
394
395 if (file) {
396 /*
397 * We are replying to a file.
398 */
399 anot = 0; /* we don't want to annotate a file */
400 } else {
401 /*
402 * We are replying to a message.
403 */
404 if (!msg)
405 msg = "cur";
406 if (!folder)
407 folder = getfolder (1);
408 maildir = m_maildir (folder);
409
410 if (chdir (maildir) == NOTOK)
411 adios (maildir, "unable to change directory to");
412
413 /* read folder and create message structure */
414 if (!(mp = folder_read (folder, 1)))
415 adios (NULL, "unable to read folder %s", folder);
416
417 /* check for empty folder */
418 if (mp->nummsg == 0)
419 adios (NULL, "no messages in %s", folder);
420
421 /* parse the message range/sequence/name and set SELECTED */
422 if (!m_convert (mp, msg))
423 done (1);
424 seq_setprev (mp); /* set the previous-sequence */
425
426 if (mp->numsel > 1)
427 adios (NULL, "only one message at a time!");
428
429 context_replace (pfolder, folder); /* update current folder */
430 seq_setcur (mp, mp->lowsel); /* update current message */
431 seq_save (mp); /* synchronize sequences */
432 context_save (); /* save the context file */
433 }
434
435 msg = file ? file : mh_xstrdup(m_name (mp->lowsel));
436
437 if ((in = fopen (msg, "r")) == NULL)
438 adios (msg, "unable to open");
439
440 /* find form (components) file */
441 if (!form) {
442 if (groupreply)
443 form = etcpath (replgroupcomps);
444 else
445 form = etcpath (replcomps);
446 }
447
448 replout (in, msg, drft, mp, outputlinelen, mime, form, filter,
449 fcc, fmtproc);
450 fclose (in);
451
452 {
453 char *filename = file ? file : concat (mp->foldpath, "/", msg, NULL);
454
455 for (n = 0; n < svector_size (convert_types); ++n) {
456 add_convert_header (svector_at (convert_types, n),
457 svector_at (convert_args, n),
458 filename, drft);
459 }
460 if (! file) {
461 free (filename);
462 }
463 }
464
465 if (nwhat)
466 done (0);
467 what_now (ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
468 inplace, cwd, atfile);
469
470 svector_free (convert_args);
471 svector_free (convert_types);
472
473 done (1);
474 return 1;
475 }
476
477 void
478 docc (char *cp, int ccflag)
479 {
480 switch (smatch (cp, ccswitches)) {
481 case AMBIGSW:
482 ambigsw (cp, ccswitches);
483 done (1);
484 case UNKWNSW:
485 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
486
487 case CTOSW:
488 ccto = ccflag;
489 break;
490
491 case CCCSW:
492 cccc = ccflag;
493 break;
494
495 case CMESW:
496 ccme = ccflag;
497 break;
498
499 case CALSW:
500 ccto = cccc = ccme = ccflag;
501 break;
502 }
503 }
504
505 /*
506 * Add pseudoheaders that will pass the convert arguments to
507 * mhbuild. They have the form:
508 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
509 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
510 * The ARGS pseudoheader is optional, but we always add it when
511 * -convertargs is used.
512 */
513 void
514 add_convert_header (const char *convert_type, char *convert_arg,
515 char *filename, char *drft) {
516 char *field_name;
517
518 field_name = concat (MHBUILD_FILE_PSEUDOHEADER, convert_type, NULL);
519 annotate (drft, field_name, filename, 1, 0, -2, 1);
520 free (field_name);
521
522 field_name = concat (MHBUILD_ARGS_PSEUDOHEADER, convert_type, NULL);
523 annotate (drft, field_name, convert_arg, 1, 0, -2, 1);
524 free (field_name);
525 }