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