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[] = {
28 { "alias aliasfile", 0 },
38 { "dist", -4 }, /* interface from dist */
40 { "library directory", -7 },
45 /* flags for headers->flags */
46 #define HNOP 0x0000 /* just used to keep .set around */
47 #define HBAD 0x0001 /* bad header - don't let it through */
48 #define HADR 0x0002 /* header has an address field */
49 #define HSUB 0x0004 /* Subject: header */
50 #define HTRY 0x0008 /* try to send to addrs on header */
51 #define HBCC 0x0010 /* don't output this header */
52 #define HFCC 0x0020 /* FCC: type header */
53 #define HIGN 0x0040 /* ignore this header */
55 /* flags for headers->set */
56 #define MFRM 0x0001 /* we've seen a From: */
57 #define MDAT 0x0002 /* we've seen a Date: */
58 #define MRFM 0x0004 /* we've seen a Resent-From: */
59 #define MVIS 0x0008 /* we've seen sighted addrs */
60 #define MINV 0x0010 /* we've seen blind addrs */
61 #define MRDT 0x0020 /* we've seen a Resent-Date: */
69 static struct headers NHeaders[] = {
70 { "Return-Path", HBAD, 0 },
71 { "Received", HBAD, 0 },
72 { "Reply-To", HADR, 0 },
73 { "From", HADR, MFRM },
74 { "Sender", HADR|HBAD, 0 },
75 { "Date", HNOP, MDAT },
76 { "Subject", HSUB, 0 },
77 { "To", HADR|HTRY, MVIS },
78 { "Cc", HADR|HTRY, MVIS },
79 { "Bcc", HADR|HTRY|HBCC, MINV },
80 { "Message-Id", HBAD, 0 },
82 { "Envelope-From", HIGN, 0 },
86 static struct headers RHeaders[] = {
87 { "Resent-Reply-To", HADR, 0 },
88 { "Resent-From", HADR, MRFM },
89 { "Resent-Sender", HADR|HBAD, 0 },
90 { "Resent-Date", HNOP, MRDT },
91 { "Resent-Subject", HSUB, 0 },
92 { "Resent-To", HADR|HTRY, MVIS },
93 { "Resent-Cc", HADR|HTRY, MVIS },
94 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
95 { "Resent-Message-Id", HBAD, 0 },
96 { "Resent-Fcc", HFCC, 0 },
97 { "Reply-To", HADR, 0 },
99 { "Envelope-From", HIGN, 0 },
104 static int badmsg = 0;
105 static int verbose = 0;
106 static int debug = 0;
107 static int aliasflg = 0; /* if going to process aliases */
109 static unsigned msgflags = 0; /* what we've seen */
117 static char *subject = NULL; /* the subject field for BCC'ing */
118 static char fccs[BUFSIZ] = "";
119 struct mailname *bccs = NULL; /* list of the bcc recipients */
121 static struct headers *hdrtab; /* table for the message we're doing */
122 static FILE *out; /* output (temp) file */
127 static void putfmt(char *, char *, FILE *);
128 static void finish_headers(FILE *);
129 static int get_header(char *, struct headers *);
130 static void putadr(char *, struct mailname *);
131 static int putone(char *, int, int);
132 static void process_fcc(char *);
133 static void fcc(char *, char *);
134 static void process_bccs(char *);
138 main(int argc, char **argv)
141 char *cp, *msg = NULL, **argp, **arguments;
142 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
145 setlocale(LC_ALL, "");
146 invo_name = mhbasename(argv[0]);
148 /* foil search of user profile/context */
149 if (context_foil(NULL) == -1)
152 arguments = getarguments(invo_name, argc, argv, 0);
155 while ((cp = *argp++)) {
157 switch (smatch(++cp, switches)) {
159 ambigsw(cp, switches);
162 adios(NULL, "-%s unknown", cp);
165 snprintf(buf, sizeof(buf),
166 "%s [switches] file",
168 print_help(buf, switches, 1);
171 print_version(invo_name);
190 if (!(cp = *argp++) || *cp == '-')
191 adios(NULL, "missing argument to %s",
194 if ((state = alias(cp)) != AK_OK)
195 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
202 if (!(cp = *argp++) || *cp == '-')
203 adios(NULL, "missing argument to %s",
205 /* create a minimal context */
206 if (context_foil(cp) == -1)
212 adios(NULL, "only one message at a time!");
218 adios(NULL, "usage: %s [switches] file", invo_name);
220 if ((in = fopen(msg, "r")) == NULL)
221 adios(msg, "unable to open");
227 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
230 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
232 for (compnum = 1, state = FLD;;) {
233 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
236 putfmt(name, buf, out);
242 while (state == FLDPLUS) {
243 state = m_getfld(state, name, buf,
247 putfmt(name, cp, out);
253 fprintf(out, "\n%s", buf);
254 while (state == BODY) {
255 state = m_getfld(state, name, buf,
267 adios(NULL, "message format error in component #%d",
271 adios(NULL, "getfld() returned %d", state);
290 process_bccs(tmpfil);
291 if (!(msgflags & MVIS)) {
292 /* only Bcc rcpts: we're finished here */
299 ** re-open the temp file, unlink it and exec sendmail, giving it
300 ** the msg temp file as std in.
302 if (!freopen(tmpfil, "r", stdin)) {
303 adios(tmpfil, "can't reopen for sendmail");
308 *argp++ = "send-mail";
309 *argp++ = "-m"; /* send to me too */
310 *argp++ = "-t"; /* read msg for recipients */
311 *argp++ = "-i"; /* don't stop on "." */
316 execv(sendmail, sargv);
317 adios(sendmail, "can't exec");
322 /* DRAFT GENERATION */
325 putfmt(char *name, char *str, FILE *out)
331 /* remove all leading whitespace (even newlines) */
332 while (*str==' ' || *str=='\t' || *str=='\n') {
336 if ((i = get_header(name, hdrtab)) == NOTOK) {
337 /* no header we would care for: push it through */
338 fprintf(out, "%s: %s", name, str);
341 /* it's one of the interesting headers */
344 if (hdr->flags & HIGN || !*str) {
348 if (hdr->flags & HBAD) {
349 advise(NULL, "illegal header line -- %s:", name);
354 msgflags |= hdr->set;
356 if (hdr->flags & HFCC) {
361 if (hdr->flags & HBCC) {
362 struct mailname *mp = NULL;
364 /* Create list of Bcc addrs. */
365 while ((cp = getname(str))) {
366 mp = getm(cp, NULL, 0, AD_HOST, NULL);
367 mp->m_next = bccs; /* push */
373 if (aliasflg && hdr->flags & HTRY) {
375 ** This header contains address(es) that we have to do
376 ** alias expansion on. Because of the saved state in
377 ** getname we have to put all the addresses into a list.
379 struct mailname *f = NULL;
380 struct mailname *mp = NULL;
382 while ((cp = getname(str))) {
383 mp = getm(cp, NULL, 0, AD_HOST, NULL);
388 mp->m_next = f->m_next;
395 /* Now munch on the list, possibly expanding aliases */
401 ** The author(s) of spost decided that alias substitution wasn't
402 ** necessary for the non-HTRY headers. Unfortunately, one of
403 ** those headers is "From:", and having alias substitution work on
404 ** that is extremely useful for someone with a lot of POP3 email
405 ** accounts or aliases. post supports aliasing of "From:"...
407 ** Since "From:"-processing is incompletely implemented in this
408 ** unsupported and undocumented spost backend, I'm not going
409 ** to take the time to implement my new draft-From:-based email
410 ** address masquerading. If I do ever implement it here, I'd almost
411 ** certainly want to implement "From:" line alias processing as well.
412 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
415 ** Although there is no masquerading anymore in mmh, we might want
416 ** to have aliasing of From: addresses. Think about it.
417 ** -- meillo@marmaro.de 2012-02
420 if (hdr->flags & HSUB) {
421 subject = getcpy(str);
423 fprintf(out, "%s: %s", name, str);
428 ** Add yet missing headers.
431 finish_headers(FILE *out)
434 char from[BUFSIZ]; /* my network address */
435 char signature[BUFSIZ]; /* my signature */
436 char *resentstr = (msgstate == resent) ? "Resent-" : "";
438 if (!(msgflags & MDAT)) {
439 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
442 strncpy(from, getusername(), sizeof(from));
443 if ((cp = getfullname()) && *cp) {
444 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
446 snprintf(signature, sizeof(signature), "%s", from);
448 if (!(msgflags & MFRM)) {
449 fprintf(out, "%sFrom: %s\n", resentstr, signature);
451 /* In case the From: header contains multiple addresses. */
452 fprintf(out, "%sSender: %s\n", resentstr, from);
454 if (!(msgflags & MVIS)) {
455 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
459 adios(NULL, "re-format message and try again");
465 ** Return index of the requested header in the table, or NOTOK if missing.
468 get_header(char *header, struct headers *table)
472 for (h=table; h->value; h++) {
473 if (mh_strcasecmp(header, h->value)==0) {
483 ** output the address list for header "name". The address list
484 ** is a linked list of mailname structs. "nl" points to the head
485 ** of the list. Alias substitution should be done on nl.
488 putadr(char *name, struct mailname *nl)
490 struct mailname *mp, *mp2;
495 fprintf(out, "%s: ", name);
496 namelen = strlen(name) + 2;
499 for (mp = nl; mp; ) {
500 if (linepos > MAX_SM_FIELD) {
501 fprintf(out, "\n%s: ", name);
505 /* a local name - see if it's an alias */
506 cp = akvalue(mp->m_mbox);
507 if (cp == mp->m_mbox) {
508 /* wasn't an alias - use what the user typed */
509 linepos = putone(mp->m_text, linepos, namelen);
511 /* an alias - expand it */
512 while ((cp = getname(cp))) {
513 if (linepos > MAX_SM_FIELD) {
514 fprintf(out, "\n%s: ", name);
517 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
519 mp2->m_pers = getcpy(mp->m_mbox);
520 linepos = putone(adrformat(mp2), linepos, namelen);
522 linepos = putone(mp2->m_text,
530 /* not a local name - use what the user typed */
531 linepos = putone(mp->m_text, linepos, namelen);
541 putone(char *adr, int pos, int indent)
549 else if (linepos+len > OUTPUTLINELEN) {
550 fprintf(out, ",\n%*s", indent, "");
566 process_fcc(char *str)
571 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
572 adios(NULL, "Too much Fcc data");
574 /* todo: better have three states: SEPARATOR, PLUS, WORD */
575 for (cp=pp=str; *cp; cp++) {
584 if (*pp=='+' || *pp=='@') {
604 fcc(char *file, char *folders)
610 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
614 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
615 adios(NULL, "Too much Fcc data");
617 /* hack: read from /dev/null and refile(1) won't question us */
618 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
620 status = system(cmd);
622 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
623 msgstate == resent ? "Resent-" : "", folders);
624 } else if (status != 0) {
625 fprintf(stderr, "%sFcc %s: Problems occured.\n",
626 msgstate == resent ? "Resent-" : "", folders);
634 process_bccs(char *origmsg)
636 char *bccdraft = NULL;
638 struct mailname *mp = NULL;
641 for (mp=bccs; mp; mp=mp->m_next) {
643 ** Note: This draft file will be left existing by send(1),
644 ** although renamed with backup prefix.
645 ** TODO: We should have it removed eventually.
647 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
648 fprintf(out, "To: %s\n", mp->m_text);
649 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
650 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
651 fprintf(out, "------------\n");
654 snprintf(buf, sizeof buf, "send %s", bccdraft);
655 if (system(buf) != 0) {
656 admonish(invo_name, "Problems to send Bcc to %s",
660 /* TODO: unlink renamed bcc draft after send(1) */