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 leading whitespace */
318 while (*str==' ' || *str=='\t') {
322 if ((i = get_header(name, hdrtab)) == NOTOK) {
323 /* no header we would care for */
324 if (mh_strcasecmp(name, attach_hdr)!=0 &&
325 mh_strcasecmp(name, sign_hdr)!=0 &&
326 mh_strcasecmp(name, enc_hdr)!=0) {
327 /* push it through */
328 fprintf(out, "%s: %s", name, str);
332 /* it's one of the interesting headers */
335 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
339 if (hdr->flags & HBAD) {
340 advise(NULL, "illegal header line -- %s:", name);
345 msgflags |= hdr->set;
347 if (hdr->flags & HFCC) {
352 if (hdr->flags & HBCC) {
353 struct mailname *mp = NULL;
355 /* Create list of Bcc addrs. */
356 while ((cp = getname(str))) {
357 mp = getm(cp, NULL, 0, AD_HOST, NULL);
358 mp->m_next = bccs; /* push */
364 if (aliasflg && hdr->flags & HTRY) {
366 ** This header contains address(es) that we have to do
367 ** alias expansion on. Because of the saved state in
368 ** getname we have to put all the addresses into a list.
370 struct mailname *f = NULL;
371 struct mailname *mp = NULL;
373 while ((cp = getname(str))) {
374 mp = getm(cp, NULL, 0, AD_HOST, NULL);
379 mp->m_next = f->m_next;
386 /* Now munch on the list, possibly expanding aliases */
392 ** The author(s) of spost decided that alias substitution wasn't
393 ** necessary for the non-HTRY headers. Unfortunately, one of
394 ** those headers is "From:", and having alias substitution work on
395 ** that is extremely useful for someone with a lot of POP3 email
396 ** accounts or aliases. post supports aliasing of "From:"...
398 ** Since "From:"-processing is incompletely implemented in this
399 ** unsupported and undocumented spost backend, I'm not going
400 ** to take the time to implement my new draft-From:-based email
401 ** address masquerading. If I do ever implement it here, I'd almost
402 ** certainly want to implement "From:" line alias processing as well.
403 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
406 ** Although there is no masquerading anymore in mmh, we might want
407 ** to have aliasing of From: addresses. Think about it.
408 ** -- meillo@marmaro.de 2012-02
411 if (hdr->flags & HSUB) {
412 subject = getcpy(str);
414 fprintf(out, "%s: %s", name, str);
419 ** Add yet missing headers.
422 finish_headers(FILE *out)
425 char from[BUFSIZ]; /* my network address */
426 char signature[BUFSIZ]; /* my signature */
427 char *resentstr = (msgstate == resent) ? "Resent-" : "";
429 if (!(msgflags & MDAT)) {
430 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
433 strncpy(from, getusername(), sizeof(from));
434 if ((cp = getfullname()) && *cp) {
435 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
437 snprintf(signature, sizeof(signature), "%s", from);
439 if (!(msgflags & MFRM)) {
440 fprintf(out, "%sFrom: %s\n", resentstr, signature);
442 /* In case the From: header contains multiple addresses. */
443 fprintf(out, "%sSender: %s\n", resentstr, from);
445 if (!(msgflags & MVIS)) {
446 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
450 adios(NULL, "re-format message and try again");
456 ** Return index of the requested header in the table, or NOTOK if missing.
459 get_header(char *header, struct headers *table)
463 for (h=table; h->value; h++) {
464 if (mh_strcasecmp(header, h->value)==0) {
474 ** output the address list for header "name". The address list
475 ** is a linked list of mailname structs. "nl" points to the head
476 ** of the list. Alias substitution should be done on nl.
479 putadr(char *name, struct mailname *nl)
481 struct mailname *mp, *mp2;
486 fprintf(out, "%s: ", name);
487 namelen = strlen(name) + 2;
490 for (mp = nl; mp; ) {
491 if (linepos > MAX_SM_FIELD) {
492 fprintf(out, "\n%s: ", name);
496 /* a local name - see if it's an alias */
497 cp = akvalue(mp->m_mbox);
498 if (cp == mp->m_mbox) {
499 /* wasn't an alias - use what the user typed */
500 linepos = putone(mp->m_text, linepos, namelen);
502 /* an alias - expand it */
503 while ((cp = getname(cp))) {
504 if (linepos > MAX_SM_FIELD) {
505 fprintf(out, "\n%s: ", name);
508 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
510 mp2->m_pers = getcpy(mp->m_mbox);
511 linepos = putone(adrformat(mp2), linepos, namelen);
513 linepos = putone(mp2->m_text,
521 /* not a local name - use what the user typed */
522 linepos = putone(mp->m_text, linepos, namelen);
532 putone(char *adr, int pos, int indent)
540 else if (linepos+len > OUTPUTLINELEN) {
541 fprintf(out, ",\n%*s", indent, "");
557 process_fcc(char *str)
562 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
563 adios(NULL, "Too much Fcc data");
565 /* todo: better have three states: SEPARATOR, PLUS, WORD */
566 for (cp=pp=str; *cp; cp++) {
575 if (*pp=='+' || *pp=='@') {
595 fcc(char *file, char *folders)
601 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
605 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
606 adios(NULL, "Too much Fcc data");
608 /* hack: read from /dev/null and refile(1) won't question us */
609 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
611 status = system(cmd);
613 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
614 msgstate == resent ? "Resent-" : "", folders);
615 } else if (status != 0) {
616 fprintf(stderr, "%sFcc %s: Problems occured.\n",
617 msgstate == resent ? "Resent-" : "", folders);
625 process_bccs(char *origmsg)
627 char *bccdraft = NULL;
629 struct mailname *mp = NULL;
632 for (mp=bccs; mp; mp=mp->m_next) {
633 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
634 fprintf(out, "To: %s\n", mp->m_text);
635 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
636 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
637 fprintf(out, "------------\n");
640 snprintf(buf, sizeof buf, "send %s", bccdraft);
641 if (system(buf) != 0) {
642 admonish(invo_name, "Problems to send Bcc to %s",