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