]> diplodocus.org Git - nmh/blobdiff - uip/mhbuildsbr.c
Expanded mhpath(1) man page discussion of its handling of out-of-range
[nmh] / uip / mhbuildsbr.c
index 58540f1b35e261341d69c3eb5641553f600ffa3e..df9bf5b75dd07ff6dc04d868837b469b3beb68fa 100644 (file)
@@ -78,7 +78,7 @@ 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 void set_id (CT, int);
@@ -129,7 +129,7 @@ static void directive_pop(void)
  */
 
 CT
-build_mime (char *infile, int directives, int header_encoding)
+build_mime (char *infile, int autobuild, int directives, int header_encoding)
 {
     int        compnum, state;
     char buf[BUFSIZ], name[NAMESZ];
@@ -176,13 +176,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 +214,30 @@ 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);
+               free(vp);
+               if (attach_tail) {
                    attach_tail->next = entry;
                    attach_tail = entry;
                } else {
@@ -228,12 +251,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:
@@ -320,7 +341,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);
        }
 
@@ -1778,9 +1799,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 +1863,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 +1890,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);
 }