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