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