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