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 */
43 /* flags for headers->flags */
44 #define HNOP 0x0000 /* just used to keep .set around */
45 #define HBAD 0x0001 /* bad header - don't let it through */
46 #define HADR 0x0002 /* header has an address field */
47 #define HSUB 0x0004 /* Subject: header */
48 #define HTRY 0x0008 /* try to send to addrs on header */
49 #define HBCC 0x0010 /* don't output this header */
50 #define HFCC 0x0020 /* FCC: type header */
51 #define HIGN 0x0040 /* ignore this header */
53 /* flags for headers->set */
54 #define MFRM 0x0001 /* we've seen a From: */
55 #define MDAT 0x0002 /* we've seen a Date: */
56 #define MRFM 0x0004 /* we've seen a Resent-From: */
57 #define MVIS 0x0008 /* we've seen sighted addrs */
58 #define MINV 0x0010 /* we've seen blind addrs */
59 #define MRDT 0x0020 /* we've seen a Resent-Date: */
67 static struct headers NHeaders[] = {
68 { "Return-Path", HBAD, 0 },
69 { "Received", HBAD, 0 },
70 { "Reply-To", HADR, 0 },
71 { "From", HADR, MFRM },
72 { "Sender", HADR|HBAD, 0 },
73 { "Date", HNOP, MDAT },
74 { "Subject", HSUB, 0 },
75 { "To", HADR|HTRY, MVIS },
76 { "Cc", HADR|HTRY, MVIS },
77 { "Bcc", HADR|HTRY|HBCC, MINV },
78 { "Message-Id", HBAD, 0 },
80 { "Envelope-From", HIGN, 0 },
84 static struct headers RHeaders[] = {
85 { "Resent-Reply-To", HADR, 0 },
86 { "Resent-From", HADR, MRFM },
87 { "Resent-Sender", HADR|HBAD, 0 },
88 { "Resent-Date", HNOP, MRDT },
89 { "Resent-Subject", HSUB, 0 },
90 { "Resent-To", HADR|HTRY, MVIS },
91 { "Resent-Cc", HADR|HTRY, MVIS },
92 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
93 { "Resent-Message-Id", HBAD, 0 },
94 { "Resent-Fcc", HFCC, 0 },
95 { "Reply-To", HADR, 0 },
97 { "Envelope-From", HIGN, 0 },
102 static int badmsg = 0;
103 static int verbose = 0;
104 static int debug = 0;
105 static int aliasflg = 0; /* if going to process aliases */
107 static unsigned msgflags = 0; /* what we've seen */
115 static char *subject = NULL; /* the subject field for BCC'ing */
116 static char fccs[BUFSIZ] = "";
117 struct mailname *bccs = NULL; /* list of the bcc recipients */
119 static struct headers *hdrtab; /* table for the message we're doing */
120 static FILE *out; /* output (temp) file */
125 static void putfmt(char *, char *, FILE *);
126 static void finish_headers(FILE *);
127 static int get_header(char *, struct headers *);
128 static void putadr(char *, struct mailname *);
129 static int putone(char *, int, int);
130 static void process_fcc(char *);
131 static void fcc(char *, char *);
132 static void process_bccs(char *);
136 main(int argc, char **argv)
139 char *cp, *msg = NULL, **argp, **arguments;
140 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
143 setlocale(LC_ALL, "");
144 invo_name = mhbasename(argv[0]);
148 arguments = getarguments(invo_name, argc, argv, 0);
151 while ((cp = *argp++)) {
153 switch (smatch(++cp, switches)) {
155 ambigsw(cp, switches);
158 adios(NULL, "-%s unknown", cp);
161 snprintf(buf, sizeof(buf),
162 "%s [switches] file",
164 print_help(buf, switches, 1);
167 print_version(invo_name);
186 if (!(cp = *argp++) || *cp == '-')
187 adios(NULL, "missing argument to %s",
190 if ((state = alias(cp)) != AK_OK)
191 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
199 adios(NULL, "only one message at a time!");
205 adios(NULL, "usage: %s [switches] file", invo_name);
207 if ((in = fopen(msg, "r")) == NULL)
208 adios(msg, "unable to open");
214 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
217 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
219 for (compnum = 1, state = FLD;;) {
220 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
223 putfmt(name, buf, out);
229 while (state == FLDPLUS) {
230 state = m_getfld(state, name, buf,
234 putfmt(name, cp, out);
240 fprintf(out, "\n%s", buf);
241 while (state == BODY) {
242 state = m_getfld(state, name, buf,
254 adios(NULL, "message format error in component #%d",
258 adios(NULL, "getfld() returned %d", state);
277 process_bccs(tmpfil);
278 if (!(msgflags & MVIS)) {
279 /* only Bcc rcpts: we're finished here */
286 ** re-open the temp file, unlink it and exec sendmail, giving it
287 ** the msg temp file as std in.
289 if (!freopen(tmpfil, "r", stdin)) {
290 adios(tmpfil, "can't reopen for sendmail");
295 *argp++ = "send-mail";
296 *argp++ = "-m"; /* send to me too */
297 *argp++ = "-t"; /* read msg for recipients */
298 *argp++ = "-i"; /* don't stop on "." */
303 execv(sendmail, sargv);
304 adios(sendmail, "can't exec");
309 /* DRAFT GENERATION */
312 putfmt(char *name, char *str, FILE *out)
318 /* remove all leading whitespace (even newlines) */
319 while (*str==' ' || *str=='\t' || *str=='\n') {
323 if ((i = get_header(name, hdrtab)) == NOTOK) {
324 /* no header we would care for: push it through */
325 fprintf(out, "%s: %s", name, str);
328 /* it's one of the interesting headers */
331 if (hdr->flags & HIGN || !*str) {
335 if (hdr->flags & HBAD) {
336 advise(NULL, "illegal header line -- %s:", name);
341 msgflags |= hdr->set;
343 if (hdr->flags & HFCC) {
348 if (hdr->flags & HBCC) {
349 struct mailname *mp = NULL;
351 /* Create list of Bcc addrs. */
352 while ((cp = getname(str))) {
353 mp = getm(cp, NULL, 0, AD_HOST, NULL);
354 mp->m_next = bccs; /* push */
360 if (aliasflg && hdr->flags & HTRY) {
362 ** This header contains address(es) that we have to do
363 ** alias expansion on. Because of the saved state in
364 ** getname we have to put all the addresses into a list.
366 struct mailname *f = NULL;
367 struct mailname *mp = NULL;
369 while ((cp = getname(str))) {
370 mp = getm(cp, NULL, 0, AD_HOST, NULL);
375 mp->m_next = f->m_next;
382 /* Now munch on the list, possibly expanding aliases */
388 ** The author(s) of spost decided that alias substitution wasn't
389 ** necessary for the non-HTRY headers. Unfortunately, one of
390 ** those headers is "From:", and having alias substitution work on
391 ** that is extremely useful for someone with a lot of POP3 email
392 ** accounts or aliases. post supports aliasing of "From:"...
394 ** Since "From:"-processing is incompletely implemented in this
395 ** unsupported and undocumented spost backend, I'm not going
396 ** to take the time to implement my new draft-From:-based email
397 ** address masquerading. If I do ever implement it here, I'd almost
398 ** certainly want to implement "From:" line alias processing as well.
399 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
402 ** Although there is no masquerading anymore in mmh, we might want
403 ** to have aliasing of From: addresses. Think about it.
404 ** -- meillo@marmaro.de 2012-02
407 if (hdr->flags & HSUB) {
408 subject = getcpy(str);
410 fprintf(out, "%s: %s", name, str);
415 ** Add yet missing headers.
418 finish_headers(FILE *out)
421 char from[BUFSIZ]; /* my network address */
422 char signature[BUFSIZ]; /* my signature */
423 char *resentstr = (msgstate == resent) ? "Resent-" : "";
425 if (!(msgflags & MDAT)) {
426 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
429 strncpy(from, getusername(), sizeof(from));
430 if ((cp = getfullname()) && *cp) {
431 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
433 snprintf(signature, sizeof(signature), "%s", from);
435 if (!(msgflags & MFRM)) {
436 fprintf(out, "%sFrom: %s\n", resentstr, signature);
438 /* In case the From: header contains multiple addresses. */
439 fprintf(out, "%sSender: %s\n", resentstr, from);
441 if (!(msgflags & MVIS)) {
442 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
446 adios(NULL, "re-format message and try again");
452 ** Return index of the requested header in the table, or NOTOK if missing.
455 get_header(char *header, struct headers *table)
459 for (h=table; h->value; h++) {
460 if (mh_strcasecmp(header, h->value)==0) {
470 ** output the address list for header "name". The address list
471 ** is a linked list of mailname structs. "nl" points to the head
472 ** of the list. Alias substitution should be done on nl.
475 putadr(char *name, struct mailname *nl)
477 struct mailname *mp, *mp2;
482 fprintf(out, "%s: ", name);
483 namelen = strlen(name) + 2;
486 for (mp = nl; mp; ) {
487 if (linepos > MAX_SM_FIELD) {
488 fprintf(out, "\n%s: ", name);
492 /* a local name - see if it's an alias */
493 cp = akvalue(mp->m_mbox);
494 if (cp == mp->m_mbox) {
495 /* wasn't an alias - use what the user typed */
496 linepos = putone(mp->m_text, linepos, namelen);
498 /* an alias - expand it */
499 while ((cp = getname(cp))) {
500 if (linepos > MAX_SM_FIELD) {
501 fprintf(out, "\n%s: ", name);
504 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
506 mp2->m_pers = getcpy(mp->m_mbox);
507 linepos = putone(adrformat(mp2), linepos, namelen);
509 linepos = putone(mp2->m_text,
517 /* not a local name - use what the user typed */
518 linepos = putone(mp->m_text, linepos, namelen);
528 putone(char *adr, int pos, int indent)
536 else if (linepos+len > OUTPUTLINELEN) {
537 fprintf(out, ",\n%*s", indent, "");
553 process_fcc(char *str)
558 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
559 adios(NULL, "Too much Fcc data");
561 /* todo: better have three states: SEPARATOR, PLUS, WORD */
562 for (cp=pp=str; *cp; cp++) {
571 if (*pp=='+' || *pp=='@') {
591 fcc(char *file, char *folders)
597 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
601 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
602 adios(NULL, "Too much Fcc data");
604 /* hack: read from /dev/null and refile(1) won't question us */
605 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
607 status = system(cmd);
609 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
610 msgstate == resent ? "Resent-" : "", folders);
611 } else if (status != 0) {
612 fprintf(stderr, "%sFcc %s: Problems occured.\n",
613 msgstate == resent ? "Resent-" : "", folders);
621 process_bccs(char *origmsg)
623 char *bccdraft = NULL;
625 struct mailname *mp = NULL;
628 for (mp=bccs; mp; mp=mp->m_next) {
630 ** Note: This draft file will be left existing by send(1),
631 ** although renamed with backup prefix.
632 ** TODO: We should have it removed eventually.
634 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
635 fprintf(out, "To: %s\n", mp->m_text);
636 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
637 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
638 fprintf(out, "------------\n");
641 snprintf(buf, sizeof buf, "send %s", bccdraft);
642 if (system(buf) != 0) {
643 admonish(invo_name, "Problems to send Bcc to %s",
647 /* TODO: unlink renamed bcc draft after send(1) */