7454c8963d87ca80046dd008fa067751afbe8423
[mmh] / uip / spost.c
1
2 /*
3  * spost.c -- feed messages to sendmail
4  *
5  * This is a simpler, faster, replacement for "post" for use
6  * when "sendmail" is the transport system.
7  *
8  * This code is Copyright (c) 2002, by the authors of nmh.  See the
9  * COPYRIGHT file in the root directory of the nmh distribution for
10  * complete copyright information.
11  */
12
13 #include <h/mh.h>
14 #include <signal.h>
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
18 #include <h/tws.h>
19 #include <h/mts.h>
20 #include <h/utils.h>
21
22 #define MAX_SM_FIELD    1476    /* < largest hdr field sendmail will accept */
23 #define FCCS            10      /* max number of fccs allowed */
24
25 struct swit switches[] = {
26 #define FILTSW               0
27     { "filter filterfile", 0 },
28 #define NFILTSW              1
29     { "nofilter", 0 },
30 #define REMVSW               2
31     { "remove", 0 },
32 #define NREMVSW              3
33     { "noremove", 0 },
34 #define VERBSW               4
35     { "verbose", 0 },
36 #define NVERBSW              5
37     { "noverbose", 0 },
38 #define WATCSW               6
39     { "watch", 0 },
40 #define NWATCSW              7
41     { "nowatch", 0 },
42 #define BACKSW               8
43     { "backup", 0 },
44 #define NBACKSW              9
45     { "nobackup", 0 },
46 #define ALIASW              10
47     { "alias aliasfile", 0 },
48 #define NALIASW             11
49     { "noalias", 0 },
50 #define WIDTHSW             12
51     { "width columns", 0 },
52 #define VERSIONSW           13
53     { "version", 0 },
54 #define HELPSW              14
55     { "help", 0 },
56 #define DEBUGSW             15
57     { "debug", -5 },
58 #define DISTSW              16
59     { "dist", -4 },             /* interface from dist */
60 #define WHOMSW              17
61     { "whom", -4 },             /* interface from whom */
62 #define PUSHSW              18  /* fork to sendmail then exit */
63     { "push", -4 },
64 #define NPUSHSW             19  /* exec sendmail */
65     { "nopush", -6 },
66 #define LIBSW               20
67     { "library directory", -7 },
68 #define ANNOSW              21
69     { "idanno number", -6 },
70     { NULL, 0 }
71 };
72
73
74 /* flags for headers->flags */
75 #define HNOP    0x0000          /* just used to keep .set around */
76 #define HBAD    0x0001          /* bad header - don't let it through */
77 #define HADR    0x0002          /* header has an address field */
78 #define HSUB    0x0004          /* Subject: header */
79 #define HTRY    0x0008          /* try to send to addrs on header */
80 #define HBCC    0x0010          /* don't output this header */
81 /* #define      HMNG    0x0020  */      /* munge this header */
82 #define HNGR    0x0040          /* no groups allowed in this header */
83 #define HFCC    0x0080          /* FCC: type header */
84 #define HNIL    0x0100          /* okay for this header not to have addrs */
85 #define HIGN    0x0200          /* ignore this header */
86
87 /* flags for headers->set */
88 #define MFRM    0x0001          /* we've seen a From: */
89 #define MDAT    0x0002          /* we've seen a Date: */
90 #define MRFM    0x0004          /* we've seen a Resent-From: */
91 #define MVIS    0x0008          /* we've seen sighted addrs */
92 #define MINV    0x0010          /* we've seen blind addrs */
93 #define MRDT    0x0020          /* we've seen a Resent-Date: */
94
95 struct headers {
96     char *value;
97     unsigned int flags;
98     unsigned int set;
99 };
100
101
102 static struct headers NHeaders[] = {
103     { "Return-Path", HBAD,                0 },
104     { "Received",    HBAD,                0 },
105     { "Reply-To",    HADR|HNGR,           0 },
106     { "From",        HADR|HNGR,           MFRM },
107     { "Sender",      HADR|HBAD,           0 },
108     { "Date",        HNOP,                MDAT },
109     { "Subject",     HSUB,                0 },
110     { "To",          HADR|HTRY,           MVIS },
111     { "cc",          HADR|HTRY,           MVIS },
112     { "Bcc",         HADR|HTRY|HBCC|HNIL, MINV },
113     { "Message-Id",  HBAD,                0 },
114     { "Fcc",         HFCC,                0 },
115     { "Envelope-From",HIGN,               0 },
116     { NULL,          0,                   0 }
117 };
118
119 static struct headers RHeaders[] = {
120     { "Resent-Reply-To",   HADR|HNGR,      0 },
121     { "Resent-From",       HADR|HNGR,      MRFM },
122     { "Resent-Sender",     HADR|HBAD,      0 },
123     { "Resent-Date",       HNOP,           MRDT },
124     { "Resent-Subject",    HSUB,           0 },
125     { "Resent-To",         HADR|HTRY,      MVIS },
126     { "Resent-cc",         HADR|HTRY,      MVIS },
127     { "Resent-Bcc",        HADR|HTRY|HBCC, MINV },
128     { "Resent-Message-Id", HBAD,           0 },
129     { "Resent-Fcc",        HFCC,           0 },
130     { "Reply-To",          HADR,           0 },
131     { "Fcc",               HIGN,           0 },
132     { "Envelope-From",     HIGN,           0 },
133     { NULL,                0,              0 }
134 };
135
136
137 static short fccind = 0;        /* index into fccfold[] */
138
139 static int badmsg = 0;          /* message has bad semantics            */
140 static int verbose = 0;         /* spell it out                         */
141 static int debug = 0;           /* debugging post                       */
142 static int rmflg = 1;           /* remove temporary file when done      */
143 static int watch = 0;           /* watch the delivery process           */
144 static int backflg = 0;         /* rename input file as *.bak when done */
145 static int whomflg = 0;         /* if just checking addresses           */
146 static int pushflg = 0;         /* if going to fork to sendmail         */
147 static int aliasflg = -1;       /* if going to process aliases          */
148 static int outputlinelen=72;
149
150 static unsigned msgflags = 0;   /* what we've seen */
151
152 static enum {
153     normal, resent
154 } msgstate = normal;
155
156 static char tmpfil[] = "/tmp/pstXXXXXX";
157
158 static char from[BUFSIZ];       /* my network address */
159 static char signature[BUFSIZ];  /* my signature */
160 static char *filter = NULL;     /* the filter for BCC'ing */
161 static char *subject = NULL;    /* the subject field for BCC'ing */
162 static char *fccfold[FCCS];     /* foldernames for FCC'ing */
163
164 static struct headers *hdrtab;  /* table for the message we're doing */
165 static FILE *out;               /* output (temp) file */
166
167 extern char *sendmail;
168
169 /*
170  * external prototypes
171  */
172 extern char *getfullname (void);
173 extern char *getusername (void);
174
175 extern boolean  draft_from_masquerading;  /* defined in mts.c */
176
177 /*
178  * static prototypes
179  */
180 static void putfmt (char *, char *, FILE *);
181 static void start_headers (void);
182 static void finish_headers (FILE *);
183 static int get_header (char *, struct headers *);
184 static void putadr (char *, struct mailname *);
185 static int putone (char *, int, int);
186 static void insert_fcc (struct headers *, unsigned char *);
187 static void file (char *);
188 static void fcc (char *, char *);
189
190 #if 0
191 static void die (char *, char *, ...);
192 static void make_bcc_file (void);
193 #endif
194
195
196 int
197 main (int argc, char **argv)
198 {
199     int state, i, pid, compnum;
200     char *cp, *msg = NULL, **argp, **arguments;
201     char *sargv[16], buf[BUFSIZ], name[NAMESZ];
202     FILE *in;
203
204 #ifdef LOCALE
205     setlocale(LC_ALL, "");
206 #endif
207     invo_name = r1bindex (argv[0], '/');
208
209     /* foil search of user profile/context */
210     if (context_foil (NULL) == -1)
211         done (1);
212
213     mts_init (invo_name);
214     arguments = getarguments (invo_name, argc, argv, 0);
215     argp = arguments;
216
217     while ((cp = *argp++)) {
218         if (*cp == '-') {
219             switch (smatch (++cp, switches)) {
220                 case AMBIGSW: 
221                     ambigsw (cp, switches);
222                     done (1);
223                 case UNKWNSW: 
224                     adios (NULL, "-%s unknown", cp);
225
226                 case HELPSW: 
227                     snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
228                     print_help (buf, switches, 1);
229                     done (1);
230                 case VERSIONSW:
231                     print_version(invo_name);
232                     done (1);
233
234                 case DEBUGSW: 
235                     debug++;
236                     continue;
237
238                 case DISTSW:
239                     msgstate = resent;
240                     continue;
241
242                 case WHOMSW:
243                     whomflg++;
244                     continue;
245
246                 case FILTSW:
247                     if (!(filter = *argp++) || *filter == '-')
248                         adios (NULL, "missing argument to %s", argp[-2]);
249                     continue;
250                 case NFILTSW:
251                     filter = NULL;
252                     continue;
253                 
254                 case REMVSW: 
255                     rmflg++;
256                     continue;
257                 case NREMVSW: 
258                     rmflg = 0;
259                     continue;
260
261                 case BACKSW: 
262                     backflg++;
263                     continue;
264                 case NBACKSW: 
265                     backflg = 0;
266                     continue;
267
268                 case VERBSW: 
269                     verbose++;
270                     continue;
271                 case NVERBSW: 
272                     verbose = 0;
273                     continue;
274
275                 case WATCSW: 
276                     watch++;
277                     continue;
278                 case NWATCSW: 
279                     watch = 0;
280                     continue;
281                 
282                 case PUSHSW:
283                     pushflg++;
284                     continue;
285                 case NPUSHSW:
286                     pushflg = 0;
287                     continue;
288
289                 case ALIASW:
290                     if (!(cp = *argp++) || *cp == '-')
291                         adios (NULL, "missing argument to %s", argp[-2]);
292                     if (aliasflg < 0)
293                         alias (AliasFile);/* load default aka's */
294                     aliasflg = 1;
295                     if ((state = alias(cp)) != AK_OK)
296                         adios (NULL, "aliasing error in file %s - %s",
297                                cp, akerror(state) );
298                     continue;
299                 case NALIASW:
300                     aliasflg = 0;
301                     continue;
302
303                 case WIDTHSW:
304                     if (!(cp = *argp++) || *cp == '-')
305                         adios (NULL, "missing argument to %s", argp[-2]);
306                     outputlinelen = atoi (cp);
307                     if (outputlinelen <= 10)
308                         outputlinelen = 72;
309                     continue;
310
311                 case LIBSW:
312                     if (!(cp = *argp++) || *cp == '-')
313                         adios (NULL, "missing argument to %s", argp[-2]);
314                     /* create a minimal context */
315                     if (context_foil (cp) == -1)
316                         done(1);
317                     continue;
318
319                 case ANNOSW:
320                     /* -idanno switch ignored */
321                     if (!(cp = *argp++) || *cp == '-')
322                         adios (NULL, "missing argument to %s", argp[-2]);
323                     continue;
324             }
325         }
326         if (msg)
327             adios (NULL, "only one message at a time!");
328         else
329             msg = cp;
330     }
331
332     if (aliasflg < 0)
333         alias (AliasFile);      /* load default aka's */
334
335     if (!msg)
336         adios (NULL, "usage: %s [switches] file", invo_name);
337
338     if ((in = fopen (msg, "r")) == NULL)
339         adios (msg, "unable to open");
340
341     start_headers ();
342     if (debug) {
343         verbose++;
344         out = stdout;
345     }
346     else {
347             if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
348                 adios (tmpfil, "unable to create");
349         }
350
351     hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
352
353     for (compnum = 1, state = FLD;;) {
354         switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
355             case FLD: 
356                 compnum++;
357                 putfmt (name, buf, out);
358                 continue;
359
360             case FLDPLUS: 
361                 compnum++;
362                 cp = add (buf, cp);
363                 while (state == FLDPLUS) {
364                     state = m_getfld (state, name, buf, sizeof(buf), in);
365                     cp = add (buf, cp);
366                 }
367                 putfmt (name, cp, out);
368                 free (cp);
369                 continue;
370
371             case BODY: 
372                 finish_headers (out);
373                 fprintf (out, "\n%s", buf);
374                 if(whomflg == 0)
375                     while (state == BODY) {
376                         state = m_getfld (state, name, buf, sizeof(buf), in);
377                         fputs (buf, out);
378                     }
379                 break;
380
381             case FILEEOF: 
382                 finish_headers (out);
383                 break;
384
385             case LENERR: 
386             case FMTERR: 
387                 adios (NULL, "message format error in component #%d",
388                         compnum);
389
390             default: 
391                 adios (NULL, "getfld() returned %d", state);
392         }
393         break;
394     }
395
396     fclose (in);
397     if (backflg && !whomflg) {
398         strncpy (buf, m_backup (msg), sizeof(buf));
399         if (rename (msg, buf) == NOTOK)
400             advise (buf, "unable to rename %s to", msg);
401     }
402
403     if (debug) {
404         done (0);
405     }
406     else
407         fclose (out);
408
409     file (tmpfil);
410
411     /*
412      * re-open the temp file, unlink it and exec sendmail, giving it
413      * the msg temp file as std in.
414      */
415     if ( freopen( tmpfil, "r", stdin) == NULL)
416         adios (tmpfil, "can't reopen for sendmail");
417     if (rmflg)
418         unlink (tmpfil);
419
420     argp = sargv;
421     *argp++ = "send-mail";
422     *argp++ = "-m";     /* send to me too */
423     *argp++ = "-t";     /* read msg for recipients */
424     *argp++ = "-i";     /* don't stop on "." */
425     if (whomflg)
426         *argp++ = "-bv";
427     if (watch || verbose)
428         *argp++ = "-v";
429     *argp = NULL;
430
431     if (pushflg && !(watch || verbose)) {
432         /* fork to a child to run sendmail */
433         for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
434             sleep(5);
435         switch (pid) {
436             case NOTOK:
437                 fprintf (verbose ? stdout : stderr, "%s: can't fork to %s\n",
438                          invo_name, sendmail);
439                 exit(-1);
440             case OK:
441                 /* we're the child .. */
442                 break;
443             default:
444                 exit(0);
445         }
446     }
447     execv ( sendmail, sargv);
448     adios ( sendmail, "can't exec");
449     return 0;  /* dead code to satisfy the compiler */
450 }
451
452 /* DRAFT GENERATION */
453
454 static void
455 putfmt (char *name, char *str, FILE *out)
456 {
457     int i;
458     char *cp, *pp;
459     struct headers *hdr;
460
461     while (*str == ' ' || *str == '\t')
462         str++;
463
464     if ((i = get_header (name, hdrtab)) == NOTOK) {
465         fprintf (out, "%s: %s", name, str);
466         return;
467     }
468
469     hdr = &hdrtab[i];
470     if (hdr->flags & HIGN)
471         return;
472     if (hdr->flags & HBAD) {
473         advise (NULL, "illegal header line -- %s:", name);
474         badmsg++;
475         return;
476     }
477     msgflags |= hdr->set;
478
479     if (hdr->flags & HSUB)
480         subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
481
482     if (hdr->flags & HFCC) {
483         if ((cp = strrchr(str, '\n')))
484             *cp = 0;
485         for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
486             *cp++ = 0;
487             insert_fcc (hdr, pp);
488         }
489         insert_fcc (hdr, pp);
490         return;
491     }
492
493 #ifdef notdef
494     if (hdr->flags & HBCC) {
495         insert_bcc(str);
496         return;
497     }
498 #endif  /* notdef */
499
500     if (*str != '\n' && *str != '\0') {
501         if (aliasflg && hdr->flags & HTRY) {
502             /* this header contains address(es) that we have to do
503              * alias expansion on.  Because of the saved state in
504              * getname we have to put all the addresses into a list.
505              * We then let putadr munch on that list, possibly
506              * expanding aliases.
507              */
508             register struct mailname *f = 0;
509             register struct mailname *mp = 0;
510
511             while ((cp = getname(str))) {
512                 mp = getm( cp, NULL, 0, AD_HOST, NULL);
513                 if (f == 0) {
514                     f = mp;
515                     mp->m_next = mp;
516                 } else {
517                     mp->m_next = f->m_next;
518                     f->m_next = mp;
519                     f = mp;
520                 }
521             }
522             f = mp->m_next; mp->m_next = 0;
523             putadr( name, f );
524         } else {
525             /* The author(s) of spost decided that alias substitution wasn't
526                necessary for the non-HTRY headers.  Unfortunately, one of those
527                headers is "From:", and having alias substitution work on that is
528                extremely useful for someone with a lot of POP3 email accounts or
529                aliases.  post supports aliasing of "From:"...
530
531                Since "From:"-processing is incompletely implemented in this
532                unsupported and undocumented spost backend, I'm not going to take
533                the time to implement my new draft-From:-based email address
534                masquerading.  If I do ever implement it here, I'd almost
535                certainly want to implement "From:" line alias processing as
536                well.  -- Dan Harkless <dan-nmh@dilvish.speed.net> */
537             fprintf (out, "%s: %s", name, str );
538         }
539     }
540 }
541
542
543 static void
544 start_headers (void)
545 {
546     char *cp;
547     char sigbuf[BUFSIZ];
548
549     strncpy(from, getusername(), sizeof(from));
550
551     if ((cp = getfullname ()) && *cp) {
552         strncpy (sigbuf, cp, sizeof(sigbuf));
553         snprintf (signature, sizeof(signature), "%s <%s>", sigbuf,  from);
554     }
555     else
556         snprintf (signature, sizeof(signature), "%s",  from);
557 }
558
559
560 static void
561 finish_headers (FILE *out)
562 {
563     switch (msgstate) {
564         case normal: 
565             if (!(msgflags & MDAT))
566                 fprintf (out, "Date: %s\n", dtimenow (0));
567             
568             if (msgflags & MFRM) {
569                 /* There was already a From: in the draft.  Don't add one. */
570                 if (!draft_from_masquerading)
571                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
572                        so we'll reveal the user's actual account@thismachine
573                        address in a Sender: header (and use it as the envelope
574                        From: later). */
575                     fprintf (out, "Sender: %s\n", from);
576             }
577             else
578                 fprintf (out, "From: %s\n", signature);
579            
580 #ifdef notdef
581             if (!(msgflags & MVIS))
582                 fprintf (out, "Bcc: Blind Distribution List: ;\n");
583 #endif  /* notdef */
584             break;
585
586         case resent: 
587             if (!(msgflags & MRDT))
588                 fprintf (out, "Resent-Date: %s\n", dtimenow(0));
589             if (msgflags & MRFM) {
590                 /* There was already a Resent-From: in draft.  Don't add one. */
591                 if (!draft_from_masquerading)
592                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
593                        so we'll reveal the user's actual account@thismachine
594                        address in a Sender: header (and use it as the envelope
595                        From: later). */
596                     fprintf (out, "Resent-Sender: %s\n", from);
597             }
598             else
599                 /* Construct a Resent-From: header. */
600                 fprintf (out, "Resent-From: %s\n", signature);
601 #ifdef notdef
602             if (!(msgflags & MVIS))
603                 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
604 #endif  /* notdef */
605             break;
606     }
607
608     if (badmsg)
609         adios (NULL, "re-format message and try again");
610 }
611
612
613 static int
614 get_header (char *header, struct headers *table)
615 {
616     struct headers *h;
617
618     for (h = table; h->value; h++)
619         if (!mh_strcasecmp (header, h->value))
620             return (h - table);
621
622     return NOTOK;
623 }
624
625
626 /*
627  * output the address list for header "name".  The address list
628  * is a linked list of mailname structs.  "nl" points to the head
629  * of the list.  Alias substitution should be done on nl.
630  */
631 static void
632 putadr (char *name, struct mailname *nl)
633 {
634     register struct mailname *mp, *mp2;
635     register int linepos;
636     register char *cp;
637     int namelen;
638
639     fprintf (out, "%s: ", name);
640     namelen = strlen(name) + 2;
641     linepos = namelen;
642
643     for (mp = nl; mp; ) {
644         if (linepos > MAX_SM_FIELD) {
645                 fprintf (out, "\n%s: ", name);
646                 linepos = namelen;
647         }
648         if (mp->m_nohost) {
649             /* a local name - see if it's an alias */
650             cp = akvalue(mp->m_mbox);
651             if (cp == mp->m_mbox)
652                 /* wasn't an alias - use what the user typed */
653                 linepos = putone( mp->m_text, linepos, namelen );
654             else
655                 /* an alias - expand it */
656                 while ((cp = getname(cp))) {
657                     if (linepos > MAX_SM_FIELD) {
658                             fprintf (out, "\n%s: ", name);
659                             linepos = namelen;
660                     }
661                     mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
662                     if (akvisible()) {
663                         mp2->m_pers = getcpy(mp->m_mbox);
664                         linepos = putone( adrformat(mp2), linepos, namelen );
665                     } else {
666                         linepos = putone( mp2->m_text, linepos, namelen );
667                     }
668                     mnfree( mp2 );
669                 }
670         } else {
671             /* not a local name - use what the user typed */
672             linepos = putone( mp->m_text, linepos, namelen );
673         }
674         mp2 = mp;
675         mp = mp->m_next;
676         mnfree( mp2 );
677     }
678     putc( '\n', out );
679 }
680
681 static int
682 putone (char *adr, int pos, int indent)
683 {
684     register int len;
685     static int linepos;
686
687     len = strlen( adr );
688     if (pos == indent)
689         linepos = pos;
690     else if ( linepos+len > outputlinelen ) {
691         fprintf ( out, ",\n%*s", indent, "");
692         linepos = indent;
693         pos += indent + 2;
694     }
695     else {
696         fputs( ", ", out );
697         linepos += 2;
698         pos += 2;
699     }
700     fputs( adr, out );
701
702     linepos += len;
703     return (pos+len);
704 }
705
706
707 static void
708 insert_fcc (struct headers *hdr, unsigned char *pp)
709 {
710     unsigned char   *cp;
711
712     for (cp = pp; isspace (*cp); cp++)
713         continue;
714     for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
715         continue;
716     if (pp >= cp)
717         *++pp = 0;
718     if (*cp == 0)
719         return;
720
721     if (fccind >= FCCS)
722         adios (NULL, "too many %ss", hdr->value);
723     fccfold[fccind++] = getcpy (cp);
724 }
725
726 #if 0
727 /* BCC GENERATION */
728
729 static void
730 make_bcc_file (void)
731 {
732     pid_t child_id;
733     int fd, i, status;
734     char *vec[6];
735     FILE * in, *out;
736
737     fd = mkstemp(bccfil);
738     if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
739         adios (bccfil, "unable to create");
740     chmod (bccfil, 0600);
741
742     fprintf (out, "Date: %s\n", dtimenow (0));
743     if (msgflags & MFRM) {
744       /* There was already a From: in the draft.  Don't add one. */
745       if (!draft_from_masquerading)
746         /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
747            so we'll reveal the user's actual account@thismachine
748            address in a Sender: header (and use it as the envelope
749            From: later). */
750         fprintf (out, "Sender: %s\n", from);
751     }
752     else
753       /* Construct a From: header. */
754       fprintf (out, "From: %s\n", signature);
755     if (subject)
756         fprintf (out, "Subject: %s", subject);
757     fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
758     fflush (out);
759
760     if (filter == NULL) {
761         if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
762             adios (NULL, "unable to re-open");
763         cpydgst (fd, fileno (out), tmpfil, bccfil);
764         close (fd);
765     }
766     else {
767         vec[0] = r1bindex (mhlproc, '/');
768
769         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
770             sleep (5);
771         switch (child_id) {
772             case NOTOK: 
773                 adios ("vfork", "unable to");
774
775             case OK: 
776                 dup2 (fileno (out), 1);
777
778                 i = 1;
779                 vec[i++] = "-forward";
780                 vec[i++] = "-form";
781                 vec[i++] = filter;
782                 vec[i++] = tmpfil;
783                 vec[i] = NULL;
784
785                 execvp (mhlproc, vec);
786                 adios (mhlproc, "unable to exec");
787
788             default: 
789                 if (status = pidwait(child_id, OK))
790                     admonish (NULL, "%s lost (status=0%o)", vec[0], status);
791                 break;
792         }
793     }
794
795     fseek (out, 0L, SEEK_END);
796     fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
797     fclose (out);
798 }
799 #endif  /* if 0 */
800
801 /* FCC INTERACTION */
802
803 static void
804 file (char *path)
805 {
806     int i;
807
808     if (fccind == 0)
809         return;
810
811     for (i = 0; i < fccind; i++)
812         if (whomflg)
813             printf ("Fcc: %s\n", fccfold[i]);
814         else
815             fcc (path, fccfold[i]);
816 }
817
818
819 static void
820 fcc (char *file, char *folder)
821 {
822     pid_t child_id;
823     int i, status;
824     char fold[BUFSIZ];
825
826     if (verbose)
827         printf ("%sFcc: %s\n", msgstate == resent ? "Resent-" : "", folder);
828     fflush (stdout);
829
830     for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
831         sleep (5);
832     switch (child_id) {
833         case NOTOK: 
834             if (!verbose)
835                 fprintf (stderr, "  %sFcc %s: ",
836                         msgstate == resent ? "Resent-" : "", folder);
837             fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
838             break;
839
840         case OK: 
841             snprintf (fold, sizeof(fold), "%s%s",
842                     *folder == '+' || *folder == '@' ? "" : "+", folder);
843             execlp (fileproc, r1bindex (fileproc, '/'),
844                     "-link", "-file", file, fold, NULL);
845             _exit (-1);
846
847         default: 
848             if ((status = pidwait(child_id, OK))) {
849                 if (!verbose)
850                     fprintf (stderr, "  %sFcc %s: ",
851                             msgstate == resent ? "Resent-" : "", folder);
852                 fprintf (verbose ? stdout : stderr,
853                         " errored (0%o)\n", status);
854             }
855     }
856
857     fflush (stdout);
858 }
859
860
861 #if 0
862
863 /*
864  * TERMINATION
865  */
866
867 static void
868 die (char *what, char *fmt, ...)
869 {
870     va_list ap;
871
872     va_start(ap, fmt);
873     advertise (what, NULL, fmt, ap);
874     va_end(ap);
875
876     done (1);
877 }
878 #endif