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