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