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