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