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