3 * post.c -- enter messages into the mail transport system
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
13 #include <h/dropsbr.h>
16 #include <zotnet/tws/tws.h>
17 #include <zotnet/mts/mts.h>
24 # include <mts/mmdf/util.h>
25 # include <mts/mmdf/mmdf.h>
29 * Currently smtp and sendmail use
30 * the same interface for posting.
37 # include <mts/smtp/smtp.h>
41 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
44 #define FCCS 10 /* max number of fccs allowed */
46 static struct swit switches[] = {
48 { "alias aliasfile", 0 },
50 { "check", -5 }, /* interface from whom */
52 { "nocheck", -7 }, /* interface from whom */
56 { "dist", -4 }, /* interface from dist */
58 { "filter filterfile", 0 },
66 { "library directory", -7 }, /* interface from send, whom */
84 { "whom", -4 }, /* interface from whom */
86 { "width columns", 0 },
92 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
93 #define NBITSTUFFSW 23
94 { "nodashstuffing", -14 },
96 { "mail", -4 }, /* specify MAIL smtp mode */
98 { "saml", -4 }, /* specify SAML smtp mode */
100 { "send", -4 }, /* specify SEND smtp mode */
102 { "soml", -4 }, /* specify SOML smtp mode */
104 { "idanno number", -6 }, /* interface from send */
106 { "deliver address-list", -7 },
108 { "client host", -6 },
110 { "server host", -6 }, /* specify alternate SMTP server */
112 { "snoop", -5 }, /* snoop the SMTP transaction */
114 { "fill-in file", -7 },
132 * flags for headers->flags
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 */
148 * flags for headers->set
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 */
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 },
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 },
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 },
188 { "Sender", HADR|HNGR|HMNG, 0 },
190 { "Sender", HADR|HNGR, 0 },
192 { "Date", HNOP, MDAT },
193 { "To", HADR|HNIL, 0 },
194 { "cc", HADR|HNIL, 0 },
195 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
200 static short fccind = 0; /* index into fccfold[] */
201 static short outputlinelen = OUTPUTLINELEN;
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 */
221 static unsigned msgflags = 0; /* what we've seen */
225 static int msgstate = NORMAL;
227 static time_t tclock = 0; /* the time we started (more or less) */
229 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
231 static char tmpfil[BUFSIZ];
232 static char bccfil[BUFSIZ];
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 */
240 static struct headers *hdrtab; /* table for the message we're doing */
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 */
248 static char *submitmode = "m"; /* deliver to mailbox only */
249 static char submitopts[6] = "vl"; /* initial options for submit */
253 static int snoop = 0;
254 static int smtpmode = S_MAIL;
255 static char *clientsw = NULL;
256 static char *serversw = NULL;
258 extern struct smtp sm_reply;
261 static char prefix[] = "----- =_aaaaaaaaaa";
263 static int fill_up = 0;
264 static char *fill_in = NULL;
265 static char *partno = NULL;
266 static int queued = 0;
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);
298 main (int argc, char **argv)
300 int state, compnum, dashstuff = 0;
301 char *cp, *msg = NULL, **argp, **arguments;
302 char buf[BUFSIZ], name[NAMESZ];
306 setlocale(LC_ALL, "");
308 invo_name = r1bindex (argv[0], '/');
310 /* foil search of user profile/context */
311 if (context_foil (NULL) == -1)
314 mts_init (invo_name);
315 arguments = getarguments (invo_name, argc, argv, 0);
318 #if defined(MMDFMTS) && defined(MMDFII)
319 mmdf_init (invo_name);
320 #endif /* MMDFMTS and MMDFII */
322 while ((cp = *argp++)) {
324 switch (smatch (++cp, switches)) {
326 ambigsw (cp, switches);
329 adios (NULL, "-%s unknown", cp);
332 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
333 print_help (buf, switches, 0);
336 print_version(invo_name);
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)
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));
371 if (!(filter = *argp++) || *filter == '-')
372 adios (NULL, "missing argument to %s", argp[-2]);
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);
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);
444 case SOMLSW: /* for right now, sigh... */
454 if (!(cp = *argp++) || *cp == '-')
455 adios (NULL, "missing argument to %s", argp[-2]);
461 if (!(cp = *argp++) || *cp == '-')
462 adios (NULL, "missing argument to %s", argp[-2]);
481 if (!(clientsw = *argp++) || *clientsw == '-')
482 adios (NULL, "missing argument to %s", argp[-2]);
485 if (!(serversw = *argp++) || *serversw == '-')
486 adios (NULL, "missing argument to %s", argp[-2]);
494 if (!(fill_in = *argp++) || *fill_in == '-')
495 adios (NULL, "missing argument to %s", argp[-2]);
501 if (!(partno = *argp++) || *partno == '-')
502 adios (NULL, "missing argument to %s", argp[-2]);
511 adios (NULL, "only one message at a time!");
519 adios (NULL, "usage: %s [switches] file", invo_name);
521 if (outputlinelen < 10)
522 adios (NULL, "impossible width %d", outputlinelen);
524 if ((in = fopen (msg, "r")) == NULL)
525 adios (msg, "unable to open");
530 discard (out = stdout); /* XXX: reference discard() to help loader */
533 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
534 adios ("/dev/null", "unable to open");
536 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
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");
543 chmod (tmpfil, 0600);
547 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
549 for (compnum = 1, state = FLD;;) {
550 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
555 cp = add (buf, NULL);
556 while (state == FLDPLUS) {
557 state = m_getfld (state, name, buf, sizeof(buf), in);
560 putfmt (name, cp, out);
564 finish_headers (out);
569 finish_headers (out);
570 if (whomsw && !fill_in)
572 fprintf (out, "\n%s", buf);
573 while (state == BODY) {
574 state = m_getfld (state, name, buf, sizeof(buf), in);
580 finish_headers (out);
585 adios (NULL, "message format error in component #%d", compnum);
588 adios (NULL, "getfld() returned %d", state);
604 /* If we are doing a "whom" check */
607 verify_all_addresses (1);
612 strcat (submitopts, submitmode);
614 strcat (submitopts, "nw");
617 if (msgflags & MINV) {
618 make_bcc_file (dashstuff);
619 if (msgflags & MVIS) {
620 verify_all_addresses (verbose);
621 post (tmpfil, 0, verbose);
623 post (bccfil, 1, verbose);
626 post (tmpfil, 0, isatty (1));
633 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
644 putfmt (char *name, char *str, FILE *out)
646 int count, grp, i, keep;
649 struct mailname *mp, *np;
652 while (*str == ' ' || *str == '\t')
655 if (msgstate == NORMAL && uprf (name, "resent")) {
656 advise (NULL, "illegal header line -- %s:", name);
661 if ((i = get_header (name, hdrtab)) == NOTOK) {
662 fprintf (out, "%s: %s", name, str);
667 if (hdr->flags & HIGN) {
669 fprintf (out, "%s: %s", name, str);
672 if (hdr->flags & HBAD) {
674 fprintf (out, "%s: %s", name, str);
676 advise (NULL, "illegal header line -- %s:", name);
681 msgflags |= (hdr->set & ~(MVIS | MINV));
683 if (hdr->flags & HSUB)
684 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
685 if (hdr->flags & HFCC) {
687 fprintf (out, "%s: %s", name, str);
691 if ((cp = strrchr(str, '\n')))
693 for (cp = pp = str; cp = strchr(pp, ','); pp = cp) {
695 insert_fcc (hdr, pp);
697 insert_fcc (hdr, pp);
701 if (!(hdr->flags & HADR)) {
702 fprintf (out, "%s: %s", name, str);
706 tmpaddrs.m_next = NULL;
707 for (count = 0; cp = getname (str); count++)
708 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
712 tmpaddrs.m_next = mp;
716 if (hdr->flags & HTRY)
722 if (hdr->flags & HNIL)
723 fprintf (out, "%s: %s", name, str);
726 advise (NULL, "%s: field requires at least one address", name);
733 nameoutput = linepos = 0;
734 snprintf (namep, sizeof(namep), "%s%s",
735 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
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 : "";
743 putgrp (namep, np->m_gname, out, hdr->flags);
744 while ((cp = getname (pp))) {
745 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
749 if (hdr->flags & HBCC)
752 mp->m_ingrp = np->m_ingrp;
755 putgrp (namep, mp->m_gname, out, hdr->flags);
758 if (putadr (namep, qp, mp, out, hdr->flags))
759 msgflags |= (hdr->set & (MVIS | MINV));
768 if (hdr->flags & HBCC)
771 putgrp (namep, mp->m_gname, out, hdr->flags);
774 keep = putadr (namep, "", mp, out, hdr->flags);
778 msgflags |= (hdr->set & (MVIS | MINV));
784 if (grp > 0 && (hdr->flags & HNGR)) {
785 advise (NULL, "%s: field does not allow groups", name);
789 if (fill_in && grp > 0)
800 char myhost[BUFSIZ], sigbuf[BUFSIZ];
807 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
808 strncpy (myhost, LocalName (), sizeof(myhost));
810 for (cp = myhost; *cp; cp++)
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);
825 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
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.
837 finish_headers (FILE *out)
841 if (whomsw && !fill_up)
844 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
846 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
847 (int) getpid (), tclock, LocalName ());
849 fprintf (out, "Sender: %s\n", from);
851 fprintf (out, "From: %s\n", signature);
855 if (!(msgflags & MVIS))
856 fprintf (out, "Bcc: Blind Distribution List: ;\n");
860 if (!(msgflags & MDAT)) {
861 advise (NULL, "message has no Date: header");
864 if (!(msgflags & MFRM)) {
865 advise (NULL, "message has no From: header");
868 if (whomsw && !fill_up)
871 #ifdef MMDFI /* sigh */
872 fprintf (out, "Sender: %s\n", from);
875 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
877 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
878 (int) getpid (), tclock, LocalName ());
880 fprintf (out, "Resent-Sender: %s\n", from);
882 fprintf (out, "Resent-From: %s\n", signature);
885 if (!(msgflags & MVIS))
886 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
891 adios (NULL, "re-format message and try again");
893 adios (NULL, "no addressees");
898 get_header (char *header, struct headers *table)
902 for (h = table; h->value; h++)
903 if (!strcasecmp (header, h->value))
911 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
917 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
919 if (!fill_in && (flags & (HBCC | HDCC)) || mp->m_ingrp)
923 fprintf (out, "%s: ", name);
924 linepos += (nameoutput = strlen (name) + 2);
927 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
928 mp->m_pers = getcpy (aka);
930 if (mp->m_gname && !fill_in) {
931 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
941 if (linepos != nameoutput)
942 if (len + linepos + 2 > outputlinelen)
943 fprintf (out, ",\n%*s", linepos = nameoutput, "");
952 return (flags & HTRY);
957 putgrp (char *name, char *group, FILE *out, unsigned int flags)
962 if (!fill_in && (flags & HBCC))
966 fprintf (out, "%s: ", name);
967 linepos += (nameoutput = strlen (name) + 2);
969 linepos -= strlen (group);
972 cp = fill_in ? group : concat (group, ";", NULL);
975 if (linepos > nameoutput)
976 if (len + linepos + 2 > outputlinelen) {
977 fprintf (out, ",\n%*s", nameoutput, "");
978 linepos = nameoutput;
991 insert (struct mailname *np)
995 if (np->m_mbox == NULL)
998 for (mp = np->m_type == LOCALHOST ? &localaddrs
999 : np->m_type == UUCPHOST ? &uuaddrs
1003 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1004 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1005 && np->m_bcc == mp->m_next->m_bcc)
1018 struct mailname *mp;
1020 printf ("-------\n\t-- Addresses --\nlocal:\t");
1021 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1022 printf ("%s%s%s", mp->m_mbox,
1023 mp->m_bcc ? "[BCC]" : "",
1024 mp->m_next ? ",\n\t" : "");
1026 printf ("\nnet:\t");
1027 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1028 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1029 mp->m_mbox, mp->m_host,
1030 mp->m_bcc ? "[BCC]" : "",
1031 mp->m_next ? ",\n\t" : "");
1033 printf ("\nuucp:\t");
1034 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1035 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1036 mp->m_bcc ? "[BCC]" : "",
1037 mp->m_next ? ",\n\t" : "");
1039 printf ("\n\t-- Folder Copies --\nfcc:\t");
1040 for (i = 0; i < fccind; i++)
1041 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1049 struct mailname *mp;
1051 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1052 if (annoaux (mp) == NOTOK)
1055 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1056 if (annoaux (mp) == NOTOK)
1059 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1060 if (annoaux (mp) == NOTOK)
1070 annoaux (struct mailname *mp)
1073 char buffer[BUFSIZ];
1075 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1076 i = strlen (buffer);
1078 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1083 insert_fcc (struct headers *hdr, char *pp)
1087 for (cp = pp; isspace (*cp); cp++)
1089 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1097 adios (NULL, "too many %ss", hdr->value);
1098 fccfold[fccind++] = getcpy (cp);
1106 make_bcc_file (int dashstuff)
1113 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1114 if ((out = fopen (bccfil, "w")) == NULL)
1115 adios (bccfil, "unable to create");
1116 chmod (bccfil, 0600);
1118 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1120 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1121 (int) getpid (), tclock, LocalName ());
1122 fprintf (out, "From: %s\n", signature);
1124 fprintf (out, "Subject: %s", subject);
1125 fprintf (out, "BCC:\n");
1128 * Use MIME encapsulation for Bcc messages
1134 * Check if any lines in the message clash with the
1135 * prefix for the MIME multipart separator. If there
1136 * is a clash, increment one of the letters in the
1137 * prefix and check again.
1139 if ((cp = strchr(prefix, 'a')) == NULL)
1140 adios (NULL, "lost prefix start");
1141 while (find_prefix () == NOTOK) {
1146 adios (NULL, "can't find a unique delimiter string");
1151 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1152 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1153 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1155 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1161 * Do mhl filtering of Bcc messages instead
1162 * of MIME encapsulation.
1164 if (filter != NULL) {
1165 vec[0] = r1bindex (mhlproc, '/');
1167 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1171 adios ("fork", "unable to");
1174 dup2 (fileno (out), 1);
1177 vec[i++] = "-forward";
1182 /* was the flag -[no]dashstuffing specified? */
1184 vec[i++] = "-dashstuffing";
1185 else if (dashstuff < 0)
1186 vec[i++] = "-nodashstuffing";
1189 execvp (mhlproc, vec);
1190 fprintf (stderr, "unable to exec ");
1195 pidXwait (child_id, mhlproc);
1199 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1200 adios (tmpfil, "unable to re-open");
1203 * If using MIME encapsulation, or if the -nodashstuffing
1204 * flag was given, then just copy message. Else do
1205 * RFC934 quoting (dashstuffing).
1207 if (mime || dashstuff < 0)
1208 cpydata (fd, fileno (out), tmpfil, bccfil);
1210 cpydgst (fd, fileno (out), tmpfil, bccfil);
1214 fseek (out, 0L, SEEK_END);
1216 fprintf (out, "\n--%s--\n", prefix);
1218 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1224 * Scan message to check if any lines clash with
1225 * the prefix of the MIME multipart separator.
1232 char buffer[BUFSIZ];
1235 if ((in = fopen (tmpfil, "r")) == NULL)
1236 adios (tmpfil, "unable to re-open");
1238 len = strlen (prefix);
1241 while (fgets (buffer, sizeof(buffer) - 1, in))
1242 if (buffer[0] == '-' && buffer[1] == '-') {
1245 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1249 if (strcmp (buffer + 2, prefix) == 0) {
1260 #define plural(x) (x == 1 ? "" : "s")
1265 if (badadr && unkadr)
1266 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1267 badadr, plural (badadr), unkadr, plural (badadr));
1269 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1271 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1276 do_addresses (int bccque, int talk)
1280 struct mailname *lp;
1283 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1284 if (lp->m_bcc ? bccque : !bccque) {
1286 printf (" -- Local Recipients --\n");
1287 do_an_address (lp, talk);
1292 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1293 if (lp->m_bcc ? bccque : !bccque) {
1295 printf (" -- UUCP Recipients --\n");
1296 do_an_address (lp, talk);
1301 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1302 if (lp->m_bcc ? bccque : !bccque) {
1304 printf (" -- Network Recipients --\n");
1305 do_an_address (lp, talk);
1312 if (rp_isbad (retval = mm_waend ()))
1313 die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
1314 #endif /* MMDFMTS */
1317 if (rp_isbad (retval = sm_waend ()))
1318 die (NULL, "problem ending addresses; %s", rp_string (retval));
1319 #endif /* SENDMTS */
1324 * MTS-SPECIFIC INTERACTION
1329 * SENDMAIL/SMTP routines
1335 post (char *file, int bccque, int talk)
1340 onex = !(msgflags & MINV) || bccque;
1342 if (msgflags & MINV)
1343 printf (" -- Posting for %s Recipients --\n",
1344 bccque ? "Blind" : "Sighted");
1346 printf (" -- Posting for All Recipients --\n");
1351 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1352 snoop, onex, queued))
1353 || rp_isbad (retval = sm_winit (smtpmode, from)))
1354 die (NULL, "problem initializing server; %s", rp_string (retval));
1356 do_addresses (bccque, talk && verbose);
1357 if ((fd = open (file, O_RDONLY)) == NOTOK)
1358 die (file, "unable to re-open");
1363 sm_end (onex ? OK : DONE);
1367 if (msgflags & MINV)
1368 printf (" -- %s Recipient Copies Posted --\n",
1369 bccque ? "Blind" : "Sighted");
1371 printf (" -- Recipient Copies Posted --\n");
1378 /* Address Verification */
1381 verify_all_addresses (int talk)
1384 struct mailname *lp;
1388 if (!whomsw || checksw)
1389 if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0, 0))
1390 || rp_isbad (retval = sm_winit (smtpmode, from)))
1391 die (NULL, "problem initializing server; %s", rp_string (retval));
1393 if (talk && !whomsw)
1394 printf (" -- Address Verification --\n");
1395 if (talk && localaddrs.m_next)
1396 printf (" -- Local Recipients --\n");
1397 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1398 do_an_address (lp, talk);
1400 if (talk && uuaddrs.m_next)
1401 printf (" -- UUCP Recipients --\n");
1402 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1403 do_an_address (lp, talk);
1405 if (talk && netaddrs.m_next)
1406 printf (" -- Network Recipients --\n");
1407 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1408 do_an_address (lp, talk);
1411 if (talk && !whomsw)
1412 printf (" -- Address Verification Successful --\n");
1414 if (!whomsw || checksw)
1423 do_an_address (struct mailname *lp, int talk)
1429 switch (lp->m_type) {
1433 strncpy (addr, mbox, sizeof(addr));
1437 mbox = auxformat (lp, 0);
1439 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1442 default: /* let SendMail decide if the host is bad */
1445 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1450 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1452 if (whomsw && !checksw) {
1460 switch (retval = sm_wadr (mbox, host,
1461 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1464 printf ("address ok\n");
1470 fprintf (stderr, " %s: ", addr);
1471 fprintf (talk ? stdout : stderr, "loses; %s\n",
1472 rp_string (retval));
1478 fprintf (stderr, " %s: ", addr);
1479 die (NULL, "unexpected response; %s", rp_string (retval));
1487 do_text (char *file, int fd)
1492 lseek (fd, (off_t) 0, SEEK_SET);
1494 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1495 if (rp_isbad (retval = sm_wtxt (buf, state)))
1496 die (NULL, "problem writing text; %s\n", rp_string (retval));
1500 die (file, "problem reading from");
1502 switch (retval = sm_wtend ()) {
1508 die (NULL, "posting failed; %s", rp_string (retval));
1511 die (NULL, "unexpected response; %s", rp_string (retval));
1515 #endif /* SENDMTS */
1524 post (char *file, int bccque, int talk)
1530 struct rp_bufstruct reply;
1533 onex = !(msgflags & MINV) || bccque;
1535 if (msgflags & MINV)
1536 printf (" -- Posting for %s Recipients --\n",
1537 bccque ? "Blind" : "Sighted");
1539 printf (" -- Posting for All Recipients --\n");
1544 if (rp_isbad (retval = mm_init ())
1545 || rp_isbad (retval = mm_sbinit ())
1546 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1547 die (NULL, "problem initializing MMDF system [%s]",
1548 rp_valstr (retval));
1550 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1551 die (NULL, "problem with sender address [%s]",
1552 rp_valstr (retval));
1555 do_addresses (bccque, talk && verbose);
1556 if ((fd = open (file, O_RDONLY)) == NOTOK)
1557 die (file, "unable to re-open");
1567 if (msgflags & MINV)
1568 printf (" -- %s Recipient Copies Posted --\n",
1569 bccque ? "Blind" : "Sighted");
1571 printf (" -- Recipient Copies Posted --\n");
1576 /* Address Verification */
1579 verify_all_addresses (int talk)
1582 struct mailname *lp;
1586 struct rp_bufstruct reply;
1591 if (!whomsw || checksw) {
1592 if (rp_isbad (retval = mm_init ())
1593 || rp_isbad (retval = mm_sbinit ())
1594 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1595 die (NULL, "problem initializing MMDF system [%s]",
1596 rp_valstr (retval));
1598 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1599 die (NULL, "problem with sender address [%s]", rp_valstr (retval));
1603 if (talk && !whomsw)
1604 printf (" -- Address Verification --\n");
1605 if (talk && localaddrs.m_next)
1606 printf (" -- Local Recipients --\n");
1607 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1608 do_an_address (lp, talk);
1610 if (talk && uuaddrs.m_next)
1611 printf (" -- UUCP Recipients --\n");
1612 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1613 do_an_address (lp, talk);
1615 if (talk && netaddrs.m_next)
1616 printf (" -- Network Recipients --\n");
1617 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1618 do_an_address (lp, talk);
1621 if (talk && !whomsw)
1622 printf (" -- Address Verification Successful --\n");
1624 if (!whomsw || checksw)
1633 do_an_address (struct mailname *lp, int talk)
1636 char *mbox, *host, *text, *path;
1638 struct rp_bufstruct reply;
1640 switch (lp->m_type) {
1643 host = LocalName ();
1644 strncpy (addr, mbox, sizeof(addr));
1648 fprintf (talk ? stdout : stderr, " %s!%s: %s\n",
1649 lp->m_host, lp->m_mbox, "not supported; UUCP address");
1654 default: /* let MMDF decide if the host is bad */
1657 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1662 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1664 if (whomsw && !checksw) {
1674 path = concat (lp->m_path, mbox, "@", host, NULL);
1678 if (rp_isbad (retval = mm_wadr (path ? NULL : host, path ? path : mbox))
1679 || rp_isbad (retval = mm_rrply (&reply, &len)))
1680 die (NULL, "problem submitting address [%s]", rp_valstr (retval));
1682 switch (rp_gval (reply.rp_val)) {
1685 printf ("address ok\n");
1692 printf ("nameserver timeout - queued for checking\n");
1703 text = "temporary nameserver failure";
1710 text = "not deliverable";
1714 text = "try again later";
1718 text = "nothing done";
1723 fprintf (stderr, " %s: ", addr);
1724 text = "unexpected response";
1725 die (NULL, "%s;\n [%s] -- %s", text,
1726 rp_valstr (reply.rp_val), reply.rp_line);
1730 fprintf (stderr, " %s: ", addr);
1731 fprintf (talk ? stdout : stderr, "%s;\n %s\n", text, reply.rp_line);
1739 do_text (char *file, int fd)
1743 struct rp_bufstruct reply;
1745 lseek (fd, (off_t) 0, SEEK_SET);
1747 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1748 if (rp_isbad (mm_wtxt (buf, state)))
1749 die (NULL, "problem writing text [%s]\n", rp_valstr (retval));
1753 die (file, "problem reading from");
1755 if (rp_isbad (retval = mm_wtend ()))
1756 die (NULL, "problem ending text [%s]\n", rp_valstr (retval));
1758 if (rp_isbad (retval = mm_rrply (&reply, &state)))
1759 die (NULL, "problem getting submission status [%s]\n",
1760 rp_valstr (retval));
1762 switch (rp_gval (reply.rp_val)) {
1768 die (NULL, "you lose; %s", reply.rp_line);
1771 die (NULL, "no delivery occurred; %s", reply.rp_line);
1774 die (NULL, "try again later; %s", reply.rp_line);
1777 die (NULL, "nothing done; %s", reply.rp_line);
1780 die (NULL, "unexpected response;\n\t[%s] -- %s",
1781 rp_valstr (reply.rp_val), reply.rp_line);
1785 #endif /* MMDFMTS */
1795 #ifndef RELIABLE_SIGNALS
1796 SIGNAL (i, SIG_IGN);
1800 if (msgflags & MINV)
1804 if (!whomsw || checksw)
1806 #endif /* MMDFMTS */
1809 if (!whomsw || checksw)
1811 #endif /* SENDMTS */
1823 hstat = SIGNAL2 (SIGHUP, sigser);
1824 istat = SIGNAL2 (SIGINT, sigser);
1825 qstat = SIGNAL2 (SIGQUIT, sigser);
1826 tstat = SIGNAL2 (SIGTERM, sigser);
1836 SIGNAL (SIGHUP, hstat);
1837 SIGNAL (SIGINT, istat);
1838 SIGNAL (SIGQUIT, qstat);
1839 SIGNAL (SIGTERM, tstat);
1847 p_refile (char *file)
1855 printf (" -- Filing Folder Copies --\n");
1856 for (i = 0; i < fccind; i++)
1857 fcc (file, fccfold[i]);
1859 printf (" -- Folder Copies Filed --\n");
1864 * Call the `fileproc' to add the file to the folder.
1868 fcc (char *file, char *folder)
1875 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1878 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1884 fprintf (stderr, " %sFcc %s: ",
1885 msgstate == RESENT ? "Resent-" : "", folder);
1886 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1890 /* see if we need to add `+' */
1891 snprintf (fold, sizeof(fold), "%s%s",
1892 *folder == '+' || *folder == '@' ? "" : "+", folder);
1894 /* now exec the fileproc */
1895 execlp (fileproc, r1bindex (fileproc, '/'),
1896 "-link", "-file", file, fold, NULL);
1900 if ((status = pidwait (child_id, OK))) {
1902 fprintf (stderr, " %sFcc %s: ",
1903 msgstate == RESENT ? "Resent-" : "", folder);
1904 pidstatus (status, verbose ? stdout : stderr, NULL);
1907 printf ("folder ok\n");
1919 die (char *what, char *fmt, ...)
1924 if (msgflags & MINV)
1928 if (!whomsw || checksw)
1930 #endif /* MMDFMTS */
1933 if (!whomsw || checksw)
1935 #endif /* SENDMTS */
1938 advertise (what, NULL, fmt, ap);
1946 * err_abrt() is used by the mm_ routines
1947 * do not, under *ANY* circumstances, remove it from post,
1948 * or you will lose *BIG*
1952 err_abrt (int code, char *fmt, ...)
1954 char buffer[BUFSIZ];
1957 snprintf (buffer, sizeof(buffer), "[%s]", rp_valstr (code));
1960 advertise (buffer, NULL, fmt, ap);
1965 #endif /* MMDFMTS */