]> diplodocus.org Git - nmh/blob - uip/forw.c
Removed temporary probes added in commit
[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 #ifdef LOCALE
123 setlocale(LC_ALL, "");
124 #endif
125 invo_name = r1bindex (argv[0], '/');
126
127 /* read user profile/context */
128 context_read();
129
130 arguments = getarguments (invo_name, argc, argv, 1);
131 argp = arguments;
132
133 while ((cp = *argp++)) {
134 if (*cp == '-') {
135 switch (smatch (++cp, switches)) {
136 case AMBIGSW:
137 ambigsw (cp, switches);
138 done (1);
139 case UNKWNSW:
140 adios (NULL, "-%s unknown", cp);
141
142 case HELPSW:
143 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
144 invo_name);
145 print_help (buf, switches, 1);
146 done (0);
147 case VERSIONSW:
148 print_version(invo_name);
149 done (0);
150
151 case ANNOSW:
152 anot++;
153 continue;
154 case NANNOSW:
155 anot = 0;
156 continue;
157
158 case EDITRSW:
159 if (!(ed = *argp++) || *ed == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 nedit = 0;
162 continue;
163 case NEDITSW:
164 nedit++;
165 continue;
166
167 case WHATSW:
168 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 nwhat = 0;
171 continue;
172 case BILDSW:
173 buildsw++; /* fall... */
174 case NWHATSW:
175 nwhat++;
176 continue;
177
178 case FILESW:
179 if (file)
180 adios (NULL, "only one file at a time!");
181 if (!(cp = *argp++) || *cp == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
183 file = path (cp, TFILE);
184 continue;
185 case FILTSW:
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
188 filter = getcpy (etcpath (cp));
189 mime = 0;
190 continue;
191 case FORMSW:
192 if (!(form = *argp++) || *form == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
194 continue;
195
196 case FRMTSW:
197 filter = getcpy (etcpath (mhlforward));
198 continue;
199 case NFRMTSW:
200 filter = NULL;
201 continue;
202
203 case INPLSW:
204 inplace++;
205 continue;
206 case NINPLSW:
207 inplace = 0;
208 continue;
209
210 case MIMESW:
211 mime++;
212 filter = NULL;
213 continue;
214 case NMIMESW:
215 mime = 0;
216 continue;
217
218 case DGSTSW:
219 if (!(cp = *argp++) || *cp == '-')
220 adios (NULL, "missing argument to %s", argp[-2]);
221 digest = getcpy(cp);
222 mime = 0;
223 continue;
224 case ISSUESW:
225 if (!(cp = *argp++) || *cp == '-')
226 adios (NULL, "missing argument to %s", argp[-2]);
227 if ((issue = atoi (cp)) < 1)
228 adios (NULL, "bad argument %s %s", argp[-2], cp);
229 continue;
230 case VOLUMSW:
231 if (!(cp = *argp++) || *cp == '-')
232 adios (NULL, "missing argument to %s", argp[-2]);
233 if ((volume = atoi (cp)) < 1)
234 adios (NULL, "bad argument %s %s", argp[-2], cp);
235 continue;
236
237 case DFOLDSW:
238 if (dfolder)
239 adios (NULL, "only one draft folder at a time!");
240 if (!(cp = *argp++) || *cp == '-')
241 adios (NULL, "missing argument to %s", argp[-2]);
242 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
243 *cp != '@' ? TFOLDER : TSUBCWF);
244 continue;
245 case DMSGSW:
246 if (dmsg)
247 adios (NULL, "only one draft message at a time!");
248 if (!(dmsg = *argp++) || *dmsg == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 continue;
251 case NDFLDSW:
252 dfolder = NULL;
253 isdf = NOTOK;
254 continue;
255
256 case BITSTUFFSW:
257 dashstuff = 1; /* trinary logic */
258 continue;
259 case NBITSTUFFSW:
260 dashstuff = -1; /* trinary logic */
261 continue;
262
263 case FROMSW:
264 if (!(cp = *argp++) || *cp == '-')
265 adios (NULL, "missing argument to %s", argp[-2]);
266 from = addlist(from, cp);
267 continue;
268 case TOSW:
269 if (!(cp = *argp++) || *cp == '-')
270 adios (NULL, "missing argument to %s", argp[-2]);
271 to = addlist(to, cp);
272 continue;
273 case CCSW:
274 if (!(cp = *argp++) || *cp == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
276 cc = addlist(cc, cp);
277 continue;
278 case FCCSW:
279 if (!(cp = *argp++) || *cp == '-')
280 adios (NULL, "missing argument to %s", argp[-2]);
281 fcc = addlist(fcc, cp);
282 continue;
283 case SUBJECTSW:
284 if (!(cp = *argp++) || *cp == '-')
285 adios (NULL, "missing argument to %s", argp[-2]);
286 subject = getcpy(cp);
287 continue;
288
289 case WIDTHSW:
290 if (!(cp = *argp++) || *cp == '-')
291 adios (NULL, "missing argument to %s", argp[-2]);
292 if ((outputlinelen = atoi(cp)) < 10)
293 adios (NULL, "impossible width %d", outputlinelen);
294 continue;
295 }
296 }
297 if (*cp == '+' || *cp == '@') {
298 if (folder)
299 adios (NULL, "only one folder at a time!");
300 else
301 folder = pluspath (cp);
302 } else {
303 app_msgarg(&msgs, cp);
304 }
305 }
306
307 cwd = getcpy (pwd ());
308
309 if (!context_find ("path"))
310 free (path ("./", TFOLDER));
311 if (file && (msgs.size || folder))
312 adios (NULL, "can't mix files and folders/msgs");
313
314 try_it_again:
315
316 strncpy (drft, buildsw ? m_maildir ("draft")
317 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
318
319 /* Check if a draft already exists */
320 if (!buildsw && stat (drft, &st) != NOTOK) {
321 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
322 for (i = LISTDSW; i != YESW;) {
323 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
324 done (1);
325 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
326 case NOSW:
327 done (0);
328 case NEWSW:
329 dmsg = NULL;
330 goto try_it_again;
331 case YESW:
332 break;
333 case LISTDSW:
334 showfile (++argp, drft);
335 break;
336 case REFILSW:
337 if (refile (++argp, drft) == 0)
338 i = YESW;
339 break;
340 default:
341 advise (NULL, "say what?");
342 break;
343 }
344 }
345 }
346
347 if (file) {
348 /*
349 * Forwarding a file.
350 */
351 anot = 0; /* don't want to annotate a file */
352 } else {
353 /*
354 * Forwarding a message.
355 */
356 if (!msgs.size)
357 app_msgarg(&msgs, "cur");
358 if (!folder)
359 folder = getfolder (1);
360 maildir = m_maildir (folder);
361
362 if (chdir (maildir) == NOTOK)
363 adios (maildir, "unable to change directory to");
364
365 /* read folder and create message structure */
366 if (!(mp = folder_read (folder, 1)))
367 adios (NULL, "unable to read folder %s", folder);
368
369 /* check for empty folder */
370 if (mp->nummsg == 0)
371 adios (NULL, "no messages in %s", folder);
372
373 /* parse all the message ranges/sequences and set SELECTED */
374 for (msgnum = 0; msgnum < msgs.size; msgnum++)
375 if (!m_convert (mp, msgs.msgs[msgnum]))
376 done (1);
377
378 seq_setprev (mp); /* set the previous sequence */
379
380 /*
381 * Find the first message in our set and use it as the input
382 * for the component scanner
383 */
384
385 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
386 if (is_selected (mp, msgnum)) {
387 fwdmsg = strdup(m_name(msgnum));
388 break;
389 }
390
391 if (! fwdmsg)
392 adios (NULL, "Unable to find input message");
393 }
394
395 if (filter && access (filter, R_OK) == NOTOK)
396 adios (filter, "unable to read");
397
398 /*
399 * Open form (component) file.
400 */
401 if (digest) {
402 if (issue == 0) {
403 snprintf (buf, sizeof(buf), IFORMAT, digest);
404 if (volume == 0
405 && (cp = context_find (buf))
406 && ((issue = atoi (cp)) < 0))
407 issue = 0;
408 issue++;
409 }
410 if (volume == 0) {
411 snprintf (buf, sizeof(buf), VFORMAT, digest);
412 if ((cp = context_find (buf)) == NULL || (volume = atoi (cp)) <= 0)
413 volume = 1;
414 }
415 if (!form)
416 form = digestcomps;
417 } else {
418 if (!form)
419 form = forwcomps;
420 }
421
422 dat[0] = digest ? issue : msgnum;
423 dat[1] = volume;
424 dat[2] = 0;
425 dat[3] = outputlinelen;
426 dat[4] = 0;
427
428
429 in = build_form (form, digest, dat, from, to, cc, fcc, subject,
430 file ? file : fwdmsg);
431
432 if ((out = creat (drft, m_gmprot ())) == NOTOK)
433 adios (drft, "unable to create");
434
435 /*
436 * copy the components into the draft
437 */
438 cpydata (in, out, form, drft);
439 close (in);
440
441 if (file) {
442 /* just copy the file into the draft */
443 if ((in = open (file, O_RDONLY)) == NOTOK)
444 adios (file, "unable to open");
445 cpydata (in, out, file, drft);
446 close (in);
447 close (out);
448 } else {
449 /*
450 * If filter file is defined, then format the
451 * messages into the draft using mhlproc.
452 */
453 if (filter)
454 mhl_draft (out, digest, volume, issue, drft, filter, dashstuff);
455 else if (mime)
456 copy_mime_draft (out);
457 else
458 copy_draft (out, digest, drft, volume, issue, dashstuff);
459 close (out);
460
461 if (digest) {
462 snprintf (buf, sizeof(buf), IFORMAT, digest);
463 snprintf (value, sizeof(value), "%d", issue);
464 context_replace (buf, getcpy (value));
465 snprintf (buf, sizeof(buf), VFORMAT, digest);
466 snprintf (value, sizeof(value), "%d", volume);
467 context_replace (buf, getcpy (value));
468 }
469
470 context_replace (pfolder, folder); /* update current folder */
471 seq_setcur (mp, mp->lowsel); /* update current message */
472 seq_save (mp); /* synchronize sequences */
473 context_save (); /* save the context file */
474 }
475
476 if (nwhat)
477 done (0);
478 what_now (ed, nedit, NOUSE, drft, NULL, 0, mp,
479 anot ? "Forwarded" : NULL, inplace, cwd, 0);
480 done (1);
481 return 1;
482 }
483
484
485 /*
486 * Filter the messages you are forwarding, into the
487 * draft calling the mhlproc, and reading its output
488 * from a pipe.
489 */
490
491 static void
492 mhl_draft (int out, char *digest, int volume, int issue,
493 char *file, char *filter, int dashstuff)
494 {
495 pid_t child_id;
496 int i, msgnum, pd[2];
497 char buf1[BUFSIZ];
498 char buf2[BUFSIZ];
499 char *program;
500 struct msgs_array vec = { 0, 0, NULL };
501
502 if (pipe (pd) == NOTOK)
503 adios ("pipe", "unable to create");
504
505 argsplit_msgarg(&vec, mhlproc, &program);
506
507 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
508 sleep (5);
509
510 switch (child_id) {
511 case NOTOK:
512 adios ("fork", "unable to");
513
514 case OK:
515 close (pd[0]);
516 dup2 (pd[1], 1);
517 close (pd[1]);
518
519 i = 1;
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, getcpy (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 register 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 mp->numsel > 1 ? "s" : "");
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 write (out, buffer, strlen (buffer));
610
611 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
612 admonish (msgnam, "unable to read message");
613 continue;
614 }
615
616 /*
617 * Copy the message. Add RFC934 quoting (dashstuffing)
618 * unless given the -nodashstuffing flag.
619 */
620 if (dashstuff >= 0)
621 cpydgst (fd, out, msgnam, file);
622 else
623 cpydata (fd, out, msgnam, file);
624
625 close (fd);
626 msgcnt++;
627 }
628 }
629
630 if (digest) {
631 strncpy (buffer, delim4, sizeof(buffer));
632 } else {
633 snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
634 mp->numsel > 1 ? "s" : "");
635 }
636 write (out, buffer, strlen (buffer));
637
638 if (digest) {
639 snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
640 digest, volume, issue);
641 i = strlen (buffer);
642 for (bp = buffer + i; i > 1; i--)
643 *bp++ = '*';
644 *bp++ = '\n';
645 *bp = 0;
646 write (out, buffer, strlen (buffer));
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 mp->numsel == 1 ? "" : "s", mp->foldpath);
663 write (out, buffer, strlen (buffer));
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 write (out, buffer, strlen (buffer));
668 }
669 write (out, "\n", 1);
670 }