]> diplodocus.org Git - nmh/blob - uip/forw.c
9eeb6123d6864d839c73524c31e2bac2d4b78474
[nmh] / uip / forw.c
1
2 /*
3 * forw.c -- forward a message, or group of messages.
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/tws.h>
13 #include <h/utils.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) /* interface from msh */ \
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], value[10];
116 char **argp, **arguments;
117 struct stat st;
118 struct msgs_array msgs = { 0, 0, NULL };
119
120 int buildsw = 0;
121
122 setlocale(LC_ALL, "");
123 invo_name = r1bindex (argv[0], '/');
124
125 /* read user profile/context */
126 context_read();
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 adios (NULL, "-%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++;
151 continue;
152 case NANNOSW:
153 anot = 0;
154 continue;
155
156 case EDITRSW:
157 if (!(ed = *argp++) || *ed == '-')
158 adios (NULL, "missing argument to %s", argp[-2]);
159 nedit = 0;
160 continue;
161 case NEDITSW:
162 nedit++;
163 continue;
164
165 case WHATSW:
166 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 nwhat = 0;
169 continue;
170 case BILDSW:
171 buildsw++; /* fall... */
172 case NWHATSW:
173 nwhat++;
174 continue;
175
176 case FILESW:
177 if (file)
178 adios (NULL, "only one file at a time!");
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 file = path (cp, TFILE);
182 continue;
183 case FILTSW:
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 filter = getcpy (etcpath (cp));
187 mime = 0;
188 continue;
189 case FORMSW:
190 if (!(form = *argp++) || *form == '-')
191 adios (NULL, "missing argument to %s", argp[-2]);
192 continue;
193
194 case FRMTSW:
195 filter = getcpy (etcpath (mhlforward));
196 continue;
197 case NFRMTSW:
198 filter = NULL;
199 continue;
200
201 case INPLSW:
202 inplace++;
203 continue;
204 case NINPLSW:
205 inplace = 0;
206 continue;
207
208 case MIMESW:
209 mime++;
210 filter = NULL;
211 continue;
212 case NMIMESW:
213 mime = 0;
214 continue;
215
216 case DGSTSW:
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
219 digest = getcpy(cp);
220 mime = 0;
221 continue;
222 case ISSUESW:
223 if (!(cp = *argp++) || *cp == '-')
224 adios (NULL, "missing argument to %s", argp[-2]);
225 if ((issue = atoi (cp)) < 1)
226 adios (NULL, "bad argument %s %s", argp[-2], cp);
227 continue;
228 case VOLUMSW:
229 if (!(cp = *argp++) || *cp == '-')
230 adios (NULL, "missing argument to %s", argp[-2]);
231 if ((volume = atoi (cp)) < 1)
232 adios (NULL, "bad argument %s %s", argp[-2], cp);
233 continue;
234
235 case DFOLDSW:
236 if (dfolder)
237 adios (NULL, "only one draft folder at a time!");
238 if (!(cp = *argp++) || *cp == '-')
239 adios (NULL, "missing argument to %s", argp[-2]);
240 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
241 *cp != '@' ? TFOLDER : TSUBCWF);
242 continue;
243 case DMSGSW:
244 if (dmsg)
245 adios (NULL, "only one draft message at a time!");
246 if (!(dmsg = *argp++) || *dmsg == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
248 continue;
249 case NDFLDSW:
250 dfolder = NULL;
251 isdf = NOTOK;
252 continue;
253
254 case BITSTUFFSW:
255 dashstuff = 1; /* trinary logic */
256 continue;
257 case NBITSTUFFSW:
258 dashstuff = -1; /* trinary logic */
259 continue;
260
261 case FROMSW:
262 if (!(cp = *argp++) || *cp == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
264 from = addlist(from, cp);
265 continue;
266 case TOSW:
267 if (!(cp = *argp++) || *cp == '-')
268 adios (NULL, "missing argument to %s", argp[-2]);
269 to = addlist(to, cp);
270 continue;
271 case CCSW:
272 if (!(cp = *argp++) || *cp == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
274 cc = addlist(cc, cp);
275 continue;
276 case FCCSW:
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
279 fcc = addlist(fcc, cp);
280 continue;
281 case SUBJECTSW:
282 if (!(cp = *argp++) || *cp == '-')
283 adios (NULL, "missing argument to %s", argp[-2]);
284 subject = getcpy(cp);
285 continue;
286
287 case WIDTHSW:
288 if (!(cp = *argp++) || *cp == '-')
289 adios (NULL, "missing argument to %s", argp[-2]);
290 if ((outputlinelen = atoi(cp)) < 10)
291 adios (NULL, "impossible width %d", outputlinelen);
292 continue;
293 }
294 }
295 if (*cp == '+' || *cp == '@') {
296 if (folder)
297 adios (NULL, "only one folder at a time!");
298 else
299 folder = pluspath (cp);
300 } else {
301 app_msgarg(&msgs, cp);
302 }
303 }
304
305 cwd = getcpy (pwd ());
306
307 if (!context_find ("path"))
308 free (path ("./", TFOLDER));
309 if (file && (msgs.size || folder))
310 adios (NULL, "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 = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
322 done (1);
323 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
324 case NOSW:
325 done (0);
326 case NEWSW:
327 dmsg = NULL;
328 goto try_it_again;
329 case YESW:
330 break;
331 case LISTDSW:
332 showfile (++argp, drft);
333 break;
334 case REFILSW:
335 if (refile (++argp, drft) == 0)
336 i = YESW;
337 break;
338 default:
339 advise (NULL, "say what?");
340 break;
341 }
342 }
343 }
344
345 if (file) {
346 /*
347 * Forwarding a file.
348 */
349 anot = 0; /* don't want to annotate a file */
350 } else {
351 /*
352 * Forwarding a message.
353 */
354 if (!msgs.size)
355 app_msgarg(&msgs, "cur");
356 if (!folder)
357 folder = getfolder (1);
358 maildir = m_maildir (folder);
359
360 if (chdir (maildir) == NOTOK)
361 adios (maildir, "unable to change directory to");
362
363 /* read folder and create message structure */
364 if (!(mp = folder_read (folder, 1)))
365 adios (NULL, "unable to read folder %s", folder);
366
367 /* check for empty folder */
368 if (mp->nummsg == 0)
369 adios (NULL, "no messages in %s", folder);
370
371 /* parse all the message ranges/sequences and set SELECTED */
372 for (msgnum = 0; msgnum < msgs.size; msgnum++)
373 if (!m_convert (mp, msgs.msgs[msgnum]))
374 done (1);
375
376 seq_setprev (mp); /* set the previous sequence */
377
378 /*
379 * Find the first message in our set and use it as the input
380 * for the component scanner
381 */
382
383 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
384 if (is_selected (mp, msgnum)) {
385 fwdmsg = strdup(m_name(msgnum));
386 break;
387 }
388
389 if (! fwdmsg)
390 adios (NULL, "Unable to find input message");
391 }
392
393 if (filter && access (filter, R_OK) == NOTOK)
394 adios (filter, "unable to read");
395
396 /*
397 * Open form (component) file.
398 */
399 if (digest) {
400 if (issue == 0) {
401 snprintf (buf, sizeof(buf), IFORMAT, digest);
402 if (volume == 0
403 && (cp = context_find (buf))
404 && ((issue = atoi (cp)) < 0))
405 issue = 0;
406 issue++;
407 }
408 if (volume == 0) {
409 snprintf (buf, sizeof(buf), VFORMAT, digest);
410 if ((cp = context_find (buf)) == NULL || (volume = atoi (cp)) <= 0)
411 volume = 1;
412 }
413 if (!form)
414 form = digestcomps;
415 } else {
416 if (!form)
417 form = forwcomps;
418 }
419
420 dat[0] = digest ? issue : msgnum;
421 dat[1] = volume;
422 dat[2] = 0;
423 dat[3] = outputlinelen;
424 dat[4] = 0;
425
426
427 in = build_form (form, digest, dat, from, to, cc, fcc, subject,
428 file ? file : fwdmsg);
429
430 if ((out = creat (drft, m_gmprot ())) == NOTOK)
431 adios (drft, "unable to create");
432
433 /*
434 * copy the components into the draft
435 */
436 cpydata (in, out, form, drft);
437 close (in);
438
439 if (file) {
440 /* just copy the file into the draft */
441 if ((in = open (file, O_RDONLY)) == NOTOK)
442 adios (file, "unable to open");
443 cpydata (in, out, file, drft);
444 close (in);
445 close (out);
446 } else {
447 /*
448 * If filter file is defined, then format the
449 * messages into the draft using mhlproc.
450 */
451 if (filter)
452 mhl_draft (out, digest, volume, issue, drft, filter, dashstuff);
453 else if (mime)
454 copy_mime_draft (out);
455 else
456 copy_draft (out, digest, drft, volume, issue, dashstuff);
457 close (out);
458
459 if (digest) {
460 snprintf (buf, sizeof(buf), IFORMAT, digest);
461 snprintf (value, sizeof(value), "%d", issue);
462 context_replace (buf, getcpy (value));
463 snprintf (buf, sizeof(buf), VFORMAT, digest);
464 snprintf (value, sizeof(value), "%d", volume);
465 context_replace (buf, getcpy (value));
466 }
467
468 context_replace (pfolder, folder); /* update current folder */
469 seq_setcur (mp, mp->lowsel); /* update current message */
470 seq_save (mp); /* synchronize sequences */
471 context_save (); /* save the context file */
472 }
473
474 if (nwhat)
475 done (0);
476 what_now (ed, nedit, NOUSE, drft, NULL, 0, mp,
477 anot ? "Forwarded" : NULL, inplace, cwd, 0);
478 done (1);
479 return 1;
480 }
481
482
483 /*
484 * Filter the messages you are forwarding, into the
485 * draft calling the mhlproc, and reading its output
486 * from a pipe.
487 */
488
489 static void
490 mhl_draft (int out, char *digest, int volume, int issue,
491 char *file, char *filter, int dashstuff)
492 {
493 pid_t child_id;
494 int i, msgnum, pd[2];
495 char buf1[BUFSIZ];
496 char buf2[BUFSIZ];
497 char *program;
498 struct msgs_array vec = { 0, 0, NULL };
499
500 if (pipe (pd) == NOTOK)
501 adios ("pipe", "unable to create");
502
503 argsplit_msgarg(&vec, mhlproc, &program);
504
505 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
506 sleep (5);
507
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 i = 1;
518 app_msgarg(&vec, "-forwall");
519 app_msgarg(&vec, "-form");
520 app_msgarg(&vec, filter);
521
522 if (digest) {
523 app_msgarg(&vec, "-digest");
524 app_msgarg(&vec, digest);
525 app_msgarg(&vec, "-issue");
526 snprintf (buf1, sizeof(buf1), "%d", issue);
527 app_msgarg(&vec, buf1);
528 app_msgarg(&vec, "-volume");
529 snprintf (buf2, sizeof(buf2), "%d", volume);
530 app_msgarg(&vec, buf2);
531 }
532
533 /*
534 * Are we dashstuffing (quoting) the lines that begin
535 * with `-'. We use the mhl default (don't add any flag)
536 * unless the user has specified a specific flag.
537 */
538 if (dashstuff > 0)
539 app_msgarg(&vec, "-dashstuffing");
540 else if (dashstuff < 0)
541 app_msgarg(&vec, "-nodashstuffing");
542
543 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
544 if (is_selected (mp, msgnum))
545 app_msgarg(&vec, getcpy (m_name (msgnum)));
546 }
547
548 app_msgarg(&vec, NULL);
549
550 execvp (program, vec.msgs);
551 fprintf (stderr, "unable to exec ");
552 perror (mhlproc);
553 _exit (-1);
554
555 default:
556 close (pd[1]);
557 cpydata (pd[0], out, vec.msgs[0], file);
558 close (pd[0]);
559 pidXwait(child_id, mhlproc);
560 break;
561 }
562 }
563
564
565 /*
566 * Copy the messages into the draft. The messages are
567 * not filtered through the mhlproc. Do dashstuffing if
568 * necessary.
569 */
570
571 static void
572 copy_draft (int out, char *digest, char *file, int volume, int issue, int dashstuff)
573 {
574 int fd,i, msgcnt, msgnum;
575 int len, buflen;
576 register char *bp, *msgnam;
577 char buffer[BUFSIZ];
578
579 msgcnt = 1;
580 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
581 if (is_selected (mp, msgnum)) {
582 if (digest) {
583 strncpy (buffer, msgnum == mp->lowsel ? delim3 : delim4,
584 sizeof(buffer));
585 } else {
586 /* Get buffer ready to go */
587 bp = buffer;
588 buflen = sizeof(buffer);
589
590 strncpy (bp, "\n-------", buflen);
591 len = strlen (bp);
592 bp += len;
593 buflen -= len;
594
595 if (msgnum == mp->lowsel) {
596 snprintf (bp, buflen, " Forwarded Message%s",
597 mp->numsel > 1 ? "s" : "");
598 } else {
599 snprintf (bp, buflen, " Message %d", msgcnt);
600 }
601 len = strlen (bp);
602 bp += len;
603 buflen -= len;
604
605 strncpy (bp, "\n\n", buflen);
606 }
607 write (out, buffer, strlen (buffer));
608
609 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
610 admonish (msgnam, "unable to read message");
611 continue;
612 }
613
614 /*
615 * Copy the message. Add RFC934 quoting (dashstuffing)
616 * unless given the -nodashstuffing flag.
617 */
618 if (dashstuff >= 0)
619 cpydgst (fd, out, msgnam, file);
620 else
621 cpydata (fd, out, msgnam, file);
622
623 close (fd);
624 msgcnt++;
625 }
626 }
627
628 if (digest) {
629 strncpy (buffer, delim4, sizeof(buffer));
630 } else {
631 snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
632 mp->numsel > 1 ? "s" : "");
633 }
634 write (out, buffer, strlen (buffer));
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 write (out, buffer, strlen (buffer));
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 write (out, buffer, strlen (buffer));
662 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
663 if (is_selected (mp, msgnum)) {
664 snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
665 write (out, buffer, strlen (buffer));
666 }
667 write (out, "\n", 1);
668 }