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>
17 #include <zotnet/mts/mts.h>
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
27 # ifdef TM_IN_SYS_TIME
28 # include <sys/time.h>
35 # include <mts/mmdf/util.h>
36 # include <mts/mmdf/mmdf.h>
40 # include <mts/smtp/smtp.h>
44 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
47 #define FCCS 10 /* max number of fccs allowed */
49 /* In the following array of structures, the numeric second field of the
50 structures (minchars) is apparently used like this:
52 -# : Switch can be abbreviated to # characters; switch hidden in -help.
53 0 : Switch can't be abbreviated; switch shown in -help.
54 # : Switch can be abbreviated to # characters; switch shown in -help. */
56 static struct swit switches[] = {
58 { "alias aliasfile", 0 },
60 { "check", -5 }, /* interface from whom */
62 { "nocheck", -7 }, /* interface from whom */
66 { "dist", -4 }, /* interface from dist */
68 { "filter filterfile", 0 },
76 { "library directory", -7 }, /* interface from send, whom */
94 { "whom", -4 }, /* interface from whom */
96 { "width columns", 0 },
101 #define BITSTUFFSW 22
102 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
103 #define NBITSTUFFSW 23
104 { "nodashstuffing", -14 },
106 { "mail", -4 }, /* specify MAIL smtp mode */
108 { "saml", -4 }, /* specify SAML smtp mode */
110 { "send", -4 }, /* specify SEND smtp mode */
112 { "soml", -4 }, /* specify SOML smtp mode */
114 { "idanno number", -6 }, /* interface from send */
116 { "deliver address-list", -7 },
118 { "client host", -6 },
120 { "server host", -6 }, /* specify alternate SMTP server */
122 { "snoop", -5 }, /* snoop the SMTP transaction */
124 { "fill-in file", -7 },
142 * flags for headers->flags
144 #define HNOP 0x0000 /* just used to keep .set around */
145 #define HBAD 0x0001 /* bad header - don't let it through */
146 #define HADR 0x0002 /* header has an address field */
147 #define HSUB 0x0004 /* Subject: header */
148 #define HTRY 0x0008 /* try to send to addrs on header */
149 #define HBCC 0x0010 /* don't output this header */
150 #define HMNG 0x0020 /* munge this header */
151 #define HNGR 0x0040 /* no groups allowed in this header */
152 #define HFCC 0x0080 /* FCC: type header */
153 #define HNIL 0x0100 /* okay for this header not to have addrs */
154 #define HIGN 0x0200 /* ignore this header */
155 #define HDCC 0x0400 /* another undocumented feature */
158 * flags for headers->set
160 #define MFRM 0x0001 /* we've seen a From: */
161 #define MDAT 0x0002 /* we've seen a Date: */
162 #define MRFM 0x0004 /* we've seen a Resent-From: */
163 #define MVIS 0x0008 /* we've seen sighted addrs */
164 #define MINV 0x0010 /* we've seen blind addrs */
167 static struct headers NHeaders[] = {
168 { "Return-Path", HBAD, 0 },
169 { "Received", HBAD, 0 },
170 { "Reply-To", HADR|HNGR, 0 },
171 { "From", HADR|HNGR, MFRM },
172 { "Sender", HADR|HBAD, 0 },
174 { "Subject", HSUB, 0 },
175 { "To", HADR|HTRY, MVIS },
176 { "cc", HADR|HTRY, MVIS },
177 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
178 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
179 { "Message-ID", HBAD, 0 },
184 static struct headers RHeaders[] = {
185 { "Resent-Reply-To", HADR|HNGR, 0 },
186 { "Resent-From", HADR|HNGR, MRFM },
187 { "Resent-Sender", HADR|HBAD, 0 },
188 { "Resent-Date", HBAD, 0 },
189 { "Resent-Subject", HSUB, 0 },
190 { "Resent-To", HADR|HTRY, MVIS },
191 { "Resent-cc", HADR|HTRY, MVIS },
192 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
193 { "Resent-Message-ID", HBAD, 0 },
194 { "Resent-Fcc", HFCC, 0 },
195 { "Reply-To", HADR, 0 },
196 { "From", HADR|HNGR, MFRM },
198 { "Sender", HADR|HNGR|HMNG, 0 },
200 { "Sender", HADR|HNGR, 0 },
202 { "Date", HNOP, MDAT },
203 { "To", HADR|HNIL, 0 },
204 { "cc", HADR|HNIL, 0 },
205 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
210 static short fccind = 0; /* index into fccfold[] */
211 static short outputlinelen = OUTPUTLINELEN;
213 static int pfd = NOTOK; /* fd to write annotation list to */
214 static uid_t myuid= -1; /* my user id */
215 static gid_t mygid= -1; /* my group id */
216 static int recipients = 0; /* how many people will get a copy */
217 static int unkadr = 0; /* how many of those were unknown */
218 static int badadr = 0; /* number of bad addrs */
219 static int badmsg = 0; /* message has bad semantics */
220 static int verbose = 0; /* spell it out */
221 static int format = 1; /* format addresses */
222 static int mime = 0; /* use MIME-style encapsulations for Bcc */
223 static int msgid = 0; /* add msgid */
224 static int debug = 0; /* debugging post */
225 static int watch = 0; /* watch the delivery process */
226 static int whomsw = 0; /* we are whom not post */
227 static int checksw = 0; /* whom -check */
228 static int linepos=0; /* putadr()'s position on the line */
229 static int nameoutput=0; /* putadr() has output header name */
231 static unsigned msgflags = 0; /* what we've seen */
235 static int msgstate = NORMAL;
237 static time_t tclock = 0; /* the time we started (more or less) */
239 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
241 static char tmpfil[BUFSIZ];
242 static char bccfil[BUFSIZ];
244 static char from[BUFSIZ]; /* my network address */
245 static char signature[BUFSIZ]; /* my signature */
246 static char *filter = NULL; /* the filter for BCC'ing */
247 static char *subject = NULL; /* the subject field for BCC'ing */
248 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
250 static struct headers *hdrtab; /* table for the message we're doing */
252 static struct mailname localaddrs={NULL}; /* local addrs */
253 static struct mailname netaddrs={NULL}; /* network addrs */
254 static struct mailname uuaddrs={NULL}; /* uucp addrs */
255 static struct mailname tmpaddrs={NULL}; /* temporary queue */
258 static char *submitmode = "m"; /* deliver to mailbox only */
259 static char submitopts[6] = "vl"; /* initial options for submit */
263 static int snoop = 0;
264 static int smtpmode = S_MAIL;
265 static char *clientsw = NULL;
266 static char *serversw = NULL;
268 extern struct smtp sm_reply;
271 static char prefix[] = "----- =_aaaaaaaaaa";
273 static int fill_up = 0;
274 static char *fill_in = NULL;
275 static char *partno = NULL;
276 static int queued = 0;
278 extern boolean draft_from_masquerading; /* defined in mts.c */
283 static void putfmt (char *, char *, FILE *);
284 static void start_headers (void);
285 static void finish_headers (FILE *);
286 static int get_header (char *, struct headers *);
287 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
288 static void putgrp (char *, char *, FILE *, unsigned int);
289 static int insert (struct mailname *);
290 static void pl (void);
291 static void anno (void);
292 static int annoaux (struct mailname *);
293 static void insert_fcc (struct headers *, char *);
294 static void make_bcc_file (int);
295 static void verify_all_addresses (int);
296 static void chkadr (void);
297 static void sigon (void);
298 static void sigoff (void);
299 static void p_refile (char *);
300 static void fcc (char *, char *);
301 static void die (char *, char *, ...);
302 static void post (char *, int, int);
303 static void do_text (char *file, int fd);
304 static void do_an_address (struct mailname *, int);
305 static void do_addresses (int, int);
306 static int find_prefix (void);
310 main (int argc, char **argv)
312 int state, compnum, dashstuff = 0;
313 char *cp, *msg = NULL, **argp, **arguments;
314 char buf[BUFSIZ], name[NAMESZ];
318 setlocale(LC_ALL, "");
320 invo_name = r1bindex (argv[0], '/');
322 /* foil search of user profile/context */
323 if (context_foil (NULL) == -1)
326 mts_init (invo_name);
327 arguments = getarguments (invo_name, argc, argv, 0);
330 #if defined(MMDFMTS) && defined(MMDFII)
331 mmdf_init (invo_name);
332 #endif /* MMDFMTS and MMDFII */
334 while ((cp = *argp++)) {
336 switch (smatch (++cp, switches)) {
338 ambigsw (cp, switches);
341 adios (NULL, "-%s unknown", cp);
344 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
345 print_help (buf, switches, 0);
348 print_version(invo_name);
352 if (!(cp = *argp++) || *cp == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
354 /* create a minimal context */
355 if (context_foil (cp) == -1)
360 if (!(cp = *argp++) || *cp == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 if ((state = alias (cp)) != AK_OK)
363 adios (NULL, "aliasing error in %s - %s",
364 cp, akerror (state));
383 if (!(filter = *argp++) || *filter == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
439 if (!(cp = *argp++) || *cp == '-')
440 adios (NULL, "missing argument to %s", argp[-2]);
441 if ((outputlinelen = atoi (cp)) < 10)
442 adios (NULL, "impossible width %d", outputlinelen);
446 if (!(cp = *argp++) || *cp == '-')
447 adios (NULL, "missing argument to %s", argp[-2]);
448 if ((pfd = atoi (cp)) <= 2)
449 adios (NULL, "bad argument %s %s", argp[-2], cp);
456 case SOMLSW: /* for right now, sigh... */
466 if (!(cp = *argp++) || *cp == '-')
467 adios (NULL, "missing argument to %s", argp[-2]);
473 if (!(cp = *argp++) || *cp == '-')
474 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(clientsw = *argp++) || *clientsw == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
497 if (!(serversw = *argp++) || *serversw == '-')
498 adios (NULL, "missing argument to %s", argp[-2]);
506 if (!(fill_in = *argp++) || *fill_in == '-')
507 adios (NULL, "missing argument to %s", argp[-2]);
513 if (!(partno = *argp++) || *partno == '-')
514 adios (NULL, "missing argument to %s", argp[-2]);
523 adios (NULL, "only one message at a time!");
531 adios (NULL, "usage: %s [switches] file", invo_name);
533 if (outputlinelen < 10)
534 adios (NULL, "impossible width %d", outputlinelen);
536 if ((in = fopen (msg, "r")) == NULL)
537 adios (msg, "unable to open");
542 discard (out = stdout); /* XXX: reference discard() to help loader */
545 if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
546 adios ("/dev/null", "unable to open");
548 strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
550 if ((out = fopen (tmpfil, "w")) == NULL) {
551 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
552 if ((out = fopen (tmpfil, "w")) == NULL)
553 adios (tmpfil, "unable to create");
555 chmod (tmpfil, 0600);
559 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
561 for (compnum = 1, state = FLD;;) {
562 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
567 cp = add (buf, NULL);
568 while (state == FLDPLUS) {
569 state = m_getfld (state, name, buf, sizeof(buf), in);
572 putfmt (name, cp, out);
576 finish_headers (out);
581 finish_headers (out);
582 if (whomsw && !fill_in)
584 fprintf (out, "\n%s", buf);
585 while (state == BODY) {
586 state = m_getfld (state, name, buf, sizeof(buf), in);
592 finish_headers (out);
597 adios (NULL, "message format error in component #%d", compnum);
600 adios (NULL, "getfld() returned %d", state);
616 /* If we are doing a "whom" check */
619 verify_all_addresses (1);
624 strcat (submitopts, submitmode);
626 strcat (submitopts, "nw");
629 if (msgflags & MINV) {
630 make_bcc_file (dashstuff);
631 if (msgflags & MVIS) {
632 verify_all_addresses (verbose);
633 post (tmpfil, 0, verbose);
635 post (bccfil, 1, verbose);
638 post (tmpfil, 0, isatty (1));
645 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
656 putfmt (char *name, char *str, FILE *out)
658 int count, grp, i, keep;
661 struct mailname *mp, *np;
664 while (*str == ' ' || *str == '\t')
667 if (msgstate == NORMAL && uprf (name, "resent")) {
668 advise (NULL, "illegal header line -- %s:", name);
673 if ((i = get_header (name, hdrtab)) == NOTOK) {
674 fprintf (out, "%s: %s", name, str);
679 if (hdr->flags & HIGN) {
681 fprintf (out, "%s: %s", name, str);
684 if (hdr->flags & HBAD) {
686 fprintf (out, "%s: %s", name, str);
688 advise (NULL, "illegal header line -- %s:", name);
693 msgflags |= (hdr->set & ~(MVIS | MINV));
695 if (hdr->flags & HSUB)
696 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
697 if (hdr->flags & HFCC) {
699 fprintf (out, "%s: %s", name, str);
703 if ((cp = strrchr(str, '\n')))
705 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
707 insert_fcc (hdr, pp);
709 insert_fcc (hdr, pp);
713 if (!(hdr->flags & HADR)) {
714 fprintf (out, "%s: %s", name, str);
718 tmpaddrs.m_next = NULL;
719 for (count = 0; (cp = getname (str)); count++)
720 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
724 tmpaddrs.m_next = mp;
728 if (hdr->flags & HTRY)
734 if (hdr->flags & HNIL)
735 fprintf (out, "%s: %s", name, str);
738 advise (NULL, "%s: field requires at least one address", name);
745 nameoutput = linepos = 0;
746 snprintf (namep, sizeof(namep), "%s%s",
747 !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
749 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
750 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
751 /* The address doesn't include a host, so it might be an alias. */
752 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
753 qp = akvisible () ? mp->m_mbox : "";
756 putgrp (namep, np->m_gname, out, hdr->flags);
757 while ((cp = getname (pp))) {
758 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
763 if (draft_from_masquerading && ((msgstate == RESENT)
765 : (hdr->set & MFRM)))
766 /* The user manually specified a [Resent-]From: address in
767 their draft and the "masquerade:" line in mts.conf
768 doesn't contain "draft_from", so we'll set things up to
769 use the actual email address embedded in the draft
770 [Resent-]From: (after alias substitution, and without the
771 GECOS full name or angle brackets) as the envelope
773 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
775 if (hdr->flags & HBCC)
778 mp->m_ingrp = np->m_ingrp;
781 putgrp (namep, mp->m_gname, out, hdr->flags);
784 if (putadr (namep, qp, mp, out, hdr->flags))
785 msgflags |= (hdr->set & (MVIS | MINV));
794 /* Address includes a host, so no alias substitution is needed. */
795 if (draft_from_masquerading && ((msgstate == RESENT)
797 : (hdr->set & MFRM)))
798 /* The user manually specified a [Resent-]From: address in
799 their draft and the "masquerade:" line in mts.conf
800 doesn't contain "draft_from", so we'll set things up to
801 use the actual email address embedded in the draft
802 [Resent-]From: (after alias substitution, and without the
803 GECOS full name or angle brackets) as the envelope
805 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
807 if (hdr->flags & HBCC)
810 putgrp (namep, mp->m_gname, out, hdr->flags);
813 keep = putadr (namep, "", mp, out, hdr->flags);
817 msgflags |= (hdr->set & (MVIS | MINV));
823 if (grp > 0 && (hdr->flags & HNGR)) {
824 advise (NULL, "%s: field does not allow groups", name);
828 if (fill_in && grp > 0)
839 char myhost[BUFSIZ], sigbuf[BUFSIZ];
846 strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
847 strncpy (myhost, LocalName (), sizeof(myhost));
849 for (cp = myhost; *cp; cp++)
852 if ((cp = getfullname ()) && *cp) {
853 strncpy (sigbuf, cp, sizeof(sigbuf));
854 snprintf (signature, sizeof(signature), "%s <%s>",
855 sigbuf, adrsprintf (NULL, NULL));
856 if ((cp = getname (signature)) == NULL)
857 adios (NULL, "getname () failed -- you lose extraordinarily big");
858 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
859 adios (NULL, "bad signature '%s'", sigbuf);
864 strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
870 * Now that we've outputted the header fields in the draft
871 * message, we will now output any remaining header fields
872 * that we need to add/create.
876 finish_headers (FILE *out)
880 if (whomsw && !fill_up)
883 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
885 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
886 (int) getpid (), (long) tclock, LocalName ());
887 if (msgflags & MFRM) {
888 /* There was already a From: in the draft. Don't add one. */
889 if (!draft_from_masquerading)
890 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
891 so we'll reveal the user's actual account@thismachine
892 address in a Sender: header (and use it as the envelope
894 fprintf (out, "Sender: %s\n", from);
897 /* Construct a From: header. */
898 fprintf (out, "From: %s\n", signature);
902 if (!(msgflags & MVIS))
903 fprintf (out, "Bcc: Blind Distribution List: ;\n");
907 if (!(msgflags & MDAT)) {
908 advise (NULL, "message has no Date: header");
911 if (!(msgflags & MFRM)) {
912 advise (NULL, "message has no From: header");
915 if (whomsw && !fill_up)
918 #ifdef MMDFI /* sigh */
919 fprintf (out, "Sender: %s\n", from);
922 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
924 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
925 (int) getpid (), (long) tclock, LocalName ());
926 if (msgflags & MRFM) {
927 /* There was already a Resent-From: in draft. Don't add one. */
928 if (!draft_from_masquerading)
929 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
930 so we'll reveal the user's actual account@thismachine
931 address in a Sender: header (and use it as the envelope
933 fprintf (out, "Resent-Sender: %s\n", from);
936 /* Construct a Resent-From: header. */
937 fprintf (out, "Resent-From: %s\n", signature);
940 if (!(msgflags & MVIS))
941 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
946 adios (NULL, "re-format message and try again");
948 adios (NULL, "no addressees");
953 get_header (char *header, struct headers *table)
957 for (h = table; h->value; h++)
958 if (!strcasecmp (header, h->value))
966 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
972 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
974 if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
978 fprintf (out, "%s: ", name);
979 linepos += (nameoutput = strlen (name) + 2);
982 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
983 mp->m_pers = getcpy (aka);
985 if (mp->m_gname && !fill_in) {
986 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
996 if (linepos != nameoutput) {
997 if (len + linepos + 2 > outputlinelen)
998 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1008 return (flags & HTRY);
1013 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1018 if (!fill_in && (flags & HBCC))
1022 fprintf (out, "%s: ", name);
1023 linepos += (nameoutput = strlen (name) + 2);
1025 linepos -= strlen (group);
1028 cp = fill_in ? group : concat (group, ";", NULL);
1031 if (linepos > nameoutput) {
1032 if (len + linepos + 2 > outputlinelen) {
1033 fprintf (out, ",\n%*s", nameoutput, "");
1034 linepos = nameoutput;
1048 insert (struct mailname *np)
1050 struct mailname *mp;
1052 if (np->m_mbox == NULL)
1055 for (mp = np->m_type == LOCALHOST ? &localaddrs
1056 : np->m_type == UUCPHOST ? &uuaddrs
1060 if (!strcasecmp (np->m_host, mp->m_next->m_host)
1061 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1062 && np->m_bcc == mp->m_next->m_bcc)
1075 struct mailname *mp;
1077 printf ("-------\n\t-- Addresses --\nlocal:\t");
1078 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1079 printf ("%s%s%s", mp->m_mbox,
1080 mp->m_bcc ? "[BCC]" : "",
1081 mp->m_next ? ",\n\t" : "");
1083 printf ("\nnet:\t");
1084 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1085 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1086 mp->m_mbox, mp->m_host,
1087 mp->m_bcc ? "[BCC]" : "",
1088 mp->m_next ? ",\n\t" : "");
1090 printf ("\nuucp:\t");
1091 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1092 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1093 mp->m_bcc ? "[BCC]" : "",
1094 mp->m_next ? ",\n\t" : "");
1096 printf ("\n\t-- Folder Copies --\nfcc:\t");
1097 for (i = 0; i < fccind; i++)
1098 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1106 struct mailname *mp;
1108 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1109 if (annoaux (mp) == NOTOK)
1112 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1113 if (annoaux (mp) == NOTOK)
1116 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1117 if (annoaux (mp) == NOTOK)
1127 annoaux (struct mailname *mp)
1130 char buffer[BUFSIZ];
1132 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1133 i = strlen (buffer);
1135 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1140 insert_fcc (struct headers *hdr, char *pp)
1144 for (cp = pp; isspace (*cp); cp++)
1146 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1154 adios (NULL, "too many %ss", hdr->value);
1155 fccfold[fccind++] = getcpy (cp);
1163 make_bcc_file (int dashstuff)
1170 strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
1171 if ((out = fopen (bccfil, "w")) == NULL)
1172 adios (bccfil, "unable to create");
1173 chmod (bccfil, 0600);
1175 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1177 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1178 (int) getpid (), (long) tclock, LocalName ());
1179 fprintf (out, "From: %s\n", signature);
1181 fprintf (out, "Subject: %s", subject);
1182 fprintf (out, "BCC:\n");
1185 * Use MIME encapsulation for Bcc messages
1191 * Check if any lines in the message clash with the
1192 * prefix for the MIME multipart separator. If there
1193 * is a clash, increment one of the letters in the
1194 * prefix and check again.
1196 if ((cp = strchr(prefix, 'a')) == NULL)
1197 adios (NULL, "lost prefix start");
1198 while (find_prefix () == NOTOK) {
1203 adios (NULL, "can't find a unique delimiter string");
1208 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1209 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1210 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1212 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1218 * Do mhl filtering of Bcc messages instead
1219 * of MIME encapsulation.
1221 if (filter != NULL) {
1222 vec[0] = r1bindex (mhlproc, '/');
1224 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1228 adios ("fork", "unable to");
1231 dup2 (fileno (out), 1);
1234 vec[i++] = "-forward";
1239 /* was the flag -[no]dashstuffing specified? */
1241 vec[i++] = "-dashstuffing";
1242 else if (dashstuff < 0)
1243 vec[i++] = "-nodashstuffing";
1246 execvp (mhlproc, vec);
1247 fprintf (stderr, "unable to exec ");
1252 pidXwait (child_id, mhlproc);
1256 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1257 adios (tmpfil, "unable to re-open");
1260 * If using MIME encapsulation, or if the -nodashstuffing
1261 * flag was given, then just copy message. Else do
1262 * RFC934 quoting (dashstuffing).
1264 if (mime || dashstuff < 0)
1265 cpydata (fd, fileno (out), tmpfil, bccfil);
1267 cpydgst (fd, fileno (out), tmpfil, bccfil);
1271 fseek (out, 0L, SEEK_END);
1273 fprintf (out, "\n--%s--\n", prefix);
1275 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1281 * Scan message to check if any lines clash with
1282 * the prefix of the MIME multipart separator.
1289 char buffer[BUFSIZ];
1292 if ((in = fopen (tmpfil, "r")) == NULL)
1293 adios (tmpfil, "unable to re-open");
1295 len = strlen (prefix);
1298 while (fgets (buffer, sizeof(buffer) - 1, in))
1299 if (buffer[0] == '-' && buffer[1] == '-') {
1302 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1306 if (strcmp (buffer + 2, prefix) == 0) {
1317 #define plural(x) (x == 1 ? "" : "s")
1322 if (badadr && unkadr)
1323 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1324 badadr, plural (badadr), unkadr, plural (badadr));
1326 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1328 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1333 do_addresses (int bccque, int talk)
1337 struct mailname *lp;
1340 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1341 if (lp->m_bcc ? bccque : !bccque) {
1343 printf (" -- Local Recipients --\n");
1344 do_an_address (lp, talk);
1349 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1350 if (lp->m_bcc ? bccque : !bccque) {
1352 printf (" -- UUCP Recipients --\n");
1353 do_an_address (lp, talk);
1358 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1359 if (lp->m_bcc ? bccque : !bccque) {
1361 printf (" -- Network Recipients --\n");
1362 do_an_address (lp, talk);
1369 if (rp_isbad (retval = mm_waend ()))
1370 die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
1371 #endif /* MMDFMTS */
1374 if (rp_isbad (retval = sm_waend ()))
1375 die (NULL, "problem ending addresses; %s", rp_string (retval));
1376 #endif /* SMTPMTS */
1381 * MTS-SPECIFIC INTERACTION
1386 * SENDMAIL/SMTP routines
1392 post (char *file, int bccque, int talk)
1397 onex = !(msgflags & MINV) || bccque;
1399 if (msgflags & MINV)
1400 printf (" -- Posting for %s Recipients --\n",
1401 bccque ? "Blind" : "Sighted");
1403 printf (" -- Posting for All Recipients --\n");
1408 if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
1409 snoop, onex, queued))
1410 || rp_isbad (retval = sm_winit (smtpmode, from)))
1411 die (NULL, "problem initializing server; %s", rp_string (retval));
1413 do_addresses (bccque, talk && verbose);
1414 if ((fd = open (file, O_RDONLY)) == NOTOK)
1415 die (file, "unable to re-open");
1420 sm_end (onex ? OK : DONE);
1424 if (msgflags & MINV)
1425 printf (" -- %s Recipient Copies Posted --\n",
1426 bccque ? "Blind" : "Sighted");
1428 printf (" -- Recipient Copies Posted --\n");
1435 /* Address Verification */
1438 verify_all_addresses (int talk)
1441 struct mailname *lp;
1445 if (!whomsw || checksw)
1446 if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0, 0))
1447 || rp_isbad (retval = sm_winit (smtpmode, from)))
1448 die (NULL, "problem initializing server; %s", rp_string (retval));
1450 if (talk && !whomsw)
1451 printf (" -- Address Verification --\n");
1452 if (talk && localaddrs.m_next)
1453 printf (" -- Local Recipients --\n");
1454 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1455 do_an_address (lp, talk);
1457 if (talk && uuaddrs.m_next)
1458 printf (" -- UUCP Recipients --\n");
1459 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1460 do_an_address (lp, talk);
1462 if (talk && netaddrs.m_next)
1463 printf (" -- Network Recipients --\n");
1464 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1465 do_an_address (lp, talk);
1468 if (talk && !whomsw)
1469 printf (" -- Address Verification Successful --\n");
1471 if (!whomsw || checksw)
1480 do_an_address (struct mailname *lp, int talk)
1486 switch (lp->m_type) {
1490 strncpy (addr, mbox, sizeof(addr));
1494 mbox = auxformat (lp, 0);
1496 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1499 default: /* let SendMail decide if the host is bad */
1502 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1507 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1509 if (whomsw && !checksw) {
1517 switch (retval = sm_wadr (mbox, host,
1518 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1521 printf ("address ok\n");
1527 fprintf (stderr, " %s: ", addr);
1528 fprintf (talk ? stdout : stderr, "loses; %s\n",
1529 rp_string (retval));
1535 fprintf (stderr, " %s: ", addr);
1536 die (NULL, "unexpected response; %s", rp_string (retval));
1544 do_text (char *file, int fd)
1549 lseek (fd, (off_t) 0, SEEK_SET);
1551 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1552 if (rp_isbad (retval = sm_wtxt (buf, state)))
1553 die (NULL, "problem writing text; %s\n", rp_string (retval));
1557 die (file, "problem reading from");
1559 switch (retval = sm_wtend ()) {
1565 die (NULL, "posting failed; %s", rp_string (retval));
1568 die (NULL, "unexpected response; %s", rp_string (retval));
1572 #endif /* SMTPMTS */
1581 post (char *file, int bccque, int talk)
1587 struct rp_bufstruct reply;
1590 onex = !(msgflags & MINV) || bccque;
1592 if (msgflags & MINV)
1593 printf (" -- Posting for %s Recipients --\n",
1594 bccque ? "Blind" : "Sighted");
1596 printf (" -- Posting for All Recipients --\n");
1601 if (rp_isbad (retval = mm_init ())
1602 || rp_isbad (retval = mm_sbinit ())
1603 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1604 die (NULL, "problem initializing MMDF system [%s]",
1605 rp_valstr (retval));
1607 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1608 die (NULL, "problem with sender address [%s]",
1609 rp_valstr (retval));
1612 do_addresses (bccque, talk && verbose);
1613 if ((fd = open (file, O_RDONLY)) == NOTOK)
1614 die (file, "unable to re-open");
1624 if (msgflags & MINV)
1625 printf (" -- %s Recipient Copies Posted --\n",
1626 bccque ? "Blind" : "Sighted");
1628 printf (" -- Recipient Copies Posted --\n");
1633 /* Address Verification */
1636 verify_all_addresses (int talk)
1639 struct mailname *lp;
1643 struct rp_bufstruct reply;
1648 if (!whomsw || checksw) {
1649 if (rp_isbad (retval = mm_init ())
1650 || rp_isbad (retval = mm_sbinit ())
1651 || rp_isbad (retval = mm_winit (NULL, submitopts, from)))
1652 die (NULL, "problem initializing MMDF system [%s]",
1653 rp_valstr (retval));
1655 if (rp_isbad (retval = mm_rrply (&reply, &len)))
1656 die (NULL, "problem with sender address [%s]", rp_valstr (retval));
1660 if (talk && !whomsw)
1661 printf (" -- Address Verification --\n");
1662 if (talk && localaddrs.m_next)
1663 printf (" -- Local Recipients --\n");
1664 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1665 do_an_address (lp, talk);
1667 if (talk && uuaddrs.m_next)
1668 printf (" -- UUCP Recipients --\n");
1669 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1670 do_an_address (lp, talk);
1672 if (talk && netaddrs.m_next)
1673 printf (" -- Network Recipients --\n");
1674 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1675 do_an_address (lp, talk);
1678 if (talk && !whomsw)
1679 printf (" -- Address Verification Successful --\n");
1681 if (!whomsw || checksw)
1690 do_an_address (struct mailname *lp, int talk)
1693 char *mbox, *host, *text, *path;
1695 struct rp_bufstruct reply;
1697 switch (lp->m_type) {
1700 host = LocalName ();
1701 strncpy (addr, mbox, sizeof(addr));
1705 fprintf (talk ? stdout : stderr, " %s!%s: %s\n",
1706 lp->m_host, lp->m_mbox, "not supported; UUCP address");
1711 default: /* let MMDF decide if the host is bad */
1714 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1719 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1721 if (whomsw && !checksw) {
1731 path = concat (lp->m_path, mbox, "@", host, NULL);
1735 if (rp_isbad (retval = mm_wadr (path ? NULL : host, path ? path : mbox))
1736 || rp_isbad (retval = mm_rrply (&reply, &len)))
1737 die (NULL, "problem submitting address [%s]", rp_valstr (retval));
1739 switch (rp_gval (reply.rp_val)) {
1742 printf ("address ok\n");
1749 printf ("nameserver timeout - queued for checking\n");
1760 text = "temporary nameserver failure";
1767 text = "not deliverable";
1771 text = "try again later";
1775 text = "nothing done";
1780 fprintf (stderr, " %s: ", addr);
1781 text = "unexpected response";
1782 die (NULL, "%s;\n [%s] -- %s", text,
1783 rp_valstr (reply.rp_val), reply.rp_line);
1787 fprintf (stderr, " %s: ", addr);
1788 fprintf (talk ? stdout : stderr, "%s;\n %s\n", text, reply.rp_line);
1796 do_text (char *file, int fd)
1800 struct rp_bufstruct reply;
1802 lseek (fd, (off_t) 0, SEEK_SET);
1804 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1805 if (rp_isbad (mm_wtxt (buf, state)))
1806 die (NULL, "problem writing text [%s]\n", rp_valstr (retval));
1810 die (file, "problem reading from");
1812 if (rp_isbad (retval = mm_wtend ()))
1813 die (NULL, "problem ending text [%s]\n", rp_valstr (retval));
1815 if (rp_isbad (retval = mm_rrply (&reply, &state)))
1816 die (NULL, "problem getting submission status [%s]\n",
1817 rp_valstr (retval));
1819 switch (rp_gval (reply.rp_val)) {
1825 die (NULL, "you lose; %s", reply.rp_line);
1828 die (NULL, "no delivery occurred; %s", reply.rp_line);
1831 die (NULL, "try again later; %s", reply.rp_line);
1834 die (NULL, "nothing done; %s", reply.rp_line);
1837 die (NULL, "unexpected response;\n\t[%s] -- %s",
1838 rp_valstr (reply.rp_val), reply.rp_line);
1842 #endif /* MMDFMTS */
1852 #ifndef RELIABLE_SIGNALS
1853 SIGNAL (i, SIG_IGN);
1857 if (msgflags & MINV)
1861 if (!whomsw || checksw)
1863 #endif /* MMDFMTS */
1866 if (!whomsw || checksw)
1868 #endif /* SMTPMTS */
1880 hstat = SIGNAL2 (SIGHUP, sigser);
1881 istat = SIGNAL2 (SIGINT, sigser);
1882 qstat = SIGNAL2 (SIGQUIT, sigser);
1883 tstat = SIGNAL2 (SIGTERM, sigser);
1893 SIGNAL (SIGHUP, hstat);
1894 SIGNAL (SIGINT, istat);
1895 SIGNAL (SIGQUIT, qstat);
1896 SIGNAL (SIGTERM, tstat);
1904 p_refile (char *file)
1912 printf (" -- Filing Folder Copies --\n");
1913 for (i = 0; i < fccind; i++)
1914 fcc (file, fccfold[i]);
1916 printf (" -- Folder Copies Filed --\n");
1921 * Call the `fileproc' to add the file to the folder.
1925 fcc (char *file, char *folder)
1932 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1935 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1941 fprintf (stderr, " %sFcc %s: ",
1942 msgstate == RESENT ? "Resent-" : "", folder);
1943 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1947 /* see if we need to add `+' */
1948 snprintf (fold, sizeof(fold), "%s%s",
1949 *folder == '+' || *folder == '@' ? "" : "+", folder);
1951 /* now exec the fileproc */
1952 execlp (fileproc, r1bindex (fileproc, '/'),
1953 "-link", "-file", file, fold, NULL);
1957 if ((status = pidwait (child_id, OK))) {
1959 fprintf (stderr, " %sFcc %s: ",
1960 msgstate == RESENT ? "Resent-" : "", folder);
1961 pidstatus (status, verbose ? stdout : stderr, NULL);
1964 printf ("folder ok\n");
1976 die (char *what, char *fmt, ...)
1981 if (msgflags & MINV)
1985 if (!whomsw || checksw)
1987 #endif /* MMDFMTS */
1990 if (!whomsw || checksw)
1992 #endif /* SMTPMTS */
1995 advertise (what, NULL, fmt, ap);
2003 * err_abrt() is used by the mm_ routines
2004 * do not, under *ANY* circumstances, remove it from post,
2005 * or you will lose *BIG*
2009 err_abrt (int code, char *fmt, ...)
2011 char buffer[BUFSIZ];
2014 snprintf (buffer, sizeof(buffer), "[%s]", rp_valstr (code));
2017 advertise (buffer, NULL, fmt, ap);
2022 #endif /* MMDFMTS */