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