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