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