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