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