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