Removed mhmail.c because it was no longer needed and could get in
[mmh] / uip / spost.c
index 7d65ead..fef583e 100644 (file)
@@ -5,7 +5,9 @@
  * This is a simpler, faster, replacement for "post" for use
  * when "sendmail" is the transport system.
  *
- * $Id$
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mh.h>
@@ -13,9 +15,9 @@
 #include <h/addrsbr.h>
 #include <h/aliasbr.h>
 #include <h/dropsbr.h>
-#include <zotnet/tws/tws.h>
-
-#define        uptolow(c)      ((isalpha(c) && isupper (c)) ? tolower (c) : c)
+#include <h/tws.h>
+#include <h/mts.h>
+#include <h/utils.h>
 
 #define MAX_SM_FIELD   1476    /* < largest hdr field sendmail will accept */
 #define FCCS           10      /* max number of fccs allowed */
@@ -25,53 +27,45 @@ struct swit switches[] = {
     { "filter filterfile", 0 },
 #define        NFILTSW              1
     { "nofilter", 0 },
-#define        FRMTSW               2
-    { "format", 0 },
-#define        NFRMTSW              3
-    { "noformat", 0 },
-#define        REMVSW               4
+#define        REMVSW               2
     { "remove", 0 },
-#define        NREMVSW              5
+#define        NREMVSW              3
     { "noremove", 0 },
-#define        VERBSW               6
+#define        VERBSW               4
     { "verbose", 0 },
-#define        NVERBSW              7
+#define        NVERBSW              5
     { "noverbose", 0 },
-#define        WATCSW               8
+#define        WATCSW               6
     { "watch", 0 },
-#define        NWATCSW              9
+#define        NWATCSW              7
     { "nowatch", 0 },
-#define BACKSW              10
+#define BACKSW               8
     { "backup", 0 },
-#define NBACKSW             11
+#define NBACKSW              9
     { "nobackup", 0 },
-#define ALIASW              12
+#define ALIASW              10
     { "alias aliasfile", 0 },
-#define NALIASW             13
+#define NALIASW             11
     { "noalias", 0 },
-#define WIDTHSW             14
+#define WIDTHSW             12
     { "width columns", 0 },
-#define VERSIONSW           15
+#define VERSIONSW           13
     { "version", 0 },
-#define        HELPSW              16
-    { "help", 4 },
-#define        DEBUGSW             17
+#define        HELPSW              14
+    { "help", 0 },
+#define        DEBUGSW             15
     { "debug", -5 },
-#define        DISTSW              18
+#define        DISTSW              16
     { "dist", -4 },            /* interface from dist */
-#define CHKSW               19
-    { "check", -5 },           /* interface from whom */
-#define NCHKSW              20
-    { "nocheck", -7 },         /* interface from whom */
-#define WHOMSW              21
+#define WHOMSW              17
     { "whom", -4 },            /* interface from whom */
-#define PUSHSW              22 /* fork to sendmail then exit */
+#define PUSHSW              18 /* fork to sendmail then exit */
     { "push", -4 },
-#define NPUSHSW             23 /* exec sendmail */
+#define NPUSHSW             19 /* exec sendmail */
     { "nopush", -6 },
-#define LIBSW               24
+#define LIBSW               20
     { "library directory", -7 },
-#define        ANNOSW              25
+#define        ANNOSW              21
     { "idanno number", -6 },
     { NULL, 0 }
 };
@@ -84,7 +78,7 @@ struct swit switches[] = {
 #define        HSUB    0x0004          /* Subject: header */
 #define        HTRY    0x0008          /* try to send to addrs on header */
 #define        HBCC    0x0010          /* don't output this header */
-#define        HMNG    0x0020          /* mung this header */
+/* #define     HMNG    0x0020  */      /* munge this header */
 #define        HNGR    0x0040          /* no groups allowed in this header */
 #define        HFCC    0x0080          /* FCC: type header */
 #define        HNIL    0x0100          /* okay for this header not to have addrs */
@@ -118,6 +112,7 @@ static struct headers NHeaders[] = {
     { "Bcc",         HADR|HTRY|HBCC|HNIL, MINV },
     { "Message-Id",  HBAD,                0 },
     { "Fcc",         HFCC,                0 },
+    { "Envelope-From",HIGN,               0 },
     { NULL,          0,                   0 }
 };
 
@@ -134,6 +129,7 @@ static struct headers RHeaders[] = {
     { "Resent-Fcc",        HFCC,           0 },
     { "Reply-To",          HADR,           0 },
     { "Fcc",               HIGN,           0 },
+    { "Envelope-From",     HIGN,           0 },
     { NULL,                0,              0 }
 };
 
@@ -185,7 +181,7 @@ 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 *, char *);
+static void insert_fcc (struct headers *, unsigned char *);
 static void file (char *);
 static void fcc (char *, char *);
 
@@ -228,10 +224,10 @@ main (int argc, char **argv)
                case HELPSW: 
                    snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
                    print_help (buf, switches, 1);
-                   done (1);
+                   done (0);
                case VERSIONSW:
                    print_version(invo_name);
-                   done (1);
+                   done (0);
 
                case DEBUGSW: 
                    debug++;
@@ -311,8 +307,15 @@ main (int argc, char **argv)
                    continue;
 
                case LIBSW:
+                   if (!(cp = *argp++) || *cp == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   /* create a minimal context */
+                   if (context_foil (cp) == -1)
+                       done(1);
+                   continue;
+
                case ANNOSW:
-                   /* -library & -idanno switch ignored */
+                   /* -idanno switch ignored */
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
                    continue;
@@ -339,10 +342,8 @@ main (int argc, char **argv)
        out = stdout;
     }
     else {
-           mktemp (tmpfil);
-           if ((out = fopen (tmpfil, "w")) == NULL)
+           if ((out = fdopen( mkstemp (tmpfil), "w" )) == NULL )
                adios (tmpfil, "unable to create");
-           chmod (tmpfil, 0600);
        }
 
     hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
@@ -443,6 +444,7 @@ main (int argc, char **argv)
     }
     execv ( sendmail, sargv);
     adios ( sendmail, "can't exec");
+    return 0;  /* dead code to satisfy the compiler */
 }
 
 /* DRAFT GENERATION */
@@ -478,7 +480,7 @@ putfmt (char *name, char *str, FILE *out)
     if (hdr->flags & HFCC) {
        if ((cp = strrchr(str, '\n')))
            *cp = 0;
-       for (cp = pp = str; cp = strchr(pp, ','); pp = cp) {
+       for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
            *cp++ = 0;
            insert_fcc (hdr, pp);
        }
@@ -493,7 +495,7 @@ putfmt (char *name, char *str, FILE *out)
     }
 #endif /* notdef */
 
-    if (*str != '\n' && *str != '\0')
+    if (*str != '\n' && *str != '\0') {
        if (aliasflg && hdr->flags & HTRY) {
            /* this header contains address(es) that we have to do
             * alias expansion on.  Because of the saved state in
@@ -518,8 +520,21 @@ putfmt (char *name, char *str, FILE *out)
            f = mp->m_next; mp->m_next = 0;
            putadr( name, f );
        } else {
+           /* The author(s) of spost decided that alias substitution wasn't
+              necessary for the non-HTRY headers.  Unfortunately, one of those
+              headers is "From:", and having alias substitution work on that is
+              extremely useful for someone with a lot of POP3 email accounts or
+              aliases.  post supports aliasing of "From:"...
+
+              Since "From:"-processing is incompletely implemented in this
+              unsupported and undocumented spost backend, I'm not going to take
+              the time to implement my new draft-From:-based email address
+              masquerading.  If I do ever implement it here, I'd almost
+              certainly want to implement "From:" line alias processing as
+              well.  -- Dan Harkless <dan-nmh@dilvish.speed.net> */
            fprintf (out, "%s: %s", name, str );
        }
+    }
 }
 
 
@@ -547,10 +562,10 @@ finish_headers (FILE *out)
        case normal: 
            if (!(msgflags & MDAT))
                fprintf (out, "Date: %s\n", dtimenow (0));
-           if (msgflags & MFRM)
-               fprintf (out, "Sender: %s\n", from);
-           else
+           
+           if (!(msgflags & MFRM))
                fprintf (out, "From: %s\n", signature);
+          
 #ifdef notdef
            if (!(msgflags & MVIS))
                fprintf (out, "Bcc: Blind Distribution List: ;\n");
@@ -560,9 +575,8 @@ finish_headers (FILE *out)
        case resent: 
            if (!(msgflags & MRDT))
                fprintf (out, "Resent-Date: %s\n", dtimenow(0));
-           if (msgflags & MRFM)
-               fprintf (out, "Resent-Sender: %s\n", from);
-           else
+           if (!(msgflags & MRFM))
+               /* Construct a Resent-From: header. */
                fprintf (out, "Resent-From: %s\n", signature);
 #ifdef notdef
            if (!(msgflags & MVIS))
@@ -582,7 +596,7 @@ get_header (char *header, struct headers *table)
     struct headers *h;
 
     for (h = table; h->value; h++)
-       if (!strcasecmp (header, h->value))
+       if (!mh_strcasecmp (header, h->value))
            return (h - table);
 
     return NOTOK;
@@ -671,9 +685,9 @@ putone (char *adr, int pos, int indent)
 
 
 static void
-insert_fcc (struct headers *hdr, char *pp)
+insert_fcc (struct headers *hdr, unsigned char *pp)
 {
-    char   *cp;
+    unsigned char   *cp;
 
     for (cp = pp; isspace (*cp); cp++)
        continue;
@@ -700,13 +714,15 @@ make_bcc_file (void)
     char *vec[6];
     FILE * in, *out;
 
-    mktemp (bccfil);
-    if ((out = fopen (bccfil, "w")) == NULL)
+    fd = mkstemp(bccfil);
+    if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
        adios (bccfil, "unable to create");
     chmod (bccfil, 0600);
 
     fprintf (out, "Date: %s\n", dtimenow (0));
-    fprintf (out, "From: %s\n", signature);
+    if (!(msgflags & MFRM))
+      /* Construct a From: header. */
+      fprintf (out, "From: %s\n", signature);
     if (subject)
        fprintf (out, "Subject: %s", subject);
     fprintf (out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");