]> diplodocus.org Git - nmh/blob - uip/forw.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / forw.c
1 /* forw.c -- forward a message, or group of messages.
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 <fcntl.h>
10 #include <h/tws.h>
11 #include <h/utils.h>
12 #include "sbr/m_maildir.h"
13
14
15 #define IFORMAT "digest-issue-%s"
16 #define VFORMAT "digest-volume-%s"
17
18 #define FORW_SWITCHES \
19 X("annotate", 0, ANNOSW) \
20 X("noannotate", 0, NANNOSW) \
21 X("draftfolder +folder", 0, DFOLDSW) \
22 X("draftmessage msg", 0, DMSGSW) \
23 X("nodraftfolder", 0, NDFLDSW) \
24 X("editor editor", 0, EDITRSW) \
25 X("noedit", 0, NEDITSW) \
26 X("filter filterfile", 0, FILTSW) \
27 X("form formfile", 0, FORMSW) \
28 X("format", 5, FRMTSW) \
29 X("noformat", 7, NFRMTSW) \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("mime", 0, MIMESW) \
33 X("nomime", 0, NMIMESW) \
34 X("digest list", 0, DGSTSW) \
35 X("issue number", 0, ISSUESW) \
36 X("volume number", 0, VOLUMSW) \
37 X("whatnowproc program", 0, WHATSW) \
38 X("nowhatnowproc", 0, NWHATSW) \
39 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
40 X("nodashstuffing", 0, NBITSTUFFSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("file file", 4, FILESW) \
44 X("build", 5, BILDSW) /* interface from mhe */ \
45 X("from address", 0, FROMSW) \
46 X("to address", 0, TOSW) \
47 X("cc address", 0, CCSW) \
48 X("subject text", 0, SUBJECTSW) \
49 X("fcc mailbox", 0, FCCSW) \
50 X("width columns", 0, WIDTHSW) \
51
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(FORW);
54 #undef X
55
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(FORW, switches);
58 #undef X
59
60 #define DISPO_SWITCHES \
61 X("quit", 0, NOSW) \
62 X("replace", 0, YESW) \
63 X("list", 0, LISTDSW) \
64 X("refile +folder", 0, REFILSW) \
65 X("new", 0, NEWSW) \
66
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(DISPO);
69 #undef X
70
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
73 #undef X
74
75 static struct swit aqrl[] = {
76 { "quit", 0, NOSW },
77 { "replace", 0, YESW },
78 { "list", 0, LISTDSW },
79 { "refile +folder", 0, REFILSW },
80 { NULL, 0, 0 }
81 };
82
83 static char drft[BUFSIZ];
84
85 static char delim3[] =
86 "\n------------------------------------------------------------\n\n";
87 static char delim4[] = "\n------------------------------\n\n";
88
89
90 static struct msgs *mp = NULL; /* used a lot */
91
92
93 /*
94 * static prototypes
95 */
96 static void mhl_draft (int, char *, int, int, char *, char *, int);
97 static void copy_draft (int, char *, char *, int, int, int);
98 static void copy_mime_draft (int);
99
100
101 int
102 main (int argc, char **argv)
103 {
104 int anot = 0, inplace = 1, mime = 0;
105 int issue = 0, volume = 0, dashstuff = 0;
106 int nedit = 0, nwhat = 0, i, in;
107 int out, isdf = 0, msgnum = 0;
108 int outputlinelen = OUTPUTLINELEN;
109 int dat[5];
110 char *cp, *cwd, *maildir, *dfolder = NULL;
111 char *dmsg = NULL, *digest = NULL, *ed = NULL;
112 char *file = NULL, *filter = NULL, *folder = NULL, *fwdmsg = NULL;
113 char *from = NULL, *to = NULL, *cc = NULL, *subject = NULL, *fcc = NULL;
114 char *form = NULL, buf[BUFSIZ];
115 char **argp, **arguments;
116 struct stat st;
117 struct msgs_array msgs = { 0, 0, NULL };
118 int buildsw = 0;
119
120 if (nmh_init(argv[0], 1)) { return 1; }
121
122 arguments = getarguments (invo_name, argc, argv, 1);
123 argp = arguments;
124
125 while ((cp = *argp++)) {
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
136 invo_name);
137 print_help (buf, switches, 1);
138 done (0);
139 case VERSIONSW:
140 print_version(invo_name);
141 done (0);
142
143 case ANNOSW:
144 anot++;
145 continue;
146 case NANNOSW:
147 anot = 0;
148 continue;
149
150 case EDITRSW:
151 if (!(ed = *argp++) || *ed == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 nedit = 0;
154 continue;
155 case NEDITSW:
156 nedit++;
157 continue;
158
159 case WHATSW:
160 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 nwhat = 0;
163 continue;
164 case BILDSW:
165 buildsw++;
166 /* FALLTHRU */
167 case NWHATSW:
168 nwhat++;
169 continue;
170
171 case FILESW:
172 if (file)
173 adios (NULL, "only one file at a time!");
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 file = path (cp, TFILE);
177 continue;
178 case FILTSW:
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 filter = getcpy (etcpath (cp));
182 mime = 0;
183 continue;
184 case FORMSW:
185 if (!(form = *argp++) || *form == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 continue;
188
189 case FRMTSW:
190 filter = getcpy (etcpath (mhlforward));
191 continue;
192 case NFRMTSW:
193 filter = NULL;
194 continue;
195
196 case INPLSW:
197 inplace++;
198 continue;
199 case NINPLSW:
200 inplace = 0;
201 continue;
202
203 case MIMESW:
204 mime++;
205 filter = NULL;
206 continue;
207 case NMIMESW:
208 mime = 0;
209 continue;
210
211 case DGSTSW:
212 if (!(cp = *argp++) || *cp == '-')
213 adios (NULL, "missing argument to %s", argp[-2]);
214 digest = mh_xstrdup(cp);
215 mime = 0;
216 continue;
217 case ISSUESW:
218 if (!(cp = *argp++) || *cp == '-')
219 adios (NULL, "missing argument to %s", argp[-2]);
220 if ((issue = atoi (cp)) < 1)
221 adios (NULL, "bad argument %s %s", argp[-2], cp);
222 continue;
223 case VOLUMSW:
224 if (!(cp = *argp++) || *cp == '-')
225 adios (NULL, "missing argument to %s", argp[-2]);
226 if ((volume = atoi (cp)) < 1)
227 adios (NULL, "bad argument %s %s", argp[-2], cp);
228 continue;
229
230 case DFOLDSW:
231 if (dfolder)
232 adios (NULL, "only one draft folder at a time!");
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
235 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
236 *cp != '@' ? TFOLDER : TSUBCWF);
237 continue;
238 case DMSGSW:
239 if (dmsg)
240 adios (NULL, "only one draft message at a time!");
241 if (!(dmsg = *argp++) || *dmsg == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
243 continue;
244 case NDFLDSW:
245 dfolder = NULL;
246 isdf = NOTOK;
247 continue;
248
249 case BITSTUFFSW:
250 dashstuff = 1; /* ternary logic */
251 continue;
252 case NBITSTUFFSW:
253 dashstuff = -1; /* ternary logic */
254 continue;
255
256 case FROMSW:
257 if (!(cp = *argp++) || *cp == '-')
258 adios (NULL, "missing argument to %s", argp[-2]);
259 from = addlist(from, cp);
260 continue;
261 case TOSW:
262 if (!(cp = *argp++) || *cp == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
264 to = addlist(to, cp);
265 continue;
266 case CCSW:
267 if (!(cp = *argp++) || *cp == '-')
268 adios (NULL, "missing argument to %s", argp[-2]);
269 cc = addlist(cc, cp);
270 continue;
271 case FCCSW:
272 if (!(cp = *argp++) || *cp == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
274 fcc = addlist(fcc, cp);
275 continue;
276 case SUBJECTSW:
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
279 subject = mh_xstrdup(cp);
280 continue;
281
282 case WIDTHSW:
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
285 if ((outputlinelen = atoi(cp)) < 10)
286 adios (NULL, "impossible width %d", outputlinelen);
287 continue;
288 }
289 }
290 if (*cp == '+' || *cp == '@') {
291 if (folder)
292 adios (NULL, "only one folder at a time!");
293 folder = pluspath (cp);
294 } else {
295 app_msgarg(&msgs, cp);
296 }
297 }
298
299 cwd = mh_xstrdup(pwd ());
300
301 if (!context_find ("path"))
302 free (path ("./", TFOLDER));
303 if (file && (msgs.size || folder))
304 adios (NULL, "can't mix files and folders/msgs");
305
306 try_it_again:
307
308 strncpy (drft, buildsw ? m_maildir ("draft")
309 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
310
311 /* Check if a draft already exists */
312 if (!buildsw && stat (drft, &st) != NOTOK) {
313 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
314 for (i = LISTDSW; i != YESW;) {
315 if (!(argp = read_switch_multiword ("\nDisposition? ",
316 isdf ? aqrnl : aqrl)))
317 done (1);
318 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
319 case NOSW:
320 done (0);
321 case NEWSW:
322 dmsg = NULL;
323 goto try_it_again;
324 case YESW:
325 break;
326 case LISTDSW:
327 showfile (++argp, drft);
328 break;
329 case REFILSW:
330 if (refile (++argp, drft) == 0)
331 i = YESW;
332 break;
333 default:
334 inform("say what?");
335 break;
336 }
337 }
338 }
339
340 if (file) {
341 /*
342 * Forwarding a file.
343 */
344 anot = 0; /* don't want to annotate a file */
345 } else {
346 /*
347 * Forwarding a message.
348 */
349 if (!msgs.size)
350 app_msgarg(&msgs, "cur");
351 if (!folder)
352 folder = getfolder (1);
353 maildir = m_maildir (folder);
354
355 if (chdir (maildir) == NOTOK)
356 adios (maildir, "unable to change directory to");
357
358 /* read folder and create message structure */
359 if (!(mp = folder_read (folder, 1)))
360 adios (NULL, "unable to read folder %s", folder);
361
362 /* check for empty folder */
363 if (mp->nummsg == 0)
364 adios (NULL, "no messages in %s", folder);
365
366 /* parse all the message ranges/sequences and set SELECTED */
367 for (msgnum = 0; msgnum < msgs.size; msgnum++)
368 if (!m_convert (mp, msgs.msgs[msgnum]))
369 done (1);
370
371 seq_setprev (mp); /* set the previous sequence */
372
373 /*
374 * Find the first message in our set and use it as the input
375 * for the component scanner
376 */
377
378 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
379 if (is_selected (mp, msgnum)) {
380 fwdmsg = mh_xstrdup(m_name(msgnum));
381 break;
382 }
383
384 if (! fwdmsg)
385 adios (NULL, "Unable to find input message");
386 }
387
388 if (filter && access (filter, R_OK) == NOTOK)
389 adios (filter, "unable to read");
390
391 /*
392 * Open form (component) file.
393 */
394 if (digest) {
395 if (issue == 0) {
396 snprintf (buf, sizeof(buf), IFORMAT, digest);
397 if (volume == 0
398 && (cp = context_find (buf))
399 && ((issue = atoi (cp)) < 0))
400 issue = 0;
401 issue++;
402 }
403 if (volume == 0) {
404 snprintf (buf, sizeof(buf), VFORMAT, digest);
405 if ((cp = context_find (buf)) == NULL || (volume = atoi (cp)) <= 0)
406 volume = 1;
407 }
408 if (!form)
409 form = digestcomps;
410 } else {
411 if (!form)
412 form = forwcomps;
413 }
414
415 dat[0] = digest ? issue : msgnum;
416 dat[1] = volume;
417 dat[2] = 0;
418 dat[3] = outputlinelen;
419 dat[4] = 0;
420
421
422 in = build_form (form, digest, dat, from, to, cc, fcc, subject,
423 file ? file : fwdmsg);
424
425 if ((out = creat (drft, m_gmprot ())) == NOTOK)
426 adios (drft, "unable to create");
427
428 /*
429 * copy the components into the draft
430 */
431 cpydata (in, out, form, drft);
432 close (in);
433
434 if (file) {
435 /* just copy the file into the draft */
436 if ((in = open (file, O_RDONLY)) == NOTOK)
437 adios (file, "unable to open");
438 cpydata (in, out, file, drft);
439 close (in);
440 close (out);
441 } else {
442 /*
443 * If filter file is defined, then format the
444 * messages into the draft using mhlproc.
445 */
446 if (filter)
447 mhl_draft (out, digest, volume, issue, drft, filter, dashstuff);
448 else if (mime)
449 copy_mime_draft (out);
450 else
451 copy_draft (out, digest, drft, volume, issue, dashstuff);
452 close (out);
453
454 if (digest) {
455 snprintf (buf, sizeof(buf), IFORMAT, digest);
456 context_replace (buf, mh_xstrdup(m_str(issue)));
457 snprintf (buf, sizeof(buf), VFORMAT, digest);
458 context_replace (buf, mh_xstrdup(m_str(volume)));
459 }
460
461 context_replace (pfolder, folder); /* update current folder */
462 seq_setcur (mp, mp->lowsel); /* update current message */
463 seq_save (mp); /* synchronize sequences */
464 context_save (); /* save the context file */
465 }
466
467 if (nwhat)
468 done (0);
469 what_now (ed, nedit, NOUSE, drft, NULL, 0, mp,
470 anot ? "Forwarded" : NULL, inplace, cwd, 0);
471 done (1);
472 return 1;
473 }
474
475
476 /*
477 * Filter the messages you are forwarding, into the
478 * draft calling the mhlproc, and reading its output
479 * from a pipe.
480 */
481
482 static void
483 mhl_draft (int out, char *digest, int volume, int issue,
484 char *file, char *filter, int dashstuff)
485 {
486 pid_t child_id;
487 int msgnum, pd[2];
488 char buf1[BUFSIZ];
489 char buf2[BUFSIZ];
490 char *program;
491 struct msgs_array vec = { 0, 0, NULL };
492
493 if (pipe (pd) == NOTOK)
494 adios ("pipe", "unable to create");
495
496 argsplit_msgarg(&vec, mhlproc, &program);
497
498 child_id = fork();
499 switch (child_id) {
500 case NOTOK:
501 adios ("fork", "unable to");
502
503 case OK:
504 close (pd[0]);
505 dup2 (pd[1], 1);
506 close (pd[1]);
507
508 app_msgarg(&vec, "-forwall");
509 app_msgarg(&vec, "-form");
510 app_msgarg(&vec, filter);
511
512 if (digest) {
513 app_msgarg(&vec, "-digest");
514 app_msgarg(&vec, digest);
515 app_msgarg(&vec, "-issue");
516 snprintf (buf1, sizeof(buf1), "%d", issue);
517 app_msgarg(&vec, buf1);
518 app_msgarg(&vec, "-volume");
519 snprintf (buf2, sizeof(buf2), "%d", volume);
520 app_msgarg(&vec, buf2);
521 }
522
523 /*
524 * Are we dashstuffing (quoting) the lines that begin
525 * with `-'. We use the mhl default (don't add any flag)
526 * unless the user has specified a specific flag.
527 */
528 if (dashstuff > 0)
529 app_msgarg(&vec, "-dashstuffing");
530 else if (dashstuff < 0)
531 app_msgarg(&vec, "-nodashstuffing");
532
533 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
534 if (is_selected (mp, msgnum))
535 app_msgarg(&vec, mh_xstrdup(m_name (msgnum)));
536 }
537
538 app_msgarg(&vec, NULL);
539
540 execvp (program, vec.msgs);
541 fprintf (stderr, "unable to exec ");
542 perror (mhlproc);
543 _exit (-1);
544
545 default:
546 close (pd[1]);
547 cpydata (pd[0], out, vec.msgs[0], file);
548 close (pd[0]);
549 pidXwait(child_id, mhlproc);
550 break;
551 }
552 }
553
554
555 /*
556 * Copy the messages into the draft. The messages are
557 * not filtered through the mhlproc. Do dashstuffing if
558 * necessary.
559 */
560
561 static void
562 copy_draft (int out, char *digest, char *file, int volume, int issue, int dashstuff)
563 {
564 int fd,i, msgcnt, msgnum;
565 int len, buflen;
566 char *bp, *msgnam;
567 char buffer[BUFSIZ];
568
569 msgcnt = 1;
570 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
571 if (is_selected (mp, msgnum)) {
572 if (digest) {
573 strncpy (buffer, msgnum == mp->lowsel ? delim3 : delim4,
574 sizeof(buffer));
575 } else {
576 /* Get buffer ready to go */
577 bp = buffer;
578 buflen = sizeof(buffer);
579
580 strncpy (bp, "\n-------", buflen);
581 len = strlen (bp);
582 bp += len;
583 buflen -= len;
584
585 if (msgnum == mp->lowsel) {
586 snprintf (bp, buflen, " Forwarded Message%s",
587 PLURALS(mp->numsel));
588 } else {
589 snprintf (bp, buflen, " Message %d", msgcnt);
590 }
591 len = strlen (bp);
592 bp += len;
593 buflen -= len;
594
595 strncpy (bp, "\n\n", buflen);
596 }
597 if (write (out, buffer, strlen (buffer)) < 0) {
598 advise (drft, "write");
599 }
600
601 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
602 admonish (msgnam, "unable to read message");
603 continue;
604 }
605
606 /*
607 * Copy the message. Add RFC934 quoting (dashstuffing)
608 * unless given the -nodashstuffing flag.
609 */
610 if (dashstuff >= 0)
611 cpydgst (fd, out, msgnam, file);
612 else
613 cpydata (fd, out, msgnam, file);
614
615 close (fd);
616 msgcnt++;
617 }
618 }
619
620 if (digest) {
621 strncpy (buffer, delim4, sizeof(buffer));
622 } else {
623 snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
624 PLURALS(mp->numsel));
625 }
626 if (write (out, buffer, strlen (buffer)) < 0) {
627 advise (drft, "write");
628 }
629
630 if (digest) {
631 snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
632 digest, volume, issue);
633 i = strlen (buffer);
634 for (bp = buffer + i; i > 1; i--)
635 *bp++ = '*';
636 *bp++ = '\n';
637 *bp = 0;
638 if (write (out, buffer, strlen (buffer)) < 0) {
639 advise (drft, "write");
640 }
641 }
642 }
643
644
645 /*
646 * Create a mhn composition file for forwarding message.
647 */
648
649 static void
650 copy_mime_draft (int out)
651 {
652 int msgnum;
653 char buffer[BUFSIZ];
654
655 snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
656 PLURALS(mp->numsel), mp->foldpath);
657 if (write (out, buffer, strlen (buffer)) < 0) {
658 advise (drft, "write");
659 }
660 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
661 if (is_selected (mp, msgnum)) {
662 snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
663 if (write (out, buffer, strlen (buffer)) < 0) {
664 advise (drft, "write");
665 }
666 }
667 if (write (out, "\n", 1) < 0) {
668 advise (drft, "write newline");
669 }
670 }