]> diplodocus.org Git - nmh/blobdiff - uip/mhbuildsbr.c
Disable assertions by default.
[nmh] / uip / mhbuildsbr.c
index 653c5b3f380d926a6a07a313cf9789da91a29c95..e449917092e185d5934cf3579ddaa81373998ec4 100644 (file)
@@ -19,8 +19,6 @@
 #include <fcntl.h>
 #include <h/signals.h>
 #include <h/md5.h>
-#include <errno.h>
-#include <signal.h>
 #include <h/mts.h>
 #include <h/tws.h>
 #include <h/mime.h>
@@ -54,6 +52,12 @@ pid_t xpid = 0;
 
 static char prefix[] = "----- =_aaaaaaaaaa";
 
+/*
+ * Maximum size of URL token in message/external-body
+ */
+
+#define MAXURLTOKEN 40
+
 
 /* mhmisc.c */
 void content_error (char *, CT, char *, ...);
@@ -62,15 +66,9 @@ void content_error (char *, CT, char *, ...);
 int find_cache (CT, int, int *, char *, char *, int);
 
 /* mhfree.c */
-void free_content (CT);
 void free_ctinfo (CT);
 void free_encoding (CT, int);
 
-/*
- * prototypes
- */
-CT build_mime (char *, int);
-
 /*
  * static prototypes
  */
@@ -125,7 +123,7 @@ static void directive_pop(void)
  */
 
 CT
-build_mime (char *infile, int directives)
+build_mime (char *infile, int directives, int header_encoding)
 {
     int        compnum, state;
     char buf[BUFSIZ], name[NAMESZ];
@@ -134,6 +132,8 @@ build_mime (char *infile, int directives)
     struct part **pp;
     CT ct;
     FILE *in;
+    HF hp;
+    m_getfld_state_t gstate = 0;
 
     directive_init(directives);
 
@@ -161,25 +161,28 @@ build_mime (char *infile, int directives)
      * draft into the linked list of header fields for
      * the new MIME message.
      */
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
+    m_getfld_track_filepos (&gstate, in);
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            compnum++;
 
            /* abort if draft has Mime-Version header field */
-           if (!mh_strcasecmp (name, VRSN_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 (!mh_strcasecmp (name, ENCODING_FIELD))
+           if (!strcasecmp (name, ENCODING_FIELD))
                adios (NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
 
            /* ignore any Content-Type fields in the header */
-           if (!mh_strcasecmp (name, TYPE_FIELD)) {
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, buf, sizeof(buf), in);
+           if (!strcasecmp (name, TYPE_FIELD)) {
+               while (state == FLDPLUS) {
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, in);
+               }
                goto finish_field;
            }
 
@@ -189,7 +192,8 @@ build_mime (char *infile, int directives)
 
            /* if necessary, get rest of field */
            while (state == FLDPLUS) {
-               state = m_getfld (state, name, buf, sizeof(buf), in);
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, in);
                vp = add (buf, vp);     /* add to previous value */
            }
 
@@ -198,16 +202,13 @@ build_mime (char *infile, int directives)
 
 finish_field:
            /* if this wasn't the last header field, then continue */
-           if (state != FLDEOF)
-               continue;
-           /* else fall... */
+           continue;
 
        case FILEEOF:
            adios (NULL, "draft has empty body -- no directives!");
            /* NOTREACHED */
 
        case BODY:
-       case BODYEOF:
            fseek (in, (long) (-strlen (buf)), SEEK_CUR);
            break;
 
@@ -220,6 +221,18 @@ finish_field:
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
+
+    /*
+     * Iterate through the list of headers and call the function to MIME-ify
+     * them if required.
+     */
+
+    for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) {
+       if (encode_rfc2047(hp->name, &hp->value, header_encoding, NULL)) {
+           adios(NULL, "Unable to encode header \"%s\"", hp->name);
+       }
+    }
 
     /*
      * Now add the MIME-Version header field
@@ -339,12 +352,6 @@ finish_field:
 static int
 init_decoded_content (CT ct)
 {
-    CE ce;
-
-    if ((ce = (CE) calloc (1, sizeof(*ce))) == NULL)
-       adios (NULL, "out of memory");
-
-    ct->c_cefile     = ce;
     ct->c_ceopenfnx  = open7Bit;       /* since unencoded */
     ct->c_ceclosefnx = close_encoding;
     ct->c_cesizefnx  = NULL;           /* since unencoded */
@@ -400,8 +407,7 @@ static int
 user_content (FILE *in, char *file, char *buf, CT *ctp)
 {
     int        extrnal, vrsn;
-    unsigned char *cp;
-    char **ap;
+    char *cp, **ap;
     char buffer[BUFSIZ];
     struct multipart *m;
     struct part **pp;
@@ -423,7 +429,7 @@ user_content (FILE *in, char *file, char *buf, CT *ctp)
 
     /* allocate basic structure for handling decoded content */
     init_decoded_content (ct);
-    ce = ct->c_cefile;
+    ce = &ct->c_cefile;
 
     ci = &ct->c_ctinfo;
     set_id (ct, 0);
@@ -540,7 +546,7 @@ rock_and_roll:
            done (1);
 
        for (s2i = str2cts; s2i->si_key; s2i++)
-           if (!mh_strcasecmp (ci->ci_type, s2i->si_key))
+           if (!strcasecmp (ci->ci_type, s2i->si_key))
                break;
        if (!s2i->si_key && !uprf (ci->ci_type, "X-"))
            s2i++;
@@ -550,7 +556,7 @@ rock_and_roll:
         */
        switch (ct->c_type = s2i->si_val) {
        case CT_MESSAGE:
-           if (!mh_strcasecmp (ci->ci_subtype, "rfc822")) {
+           if (!strcasecmp (ci->ci_subtype, "rfc822")) {
                ct->c_encoding = CE_7BIT;
                goto call_init;
            }
@@ -586,7 +592,7 @@ call_init:
 
     /* check directive against the list of MIME types */
     for (s2i = str2cts; s2i->si_key; s2i++)
-       if (!mh_strcasecmp (ci->ci_type, s2i->si_key))
+       if (!strcasecmp (ci->ci_type, s2i->si_key))
            break;
 
     /*
@@ -607,10 +613,10 @@ call_init:
            /* NOTREACHED */
 
        case CT_MESSAGE:
-           if (!mh_strcasecmp (ci->ci_subtype, "partial"))
+           if (!strcasecmp (ci->ci_subtype, "partial"))
                adios (NULL, "sorry, \"#%s/%s\" isn't supported",
                       ci->ci_type, ci->ci_subtype);
-           if (!mh_strcasecmp (ci->ci_subtype, "external-body"))
+           if (!strcasecmp (ci->ci_subtype, "external-body"))
                adios (NULL, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
                       ci->ci_type, ci->ci_subtype);
 use_forw:
@@ -674,7 +680,7 @@ use_forw:
        if (ci->ci_magic) {
            /* check if specifies command to execute */
            if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
-               for (cp = ci->ci_magic + 1; isspace (*cp); cp++)
+               for (cp = ci->ci_magic + 1; isspace ((unsigned char) *cp); cp++)
                    continue;
                if (!*cp)
                    adios (NULL, "empty pipe command for #%s directive", ci->ci_type);
@@ -717,7 +723,7 @@ use_forw:
      * Message directive
      * #forw [+folder] [msgs]
      */
-    if (!mh_strcasecmp (ci->ci_type, "forw")) {
+    if (!strcasecmp (ci->ci_type, "forw")) {
        int msgnum;
        char *folder, *arguments[MAXARGS];
        struct msgs *mp;
@@ -746,7 +752,7 @@ use_forw:
        if (!folder)
            folder = add (getfolder (1), NULL);
 
-       if (!(mp = folder_read (folder)))
+       if (!(mp = folder_read (folder, 0)))
            adios (NULL, "unable to read folder %s", folder);
        for (ap = arguments; *ap; ap++) {
            cp = *ap;
@@ -784,7 +790,7 @@ use_forw:
                    if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
                        adios (NULL, "out of memory");
                    init_decoded_content (p);
-                   pe = p->c_cefile;
+                   pe = &p->c_cefile;
                    if (get_ctinfo ("message/rfc822", p, 0) == NOTOK)
                        done (1);
                    p->c_type = CT_MESSAGE;
@@ -823,7 +829,7 @@ use_forw:
     /*
      * #end
      */
-    if (!mh_strcasecmp (ci->ci_type, "end")) {
+    if (!strcasecmp (ci->ci_type, "end")) {
        free_content (ct);
        *ctp = NULL;
        return DONE;
@@ -832,14 +838,14 @@ use_forw:
     /*
      * #begin [ alternative | parallel ]
      */
-    if (!mh_strcasecmp (ci->ci_type, "begin")) {
+    if (!strcasecmp (ci->ci_type, "begin")) {
        if (!ci->ci_magic) {
            vrsn = MULTI_MIXED;
            cp = SubMultiPart[vrsn - 1].kv_key;
-       } else if (!mh_strcasecmp (ci->ci_magic, "alternative")) {
+       } else if (!strcasecmp (ci->ci_magic, "alternative")) {
            vrsn = MULTI_ALTERNATE;
            cp = SubMultiPart[vrsn - 1].kv_key;
-       } else if (!mh_strcasecmp (ci->ci_magic, "parallel")) {
+       } else if (!strcasecmp (ci->ci_magic, "parallel")) {
            vrsn = MULTI_PARALLEL;
            cp = SubMultiPart[vrsn - 1].kv_key;
        } else if (uprf (ci->ci_magic, "digest")) {
@@ -919,7 +925,7 @@ set_id (CT ct, int top)
 static int
 compose_content (CT ct)
 {
-    CE ce = ct->c_cefile;
+    CE ce = &ct->c_cefile;
 
     switch (ct->c_type) {
     case CT_MULTIPART:
@@ -1093,7 +1099,7 @@ raw:
            if ((out = fopen (ce->ce_file, "w")) == NULL)
                adios (ce->ce_file, "unable to open for writing");
 
-           for (i = 0; (child_id = vfork()) == NOTOK && i > 5; i++)
+           for (i = 0; (child_id = fork()) == NOTOK && i > 5; i++)
                sleep (5);
            switch (child_id) {
            case NOTOK:
@@ -1153,10 +1159,10 @@ scan_content (CT ct)
     int checklinelen = 0, linelen = 0;   /* check for 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          */
-    unsigned char *cp = NULL, buffer[BUFSIZ];
+    char *cp = NULL, buffer[BUFSIZ];
     struct text *t = NULL;
     FILE *in = NULL;
-    CE ce = ct->c_cefile;
+    CE ce = &ct->c_cefile;
 
     /*
      * handle multipart by scanning all subparts
@@ -1248,7 +1254,7 @@ scan_content (CT ct)
             */
            if (check8bit) {
                for (cp = buffer; *cp; cp++) {
-                   if (!isascii (*cp)) {
+                   if (!isascii ((unsigned char) *cp)) {
                        contains8bit = 1;
                        check8bit = 0;  /* no need to keep checking */
                    }
@@ -1266,7 +1272,7 @@ scan_content (CT ct)
            /*
             * Check if line ends with a space.
             */
-           if (checklinespace && (cp = buffer + strlen (buffer) - 2) > buffer && isspace (*cp)) {
+           if (checklinespace && (cp = buffer + strlen (buffer) - 2) > buffer && isspace ((unsigned char) *cp)) {
                linespace = 1;
                checklinespace = 0;     /* no need to keep checking */
            }
@@ -1277,10 +1283,10 @@ scan_content (CT ct)
             */
            if (checkboundary && buffer[0] == '-' && buffer[1] == '-') {
                for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
-                   if (!isspace (*cp))
+                   if (!isspace ((unsigned char) *cp))
                        break;
                *++cp = '\0';
-               if (!strncmp(buffer + 2, prefix, len) && isdigit(buffer[2 + len])) {
+               if (!strncmp(buffer + 2, prefix, len) && isdigit((unsigned char) buffer[2 + len])) {
                    boundaryclash = 1;
                    checkboundary = 0;  /* no need to keep checking */
                }
@@ -1359,7 +1365,7 @@ scan_content (CT ct)
 static int
 build_headers (CT ct)
 {
-    int        cc, mailbody, len;
+    int        cc, mailbody, extbody, len;
     char **ap, **ep;
     char *np, *vp, buffer[BUFSIZ];
     CI ci = &ct->c_ctinfo;
@@ -1401,21 +1407,56 @@ build_headers (CT ct)
     len = strlen (TYPE_FIELD) + strlen (ci->ci_type)
                + strlen (ci->ci_subtype) + 3;
 
-    mailbody = ct->c_type == CT_MESSAGE
-       && ct->c_subtype == MESSAGE_EXTERNAL
-       && ((struct exbody *) ct->c_ctparams)->eb_body;
+    extbody = ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_EXTERNAL;
+    mailbody = extbody && ((struct exbody *) ct->c_ctparams)->eb_body;
 
     /*
      * Append the attribute/value pairs to
      * the end of the Content-Type line.
      */
     for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
-       if (mailbody && !mh_strcasecmp (*ap, "body"))
+       if (mailbody && !strcasecmp (*ap, "body"))
            continue;
 
        vp = add (";", vp);
        len++;
 
+       /*
+        * According to RFC 2017, if we have a URL longer than 40 characters
+        * we have to break it across multiple lines
+        */
+
+       if (extbody && strcasecmp (*ap, "url") == 0) {
+           char *value = *ep;
+
+           /* 7 here refers to " url=\"\"" */
+           if (len + 1 + (cc = (min(MAXURLTOKEN, strlen(value)) + 7)) >=
+                                                               CPERLIN) {
+               vp = add ("\n\t", vp);
+               len = 8;
+           } else {
+               vp = add (" ", vp);
+               len++;
+           }
+
+           vp = add ("url=\"", vp);
+           len += 5;
+
+           while (strlen(value) > MAXURLTOKEN) {
+               strncpy(buffer, value, MAXURLTOKEN);
+               buffer[MAXURLTOKEN] = '\0';
+               vp = add (buffer, vp);
+               vp = add ("\n\t", vp);
+               value += MAXURLTOKEN;
+               len = 8;
+           }
+
+           vp = add (value, vp);
+           vp = add ("\"", vp);
+           len += strlen(value) + 1;
+           continue;
+       }
+
        snprintf (buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
        if (len + 1 + (cc = strlen (buffer)) >= CPERLIN) {
            vp = add ("\n\t", vp);
@@ -1590,7 +1631,7 @@ calculate_digest (CT ct, int asciiP)
     unsigned char digest[16];
     unsigned char outbuf[25];
     MD5_CTX mdContext;
-    CE ce = ct->c_cefile;
+    CE ce = &ct->c_cefile;
     char *infilename = ce->ce_file ? ce->ce_file : ct->c_file;
     FILE *in;
 
@@ -1639,7 +1680,8 @@ calculate_digest (CT ct, int asciiP)
     }
 
     /* encode the digest using base64 */
-    for (dp = digest, op = outbuf, cc = sizeof(digest) / sizeof(digest[0]);
+    for (dp = digest, op = (char *) outbuf,
+                               cc = sizeof(digest) / sizeof(digest[0]);
                cc > 0; cc -= 3, op += 4) {
        unsigned long bits;
        char *bp;