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