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