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