Renamed -version switch to -Version to remove the conflict with -verbose.
[mmh] / uip / spost.c
index 4a8ffdc..f3630d2 100644 (file)
@@ -18,7 +18,6 @@
 #include <h/utils.h>
 
 #define MAX_SM_FIELD 1476  /* < largest hdr field sendmail will accept */
-#define FCCS 10  /* max number of fccs allowed */
 
 struct swit switches[] = {
 #define VERBSW  0
@@ -34,18 +33,14 @@ struct swit switches[] = {
 #define NALIASW  5
        { "noalias", 0 },
 #define VERSIONSW  6
-       { "version", 0 },
+       { "Version", 0 },
 #define HELPSW  7
        { "help", 0 },
 #define DEBUGSW  8
        { "debug", -5 },
 #define DISTSW  9
        { "dist", -4 },  /* interface from dist */
-#define PUSHSW  10  /* fork to sendmail then exit */
-       { "push", -4 },
-#define NPUSHSW  11  /* exec sendmail */
-       { "nopush", -6 },
-#define LIBSW  12
+#define LIBSW  10
        { "library directory", -7 },
        { NULL, 0 }
 };
@@ -75,7 +70,6 @@ struct headers {
        unsigned int set;
 };
 
-
 static struct headers NHeaders[] = {
        { "Return-Path", HBAD, 0 },
        { "Received", HBAD, 0 },
@@ -89,6 +83,7 @@ static struct headers NHeaders[] = {
        { "Bcc", HADR|HTRY|HBCC, MINV },
        { "Message-Id", HBAD, 0 },
        { "Fcc", HFCC, 0 },
+       { "Envelope-From", HIGN, 0 },
        { NULL, 0, 0 }
 };
 
@@ -105,6 +100,7 @@ static struct headers RHeaders[] = {
        { "Resent-Fcc", HFCC, 0 },
        { "Reply-To", HADR, 0 },
        { "Fcc", HIGN, 0 },
+       { "Envelope-From", HIGN, 0 },
        { NULL, 0, 0 }
 };
 
@@ -113,7 +109,6 @@ static int badmsg = 0;  /* message has bad semantics */
 static int verbose = 0;  /* spell it out */
 static int debug = 0;  /* debugging post */
 static int watch = 0;  /* watch the delivery process */
-static int pushflg = 0;  /* if going to fork to sendmail */
 static int aliasflg = 0;  /* if going to process aliases */
 
 static unsigned msgflags = 0;  /* what we've seen */
@@ -122,18 +117,15 @@ static enum {
        normal, resent
 } msgstate = normal;
 
-static char tmpfil[] = "/tmp/spost-XXXXXX";
-static char bccfil[] = "/tmp/spost-bcc-XXXXXX";
+static char *tmpfil;
 
 static char *subject = NULL;  /* the subject field for BCC'ing */
 static char fccs[BUFSIZ] = "";
-struct mailname *bccs = NULL;
+struct mailname *bccs = NULL;  /* list of the bcc recipients */
 
 static struct headers *hdrtab;  /* table for the message we're doing */
 static FILE *out;  /* output (temp) file */
 
-extern char *sendmail;
-
 /*
 ** static prototypes
 */
@@ -144,13 +136,13 @@ static void putadr(char *, struct mailname *);
 static int putone(char *, int, int);
 static void process_fcc(char *);
 static void fcc(char *, char *);
-static void make_bcc_draft(char *);
+static void process_bccs(char *);
 
 
 int
 main(int argc, char **argv)
 {
-       int state, i, pid, compnum;
+       int state, compnum;
        char *cp, *msg = NULL, **argp, **arguments;
        char *sargv[16], buf[BUFSIZ], name[NAMESZ];
        FILE *in;
@@ -208,13 +200,6 @@ main(int argc, char **argv)
                                watch = 0;
                                continue;
 
-                       case PUSHSW:
-                               pushflg++;
-                               continue;
-                       case NPUSHSW:
-                               pushflg = 0;
-                               continue;
-
                        case ALIASW:
                                if (!(cp = *argp++) || *cp == '-')
                                        adios(NULL, "missing argument to %s",
@@ -253,15 +238,7 @@ main(int argc, char **argv)
                verbose++;
                out = stdout;
        } else {
-#ifdef HAVE_MKSTEMP
-               if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
-                       adios(tmpfil, "unable to create");
-#else
-               mktemp(tmpfil);
-               if ((out = fopen(tmpfil, "w")) == NULL)
-                       adios(tmpfil, "unable to create");
-               chmod(tmpfil, 0600);
-#endif
+               tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
        }
 
        hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
@@ -323,20 +300,6 @@ main(int argc, char **argv)
                fcc(tmpfil, fccs);
        }
 
-       /* process Bcc */
-       if (bccs) {
-               make_bcc_draft(tmpfil);
-       }
-
-       /*
-       ** re-open the temp file, unlink it and exec sendmail, giving it
-       ** the msg temp file as std in.
-       */
-       if (!freopen(tmpfil, "r", stdin)) {
-               adios(tmpfil, "can't reopen for sendmail");
-       }
-       unlink(tmpfil);
-
        argp = sargv;
        *argp++ = "send-mail";
        *argp++ = "-m";  /* send to me too */
@@ -344,51 +307,33 @@ main(int argc, char **argv)
        *argp++ = "-i";  /* don't stop on "." */
        if (watch || verbose) {
                *argp++ = "-v";
-               pushflg = 0;
        }
        *argp = NULL;
 
-       if (pushflg) {
-               /* fork to a child to run sendmail */
-               for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
-                       sleep(5);
-               switch (pid) {
-               case NOTOK:
-                       fprintf(verbose ? stdout : stderr,
-                                       "%s: can't fork to %s\n",
-                                       invo_name, sendmail);
-                       exit(-1);
-               case OK:
-                       /* we're the child .. */
-                       break;
-               default:
+       if (bccs) {
+               process_bccs(tmpfil);
+               if (!(msgflags & MVIS)) {
+                       /* only Bcc rcpts: we're finished here */
+                       unlink(tmpfil);
                        exit(0);
                }
        }
 
-       if (0 /*bccs*/) {
-               /* fork to a child to run sendmail */
-               for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
-                       sleep(5);
-               switch (pid) {
-               case NOTOK:
-                       fprintf(verbose ? stdout : stderr,
-                                       "%s: can't fork to %s\n",
-                                       invo_name, sendmail);
-                       exit(-1);
-               case OK:
-                       /* we're the child .. */
-                       exit(0);
-               default:
-                       exit(0);
-               }
+       /*
+       ** re-open the temp file, unlink it and exec sendmail, giving it
+       ** the msg temp file as std in.
+       */
+       if (!freopen(tmpfil, "r", stdin)) {
+               adios(tmpfil, "can't reopen for sendmail");
        }
+       unlink(tmpfil);
 
        execv(sendmail, sargv);
        adios(sendmail, "can't exec");
-       return 0;  /* dead code to satisfy the compiler */
+       return -1;
 }
 
+
 /* DRAFT GENERATION */
 
 static void
@@ -506,7 +451,7 @@ finish_headers(FILE *out)
        char *resentstr = (msgstate == resent) ? "Resent-" : "";
 
        if (!(msgflags & MDAT)) {
-               fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
+               fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
        }
 
        strncpy(from, getusername(), sizeof(from));
@@ -525,6 +470,7 @@ finish_headers(FILE *out)
                fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
        }
        if (badmsg) {
+               unlink(tmpfil);
                adios(NULL, "re-format message and try again");
        }
 }
@@ -680,12 +626,12 @@ fcc(char *file, char *folders)
                                folders);
                fflush(stdout);
        }
-       if (strlen(fileproc)+strlen(file)+strlen(folders)+100 > sizeof cmd) {
+       if (100+strlen(file)+strlen(folders) > sizeof cmd) {
                adios(NULL, "Too much Fcc data");
        }
        /* hack: read from /dev/null and refile(1) won't question us */
-       snprintf(cmd, sizeof cmd, "</dev/null %s -link -file %s %s",
-                       fileproc, file, folders);
+       snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
+                       file, folders);
        status = system(cmd);
        if (status == -1) {
                fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
@@ -700,28 +646,32 @@ fcc(char *file, char *folders)
 /* BCC GENERATION */
 
 static void
-make_bcc_draft(char *draft)
+process_bccs(char *origmsg)
 {
-       int fd;
-       FILE *out;
-
-#ifdef HAVE_MKSTEMP
-       fd = mkstemp(bccfil);
-       if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
-               adios(bccfil, "unable to create");
-#else
-       mktemp(bccfil);
-       if ((out = fopen(bccfil, "w")) == NULL)
-               adios(bccfil, "unable to create");
-#endif
-       chmod(bccfil, 0600);
+       char *bccdraft = NULL;
+       char buf[BUFSIZ];
+       struct mailname *mp = NULL;
+       FILE *out = NULL;
 
-       if (subject) {
-               fprintf(out, "Subject: BCC: %s", subject);
-       } else {
-               fprintf(out, "Subject: BCC: message without subject");
+       for (mp=bccs; mp; mp=mp->m_next) {
+               /*
+               ** Note: This draft file will be left existing by send(1),
+               ** although renamed with backup prefix.
+               ** TODO: We should have it removed eventually.
+               */
+               bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
+               fprintf(out, "To: %s\n", mp->m_text);
+               fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
+               fprintf(out, "%s: %s\n", attach_hdr, origmsg);
+               fprintf(out, "------------\n");
+               fclose(out);
+
+               snprintf(buf, sizeof buf, "send %s", bccdraft);
+               if (system(buf) != 0) {
+                       admonish(invo_name, "Problems to send Bcc to %s",
+                                       mp->m_text);
+                       unlink(bccdraft);
+               }
+               /* TODO: unlink renamed bcc draft after send(1) */
        }
-       fprintf(out, "%s: %s\n", attach_hdr, draft);
-       fprintf(out, "------------\n");
-       fclose(out);
 }