Now, spost should be quite usable.
authormarkus schnalke <meillo@marmaro.de>
Mon, 13 Feb 2012 12:51:53 +0000 (13:51 +0100)
committermarkus schnalke <meillo@marmaro.de>
Mon, 13 Feb 2012 12:51:53 +0000 (13:51 +0100)
Make use of m_mktemp2(), like elsewhere in the mmh code base.
Various rework of the Bcc handling code.
Still to do:
- Fcc handling needs improvements.
- Draft files for Bcc recipients are left existing in /tmp.

uip/spost.c

index 4a8ffdc..046f340 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
@@ -75,7 +74,6 @@ struct headers {
        unsigned int set;
 };
 
-
 static struct headers NHeaders[] = {
        { "Return-Path", HBAD, 0 },
        { "Received", HBAD, 0 },
@@ -122,18 +120,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 +139,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;
@@ -253,15 +248,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 +310,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 */
@@ -348,29 +321,27 @@ main(int argc, char **argv)
        }
        *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*/) {
+       /*
+       ** 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);
+
+       if (pushflg) {
                /* fork to a child to run sendmail */
-               for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
-                       sleep(5);
-               switch (pid) {
+               switch (fork()) {
                case NOTOK:
                        fprintf(verbose ? stdout : stderr,
                                        "%s: can't fork to %s\n",
@@ -378,17 +349,17 @@ main(int argc, char **argv)
                        exit(-1);
                case OK:
                        /* we're the child .. */
-                       exit(0);
+                       break;
                default:
                        exit(0);
                }
        }
-
        execv(sendmail, sargv);
        adios(sendmail, "can't exec");
-       return 0;  /* dead code to satisfy the compiler */
+       return -1;
 }
 
+
 /* DRAFT GENERATION */
 
 static void
@@ -525,6 +496,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");
        }
 }
@@ -700,28 +672,33 @@ 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, "%s%s %s", sendproc,
+                               pushflg ? " -push" : "", 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);
 }