Return type of (*done)() changed to void. default_done() replaced by
[mmh] / uip / post.c
1
2 /*
3  * post.c -- enter messages into the mail transport system
4  *
5  * $Id$
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 <fcntl.h>
14 #include <h/signals.h>
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
18 #include <h/mime.h>
19 #include <h/utils.h>
20
21 #include <h/tws.h>
22 #include <h/mts.h>
23
24 #include <errno.h>
25 #include <setjmp.h>
26 #include <signal.h>
27
28 #ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
30 # include <time.h>
31 #else
32 # ifdef TM_IN_SYS_TIME
33 #  include <sys/time.h>
34 # else
35 #  include <time.h>
36 # endif
37 #endif
38
39 #ifdef SMTPMTS
40 # include <mts/smtp/smtp.h>
41 #endif
42
43 #ifndef CYRUS_SASL
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a)  0
47 #endif /* CYRUS_SASL */
48
49 #define FCCS            10      /* max number of fccs allowed */
50
51 #define uptolow(c)      ((isalpha(c) && isupper (c)) ? tolower (c) : c)
52
53 /* In the following array of structures, the numeric second field of the
54    structures (minchars) is apparently used like this:
55
56    -# : Switch can be abbreviated to # characters; switch hidden in -help.
57    0  : Switch can't be abbreviated;               switch shown in -help.
58    #  : Switch can be abbreviated to # characters; switch shown in -help. */
59
60 static struct swit switches[] = {
61 #define ALIASW                    0
62     { "alias aliasfile", 0 },
63 #define CHKSW                     1
64     { "check", -5 },                    /* interface from whom */
65 #define NCHKSW                    2
66     { "nocheck", -7 },                  /* interface from whom */
67 #define DEBUGSW                   3
68     { "debug", -5 },
69 #define DISTSW                    4
70     { "dist", -4 },                     /* interface from dist */
71 #define FILTSW                    5
72     { "filter filterfile", 0 },
73 #define NFILTSW                   6
74     { "nofilter", 0 },
75 #define FRMTSW                    7
76     { "format", 0 },
77 #define NFRMTSW                   8
78     { "noformat", 0 },
79 #define LIBSW                     9
80     { "library directory", -7 },        /* interface from send, whom */
81 #define MIMESW                   10
82     { "mime", 0 },
83 #define NMIMESW                  11
84     { "nomime", 0 },
85 #define MSGDSW                   12
86     { "msgid", 0 },
87 #define NMSGDSW                  13
88     { "nomsgid", 0 },
89 #define VERBSW                   14
90     { "verbose", 0 },
91 #define NVERBSW                  15
92     { "noverbose", 0 },
93 #define WATCSW                   16
94     { "watch", 0 },
95 #define NWATCSW                  17
96     { "nowatch", 0 },
97 #define WHOMSW                   18
98     { "whom", -4 },                     /* interface from whom */
99 #define WIDTHSW                  19
100     { "width columns", 0 },
101 #define VERSIONSW                20
102     { "version", 0 },
103 #define HELPSW                   21
104     { "help", 0 },
105 #define BITSTUFFSW               22
106     { "dashstuffing", -12 },            /* should we dashstuff BCC messages? */
107 #define NBITSTUFFSW              23
108     { "nodashstuffing", -14 },
109 #define MAILSW                   24
110     { "mail", -4 },                     /* specify MAIL smtp mode */
111 #define SAMLSW                   25
112     { "saml", -4 },                     /* specify SAML smtp mode */
113 #define SENDSW                   26
114     { "send", -4 },                     /* specify SEND smtp mode */
115 #define SOMLSW                   27
116     { "soml", -4 },                     /* specify SOML smtp mode */
117 #define ANNOSW                   28
118     { "idanno number", -6 },            /* interface from send    */
119 #define DLVRSW                   29
120     { "deliver address-list", -7 },
121 #define CLIESW                   30
122     { "client host", -6 },
123 #define SERVSW                   31
124     { "server host", -6 },              /* specify alternate SMTP server */
125 #define SNOOPSW                  32
126     { "snoop", -5 },                    /* snoop the SMTP transaction */
127 #define FILLSW                   33
128     { "fill-in file", -7 },
129 #define FILLUSW                  34
130     { "fill-up", -7 },
131 #define PARTSW                   35
132     { "partno", -6 },
133 #define QUEUESW                  36
134     { "queued", -6 },
135 #define SASLSW                   37
136     { "sasl", SASLminc(-4) },
137 #define SASLMECHSW               38
138     { "saslmech", SASLminc(-5) },
139 #define USERSW                   39
140     { "user", SASLminc(-4) },
141     { NULL, 0 }
142 };
143
144
145 struct headers {
146     char *value;
147     unsigned int flags;
148     unsigned int set;
149 };
150
151 /*
152  * flags for headers->flags
153  */
154 #define HNOP  0x0000            /* just used to keep .set around          */
155 #define HBAD  0x0001            /* bad header - don't let it through      */
156 #define HADR  0x0002            /* header has an address field            */
157 #define HSUB  0x0004            /* Subject: header                        */
158 #define HTRY  0x0008            /* try to send to addrs on header         */
159 #define HBCC  0x0010            /* don't output this header               */
160 #define HMNG  0x0020            /* munge this header                      */
161 #define HNGR  0x0040            /* no groups allowed in this header       */
162 #define HFCC  0x0080            /* FCC: type header                       */
163 #define HNIL  0x0100            /* okay for this header not to have addrs */
164 #define HIGN  0x0200            /* ignore this header                     */
165 #define HDCC  0x0400            /* another undocumented feature           */
166
167 /*
168  * flags for headers->set
169  */
170 #define MFRM  0x0001            /* we've seen a From:        */
171 #define MDAT  0x0002            /* we've seen a Date:        */
172 #define MRFM  0x0004            /* we've seen a Resent-From: */
173 #define MVIS  0x0008            /* we've seen sighted addrs  */
174 #define MINV  0x0010            /* we've seen blind addrs    */
175
176
177 static struct headers NHeaders[] = {
178     { "Return-Path", HBAD,                0 },
179     { "Received",    HBAD,                0 },
180     { "Reply-To",    HADR|HNGR,           0 },
181     { "From",        HADR|HNGR,           MFRM },
182     { "Sender",      HADR|HBAD,           0 },
183     { "Date",        HBAD,                0 },
184     { "Subject",     HSUB,                0 },
185     { "To",          HADR|HTRY,           MVIS },
186     { "cc",          HADR|HTRY,           MVIS },
187     { "Bcc",         HADR|HTRY|HBCC|HNIL, MINV },
188     { "Dcc",         HADR|HTRY|HDCC|HNIL, MVIS },       /* sorta cc & bcc combined */
189     { "Message-ID",  HBAD,                0 },
190     { "Fcc",         HFCC,                0 },
191     { NULL,          0,                   0 }
192 };
193
194 static struct headers RHeaders[] = {
195     { "Resent-Reply-To",   HADR|HNGR,           0 },
196     { "Resent-From",       HADR|HNGR,           MRFM },
197     { "Resent-Sender",     HADR|HBAD,           0 },
198     { "Resent-Date",       HBAD,                0 },
199     { "Resent-Subject",    HSUB,                0 },
200     { "Resent-To",         HADR|HTRY,           MVIS },
201     { "Resent-cc",         HADR|HTRY,           MVIS },
202     { "Resent-Bcc",        HADR|HTRY|HBCC,      MINV },
203     { "Resent-Message-ID", HBAD,                0 },
204     { "Resent-Fcc",        HFCC,                0 },
205     { "Reply-To",          HADR,                0 },
206     { "From",              HADR|HNGR,           MFRM },
207     { "Sender",            HADR|HNGR,           0 },
208     { "Date",              HNOP,                MDAT },
209     { "To",                HADR|HNIL,           0 },
210     { "cc",                HADR|HNIL,           0 },
211     { "Bcc",               HADR|HTRY|HBCC|HNIL, 0 },
212     { "Fcc",               HIGN,                0 },
213     { NULL,                0,                   0 }
214 };
215
216 static short fccind = 0;        /* index into fccfold[] */
217 static short outputlinelen = OUTPUTLINELEN;
218
219 static int pfd = NOTOK;         /* fd to write annotation list to        */
220 static uid_t myuid= -1;         /* my user id                            */
221 static gid_t mygid= -1;         /* my group id                           */
222 static int recipients = 0;      /* how many people will get a copy       */
223 static int unkadr = 0;          /* how many of those were unknown        */
224 static int badadr = 0;          /* number of bad addrs                   */
225 static int badmsg = 0;          /* message has bad semantics             */
226 static int verbose = 0;         /* spell it out                          */
227 static int format = 1;          /* format addresses                      */
228 static int mime = 0;            /* use MIME-style encapsulations for Bcc */
229 static int msgid = 0;           /* add msgid                             */
230 static int debug = 0;           /* debugging post                        */
231 static int watch = 0;           /* watch the delivery process            */
232 static int whomsw = 0;          /* we are whom not post                  */
233 static int checksw = 0;         /* whom -check                           */
234 static int linepos=0;           /* putadr()'s position on the line       */
235 static int nameoutput=0;        /* putadr() has output header name       */
236 static int sasl=0;              /* Use SASL auth for SMTP                */
237 static char *saslmech=NULL;     /* Force use of particular SASL mech     */
238 static char *user=NULL;         /* Authenticate as this user             */
239
240 static unsigned msgflags = 0;   /* what we've seen */
241
242 #define NORMAL 0
243 #define RESENT 1
244 static int msgstate = NORMAL;
245
246 static time_t tclock = 0;       /* the time we started (more or less) */
247
248 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
249
250 static char tmpfil[BUFSIZ];
251 static char bccfil[BUFSIZ];
252
253 static char from[BUFSIZ];       /* my network address            */
254 static char signature[BUFSIZ];  /* my signature                  */
255 static char *filter = NULL;     /* the filter for BCC'ing        */
256 static char *subject = NULL;    /* the subject field for BCC'ing */
257 static char *fccfold[FCCS];     /* foldernames for FCC'ing       */
258
259 static struct headers  *hdrtab; /* table for the message we're doing */
260
261 static struct mailname localaddrs={NULL};       /* local addrs     */
262 static struct mailname netaddrs={NULL};         /* network addrs   */
263 static struct mailname uuaddrs={NULL};          /* uucp addrs      */
264 static struct mailname tmpaddrs={NULL};         /* temporary queue */
265
266 #ifdef SMTPMTS
267 static int snoop      = 0;
268 static int smtpmode   = S_MAIL;
269 static char *clientsw = NULL;
270 static char *serversw = NULL;
271
272 extern struct smtp sm_reply;
273 #endif /* SMTPMTS */
274
275 static char prefix[] = "----- =_aaaaaaaaaa";
276
277 static int fill_up = 0;
278 static char *fill_in = NULL;
279 static char *partno = NULL;
280 static int queued = 0;
281
282 extern boolean  draft_from_masquerading;  /* defined in mts.c */
283
284 /*
285  * static prototypes
286  */
287 static void putfmt (char *, char *, FILE *);
288 static void start_headers (void);
289 static void finish_headers (FILE *);
290 static int get_header (char *, struct headers *);
291 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
292 static void putgrp (char *, char *, FILE *, unsigned int);
293 static int insert (struct mailname *);
294 static void pl (void);
295 static void anno (void);
296 static int annoaux (struct mailname *);
297 static void insert_fcc (struct headers *, unsigned char *);
298 static void make_bcc_file (int);
299 static void verify_all_addresses (int);
300 static void chkadr (void);
301 static void sigon (void);
302 static void sigoff (void);
303 static void p_refile (char *);
304 static void fcc (char *, char *);
305 static void die (char *, char *, ...);
306 static void post (char *, int, int);
307 static void do_text (char *file, int fd);
308 static void do_an_address (struct mailname *, int);
309 static void do_addresses (int, int);
310 static int find_prefix (void);
311
312
313 int
314 main (int argc, char **argv)
315 {
316     int state, compnum, dashstuff = 0;
317     char *cp, *msg = NULL, **argp, **arguments;
318     char buf[BUFSIZ], name[NAMESZ];
319     FILE *in, *out;
320
321 #ifdef LOCALE
322     setlocale(LC_ALL, "");
323 #endif
324     invo_name = r1bindex (argv[0], '/');
325
326     /* foil search of user profile/context */
327     if (context_foil (NULL) == -1)
328         done (1);
329
330     mts_init (invo_name);
331     arguments = getarguments (invo_name, argc, argv, 0);
332     argp = arguments;
333
334     while ((cp = *argp++)) {
335         if (*cp == '-') {
336             switch (smatch (++cp, switches)) {
337                 case AMBIGSW: 
338                     ambigsw (cp, switches);
339                     done (1);
340                 case UNKWNSW: 
341                     adios (NULL, "-%s unknown", cp);
342
343                 case HELPSW: 
344                     snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
345                     print_help (buf, switches, 0);
346                     done (1);
347                 case VERSIONSW:
348                     print_version(invo_name);
349                     done (1);
350
351                 case LIBSW:
352                     if (!(cp = *argp++) || *cp == '-')
353                         adios (NULL, "missing argument to %s", argp[-2]);
354                     /* create a minimal context */
355                     if (context_foil (cp) == -1)
356                         done (1);
357                     continue;
358
359                 case ALIASW: 
360                     if (!(cp = *argp++) || *cp == '-')
361                         adios (NULL, "missing argument to %s", argp[-2]);
362                     if ((state = alias (cp)) != AK_OK)
363                         adios (NULL, "aliasing error in %s - %s",
364                                 cp, akerror (state));
365                     continue;
366
367                 case CHKSW: 
368                     checksw++;
369                     continue;
370                 case NCHKSW: 
371                     checksw = 0;
372                     continue;
373
374                 case DEBUGSW: 
375                     debug++;
376                     continue;
377
378                 case DISTSW:
379                     msgstate = RESENT;
380                     continue;
381
382                 case FILTSW:
383                     if (!(filter = *argp++) || *filter == '-')
384                         adios (NULL, "missing argument to %s", argp[-2]);
385                     mime = 0;
386                     continue;
387                 case NFILTSW:
388                     filter = NULL;
389                     continue;
390                 
391                 case FRMTSW: 
392                     format++;
393                     continue;
394                 case NFRMTSW: 
395                     format = 0;
396                     continue;
397
398                 case BITSTUFFSW:
399                     dashstuff = 1;
400                     continue;
401                 case NBITSTUFFSW:
402                     dashstuff = -1;
403                     continue;
404
405                 case MIMESW:
406                     mime++;
407                     filter = NULL;
408                     continue;
409                 case NMIMESW: 
410                     mime = 0;
411                     continue;
412
413                 case MSGDSW: 
414                     msgid++;
415                     continue;
416                 case NMSGDSW: 
417                     msgid = 0;
418                     continue;
419
420                 case VERBSW: 
421                     verbose++;
422                     continue;
423                 case NVERBSW: 
424                     verbose = 0;
425                     continue;
426
427                 case WATCSW: 
428                     watch++;
429                     continue;
430                 case NWATCSW: 
431                     watch = 0;
432                     continue;
433
434                 case WHOMSW: 
435                     whomsw++;
436                     continue;
437
438                 case WIDTHSW: 
439                     if (!(cp = *argp++) || *cp == '-')
440                         adios (NULL, "missing argument to %s", argp[-2]);
441                     if ((outputlinelen = atoi (cp)) < 10)
442                         adios (NULL, "impossible width %d", outputlinelen);
443                     continue;
444
445                 case ANNOSW: 
446                     if (!(cp = *argp++) || *cp == '-')
447                         adios (NULL, "missing argument to %s", argp[-2]);
448                     if ((pfd = atoi (cp)) <= 2)
449                         adios (NULL, "bad argument %s %s", argp[-2], cp);
450                     continue;
451
452                 case DLVRSW:
453                     if (!(cp = *argp++) || *cp == '-')
454                         adios (NULL, "missing argument to %s", argp[-2]);
455                     continue;
456
457 #ifndef SMTPMTS
458                 case CLIESW:
459                 case SERVSW:
460                     if (!(cp = *argp++) || *cp == '-')
461                         adios (NULL, "missing argument to %s", argp[-2]);
462                     continue;
463
464                 case SNOOPSW:
465                     continue;
466 #else /* SMTPMTS */
467                 case MAILSW:
468                     smtpmode = S_MAIL;
469                     continue;
470                 case SAMLSW:
471                     smtpmode = S_SAML;
472                     continue;
473                 case SOMLSW:
474                     smtpmode = S_SOML;
475                     continue;
476                 case SENDSW:
477                     smtpmode = S_SEND;
478                     continue;
479                 case CLIESW:
480                     if (!(clientsw = *argp++) || *clientsw == '-')
481                         adios (NULL, "missing argument to %s", argp[-2]);
482                     continue;
483                 case SERVSW:
484                     if (!(serversw = *argp++) || *serversw == '-')
485                         adios (NULL, "missing argument to %s", argp[-2]);
486                     continue;
487                 case SNOOPSW:
488                     snoop++;
489                     continue;
490 #endif /* SMTPMTS */
491
492                 case FILLSW:
493                     if (!(fill_in = *argp++) || *fill_in == '-')
494                         adios (NULL, "missing argument to %s", argp[-2]);
495                     continue;
496                 case FILLUSW:
497                     fill_up++;
498                     continue;
499                 case PARTSW:
500                     if (!(partno = *argp++) || *partno == '-')
501                         adios (NULL, "missing argument to %s", argp[-2]);
502                     continue;
503
504                 case QUEUESW:
505                     queued++;
506                     continue;
507                 
508                 case SASLSW:
509                     sasl++;
510                     continue;
511                 
512                 case SASLMECHSW:
513                     if (!(saslmech = *argp++) || *saslmech == '-')
514                         adios (NULL, "missing argument to %s", argp[-2]);
515                     continue;
516                 
517                 case USERSW:
518                     if (!(user = *argp++) || *user == '-')
519                         adios (NULL, "missing argument to %s", argp[-2]);
520                     continue;
521             }
522         }
523         if (msg)
524             adios (NULL, "only one message at a time!");
525         else
526             msg = cp;
527     }
528
529     alias (AliasFile);
530
531     if (!msg)
532         adios (NULL, "usage: %s [switches] file", invo_name);
533
534     if (outputlinelen < 10)
535         adios (NULL, "impossible width %d", outputlinelen);
536
537     if ((in = fopen (msg, "r")) == NULL)
538         adios (msg, "unable to open");
539
540     start_headers ();
541     if (debug) {
542         verbose++;
543         discard (out = stdout); /* XXX: reference discard() to help loader */
544     } else {
545         if (whomsw) {
546             if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
547                 adios ("/dev/null", "unable to open");
548         } else {
549             strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
550                 sizeof(tmpfil));
551             if ((out = fopen (tmpfil, "w")) == NULL) {
552                 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
553                 if ((out = fopen (tmpfil, "w")) == NULL)
554                     adios (tmpfil, "unable to create");
555             }
556             chmod (tmpfil, 0600);
557         }
558     }
559
560     hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
561
562     for (compnum = 1, state = FLD;;) {
563         switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
564             case FLD: 
565             case FLDEOF: 
566             case FLDPLUS: 
567                 compnum++;
568                 cp = add (buf, NULL);
569                 while (state == FLDPLUS) {
570                     state = m_getfld (state, name, buf, sizeof(buf), in);
571                     cp = add (buf, cp);
572                 }
573                 putfmt (name, cp, out);
574                 free (cp);
575                 if (state != FLDEOF)
576                     continue;
577                 finish_headers (out);
578                 break;
579
580             case BODY: 
581             case BODYEOF: 
582                 finish_headers (out);
583                 if (whomsw && !fill_in)
584                     break;
585                 fprintf (out, "\n%s", buf);
586                 while (state == BODY) {
587                     state = m_getfld (state, name, buf, sizeof(buf), in);
588                     fputs (buf, out);
589                 }
590                 break;
591
592             case FILEEOF: 
593                 finish_headers (out);
594                 break;
595
596             case LENERR: 
597             case FMTERR: 
598                 adios (NULL, "message format error in component #%d", compnum);
599
600             default: 
601                 adios (NULL, "getfld() returned %d", state);
602         }
603         break;
604     }
605
606     if (pfd != NOTOK)
607         anno ();
608     fclose (in);
609
610     if (debug) {
611         pl ();
612         done (0);
613     } else {
614         fclose (out);
615     }
616
617     /* If we are doing a "whom" check */
618     if (whomsw) {
619         if (!fill_up)
620             verify_all_addresses (1);
621         done (0);
622     }
623
624     if (msgflags & MINV) {
625         make_bcc_file (dashstuff);
626         if (msgflags & MVIS) {
627             verify_all_addresses (verbose);
628             post (tmpfil, 0, verbose);
629         }
630         post (bccfil, 1, verbose);
631         unlink (bccfil);
632     } else {
633         post (tmpfil, 0, isatty (1));
634     }
635
636     p_refile (tmpfil);
637     unlink (tmpfil);
638
639     if (verbose)
640         printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
641                 partno);
642     done (0);
643     return 1;
644 }
645
646
647 /*
648  * DRAFT GENERATION
649  */
650
651 static void
652 putfmt (char *name, char *str, FILE *out)
653 {
654     int count, grp, i, keep;
655     char *cp, *pp, *qp;
656     char namep[BUFSIZ];
657     struct mailname *mp, *np;
658     struct headers *hdr;
659
660     while (*str == ' ' || *str == '\t')
661         str++;
662
663     if (msgstate == NORMAL && uprf (name, "resent")) {
664         advise (NULL, "illegal header line -- %s:", name);
665         badmsg++;
666         return;
667     }
668
669     if ((i = get_header (name, hdrtab)) == NOTOK) {
670         fprintf (out, "%s: %s", name, str);
671         return;
672     }
673
674     hdr = &hdrtab[i];
675     if (hdr->flags & HIGN) {
676         if (fill_in)
677             fprintf (out, "%s: %s", name, str);
678         return;
679     }
680     if (hdr->flags & HBAD) {
681         if (fill_in)
682             fprintf (out, "%s: %s", name, str);
683         else {
684             advise (NULL, "illegal header line -- %s:", name);
685             badmsg++;
686         }
687         return;
688     }
689     msgflags |= (hdr->set & ~(MVIS | MINV));
690
691     if (hdr->flags & HSUB)
692         subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
693     if (hdr->flags & HFCC) {
694         if (fill_in) {
695             fprintf (out, "%s: %s", name, str);
696             return;
697         }
698
699         if ((cp = strrchr(str, '\n')))
700             *cp = 0;
701         for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
702             *cp++ = 0;
703             insert_fcc (hdr, pp);
704         }
705         insert_fcc (hdr, pp);
706         return;
707     }
708
709     if (!(hdr->flags & HADR)) {
710         fprintf (out, "%s: %s", name, str);
711         return;
712     }
713
714     tmpaddrs.m_next = NULL;
715     for (count = 0; (cp = getname (str)); count++)
716         if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
717             if (tmpaddrs.m_next)
718                 np->m_next = mp;
719             else
720                 tmpaddrs.m_next = mp;
721             np = mp;
722         }
723         else
724             if (hdr->flags & HTRY)
725                 badadr++;
726             else
727                 badmsg++;
728
729     if (count < 1) {
730         if (hdr->flags & HNIL)
731             fprintf (out, "%s: %s", name, str);
732         else {
733 #ifdef notdef
734             advise (NULL, "%s: field requires at least one address", name);
735             badmsg++;
736 #endif /* notdef */
737         }
738         return;
739     }
740
741     nameoutput = linepos = 0;
742     snprintf (namep, sizeof(namep), "%s%s",
743                 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
744
745     for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
746         if (mp->m_nohost) {     /* also used to test (hdr->flags & HTRY) */
747             /* The address doesn't include a host, so it might be an alias. */
748             pp = akvalue (mp->m_mbox);  /* do mh alias substitution */
749             qp = akvisible () ? mp->m_mbox : "";
750             np = mp;
751             if (np->m_gname)
752                 putgrp (namep, np->m_gname, out, hdr->flags);
753             while ((cp = getname (pp))) {
754                 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
755                     badadr++;
756                     continue;
757                 }
758
759                 if (draft_from_masquerading && ((msgstate == RESENT)
760                                                 ? (hdr->set & MRFM)
761                                                 : (hdr->set & MFRM)))
762                     /* The user manually specified a [Resent-]From: address in
763                        their draft and the "masquerade:" line in mts.conf
764                        doesn't contain "draft_from", so we'll set things up to
765                        use the actual email address embedded in the draft
766                        [Resent-]From: (after alias substitution, and without the
767                        GECOS full name or angle brackets) as the envelope
768                        From:. */
769                     strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
770
771                 if (hdr->flags & HBCC)
772                     mp->m_bcc++;
773                 if (np->m_ingrp)
774                     mp->m_ingrp = np->m_ingrp;
775                 else
776                     if (mp->m_gname)
777                         putgrp (namep, mp->m_gname, out, hdr->flags);
778                 if (mp->m_ingrp)
779                     grp++;
780                 if (putadr (namep, qp, mp, out, hdr->flags))
781                     msgflags |= (hdr->set & (MVIS | MINV));
782                 else
783                     mnfree (mp);
784             }
785             mp = np;
786             np = np->m_next;
787             mnfree (mp);
788         }
789         else {
790             /* Address includes a host, so no alias substitution is needed. */
791             if (draft_from_masquerading && ((msgstate == RESENT)
792                                             ? (hdr->set & MRFM)
793                                             : (hdr->set & MFRM)))
794                 /* The user manually specified a [Resent-]From: address in
795                    their draft and the "masquerade:" line in mts.conf
796                    doesn't contain "draft_from", so we'll set things up to
797                    use the actual email address embedded in the draft
798                    [Resent-]From: (after alias substitution, and without the
799                    GECOS full name or angle brackets) as the envelope
800                    From:. */
801                 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
802
803             if (hdr->flags & HBCC)
804                 mp->m_bcc++;
805             if (mp->m_gname)
806                 putgrp (namep, mp->m_gname, out, hdr->flags);
807             if (mp->m_ingrp)
808                 grp++;
809             keep = putadr (namep, "", mp, out, hdr->flags);
810             np = mp->m_next;
811             if (keep) {
812                 mp->m_next = NULL;
813                 msgflags |= (hdr->set & (MVIS | MINV));
814             }
815             else
816                 mnfree (mp);
817         }
818
819     if (grp > 0 && (hdr->flags & HNGR)) {
820         advise (NULL, "%s: field does not allow groups", name);
821         badmsg++;
822     }
823     if (linepos) {
824         if (fill_in && grp > 0)
825             putc (';', out);
826         putc ('\n', out);
827     }
828 }
829
830
831 static void
832 start_headers (void)
833 {
834     unsigned char  *cp;
835     char myhost[BUFSIZ], sigbuf[BUFSIZ];
836     struct mailname *mp;
837
838     myuid = getuid ();
839     mygid = getgid ();
840     time (&tclock);
841
842     strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
843     strncpy (myhost, LocalName (), sizeof(myhost));
844
845     for (cp = myhost; *cp; cp++)
846         *cp = uptolow (*cp);
847
848     if ((cp = getfullname ()) && *cp) {
849         strncpy (sigbuf, cp, sizeof(sigbuf));
850         snprintf (signature, sizeof(signature), "%s <%s>",
851                 sigbuf, adrsprintf (NULL, NULL));
852         if ((cp = getname (signature)) == NULL)
853             adios (NULL, "getname () failed -- you lose extraordinarily big");
854         if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
855             adios (NULL, "bad signature '%s'", sigbuf);
856         mnfree (mp);
857         while (getname (""))
858             continue;
859     } else {
860         strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
861     }
862 }
863
864
865 /*
866  * Now that we've outputted the header fields in the draft
867  * message, we will now output any remaining header fields
868  * that we need to add/create.
869  */
870
871 static void
872 finish_headers (FILE *out)
873 {
874     switch (msgstate) {
875         case NORMAL: 
876             if (whomsw && !fill_up)
877                 break;
878
879             fprintf (out, "Date: %s\n", dtime (&tclock, 0));
880             if (msgid)
881                 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
882                         (int) getpid (), (long) tclock, LocalName ());
883             if (msgflags & MFRM) {
884                 /* There was already a From: in the draft.  Don't add one. */
885                 if (!draft_from_masquerading)
886                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
887                        so we'll reveal the user's actual account@thismachine
888                        address in a Sender: header (and use it as the envelope
889                        From: later). */
890                     fprintf (out, "Sender: %s\n", from);
891             }
892             else
893                 /* Construct a From: header. */
894                 fprintf (out, "From: %s\n", signature);
895             if (whomsw)
896                 break;
897
898             if (!(msgflags & MVIS))
899                 fprintf (out, "Bcc: Blind Distribution List: ;\n");
900             break;
901
902         case RESENT: 
903             if (!(msgflags & MDAT)) {
904                 advise (NULL, "message has no Date: header");
905                 badmsg++;
906             }
907             if (!(msgflags & MFRM)) {
908                 advise (NULL, "message has no From: header");
909                 badmsg++;
910             }
911             if (whomsw && !fill_up)
912                 break;
913
914             fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
915             if (msgid)
916                 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
917                         (int) getpid (), (long) tclock, LocalName ());
918             if (msgflags & MRFM) {
919                 /* There was already a Resent-From: in draft.  Don't add one. */
920                 if (!draft_from_masquerading)
921                     /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
922                        so we'll reveal the user's actual account@thismachine
923                        address in a Sender: header (and use it as the envelope
924                        From: later). */
925                     fprintf (out, "Resent-Sender: %s\n", from);
926             }
927             else
928                 /* Construct a Resent-From: header. */
929                 fprintf (out, "Resent-From: %s\n", signature);
930             if (whomsw)
931                 break;
932             if (!(msgflags & MVIS))
933                 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
934             break;
935     }
936
937     if (badmsg)
938         adios (NULL, "re-format message and try again");
939     if (!recipients)
940         adios (NULL, "no addressees");
941 }
942
943
944 static int
945 get_header (char *header, struct headers *table)
946 {
947     struct headers *h;
948
949     for (h = table; h->value; h++)
950         if (!mh_strcasecmp (header, h->value))
951             return (h - table);
952
953     return NOTOK;
954 }
955
956
957 static int
958 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
959 {
960     int len;
961     char *cp;
962     char buffer[BUFSIZ];
963
964     if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
965         return 0;
966     if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
967         return 1;
968
969     if (!nameoutput) {
970         fprintf (out, "%s: ", name);
971         linepos += (nameoutput = strlen (name) + 2);
972     }
973
974     if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
975         mp->m_pers = getcpy (aka);
976     if (format) {
977         if (mp->m_gname && !fill_in) {
978             snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
979             cp = buffer;
980         } else {
981             cp = adrformat (mp);
982         }
983     } else {
984         cp = mp->m_text;
985     }
986     len = strlen (cp);
987
988     if (linepos != nameoutput) {
989         if (len + linepos + 2 > outputlinelen)
990             fprintf (out, ",\n%*s", linepos = nameoutput, "");
991         else {
992             fputs (", ", out);
993             linepos += 2;
994         }
995     }
996
997     fputs (cp, out);
998     linepos += len;
999
1000     return (flags & HTRY);
1001 }
1002
1003
1004 static void
1005 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1006 {
1007     int len;
1008     char *cp;
1009
1010     if (!fill_in && (flags & HBCC))
1011         return;
1012
1013     if (!nameoutput) {
1014         fprintf (out, "%s: ", name);
1015         linepos += (nameoutput = strlen (name) + 2);
1016         if (fill_in)
1017             linepos -= strlen (group);
1018     }
1019
1020     cp = fill_in ? group : concat (group, ";", NULL);
1021     len = strlen (cp);
1022
1023     if (linepos > nameoutput) {
1024         if (len + linepos + 2 > outputlinelen) {
1025             fprintf (out, ",\n%*s", nameoutput, "");
1026             linepos = nameoutput;
1027         }
1028         else {
1029             fputs (", ", out);
1030             linepos += 2;
1031         }
1032     }
1033
1034     fputs (cp, out);
1035     linepos += len;
1036 }
1037
1038
1039 static int
1040 insert (struct mailname *np)
1041 {
1042     struct mailname *mp;
1043
1044     if (np->m_mbox == NULL)
1045         return 0;
1046
1047     for (mp = np->m_type == LOCALHOST ? &localaddrs
1048             : np->m_type == UUCPHOST ? &uuaddrs
1049             : &netaddrs;
1050             mp->m_next;
1051             mp = mp->m_next)
1052         if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1053                 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1054                 && np->m_bcc == mp->m_next->m_bcc)
1055             return 0;
1056
1057     mp->m_next = np;
1058     recipients++;
1059     return 1;
1060 }
1061
1062
1063 static void
1064 pl (void)
1065 {
1066     int i;
1067     struct mailname *mp;
1068
1069     printf ("-------\n\t-- Addresses --\nlocal:\t");
1070     for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1071         printf ("%s%s%s", mp->m_mbox,
1072                 mp->m_bcc ? "[BCC]" : "",
1073                 mp->m_next ? ",\n\t" : "");
1074
1075     printf ("\nnet:\t");
1076     for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1077         printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1078                 mp->m_mbox, mp->m_host,
1079                 mp->m_bcc ? "[BCC]" : "",
1080                 mp->m_next ? ",\n\t" : "");
1081
1082     printf ("\nuucp:\t");
1083     for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1084         printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1085                 mp->m_bcc ? "[BCC]" : "",
1086                 mp->m_next ? ",\n\t" : "");
1087
1088     printf ("\n\t-- Folder Copies --\nfcc:\t");
1089     for (i = 0; i < fccind; i++)
1090         printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1091     printf ("\n");
1092 }
1093
1094
1095 static void
1096 anno (void)
1097 {
1098     struct mailname *mp;
1099
1100     for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1101         if (annoaux (mp) == NOTOK)
1102             goto oops;
1103
1104     for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1105         if (annoaux (mp) == NOTOK)
1106             goto oops;
1107
1108     for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1109         if (annoaux (mp) == NOTOK)
1110             break;
1111
1112 oops: ;
1113     close (pfd);
1114     pfd = NOTOK;
1115 }
1116
1117
1118 static int
1119 annoaux (struct mailname *mp)
1120 {
1121     int i;
1122     char buffer[BUFSIZ];
1123
1124     snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1125     i = strlen (buffer);
1126
1127     return (write (pfd, buffer, i) == i ? OK : NOTOK);
1128 }
1129
1130
1131 static void
1132 insert_fcc (struct headers *hdr, unsigned char *pp)
1133 {
1134     unsigned char *cp;
1135
1136     for (cp = pp; isspace (*cp); cp++)
1137         continue;
1138     for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1139         continue;
1140     if (pp >= cp)
1141         *++pp = 0;
1142     if (*cp == 0)
1143         return;
1144
1145     if (fccind >= FCCS)
1146         adios (NULL, "too many %ss", hdr->value);
1147     fccfold[fccind++] = getcpy (cp);
1148 }
1149
1150 /*
1151  * BCC GENERATION
1152  */
1153
1154 static void
1155 make_bcc_file (int dashstuff)
1156 {
1157     int fd, i;
1158     pid_t child_id;
1159     char *vec[6];
1160     FILE *out;
1161
1162     strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1163     if ((out = fopen (bccfil, "w")) == NULL)
1164         adios (bccfil, "unable to create");
1165     chmod (bccfil, 0600);
1166
1167     fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1168     if (msgid)
1169         fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1170                 (int) getpid (), (long) tclock, LocalName ());
1171     if (msgflags & MFRM) {
1172       /* There was already a From: in the draft.  Don't add one. */
1173       if (!draft_from_masquerading)
1174         /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1175            so we'll reveal the user's actual account@thismachine
1176            address in a Sender: header (and use it as the envelope
1177            From: later). */
1178         fprintf (out, "Sender: %s\n", from);
1179     }
1180     else
1181       /* Construct a From: header. */
1182       fprintf (out, "From: %s\n", signature);
1183     if (subject)
1184         fprintf (out, "Subject: %s", subject);
1185     fprintf (out, "BCC:\n");
1186
1187     /*
1188      * Use MIME encapsulation for Bcc messages
1189      */
1190     if (mime) {
1191         char *cp;
1192
1193         /*
1194          * Check if any lines in the message clash with the
1195          * prefix for the MIME multipart separator.  If there
1196          * is a clash, increment one of the letters in the
1197          * prefix and check again.
1198          */
1199         if ((cp = strchr(prefix, 'a')) == NULL)
1200             adios (NULL, "lost prefix start");
1201         while (find_prefix () == NOTOK) {
1202             if (*cp < 'z')
1203                 (*cp)++;
1204             else
1205                 if (*++cp == 0)
1206                     adios (NULL, "can't find a unique delimiter string");
1207                 else
1208                     (*cp)++;
1209         }
1210
1211         fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1212                  VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1213         fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1214     } else {
1215         fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1216     }
1217
1218     fflush (out);
1219
1220     /*
1221      * Do mhl filtering of Bcc messages instead
1222      * of MIME encapsulation.
1223      */
1224     if (filter != NULL) {
1225         vec[0] = r1bindex (mhlproc, '/');
1226
1227         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1228             sleep (5);
1229         switch (child_id) {
1230             case NOTOK: 
1231                 adios ("fork", "unable to");
1232
1233             case OK: 
1234                 dup2 (fileno (out), 1);
1235
1236                 i = 1;
1237                 vec[i++] = "-forward";
1238                 vec[i++] = "-form";
1239                 vec[i++] = filter;
1240                 vec[i++] = tmpfil;
1241
1242                 /* was the flag -[no]dashstuffing specified? */
1243                 if (dashstuff > 0)
1244                     vec[i++] = "-dashstuffing";
1245                 else if (dashstuff < 0)
1246                     vec[i++] = "-nodashstuffing";
1247                 vec[i] = NULL;
1248
1249                 execvp (mhlproc, vec);
1250                 fprintf (stderr, "unable to exec ");
1251                 perror (mhlproc);
1252                 _exit (-1);
1253
1254             default: 
1255                 pidXwait (child_id, mhlproc);
1256                 break;
1257         }
1258     } else {
1259         if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1260             adios (tmpfil, "unable to re-open");
1261
1262         /*
1263          * If using MIME encapsulation, or if the -nodashstuffing
1264          * flag was given, then just copy message.  Else do
1265          * RFC934 quoting (dashstuffing).
1266          */
1267         if (mime || dashstuff < 0)
1268             cpydata (fd, fileno (out), tmpfil, bccfil);
1269         else
1270             cpydgst (fd, fileno (out), tmpfil, bccfil);
1271         close (fd);
1272     }
1273
1274     fseek (out, 0L, SEEK_END);
1275     if (mime)
1276         fprintf (out, "\n--%s--\n", prefix);
1277     else
1278         fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1279     fclose (out);
1280 }
1281
1282
1283 /*
1284  * Scan message to check if any lines clash with
1285  * the prefix of the MIME multipart separator.
1286  */
1287
1288 static int
1289 find_prefix (void)
1290 {
1291     int len, result;
1292     unsigned char buffer[BUFSIZ];
1293     FILE *in;
1294
1295     if ((in = fopen (tmpfil, "r")) == NULL)
1296         adios (tmpfil, "unable to re-open");
1297
1298     len = strlen (prefix);
1299
1300     result = OK;
1301     while (fgets (buffer, sizeof(buffer) - 1, in))
1302         if (buffer[0] == '-' && buffer[1] == '-') {
1303             unsigned char *cp;
1304
1305             for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1306                 if (!isspace (*cp))
1307                     break;
1308             *++cp = '\0';
1309             if (strcmp (buffer + 2, prefix) == 0) {
1310                 result = NOTOK;
1311                 break;
1312             }
1313         }
1314
1315     fclose (in);
1316     return result;
1317 }
1318
1319
1320 #define plural(x) (x == 1 ? "" : "s")
1321
1322 static void
1323 chkadr (void)
1324 {
1325     if (badadr && unkadr)
1326         die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1327                 badadr, plural (badadr), unkadr, plural (badadr));
1328     if (badadr)
1329         die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1330     if (unkadr)
1331         die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1332 }
1333
1334
1335 static void
1336 do_addresses (int bccque, int talk)
1337 {
1338     int retval;
1339     int state;
1340     struct mailname *lp;
1341
1342     state = 0;
1343     for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1344         if (lp->m_bcc ? bccque : !bccque) {
1345             if (talk && !state)
1346                 printf ("  -- Local Recipients --\n");
1347             do_an_address (lp, talk);
1348             state++;
1349         }
1350
1351     state = 0;
1352     for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1353         if (lp->m_bcc ? bccque : !bccque) {
1354             if (talk && !state)
1355                 printf ("  -- UUCP Recipients --\n");
1356             do_an_address (lp, talk);
1357             state++;
1358         }
1359
1360     state = 0;
1361     for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1362         if (lp->m_bcc ? bccque : !bccque) {
1363             if (talk && !state)
1364                 printf ("  -- Network Recipients --\n");
1365             do_an_address (lp, talk);
1366             state++;
1367         }
1368
1369     chkadr ();
1370
1371 #ifdef SMTPMTS
1372     if (rp_isbad (retval = sm_waend ()))
1373         die (NULL, "problem ending addresses; %s", rp_string (retval));
1374 #endif /* SMTPMTS */
1375 }
1376
1377
1378 /*
1379  * MTS-SPECIFIC INTERACTION
1380  */
1381
1382
1383 /*
1384  * SENDMAIL/SMTP routines
1385  */
1386
1387 #ifdef SMTPMTS
1388
1389 static void
1390 post (char *file, int bccque, int talk)
1391 {
1392     int fd, onex;
1393     int retval;
1394
1395     onex = !(msgflags & MINV) || bccque;
1396     if (verbose) {
1397         if (msgflags & MINV)
1398             printf (" -- Posting for %s Recipients --\n",
1399                     bccque ? "Blind" : "Sighted");
1400         else
1401             printf (" -- Posting for All Recipients --\n");
1402     }
1403
1404     sigon ();
1405
1406     if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1407                                     snoop, onex, queued, sasl, saslmech,
1408                                     user))
1409             || rp_isbad (retval = sm_winit (smtpmode, from)))
1410         die (NULL, "problem initializing server; %s", rp_string (retval));
1411
1412     do_addresses (bccque, talk && verbose);
1413     if ((fd = open (file, O_RDONLY)) == NOTOK)
1414         die (file, "unable to re-open");
1415     do_text (file, fd);
1416     close (fd);
1417     fflush (stdout);
1418
1419     sm_end (onex ? OK : DONE);
1420     sigoff ();
1421
1422     if (verbose) {
1423         if (msgflags & MINV)
1424             printf (" -- %s Recipient Copies Posted --\n",
1425                     bccque ? "Blind" : "Sighted");
1426         else
1427             printf (" -- Recipient Copies Posted --\n");
1428     }
1429
1430     fflush (stdout);
1431 }
1432
1433
1434 /* Address Verification */
1435
1436 static void
1437 verify_all_addresses (int talk)
1438 {
1439     int retval;
1440     struct mailname *lp;
1441
1442     sigon ();
1443
1444     if (!whomsw || checksw)
1445         if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
1446                                         queued, sasl, saslmech, user))
1447                 || rp_isbad (retval = sm_winit (smtpmode, from)))
1448             die (NULL, "problem initializing server; %s", rp_string (retval));
1449
1450     if (talk && !whomsw)
1451         printf (" -- Address Verification --\n");
1452     if (talk && localaddrs.m_next)
1453         printf ("  -- Local Recipients --\n");
1454     for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1455         do_an_address (lp, talk);
1456
1457     if (talk && uuaddrs.m_next)
1458         printf ("  -- UUCP Recipients --\n");
1459     for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1460         do_an_address (lp, talk);
1461
1462     if (talk && netaddrs.m_next)
1463         printf ("  -- Network Recipients --\n");
1464     for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1465         do_an_address (lp, talk);
1466
1467     chkadr ();
1468     if (talk && !whomsw)
1469         printf (" -- Address Verification Successful --\n");
1470
1471     if (!whomsw || checksw)
1472         sm_end (DONE);
1473
1474     fflush (stdout);
1475     sigoff ();
1476 }
1477
1478
1479 static void
1480 do_an_address (struct mailname *lp, int talk)
1481 {
1482     int retval;
1483     char *mbox, *host;
1484     char addr[BUFSIZ];
1485
1486     switch (lp->m_type) {
1487         case LOCALHOST: 
1488             mbox = lp->m_mbox;
1489             host = lp->m_host;
1490             strncpy (addr, mbox, sizeof(addr));
1491             break;
1492
1493         case UUCPHOST: 
1494             mbox = auxformat (lp, 0);
1495             host = NULL;
1496             snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1497             break;
1498
1499         default:                /* let SendMail decide if the host is bad  */
1500             mbox = lp->m_mbox;
1501             host = lp->m_host;
1502             snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1503             break;
1504     }
1505
1506     if (talk)
1507         printf ("  %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1508
1509     if (whomsw && !checksw) {
1510         putchar ('\n');
1511         return;
1512     }
1513     if (talk)
1514         printf (": ");
1515     fflush (stdout);
1516
1517     switch (retval = sm_wadr (mbox, host,
1518                          lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1519         case RP_OK: 
1520             if (talk)
1521                 printf ("address ok\n");
1522             break;
1523
1524         case RP_NO: 
1525         case RP_USER: 
1526             if (!talk)
1527                 fprintf (stderr, "  %s: ", addr);
1528             fprintf (talk ? stdout : stderr, "loses; %s\n",
1529                         rp_string (retval));
1530             unkadr++;
1531             break;
1532
1533         default: 
1534             if (!talk)
1535                 fprintf (stderr, "  %s: ", addr);
1536             die (NULL, "unexpected response; %s", rp_string (retval));
1537     }
1538
1539     fflush (stdout);
1540 }
1541
1542
1543 static void
1544 do_text (char *file, int fd)
1545 {
1546     int retval, state;
1547     char buf[BUFSIZ];
1548
1549     lseek (fd, (off_t) 0, SEEK_SET);
1550
1551     while ((state = read (fd, buf, sizeof(buf))) > 0) {
1552         if (rp_isbad (retval = sm_wtxt (buf, state)))
1553             die (NULL, "problem writing text; %s\n", rp_string (retval));
1554     }
1555
1556     if (state == NOTOK)
1557         die (file, "problem reading from");
1558
1559     switch (retval = sm_wtend ()) {
1560         case RP_OK: 
1561             break;
1562
1563         case RP_NO: 
1564         case RP_NDEL: 
1565             die (NULL, "posting failed; %s", rp_string (retval));
1566
1567         default: 
1568             die (NULL, "unexpected response; %s", rp_string (retval));
1569     }
1570 }
1571
1572 #endif /* SMTPMTS */
1573
1574
1575 /*
1576  * SIGNAL HANDLING
1577  */
1578
1579 static RETSIGTYPE
1580 sigser (int i)
1581 {
1582 #ifndef RELIABLE_SIGNALS
1583     SIGNAL (i, SIG_IGN);
1584 #endif
1585
1586     unlink (tmpfil);
1587     if (msgflags & MINV)
1588         unlink (bccfil);
1589
1590 #ifdef SMTPMTS
1591     if (!whomsw || checksw)
1592         sm_end (NOTOK);
1593 #endif /* SMTPMTS */
1594
1595     done (1);
1596 }
1597
1598
1599 static void
1600 sigon (void)
1601 {
1602     if (debug)
1603         return;
1604
1605     hstat = SIGNAL2 (SIGHUP, sigser);
1606     istat = SIGNAL2 (SIGINT, sigser);
1607     qstat = SIGNAL2 (SIGQUIT, sigser);
1608     tstat = SIGNAL2 (SIGTERM, sigser);
1609 }
1610
1611
1612 static void
1613 sigoff (void)
1614 {
1615     if (debug)
1616         return;
1617
1618     SIGNAL (SIGHUP, hstat);
1619     SIGNAL (SIGINT, istat);
1620     SIGNAL (SIGQUIT, qstat);
1621     SIGNAL (SIGTERM, tstat);
1622 }
1623
1624 /*
1625  * FCC INTERACTION
1626  */
1627
1628 static void
1629 p_refile (char *file)
1630 {
1631     int i;
1632
1633     if (fccind == 0)
1634         return;
1635
1636     if (verbose)
1637         printf (" -- Filing Folder Copies --\n");
1638     for (i = 0; i < fccind; i++)
1639         fcc (file, fccfold[i]);
1640     if (verbose)
1641         printf (" -- Folder Copies Filed --\n");
1642 }
1643
1644
1645 /*
1646  * Call the `fileproc' to add the file to the folder.
1647  */
1648
1649 static void
1650 fcc (char *file, char *folder)
1651 {
1652     pid_t child_id;
1653     int i, status;
1654     char fold[BUFSIZ];
1655
1656     if (verbose)
1657         printf ("  %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1658     fflush (stdout);
1659
1660     for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1661         sleep (5);
1662
1663     switch (child_id) {
1664         case NOTOK: 
1665             if (!verbose)
1666                 fprintf (stderr, "  %sFcc %s: ",
1667                         msgstate == RESENT ? "Resent-" : "", folder);
1668             fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1669             break;
1670
1671         case OK: 
1672             /* see if we need to add `+' */
1673             snprintf (fold, sizeof(fold), "%s%s",
1674                     *folder == '+' || *folder == '@' ? "" : "+", folder);
1675
1676             /* now exec the fileproc */
1677             execlp (fileproc, r1bindex (fileproc, '/'),
1678                     "-link", "-file", file, fold, NULL);
1679             _exit (-1);
1680
1681         default: 
1682             if ((status = pidwait (child_id, OK))) {
1683                 if (!verbose)
1684                     fprintf (stderr, "  %sFcc %s: ",
1685                             msgstate == RESENT ? "Resent-" : "", folder);
1686                 pidstatus (status, verbose ? stdout : stderr, NULL);
1687             } else {
1688                 if (verbose)
1689                     printf ("folder ok\n");
1690             }
1691     }
1692
1693     fflush (stdout);
1694 }
1695
1696 /*
1697  * TERMINATION
1698  */
1699
1700 static void
1701 die (char *what, char *fmt, ...)
1702 {
1703     va_list ap;
1704
1705     unlink (tmpfil);
1706     if (msgflags & MINV)
1707         unlink (bccfil);
1708
1709 #ifdef SMTPMTS
1710     if (!whomsw || checksw)
1711         sm_end (NOTOK);
1712 #endif /* SMTPMTS */
1713
1714     va_start(ap, fmt);
1715     advertise (what, NULL, fmt, ap);
1716     va_end(ap);
1717     done (1);
1718 }