]> diplodocus.org Git - nmh/blob - uip/forw.c
Simplified m_strn() per Ralph's suggestions.
[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 #include "../sbr/m_maildir.h"
13
14
15 #define IFORMAT "digest-issue-%s"
16 #define VFORMAT "digest-volume-%s"
17
18 #define FORW_SWITCHES \
19 X("annotate", 0, ANNOSW) \
20 X("noannotate", 0, NANNOSW) \
21 X("draftfolder +folder", 0, DFOLDSW) \
22 X("draftmessage msg", 0, DMSGSW) \
23 X("nodraftfolder", 0, NDFLDSW) \
24 X("editor editor", 0, EDITRSW) \
25 X("noedit", 0, NEDITSW) \
26 X("filter filterfile", 0, FILTSW) \
27 X("form formfile", 0, FORMSW) \
28 X("format", 5, FRMTSW) \
29 X("noformat", 7, NFRMTSW) \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("mime", 0, MIMESW) \
33 X("nomime", 0, NMIMESW) \
34 X("digest list", 0, DGSTSW) \
35 X("issue number", 0, ISSUESW) \
36 X("volume number", 0, VOLUMSW) \
37 X("whatnowproc program", 0, WHATSW) \
38 X("nowhatnowproc", 0, NWHATSW) \
39 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
40 X("nodashstuffing", 0, NBITSTUFFSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("file file", 4, FILESW) \
44 X("build", 5, BILDSW) /* interface from mhe */ \
45 X("from address", 0, FROMSW) \
46 X("to address", 0, TOSW) \
47 X("cc address", 0, CCSW) \
48 X("subject text", 0, SUBJECTSW) \
49 X("fcc mailbox", 0, FCCSW) \
50 X("width columns", 0, WIDTHSW) \
51
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(FORW);
54 #undef X
55
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(FORW, switches);
58 #undef X
59
60 #define DISPO_SWITCHES \
61 X("quit", 0, NOSW) \
62 X("replace", 0, YESW) \
63 X("list", 0, LISTDSW) \
64 X("refile +folder", 0, REFILSW) \
65 X("new", 0, NEWSW) \
66
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(DISPO);
69 #undef X
70
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
73 #undef X
74
75 static struct swit aqrl[] = {
76 { "quit", 0, NOSW },
77 { "replace", 0, YESW },
78 { "list", 0, LISTDSW },
79 { "refile +folder", 0, REFILSW },
80 { NULL, 0, 0 }
81 };
82
83 static char drft[BUFSIZ];
84
85 static char delim3[] =
86 "\n------------------------------------------------------------\n\n";
87 static char delim4[] = "\n------------------------------\n\n";
88
89
90 static struct msgs *mp = NULL; /* used a lot */
91
92
93 /*
94 * static prototypes
95 */
96 static void mhl_draft (int, char *, int, int, char *, char *, int);
97 static void copy_draft (int, char *, char *, int, int, int);
98 static void copy_mime_draft (int);
99
100
101 int
102 main (int argc, char **argv)
103 {
104 int anot = 0, inplace = 1, mime = 0;
105 int issue = 0, volume = 0, dashstuff = 0;
106 int nedit = 0, nwhat = 0, i, in;
107 int out, isdf = 0, msgnum = 0;
108 int outputlinelen = OUTPUTLINELEN;
109 int dat[5];
110 char *cp, *cwd, *maildir, *dfolder = NULL;
111 char *dmsg = NULL, *digest = NULL, *ed = NULL;
112 char *file = NULL, *filter = NULL, *folder = NULL, *fwdmsg = NULL;
113 char *from = NULL, *to = NULL, *cc = NULL, *subject = NULL, *fcc = NULL;
114 char *form = NULL, buf[BUFSIZ];
115 char **argp, **arguments;
116 struct stat st;
117 struct msgs_array msgs = { 0, 0, NULL };
118 int buildsw = 0;
119
120 if (nmh_init(argv[0], 1)) { return 1; }
121
122 arguments = getarguments (invo_name, argc, argv, 1);
123 argp = arguments;
124
125 while ((cp = *argp++)) {
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
136 invo_name);
137 print_help (buf, switches, 1);
138 done (0);
139 case VERSIONSW:
140 print_version(invo_name);
141 done (0);
142
143 case ANNOSW:
144 anot++;
145 continue;
146 case NANNOSW:
147 anot = 0;
148 continue;
149
150 case EDITRSW:
151 if (!(ed = *argp++) || *ed == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 nedit = 0;
154 continue;
155 case NEDITSW:
156 nedit++;
157 continue;
158
159 case WHATSW:
160 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 nwhat = 0;
163 continue;
164 case BILDSW:
165 buildsw++;
166 /* FALLTHRU */
167 case NWHATSW:
168 nwhat++;
169 continue;
170
171 case FILESW:
172 if (file)
173 adios (NULL, "only one file at a time!");
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 file = path (cp, TFILE);
177 continue;
178 case FILTSW:
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 filter = getcpy (etcpath (cp));
182 mime = 0;
183 continue;
184 case FORMSW:
185 if (!(form = *argp++) || *form == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 continue;
188
189 case FRMTSW:
190 filter = getcpy (etcpath (mhlforward));
191 continue;
192 case NFRMTSW:
193 filter = NULL;
194 continue;
195
196 case INPLSW:
197 inplace++;
198 continue;
199 case NINPLSW:
200 inplace = 0;
201 continue;
202
203 case MIMESW:
204 mime++;
205 filter = NULL;
206 continue;
207 case NMIMESW:
208 mime = 0;
209 continue;
210
211 case DGSTSW:
212 if (!(cp = *argp++) || *cp == '-')
213 adios (NULL, "missing argument to %s", argp[-2]);
214 digest = mh_xstrdup(cp);
215 mime = 0;
216 continue;
217 case ISSUESW:
218 if (!(cp = *argp++) || *cp == '-')
219 adios (NULL, "missing argument to %s", argp[-2]);
220 if ((issue = atoi (cp)) < 1)
221 adios (NULL, "bad argument %s %s", argp[-2], cp);
222 continue;
223 case VOLUMSW:
224 if (!(cp = *argp++) || *cp == '-')
225 adios (NULL, "missing argument to %s", argp[-2]);
226 if ((volume = atoi (cp)) < 1)
227 adios (NULL, "bad argument %s %s", argp[-2], cp);
228 continue;
229
230 case DFOLDSW:
231 if (dfolder)
232 adios (NULL, "only one draft folder at a time!");
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
235 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
236 *cp != '@' ? TFOLDER : TSUBCWF);
237 continue;
238 case DMSGSW:
239 if (dmsg)
240 adios (NULL, "only one draft message at a time!");
241 if (!(dmsg = *argp++) || *dmsg == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
243 continue;
244 case NDFLDSW:
245 dfolder = NULL;
246 isdf = NOTOK;
247 continue;
248
249 case BITSTUFFSW:
250 dashstuff = 1; /* ternary logic */
251 continue;
252 case NBITSTUFFSW:
253 dashstuff = -1; /* ternary logic */
254 continue;
255
256 case FROMSW:
257 if (!(cp = *argp++) || *cp == '-')
258 adios (NULL, "missing argument to %s", argp[-2]);
259 from = addlist(from, cp);
260 continue;
261 case TOSW:
262 if (!(cp = *argp++) || *cp == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
264 to = addlist(to, cp);
265 continue;
266 case CCSW:
267 if (!(cp = *argp++) || *cp == '-')
268 adios (NULL, "missing argument to %s", argp[-2]);
269 cc = addlist(cc, cp);
270 continue;
271 case FCCSW:
272 if (!(cp = *argp++) || *cp == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
274 fcc = addlist(fcc, cp);
275 continue;
276 case SUBJECTSW:
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
279 subject = mh_xstrdup(cp);
280 continue;
281
282 case WIDTHSW:
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
285 if ((outputlinelen = atoi(cp)) < 10)
286 adios (NULL, "impossible width %d", outputlinelen);
287 continue;
288 }
289 }
290 if (*cp == '+' || *cp == '@') {
291 if (folder)
292 adios (NULL, "only one folder at a time!");
293 else
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 i, 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 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
500 sleep (5);
501
502 switch (child_id) {
503 case NOTOK:
504 adios ("fork", "unable to");
505
506 case OK:
507 close (pd[0]);
508 dup2 (pd[1], 1);
509 close (pd[1]);
510
511 app_msgarg(&vec, "-forwall");
512 app_msgarg(&vec, "-form");
513 app_msgarg(&vec, filter);
514
515 if (digest) {
516 app_msgarg(&vec, "-digest");
517 app_msgarg(&vec, digest);
518 app_msgarg(&vec, "-issue");
519 snprintf (buf1, sizeof(buf1), "%d", issue);
520 app_msgarg(&vec, buf1);
521 app_msgarg(&vec, "-volume");
522 snprintf (buf2, sizeof(buf2), "%d", volume);
523 app_msgarg(&vec, buf2);
524 }
525
526 /*
527 * Are we dashstuffing (quoting) the lines that begin
528 * with `-'. We use the mhl default (don't add any flag)
529 * unless the user has specified a specific flag.
530 */
531 if (dashstuff > 0)
532 app_msgarg(&vec, "-dashstuffing");
533 else if (dashstuff < 0)
534 app_msgarg(&vec, "-nodashstuffing");
535
536 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
537 if (is_selected (mp, msgnum))
538 app_msgarg(&vec, mh_xstrdup(m_name (msgnum)));
539 }
540
541 app_msgarg(&vec, NULL);
542
543 execvp (program, vec.msgs);
544 fprintf (stderr, "unable to exec ");
545 perror (mhlproc);
546 _exit (-1);
547
548 default:
549 close (pd[1]);
550 cpydata (pd[0], out, vec.msgs[0], file);
551 close (pd[0]);
552 pidXwait(child_id, mhlproc);
553 break;
554 }
555 }
556
557
558 /*
559 * Copy the messages into the draft. The messages are
560 * not filtered through the mhlproc. Do dashstuffing if
561 * necessary.
562 */
563
564 static void
565 copy_draft (int out, char *digest, char *file, int volume, int issue, int dashstuff)
566 {
567 int fd,i, msgcnt, msgnum;
568 int len, buflen;
569 char *bp, *msgnam;
570 char buffer[BUFSIZ];
571
572 msgcnt = 1;
573 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
574 if (is_selected (mp, msgnum)) {
575 if (digest) {
576 strncpy (buffer, msgnum == mp->lowsel ? delim3 : delim4,
577 sizeof(buffer));
578 } else {
579 /* Get buffer ready to go */
580 bp = buffer;
581 buflen = sizeof(buffer);
582
583 strncpy (bp, "\n-------", buflen);
584 len = strlen (bp);
585 bp += len;
586 buflen -= len;
587
588 if (msgnum == mp->lowsel) {
589 snprintf (bp, buflen, " Forwarded Message%s",
590 PLURALS(mp->numsel));
591 } else {
592 snprintf (bp, buflen, " Message %d", msgcnt);
593 }
594 len = strlen (bp);
595 bp += len;
596 buflen -= len;
597
598 strncpy (bp, "\n\n", buflen);
599 }
600 if (write (out, buffer, strlen (buffer)) < 0) {
601 advise (drft, "write");
602 }
603
604 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
605 admonish (msgnam, "unable to read message");
606 continue;
607 }
608
609 /*
610 * Copy the message. Add RFC934 quoting (dashstuffing)
611 * unless given the -nodashstuffing flag.
612 */
613 if (dashstuff >= 0)
614 cpydgst (fd, out, msgnam, file);
615 else
616 cpydata (fd, out, msgnam, file);
617
618 close (fd);
619 msgcnt++;
620 }
621 }
622
623 if (digest) {
624 strncpy (buffer, delim4, sizeof(buffer));
625 } else {
626 snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
627 PLURALS(mp->numsel));
628 }
629 if (write (out, buffer, strlen (buffer)) < 0) {
630 advise (drft, "write");
631 }
632
633 if (digest) {
634 snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
635 digest, volume, issue);
636 i = strlen (buffer);
637 for (bp = buffer + i; i > 1; i--)
638 *bp++ = '*';
639 *bp++ = '\n';
640 *bp = 0;
641 if (write (out, buffer, strlen (buffer)) < 0) {
642 advise (drft, "write");
643 }
644 }
645 }
646
647
648 /*
649 * Create a mhn composition file for forwarding message.
650 */
651
652 static void
653 copy_mime_draft (int out)
654 {
655 int msgnum;
656 char buffer[BUFSIZ];
657
658 snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
659 PLURALS(mp->numsel), mp->foldpath);
660 if (write (out, buffer, strlen (buffer)) < 0) {
661 advise (drft, "write");
662 }
663 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
664 if (is_selected (mp, msgnum)) {
665 snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
666 if (write (out, buffer, strlen (buffer)) < 0) {
667 advise (drft, "write");
668 }
669 }
670 if (write (out, "\n", 1) < 0) {
671 advise (drft, "write newline");
672 }
673 }