2 ** spost.c -- feed messages to sendmail
4 ** This is a simpler, faster, replacement for "post" for use
5 ** when "sendmail" is the transport system.
7 ** This code is Copyright (c) 2002, by the authors of nmh. See the
8 ** COPYRIGHT file in the root directory of the nmh distribution for
9 ** complete copyright information.
14 #include <h/addrsbr.h>
15 #include <h/aliasbr.h>
16 #include <h/dropsbr.h>
20 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
22 static struct swit switches[] = {
34 { "dist", -4 }, /* interface from dist */
39 /* flags for headers->flags */
40 #define HNOP 0x0000 /* just used to keep .set around */
41 #define HBAD 0x0001 /* bad header - don't let it through */
42 #define HADR 0x0002 /* header has an address field */
43 #define HSUB 0x0004 /* Subject: header */
44 #define HTRY 0x0008 /* try to send to addrs on header */
45 #define HBCC 0x0010 /* don't output this header */
46 #define HFCC 0x0020 /* FCC: type header */
47 #define HIGN 0x0040 /* ignore this header */
49 /* flags for headers->set */
50 #define MFRM 0x0001 /* we've seen a From: */
51 #define MDAT 0x0002 /* we've seen a Date: */
52 #define MRFM 0x0004 /* we've seen a Resent-From: */
53 #define MVIS 0x0008 /* we've seen sighted addrs */
54 #define MINV 0x0010 /* we've seen blind addrs */
55 #define MRDT 0x0020 /* we've seen a Resent-Date: */
63 static struct headers NHeaders[] = {
64 { "Return-Path", HBAD, 0 },
65 { "Received", HBAD, 0 },
66 { "Reply-To", HADR, 0 },
67 { "From", HADR, MFRM },
68 { "Sender", HADR|HBAD, 0 },
69 { "Date", HNOP, MDAT },
70 { "Subject", HSUB, 0 },
71 { "To", HADR|HTRY, MVIS },
72 { "Cc", HADR|HTRY, MVIS },
73 { "Bcc", HADR|HTRY|HBCC, MINV },
74 { "Message-Id", HBAD, 0 },
76 { "Envelope-From", HIGN, 0 },
80 static struct headers RHeaders[] = {
81 { "Resent-Reply-To", HADR, 0 },
82 { "Resent-From", HADR, MRFM },
83 { "Resent-Sender", HADR|HBAD, 0 },
84 { "Resent-Date", HNOP, MRDT },
85 { "Resent-Subject", HSUB, 0 },
86 { "Resent-To", HADR|HTRY, MVIS },
87 { "Resent-Cc", HADR|HTRY, MVIS },
88 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
89 { "Resent-Message-Id", HBAD, 0 },
90 { "Resent-Fcc", HFCC, 0 },
91 { "Reply-To", HADR, 0 },
93 { "Envelope-From", HIGN, 0 },
98 static int badmsg = 0;
99 static int verbose = 0;
100 static int debug = 0;
101 static int aliasflg = 0; /* if going to process aliases */
103 static unsigned msgflags = 0; /* what we've seen */
111 static char *subject = NULL; /* the subject field for BCC'ing */
112 static char fccs[BUFSIZ] = "";
113 struct mailname *bccs = NULL; /* list of the bcc recipients */
115 static struct headers *hdrtab; /* table for the message we're doing */
116 static FILE *out; /* output (temp) file */
121 static void putfmt(char *, char *, FILE *);
122 static void finish_headers(FILE *);
123 static int get_header(char *, struct headers *);
124 static void putadr(char *, struct mailname *);
125 static int putone(char *, int, int);
126 static void process_fcc(char *);
127 static void fcc(char *, char *);
128 static void process_bccs(char *);
132 main(int argc, char **argv)
135 char *cp, *msg = NULL, **argp, **arguments;
136 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
139 setlocale(LC_ALL, "");
140 invo_name = mhbasename(argv[0]);
144 arguments = getarguments(invo_name, argc, argv, 0);
147 while ((cp = *argp++)) {
149 switch (smatch(++cp, switches)) {
151 ambigsw(cp, switches);
154 adios(NULL, "-%s unknown", cp);
157 snprintf(buf, sizeof(buf),
158 "%s [switches] file",
160 print_help(buf, switches, 1);
163 print_version(invo_name);
183 adios(NULL, "only one message at a time!");
189 adios(NULL, "usage: %s [switches] file", invo_name);
191 if ((in = fopen(msg, "r")) == NULL)
192 adios(msg, "unable to open");
198 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
201 /* check for "Aliasfile:" profile entry */
202 if ((cp = context_find("Aliasfile"))) {
206 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
208 if ((state = alias(etcpath(*ap))) != AK_OK) {
209 adios(NULL, "aliasing error in file %s: %s",
210 *ap, akerror(state));
216 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
218 for (compnum = 1, state = FLD;;) {
219 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
222 putfmt(name, buf, out);
228 while (state == FLDPLUS) {
229 state = m_getfld(state, name, buf,
233 putfmt(name, cp, out);
239 fprintf(out, "\n%s", buf);
240 while (state == BODY) {
241 state = m_getfld(state, name, buf,
253 adios(NULL, "message format error in component #%d",
257 adios(NULL, "getfld() returned %d", state);
276 process_bccs(tmpfil);
277 if (!(msgflags & MVIS)) {
278 /* only Bcc rcpts: we're finished here */
285 ** re-open the temp file, unlink it and exec sendmail, giving it
286 ** the msg temp file as std in.
288 if (!freopen(tmpfil, "r", stdin)) {
289 adios(tmpfil, "can't reopen for sendmail");
294 *argp++ = "send-mail";
295 *argp++ = "-m"; /* send to me too */
296 *argp++ = "-t"; /* read msg for recipients */
297 *argp++ = "-i"; /* don't stop on "." */
302 execv(sendmail, sargv);
303 adios(sendmail, "can't exec");
308 /* DRAFT GENERATION */
311 putfmt(char *name, char *str, FILE *out)
317 /* remove all leading whitespace (even newlines) */
318 while (*str==' ' || *str=='\t' || *str=='\n') {
322 if ((i = get_header(name, hdrtab)) == NOTOK) {
323 /* no header we would care for: push it through */
324 fprintf(out, "%s: %s", name, str);
327 /* it's one of the interesting headers */
330 if (hdr->flags & HIGN || !*str) {
334 if (hdr->flags & HBAD) {
335 advise(NULL, "illegal header line -- %s:", name);
340 msgflags |= hdr->set;
342 if (hdr->flags & HFCC) {
347 if (hdr->flags & HBCC) {
348 struct mailname *mp = NULL;
350 /* Create list of Bcc addrs. */
351 while ((cp = getname(str))) {
352 mp = getm(cp, NULL, 0, AD_HOST, NULL);
353 mp->m_next = bccs; /* push */
359 if (aliasflg && hdr->flags & HTRY) {
361 ** This header contains address(es) that we have to do
362 ** alias expansion on. Because of the saved state in
363 ** getname we have to put all the addresses into a list.
365 struct mailname *f = NULL;
366 struct mailname *mp = NULL;
368 while ((cp = getname(str))) {
369 mp = getm(cp, NULL, 0, AD_HOST, NULL);
374 mp->m_next = f->m_next;
381 /* Now munch on the list, possibly expanding aliases */
387 ** The author(s) of spost decided that alias substitution wasn't
388 ** necessary for the non-HTRY headers. Unfortunately, one of
389 ** those headers is "From:", and having alias substitution work on
390 ** that is extremely useful for someone with a lot of POP3 email
391 ** accounts or aliases. post supports aliasing of "From:"...
393 ** Since "From:"-processing is incompletely implemented in this
394 ** unsupported and undocumented spost backend, I'm not going
395 ** to take the time to implement my new draft-From:-based email
396 ** address masquerading. If I do ever implement it here, I'd almost
397 ** certainly want to implement "From:" line alias processing as well.
398 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
401 ** Although there is no masquerading anymore in mmh, we might want
402 ** to have aliasing of From: addresses. Think about it.
403 ** -- meillo@marmaro.de 2012-02
406 if (hdr->flags & HSUB) {
407 subject = getcpy(str);
409 fprintf(out, "%s: %s", name, str);
414 ** Add yet missing headers.
417 finish_headers(FILE *out)
420 char from[BUFSIZ]; /* my network address */
421 char signature[BUFSIZ]; /* my signature */
422 char *resentstr = (msgstate == resent) ? "Resent-" : "";
424 if (!(msgflags & MDAT)) {
425 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
428 strncpy(from, getusername(), sizeof(from));
429 if ((cp = getfullname()) && *cp) {
430 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
432 snprintf(signature, sizeof(signature), "%s", from);
434 if (!(msgflags & MFRM)) {
435 fprintf(out, "%sFrom: %s\n", resentstr, signature);
437 /* In case the From: header contains multiple addresses. */
438 fprintf(out, "%sSender: %s\n", resentstr, from);
440 if (!(msgflags & MVIS)) {
441 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
445 adios(NULL, "re-format message and try again");
451 ** Return index of the requested header in the table, or NOTOK if missing.
454 get_header(char *header, struct headers *table)
458 for (h=table; h->value; h++) {
459 if (mh_strcasecmp(header, h->value)==0) {
469 ** output the address list for header "name". The address list
470 ** is a linked list of mailname structs. "nl" points to the head
471 ** of the list. Alias substitution should be done on nl.
474 putadr(char *name, struct mailname *nl)
476 struct mailname *mp, *mp2;
481 fprintf(out, "%s: ", name);
482 namelen = strlen(name) + 2;
485 for (mp = nl; mp; ) {
486 if (linepos > MAX_SM_FIELD) {
487 fprintf(out, "\n%s: ", name);
491 /* a local name - see if it's an alias */
492 cp = akvalue(mp->m_mbox);
493 if (cp == mp->m_mbox) {
494 /* wasn't an alias - use what the user typed */
495 linepos = putone(mp->m_text, linepos, namelen);
497 /* an alias - expand it */
498 while ((cp = getname(cp))) {
499 if (linepos > MAX_SM_FIELD) {
500 fprintf(out, "\n%s: ", name);
503 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
505 mp2->m_pers = getcpy(mp->m_mbox);
506 linepos = putone(adrformat(mp2), linepos, namelen);
508 linepos = putone(mp2->m_text,
516 /* not a local name - use what the user typed */
517 linepos = putone(mp->m_text, linepos, namelen);
527 putone(char *adr, int pos, int indent)
535 else if (linepos+len > OUTPUTLINELEN) {
536 fprintf(out, ",\n%*s", indent, "");
552 process_fcc(char *str)
557 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
558 adios(NULL, "Too much Fcc data");
560 /* todo: better have three states: SEPARATOR, PLUS, WORD */
561 for (cp=pp=str; *cp; cp++) {
570 if (*pp=='+' || *pp=='@') {
590 fcc(char *file, char *folders)
596 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
600 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
601 adios(NULL, "Too much Fcc data");
603 /* hack: read from /dev/null and refile(1) won't question us */
604 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
606 status = system(cmd);
608 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
609 msgstate == resent ? "Resent-" : "", folders);
610 } else if (status != 0) {
611 fprintf(stderr, "%sFcc %s: Problems occured.\n",
612 msgstate == resent ? "Resent-" : "", folders);
620 process_bccs(char *origmsg)
622 char *bccdraft = NULL;
624 struct mailname *mp = NULL;
627 for (mp=bccs; mp; mp=mp->m_next) {
628 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
629 fprintf(out, "To: %s\n", mp->m_text);
630 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
631 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
632 fprintf(out, "------------\n");
635 snprintf(buf, sizeof buf, "send %s", bccdraft);
636 if (system(buf) != 0) {
637 admonish(invo_name, "Problems to send Bcc to %s",