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