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