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