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