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