]> diplodocus.org Git - nmh/blobdiff - uip/mhbuildsbr.c
Migrated show_content_aux2() to argsplit().
[nmh] / uip / mhbuildsbr.c
index 58540f1b35e261341d69c3eb5641553f600ffa3e..b2949c3905c01bc5c54cd723388f86fe93cae4ee 100644 (file)
@@ -42,12 +42,6 @@ extern int contentidsw;
 extern int rcachesw;   /* mhcachesbr.c */
 extern int wcachesw;   /* mhcachesbr.c */
 
-/*
- * Directory to place tmp files.  This must
- * be set before these routines are called.
- */
-char *tmp;
-
 pid_t xpid = 0;
 
 static char prefix[] = "----- =_aaaaaaaaaa";
@@ -78,12 +72,12 @@ void free_encoding (CT, int);
  * static prototypes
  */
 static int init_decoded_content (CT);
-static void setup_attach_content(CT, const char *);
+static void setup_attach_content(CT, char *);
 static char *fgetstr (char *, int, FILE *);
-static int user_content (FILE *, char *, char *, CT *);
+static int user_content (FILE *, char *, CT *);
 static void set_id (CT, int);
 static int compose_content (CT);
-static int scan_content (CT);
+static int scan_content (CT, size_t);
 static int build_headers (CT);
 static char *calculate_digest (CT, int);
 
@@ -129,7 +123,8 @@ static void directive_pop(void)
  */
 
 CT
-build_mime (char *infile, int directives, int header_encoding)
+build_mime (char *infile, int autobuild, int dist, int directives,
+           int header_encoding, size_t maxunencoded)
 {
     int        compnum, state;
     char buf[BUFSIZ], name[NAMESZ];
@@ -176,13 +171,16 @@ build_mime (char *infile, int directives, int header_encoding)
        case FLDPLUS:
            compnum++;
 
-           /* abort if draft has Mime-Version header field */
-           if (!strcasecmp (name, VRSN_FIELD))
-               adios (NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
-
-           /* abort if draft has Content-Transfer-Encoding header field */
-           if (!strcasecmp (name, ENCODING_FIELD))
-               adios (NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
+           /* abort if draft has Mime-Version or C-T-E header field */
+           if (strcasecmp (name, VRSN_FIELD) == 0 ||
+               strcasecmp (name, ENCODING_FIELD) == 0) {
+               if (autobuild) {
+                   fclose(in);
+                   return NULL;
+               } else {
+                   adios (NULL, "draft shouldn't contain %s: field", name);
+               }
+           }
 
            /* ignore any Content-Type fields in the header */
            if (!strcasecmp (name, TYPE_FIELD)) {
@@ -211,10 +209,32 @@ build_mime (char *infile, int directives, int header_encoding)
 
            if (strcasecmp(ATTACH_FIELD, np) == 0) {
                struct attach_list *entry;
+               char *s = vp, *e = vp + strlen(vp) - 1;
                free(np);
+
+               /*
+                * Make sure we can find the start of this filename.
+                * If it's blank, we skip completely.  Otherwise, strip
+                * off any leading spaces and trailing newlines.
+                */
+
+               while (isspace((unsigned char) *s))
+                   s++;
+
+               while (e > s && *e == '\n')
+                   *e-- = '\0';
+
+               if (*s == '\0') {
+                   free(vp);
+                   goto finish_field;
+               }
+
                entry = mh_xmalloc(sizeof(*entry));
-               entry->filename = vp;
-               if (! attach_tail) {
+               entry->filename = getcpy(s);
+               entry->next = NULL;
+               free(vp);
+
+               if (attach_tail) {
                    attach_tail->next = entry;
                    attach_tail = entry;
                } else {
@@ -228,12 +248,10 @@ finish_field:
            /* if this wasn't the last header field, then continue */
            continue;
 
-       case FILEEOF:
-           adios (NULL, "draft has empty body -- no directives!");
-           /* NOTREACHED */
-
        case BODY:
            fseek (in, (long) (-strlen (buf)), SEEK_CUR);
+           /* fall through */
+       case FILEEOF:
            break;
 
        case LENERR:
@@ -262,9 +280,12 @@ finish_field:
      * Now add the MIME-Version header field
      * to the list of header fields.
      */
-    np = add (VRSN_FIELD, NULL);
-    vp = concat (" ", VRSN_VALUE, "\n", NULL);
-    add_header (ct, np, vp);
+
+    if (! dist) {
+       np = add (VRSN_FIELD, NULL);
+       vp = concat (" ", VRSN_VALUE, "\n", NULL);
+       add_header (ct, np, vp);
+    }
 
     /*
      * We initally assume we will find multiple contents in the
@@ -290,7 +311,7 @@ finish_field:
        struct part *part;
        CT p;
 
-       if (user_content (in, infile, buf, &p) == DONE) {
+       if (user_content (in, buf, &p) == DONE) {
            admonish (NULL, "ignoring spurious #end");
            continue;
        }
@@ -304,12 +325,6 @@ finish_field:
        part->mp_part = p;
     }
 
-    /*
-     * close the composition draft since
-     * it's not needed any longer.
-     */
-    fclose (in);
-
     /*
      * Add any Attach headers to the list of MIME parts at the end of the
      * message.
@@ -320,7 +335,7 @@ finish_field:
        struct part *part;
        CT p;
 
-       if (! access(at_entry->filename, R_OK)) {
+       if (access(at_entry->filename, R_OK) != 0) {
            adios("reading", "Unable to open %s for", at_entry->filename);
        }
 
@@ -348,9 +363,55 @@ finish_field:
        free(at_prev);
     }
 
-    /* check if any contents were found */
-    if (!m->mp_parts)
-       adios (NULL, "no content directives found");
+    /*
+     * To allow for empty message bodies, if we've found NO content at all
+     * yet cook up an empty text/plain part.
+     */
+
+    if (!m->mp_parts) {
+       CT p;
+       struct part *part;
+       struct text *t;
+
+       if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
+           adios(NULL, "out of memory");
+
+       init_decoded_content(p);
+
+       if (get_ctinfo ("text/plain", p, 0) == NOTOK)
+           done (1);
+
+       p->c_type = CT_TEXT;
+       p->c_subtype = TEXT_PLAIN;
+       p->c_encoding = CE_7BIT;
+       p->c_file = getcpy(infile);
+       /*
+        * Sigh.  ce_file contains the "decoded" contents of this part.
+        * So this seems like the best option available since we're going
+        * to call scan_content() on this.
+        */
+       p->c_cefile.ce_file = getcpy("/dev/null");
+       p->c_begin = ftell(in);
+       p->c_end = ftell(in);
+
+       if ((t = (struct text *) calloc (1, sizeof (*t))) == NULL)
+           adios (NULL, "out of memory");
+
+       t->tx_charset = CHARSET_SPECIFIED;
+       p->c_ctparams = t;
+
+       if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+           adios (NULL, "out of memory");
+       *pp = part;
+       pp = &part->mp_next;
+       part->mp_part = p;
+    }
+
+    /*
+     * close the composition draft since
+     * it's not needed any longer.
+     */
+    fclose (in);
 
     /*
      * If only one content was found, then remove and
@@ -388,7 +449,7 @@ finish_field:
      * check if prefix for multipart boundary clashes with
      * any of the contents.
      */
-    while (scan_content (ct) == NOTOK) {
+    while (scan_content (ct, maxunencoded) == NOTOK) {
        if (*cp < 'z') {
            (*cp)++;
         } else {
@@ -400,7 +461,8 @@ finish_field:
     }
 
     /* Build the rest of the header field structures */
-    build_headers (ct);
+    if (! dist)
+       build_headers (ct);
 
     return ct;
 }
@@ -466,7 +528,7 @@ fgetstr (char *s, int n, FILE *stream)
  */
 
 static int
-user_content (FILE *in, char *file, char *buf, CT *ctp)
+user_content (FILE *in, char *buf, CT *ctp)
 {
     int        extrnal, vrsn;
     char *cp, **ap;
@@ -512,8 +574,10 @@ user_content (FILE *in, char *file, char *buf, CT *ctp)
        FILE *out;
         char *cp;
 
-        cp = m_mktemp2(NULL, invo_name, NULL, &out);
-        if (cp == NULL) adios("mhbuildsbr", "unable to create temporary file");
+       if ((cp = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
+           adios("mhbuildsbr", "unable to create temporary file in %s",
+                 get_temp_dir());
+       }
 
        /* use a temp file to collect the plain text lines */
        ce->ce_file = add (cp, NULL);
@@ -933,7 +997,7 @@ use_forw:
            struct part *part;
            CT p;
 
-           if (user_content (in, file, buffer, &p) == DONE) {
+           if (user_content (in, buffer, &p) == DONE) {
                if (!m->mp_parts)
                    adios (NULL, "empty \"#begin ... #end\" sequence");
                return OK;
@@ -1072,10 +1136,10 @@ compose_content (CT ct)
            if (!(cp = ci->ci_magic))
                adios (NULL, "internal error(5)");
 
-            tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
-            if (tfile == NULL) {
-                adios("mhbuildsbr", "unable to create temporary file");
-            }
+           if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
+               adios("mhbuildsbr", "unable to create temporary file in %s",
+                     get_temp_dir());
+           }
            ce->ce_file = add (tfile, NULL);
            ce->ce_unlink = 1;
 
@@ -1214,11 +1278,12 @@ raw:
  */
 
 static int
-scan_content (CT ct)
+scan_content (CT ct, size_t maxunencoded)
 {
     int len;
-    int check8bit = 0, contains8bit = 0;  /* check if contains 8bit data                */
-    int checklinelen = 0, linelen = 0;   /* check for long lines                       */
+    int check8bit = 0, contains8bit = 0;  /* check if contains 8bit data */
+    int checklinelen = 0, linelen = 0;  /* check for long lines */ 
+    int checkllinelen = 0; /* check for extra-long lines */
     int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary   */
     int checklinespace = 0, linespace = 0;  /* check if any line ends with space          */
     char *cp = NULL, buffer[BUFSIZ];
@@ -1240,7 +1305,7 @@ scan_content (CT ct)
        for (part = m->mp_parts; part; part = part->mp_next) {
            CT p = part->mp_part;
 
-           if (scan_content (p) == NOTOK)      /* choose encoding for subpart */
+           if (scan_content (p, maxunencoded) == NOTOK)        /* choose encoding for subpart */
                return NOTOK;
 
            /* if necessary, enlarge encoding for enclosing multipart */
@@ -1254,58 +1319,81 @@ scan_content (CT ct)
     }
 
     /*
-     * Decide what to check while scanning this content.
+     * Decide what to check while scanning this content.  Note that
+     * for text content we always check for 8bit characters if the
+     * charset is unspecified, because that controls whether or not the
+     * character set is us-ascii or retrieved from the locale.
      */
-    switch (ct->c_type) {
-    case CT_TEXT:
-       check8bit = 1;
+
+    if (ct->c_type == CT_TEXT) {
+       t = (struct text *) ct->c_ctparams;
+       if (t->tx_charset == CHARSET_UNSPECIFIED)
+           check8bit = 1;
+    }
+
+    switch (ct->c_reqencoding) {
+    case CE_8BIT:
+       checkllinelen = 1;
        checkboundary = 1;
-       if (ct->c_subtype == TEXT_PLAIN) {
-           checklinelen = 0;
-           checklinespace = 0;
-       } else {
+       break;
+    case CE_QUOTED:
+       checkboundary = 1;
+       break;
+    case CE_BASE64:
+       break;
+    case CE_UNKNOWN:
+       /* Use the default rules based on content-type */
+       switch (ct->c_type) {
+       case CT_TEXT:
+           checkboundary = 1;
            checklinelen = 1;
-           checklinespace = 1;
-       }
+           if (ct->c_subtype == TEXT_PLAIN) {
+               checklinespace = 0;
+           } else {
+               checklinespace = 1;
+           }
        break;
 
-    case CT_APPLICATION:
-       check8bit = 1;
-       checklinelen = 1;
-       checklinespace = 1;
-       checkboundary = 1;
+       case CT_APPLICATION:
+           check8bit = 1;
+           checklinelen = 1;
+           checklinespace = 1;
+           checkboundary = 1;
        break;
 
-    case CT_MESSAGE:
-       check8bit = 0;
-       checklinelen = 0;
-       checklinespace = 0;
+       case CT_MESSAGE:
+           check8bit = 0;
+           checklinelen = 0;
+           checklinespace = 0;
 
-       /* don't check anything for message/external */
-       if (ct->c_subtype == MESSAGE_EXTERNAL)
-           checkboundary = 0;
-       else
-           checkboundary = 1;
-       break;
+           /* don't check anything for message/external */
+           if (ct->c_subtype == MESSAGE_EXTERNAL)
+               checkboundary = 0;
+           else
+               checkboundary = 1;
+           break;
 
-    case CT_AUDIO:
-    case CT_IMAGE:
-    case CT_VIDEO:
-       /*
-        * Don't check anything for these types,
-        * since we are forcing use of base64.
-        */
-       check8bit = 0;
-       checklinelen = 0;
-       checklinespace = 0;
-       checkboundary = 0;
-       break;
+       case CT_AUDIO:
+       case CT_IMAGE:
+       case CT_VIDEO:
+           /*
+            * Don't check anything for these types,
+            * since we are forcing use of base64, unless
+            * the content-type was specified by a mhbuild directive.
+            */
+           check8bit = 0;
+           checklinelen = 0;
+           checklinespace = 0;
+           checkboundary = 0;
+           break;
+       }
     }
 
     /*
      * Scan the unencoded content
      */
-    if (check8bit || checklinelen || checklinespace || checkboundary) {
+    if (check8bit || checklinelen || checklinespace || checkboundary ||
+       checkllinelen) {
        if ((in = fopen (ce->ce_file, "r")) == NULL)
            adios (ce->ce_file, "unable to open for reading");
        len = strlen (prefix);
@@ -1326,11 +1414,23 @@ scan_content (CT ct)
            /*
             * Check line length.
             */
-           if (checklinelen && (strlen (buffer) > CPERLIN + 1)) {
+           if (checklinelen && (strlen (buffer) > maxunencoded + 1)) {
                linelen = 1;
                checklinelen = 0;       /* no need to keep checking */
            }
 
+           /*
+            * RFC 5322 specifies that a message cannot contain a line
+            * greater than 998 characters (excluding the CRLF).  If we
+            * get one of those lines and linelen is NOT set, then abort.
+            */
+
+           if (checkllinelen && !linelen &&
+                                       (strlen(buffer) > MAXLONGLINE + 1)) {
+               adios(NULL, "Line in content exceeds maximum line limit (%d)",
+                     MAXLONGLINE);
+           }
+
            /*
             * Check if line ends with a space.
             */
@@ -1358,14 +1458,11 @@ scan_content (CT ct)
     }
 
     /*
-     * Decide which transfer encoding to use.
+     * If the content is text and didn't specify a character set,
+     * we need to figure out which one was used.
      */
-    switch (ct->c_type) {
-    case CT_TEXT:
-       /*
-        * If the text content didn't specify a character
-        * set, we need to figure out which one was used.
-        */
+
+    if (ct->c_type == CT_TEXT) {
        t = (struct text *) ct->c_ctparams;
        if (t->tx_charset == CHARSET_UNSPECIFIED) {
            CI ci = &ct->c_ctinfo;
@@ -1386,33 +1483,45 @@ scan_content (CT ct)
            *cp++ = '\0';
            *ep = cp;
        }
+    }
 
-       if (contains8bit || linelen || linespace || checksw)
-           ct->c_encoding = CE_QUOTED;
-       else
-           ct->c_encoding = CE_7BIT;
-       break;
+    /*
+     * Decide which transfer encoding to use.
+     */
 
-    case CT_APPLICATION:
-       /* For application type, use base64, except when postscript */
-       if (contains8bit || linelen || linespace || checksw)
-           ct->c_encoding = (ct->c_subtype == APPLICATION_POSTSCRIPT)
-               ? CE_QUOTED : CE_BASE64;
-       else
-           ct->c_encoding = CE_7BIT;
-       break;
+    if (ct->c_reqencoding != CE_UNKNOWN)
+       ct->c_encoding = ct->c_reqencoding;
+    else
+       switch (ct->c_type) {
+       case CT_TEXT:
+           if (contains8bit && !linelen && !linespace && !checksw)
+               ct->c_encoding = CE_8BIT;
+           else if (contains8bit || linelen || linespace || checksw)
+               ct->c_encoding = CE_QUOTED;
+           else
+               ct->c_encoding = CE_7BIT;
+           break;
 
-    case CT_MESSAGE:
-       ct->c_encoding = CE_7BIT;
-       break;
+       case CT_APPLICATION:
+           /* For application type, use base64, except when postscript */
+           if (contains8bit || linelen || linespace || checksw)
+               ct->c_encoding = (ct->c_subtype == APPLICATION_POSTSCRIPT)
+                   ? CE_QUOTED : CE_BASE64;
+           else
+               ct->c_encoding = CE_7BIT;
+           break;
 
-    case CT_AUDIO:
-    case CT_IMAGE:
-    case CT_VIDEO:
-       /* For audio, image, and video contents, just use base64 */
-       ct->c_encoding = CE_BASE64;
-       break;
-    }
+       case CT_MESSAGE:
+           ct->c_encoding = CE_7BIT;
+           break;
+
+       case CT_AUDIO:
+       case CT_IMAGE:
+       case CT_VIDEO:
+           /* For audio, image, and video contents, just use base64 */
+           ct->c_encoding = CE_BASE64;
+           break;
+        }
 
     return (boundaryclash ? NOTOK : OK);
 }
@@ -1778,9 +1887,9 @@ calculate_digest (CT ct, int asciiP)
  */
 
 static void
-setup_attach_content(CT ct, const char *filename)
+setup_attach_content(CT ct, char *filename)
 {
-    char *type, **ap, **ep;
+    char *type, **ap, **ep, *simplename = r1bindex(filename, '/');
     struct str2init *s2i;
 
     if (! (type = mime_type(filename))) {
@@ -1842,17 +1951,19 @@ setup_attach_content(CT ct, const char *filename)
        if (strcasecmp(*ap, "name") == 0) {
            if (*ep)
                free(*ep);
-           *ep = getcpy(filename);
+           *ep = getcpy(simplename);
            break;
        }
     }
 
     if (*ap == NULL) {
        *ap = getcpy("name");
-       *ep = getcpy(filename);
+       *ep = getcpy(simplename);
     }
 
-    ct->c_descr = getcpy(filename);
+    ct->c_descr = getcpy(simplename);
+    ct->c_descr = add("\n", ct->c_descr);
+    ct->c_cefile.ce_file = getcpy(filename);
 
     /*
      * If it's a text/calendar, we need to make sure it's an inline,
@@ -1867,6 +1978,6 @@ setup_attach_content(CT ct, const char *filename)
        ct->c_dispo = getcpy("attachment; filename=\"");
     }
 
-    ct->c_dispo = add(filename, ct->c_dispo);
-    ct->c_dispo = add("\"", ct->c_dispo);
+    ct->c_dispo = add(simplename, ct->c_dispo);
+    ct->c_dispo = add("\"\n", ct->c_dispo);
 }