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