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