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