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