spost: refactoring, added comments, removed unused code
authormarkus schnalke <meillo@marmaro.de>
Tue, 31 Jan 2012 23:36:54 +0000 (00:36 +0100)
committermarkus schnalke <meillo@marmaro.de>
Tue, 31 Jan 2012 23:36:54 +0000 (00:36 +0100)
uip/spost.c

index f2552dc..73787f7 100644 (file)
@@ -105,7 +105,7 @@ static struct headers NHeaders[] = {
        { "Date", HNOP, MDAT },
        { "Subject", HSUB, 0 },
        { "To", HADR|HTRY, MVIS },
-       { "cc", HADR|HTRY, MVIS },
+       { "Cc", HADR|HTRY, MVIS },
        { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
        { "Message-Id", HBAD, 0 },
        { "Fcc", HFCC, 0 },
@@ -129,8 +129,6 @@ static struct headers RHeaders[] = {
 };
 
 
-static short fccind = 0;  /* index into fccfold[] */
-
 static int badmsg = 0;  /* message has bad semantics */
 static int verbose = 0;  /* spell it out */
 static int debug = 0;  /* debugging post */
@@ -153,6 +151,7 @@ static char signature[BUFSIZ];  /* my signature */
 static char *filter = NULL;  /* the filter for BCC'ing */
 static char *subject = NULL;  /* the subject field for BCC'ing */
 static char *fccfold[FCCS];  /* foldernames for FCC'ing */
+static short fccind = 0;  /* index into fccfold[] */
 
 static struct headers *hdrtab;  /* table for the message we're doing */
 static FILE *out;  /* output (temp) file */
@@ -176,12 +175,10 @@ static void finish_headers(FILE *);
 static int get_header(char *, struct headers *);
 static void putadr(char *, struct mailname *);
 static int putone(char *, int, int);
-static void insert_fcc(struct headers *, unsigned char *);
-static void file(char *);
+static void insert_fcc(char *, unsigned char *);
 static void fcc(char *, char *);
 
 #if 0
-static void die(char *, char *, ...);
 static void make_bcc_file(void);
 #endif
 
@@ -384,11 +381,14 @@ main(int argc, char **argv)
 
        if (debug) {
                done(0);
-       }
-       else
+       } else {
                fclose(out);
+       }
 
-       file(tmpfil);
+       /* process Fcc */
+       for (i=0; i<fccind; i++) {
+               fcc(tmpfil, fccfold[i]);
+       }
 
        /*
        ** re-open the temp file, unlink it and exec sendmail, giving it
@@ -443,13 +443,16 @@ putfmt(char *name, char *str, FILE *out)
                str++;
 
        if ((i = get_header(name, hdrtab)) == NOTOK) {
+               /* some boring header: push it through */
                fprintf(out, "%s: %s", name, str);
                return;
        }
 
+       /* one of the interesting headers */
        hdr = &hdrtab[i];
-       if (hdr->flags & HIGN)
+       if (hdr->flags & HIGN) {
                return;
+       }
        if (hdr->flags & HBAD) {
                advise(NULL, "illegal header line -- %s:", name);
                badmsg++;
@@ -457,18 +460,26 @@ putfmt(char *name, char *str, FILE *out)
        }
        msgflags |= hdr->set;
 
-       if (hdr->flags & HSUB)
-               subject = subject ? add(str, add("\t", subject)) :
-                               getcpy(str);
+       if (hdr->flags & HSUB) {
+               if (subject) {
+                       /* append mupliple subject */
+                       char *cp = concat(subject, "\t", str, NULL);
+                       free(subject);
+                       subject = cp;
+               } else {
+                       subject = getcpy(str);
+               }
+       }
 
        if (hdr->flags & HFCC) {
-               if ((cp = strrchr(str, '\n')))
-                       *cp = 0;
+               if ((cp = strrchr(str, '\n'))) {
+                       *cp = '\0';
+               }
                for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
-                       *cp++ = 0;
-                       insert_fcc(hdr, pp);
+                       *cp++ = '\0';
+                       insert_fcc(hdr->value, pp);
                }
-               insert_fcc(hdr, pp);
+               insert_fcc(hdr->value, pp);
                return;
        }
 
@@ -492,7 +503,7 @@ putfmt(char *name, char *str, FILE *out)
                        register struct mailname *mp = 0;
 
                        while ((cp = getname(str))) {
-                               mp = getm( cp, NULL, 0, AD_HOST, NULL);
+                               mp = getm(cp, NULL, 0, AD_HOST, NULL);
                                if (f == 0) {
                                        f = mp;
                                        mp->m_next = mp;
@@ -503,7 +514,7 @@ putfmt(char *name, char *str, FILE *out)
                                }
                        }
                        f = mp->m_next; mp->m_next = 0;
-                       putadr( name, f );
+                       putadr(name, f);
                } else {
                        /*
                        ** The author(s) of spost decided that alias
@@ -530,99 +541,69 @@ putfmt(char *name, char *str, FILE *out)
 }
 
 
+/*
+** Construct signature name
+*/
 static void
 start_headers(void)
 {
        char *cp;
-       char sigbuf[BUFSIZ];
 
        strncpy(from, getusername(), sizeof(from));
-
        if ((cp = getfullname()) && *cp) {
-               strncpy(sigbuf, cp, sizeof(sigbuf));
-               snprintf(signature, sizeof(signature), "%s <%s>",
-                               sigbuf,  from);
-       } else
-               snprintf(signature, sizeof(signature), "%s",  from);
+               snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
+       } else {
+               snprintf(signature, sizeof(signature), "%s", from);
+       }
 }
 
 
+/*
+** Add yet missing headers.
+*/
 static void
 finish_headers(FILE *out)
 {
-       switch (msgstate) {
-       case normal:
-               if (!(msgflags & MDAT))
-                       fprintf(out, "Date: %s\n", dtimenow(0));
-
-               if (msgflags & MFRM) {
-                       /*
-                       ** There was already a From: in the draft.
-                       ** Don't add one.
-                       */
-                       if (!draft_from_masquerading)
-                               /*
-                               ** mts.conf didn't contain
-                               ** "masquerade:[...]draft_from[...]"
-                               ** so we'll reveal the user's
-                               ** actual account@thismachine
-                               ** address in a Sender: header
-                               ** (and use it as the envelope
-                               ** From: later).
-                               */
-                               fprintf(out, "Sender: %s\n", from);
-               } else
-                       fprintf(out, "From: %s\n", signature);
+       char *resentstr = (msgstate == resent) ? "Resent-" : "";
 
+       if (!(msgflags & MDAT)) {
+               fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
+       }
+       if (!(msgflags & MFRM)) {
+               fprintf(out, "%sFrom: %s\n", resentstr, signature);
+       } else if (!draft_from_masquerading) {
+               /*
+               ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
+               ** so we'll reveal the user's actual account@thismachine
+               ** address in a Sender: header (and use it as the envelope
+               ** From: later).
+               */
+               fprintf(out, "%sSender: %s\n", resentstr, from);
+       }
 #ifdef notdef
-               if (!(msgflags & MVIS))
-                       fprintf(out, "Bcc: Blind Distribution List: ;\n");
+       if (!(msgflags & MVIS))
+               fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
 #endif /* notdef */
-               break;
 
-       case resent:
-               if (!(msgflags & MRDT))
-                       fprintf(out, "Resent-Date: %s\n", dtimenow(0));
-               if (msgflags & MRFM) {
-                       /*
-                       ** There was already a Resent-From: in draft.
-                       ** Don't add one.
-                       */
-                       if (!draft_from_masquerading)
-                               /*
-                               ** mts.conf didn't contain
-                               ** "masquerade:[...]draft_from[...]"
-                               ** so we'll reveal the user's
-                               ** actual account@thismachine
-                               ** address in a Sender: header
-                               ** (and use it as the envelope
-                               ** From: later).
-                               */
-                               fprintf(out, "Resent-Sender: %s\n",
-                                               from);
-               } else
-                       /* Construct a Resent-From: header. */
-                       fprintf(out, "Resent-From: %s\n", signature);
-#ifdef notdef
-               if (!(msgflags & MVIS))
-                       fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
-#endif /* notdef */
-               break;
-       }
-
-       if (badmsg)
+       if (badmsg) {
                adios(NULL, "re-format message and try again");
+       }
 }
 
 
+/*
+** Return index of the requested header in the table, or NOTOK if missing.
+*/
 static int
 get_header(char *header, struct headers *table)
 {
        struct headers *h;
 
-       for (h = table; h->value; h++)
-               if (!mh_strcasecmp(header, h->value))
+       for (h=table; h->value; h++) {
+               if (mh_strcasecmp(header, h->value)==0) {
                        return (h - table);
+               }
+       }
 
        return NOTOK;
 }
@@ -708,22 +689,25 @@ putone(char *adr, int pos, int indent)
 }
 
 
+/*
+** Insert the normalized value from pp into fccfold[].
+*/
 static void
-insert_fcc(struct headers *hdr, unsigned char *pp)
+insert_fcc(char *name, unsigned char *pp)
 {
-       unsigned char   *cp;
+       unsigned char *cp;
 
        for (cp = pp; isspace(*cp); cp++)
                continue;
        for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
                continue;
        if (pp >= cp)
-               *++pp = 0;
-       if (*cp == 0)
+               *++pp = '\0';
+       if (!*cp)
                return;
 
        if (fccind >= FCCS)
-               adios(NULL, "too many %ss", hdr->value);
+               adios(NULL, "too many %ss", name);
        fccfold[fccind++] = getcpy(cp);
 }
 
@@ -809,20 +793,8 @@ make_bcc_file(void)
 }
 #endif /* if 0 */
 
-/* FCC INTERACTION */
-
-static void
-file(char *path)
-{
-       int i;
-
-       if (fccind == 0)
-               return;
-
-       for (i = 0; i < fccind; i++)
-               fcc(path, fccfold[i]);
-}
 
+/* FCC INTERACTION */
 
 static void
 fcc(char *file, char *folder)
@@ -840,49 +812,35 @@ fcc(char *file, char *folder)
                sleep(5);
        switch (child_id) {
        case NOTOK:
-               if (!verbose)
-                       fprintf(stderr, "  %sFcc %s: ", msgstate == resent ?
-                                       "Resent-" : "", folder);
-               fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
+               if (verbose) {
+                       printf("Sorry man, but we had no more forks.\n");
+               } else {
+                       fprintf(stderr, "Skipped %sFcc %s: unable to fork.\n",
+                                       msgstate == resent ? "Resent-" : "",
+                                       folder);
+               }
                break;
 
        case OK:
                snprintf(fold, sizeof(fold), "%s%s",
-                       *folder == '+' || *folder == '@' ? "" : "+", folder);
+                               *folder == '+' || *folder == '@' ? "" : "+",
+                               folder);
                execlp(fileproc, mhbasename(fileproc),
-                       "-link", "-file", file, fold, NULL);
+                               "-link", "-file", file, fold, NULL);
                _exit(-1);
 
        default:
                if ((status = pidwait(child_id, OK))) {
-                       if (!verbose)
-                               fprintf(stderr, "  %sFcc %s: ",
+                       if (verbose) {
+                               printf(" errored (0%o)\n", status);
+                       } else {
+                               fprintf(stderr, "  %sFcc %s: errored (0%o)\n",
                                                msgstate == resent ?
-                                               "Resent-" : "", folder);
-                       fprintf(verbose ? stdout : stderr, " errored (0%o)\n",
-                                       status);
+                                               "Resent-" : "", folder,
+                                               status);
+                       }
                }
        }
 
        fflush(stdout);
 }
-
-
-#if 0
-
-/*
-** TERMINATION
-*/
-
-static void
-die(char *what, char *fmt, ...)
-{
-       va_list ap;
-
-       va_start(ap, fmt);
-       advertise(what, NULL, fmt, ap);
-       va_end(ap);
-
-       done(1);
-}
-#endif