Hardcoded mhmail instead of mailproc.
[mmh] / uip / mhparse.c
index 3aeb351..ece4e8f 100644 (file)
@@ -13,7 +13,6 @@
 #include <errno.h>
 #include <setjmp.h>
 #include <signal.h>
-#include <h/mts.h>
 #include <h/tws.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
@@ -89,9 +88,6 @@ struct k2v SubApplication[] = {
 };
 
 
-/* ftpsbr.c */
-int ftp_get(char *, char *, char *, char *, char *, char *, int, int);
-
 /* mhcachesbr.c */
 int find_cache(CT, int, int *, char *, char *, int);
 
@@ -199,7 +195,7 @@ parse_mime(char *file)
        /*
        ** Check if file is actually standard input
        */
-       if ((is_stdin = !(strcmp(file, "-")))) {
+       if ((is_stdin = (strcmp(file, "-")==0))) {
                char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
                if (tfile == NULL) {
                        advise("mhparse", "unable to create temporary file");
@@ -816,24 +812,24 @@ magic_skip:
                if (*dp == '"') {
                        for (cp = ++dp, dp = vp;;) {
                                switch (c = *cp++) {
-                                       case '\0':
+                               case '\0':
 bad_quote:
-                                               advise(NULL, "invalid quoted-string in message %s's %s: field\n%*.*s(parameter %s)", ct->c_file, TYPE_FIELD, i, i, "", *ap);
-                                               return NOTOK;
+                                       advise(NULL, "invalid quoted-string in message %s's %s: field\n%*.*s(parameter %s)", ct->c_file, TYPE_FIELD, i, i, "", *ap);
+                                       return NOTOK;
 
-                                       case '\\':
-                                               *dp++ = c;
-                                               if ((c = *cp++) == '\0')
-                                                       goto bad_quote;
-                                               /* else fall... */
+                               case '\\':
+                                       *dp++ = c;
+                                       if ((c = *cp++) == '\0')
+                                               goto bad_quote;
+                                       /* else fall... */
 
-                                       default:
-                                               *dp++ = c;
-                                               continue;
+                               default:
+                                       *dp++ = c;
+                                       continue;
 
-                                       case '"':
-                                               *dp = '\0';
-                                               break;
+                               case '"':
+                                       *dp = '\0';
+                                       break;
                                }
                                break;
                        }
@@ -1197,7 +1193,7 @@ InitMultiPart(CT ct)
                if (buffer[0] != '-' || buffer[1] != '-')
                        continue;
                if (inout) {
-                       if (strcmp(buffer + 2, m->mp_start))
+                       if (strcmp(buffer + 2, m->mp_start)!=0)
                                continue;
 next_part:
                        if ((part = (struct part *) calloc(1, sizeof(*part)))
@@ -1365,137 +1361,133 @@ InitMessage(CT ct)
        ct->c_subtype = kv->kv_value;
 
        switch (ct->c_subtype) {
-               case MESSAGE_RFC822:
-                       break;
+       case MESSAGE_RFC822:
+               break;
 
-               case MESSAGE_PARTIAL:
-                       {
-                               char **ap, **ep;
-                               struct partial *p;
+       case MESSAGE_PARTIAL:
+               {
+               char **ap, **ep;
+               struct partial *p;
 
-                               if ((p = (struct partial *)
-                                               calloc(1, sizeof(*p))) == NULL)
-                                       adios(NULL, "out of memory");
-                               ct->c_ctparams = (void *) p;
+               if ((p = (struct partial *) calloc(1, sizeof(*p))) == NULL)
+                       adios(NULL, "out of memory");
+               ct->c_ctparams = (void *) p;
 
-                               /*
-                               ** scan for parameters "id", "number",
-                               ** and "total"
-                               */
-                               for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
-                                       if (!mh_strcasecmp(*ap, "id")) {
-                                               p->pm_partid = getcpy(*ep);
-                                               continue;
-                                       }
-                                       if (!mh_strcasecmp(*ap, "number")) {
-                                               if (sscanf(*ep, "%d", &p->pm_partno) != 1 || p->pm_partno < 1) {
+               /*
+               ** scan for parameters "id", "number",
+               ** and "total"
+               */
+               for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
+                       if (!mh_strcasecmp(*ap, "id")) {
+                               p->pm_partid = getcpy(*ep);
+                               continue;
+                       }
+                       if (!mh_strcasecmp(*ap, "number")) {
+                               if (sscanf(*ep, "%d", &p->pm_partno) != 1 || p->pm_partno < 1) {
 invalid_param:
-                                                       advise(NULL, "invalid %s parameter for \"%s/%s\" type in message %s's %s field", *ap, ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
-                                                       return NOTOK;
-                                               }
-                                               continue;
-                                       }
-                                       if (!mh_strcasecmp(*ap, "total")) {
-                                               if (sscanf(*ep, "%d", &p->pm_maxno) != 1 || p->pm_maxno < 1)
-                                                       goto invalid_param;
-                                               continue;
-                                       }
-                               }
-
-                               if (!p->pm_partid || !p->pm_partno
-                                       || (p->pm_maxno && p->pm_partno > p->pm_maxno)) {
-                                       advise(NULL, "invalid parameters for \"%s/%s\" type in message %s's %s field", ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
+                                       advise(NULL, "invalid %s parameter for \"%s/%s\" type in message %s's %s field", *ap, ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
                                        return NOTOK;
                                }
+                               continue;
                        }
-                       break;
-
-               case MESSAGE_EXTERNAL:
-                       {
-                               int exresult;
-                               struct exbody *e;
-                               CT p;
-                               FILE *fp;
+                       if (!mh_strcasecmp(*ap, "total")) {
+                               if (sscanf(*ep, "%d", &p->pm_maxno) != 1 ||
+                                               p->pm_maxno < 1)
+                                       goto invalid_param;
+                               continue;
+                       }
+               }
 
-                               if ((e = (struct exbody *)
-                                               calloc(1, sizeof(*e))) == NULL)
-                                       adios(NULL, "out of memory");
-                               ct->c_ctparams = (void *) e;
+               if (!p->pm_partid || !p->pm_partno
+                       || (p->pm_maxno && p->pm_partno > p->pm_maxno)) {
+                       advise(NULL, "invalid parameters for \"%s/%s\" type in message %s's %s field", ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
+                       return NOTOK;
+               }
+               }
+               break;
 
-                               if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
-                                       advise(ct->c_file, "unable to open for reading");
-                                       return NOTOK;
-                               }
+       case MESSAGE_EXTERNAL:
+               {
+               int exresult;
+               struct exbody *e;
+               CT p;
+               FILE *fp;
 
-                               fseek(fp = ct->c_fp, ct->c_begin, SEEK_SET);
+               if ((e = (struct exbody *) calloc(1, sizeof(*e))) == NULL)
+                       adios(NULL, "out of memory");
+               ct->c_ctparams = (void *) e;
 
-                               if (!(p = get_content(fp, ct->c_file, 0))) {
-                                       ct->c_fp = NULL;
-                                       return NOTOK;
-                               }
+               if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
+                       advise(ct->c_file, "unable to open for reading");
+                       return NOTOK;
+               }
 
-                               e->eb_parent = ct;
-                               e->eb_content = p;
-                               p->c_ctexbody = e;
-                               if ((exresult = params_external(ct, 0))
-                                               != NOTOK &&
-                                               p->c_ceopenfnx == openMail) {
-                                       int cc, size;
-                                       char *bp;
-
-                                       if ((size = ct->c_end - p->c_begin) <= 0) {
-                                               if (!e->eb_subject)
-                                                       content_error(NULL, ct, "empty body for access-type=mail-server");
-                                               goto no_body;
-                                       }
+               fseek(fp = ct->c_fp, ct->c_begin, SEEK_SET);
 
-                                       e->eb_body = bp = mh_xmalloc((unsigned) size);
-                                       fseek(p->c_fp, p->c_begin, SEEK_SET);
-                                       while (size > 0)
-                                               switch (cc = fread(bp, sizeof(*bp), size, p->c_fp)) {
-                                                       case NOTOK:
-                                                               adios("failed", "fread");
+               if (!(p = get_content(fp, ct->c_file, 0))) {
+                       ct->c_fp = NULL;
+                       return NOTOK;
+               }
 
-                                                       case OK:
-                                                               adios(NULL, "unexpected EOF from fread");
+               e->eb_parent = ct;
+               e->eb_content = p;
+               p->c_ctexbody = e;
+               if ((exresult = params_external(ct, 0)) != NOTOK &&
+                               p->c_ceopenfnx == openMail) {
+                       int cc, size;
+                       char *bp;
+
+                       if ((size = ct->c_end - p->c_begin) <= 0) {
+                               if (!e->eb_subject)
+                                       content_error(NULL, ct, "empty body for access-type=mail-server");
+                               goto no_body;
+                       }
 
-                                                       default:
-                                                               bp += cc, size -= cc;
-                                                               break;
-                                               }
-                                       *bp = 0;
+                       e->eb_body = bp = mh_xmalloc((unsigned) size);
+                       fseek(p->c_fp, p->c_begin, SEEK_SET);
+                       while (size > 0)
+                               switch (cc = fread(bp, sizeof(*bp), size, p->c_fp)) {
+                               case NOTOK:
+                                       adios("failed", "fread");
+                               case OK:
+                                       adios(NULL, "unexpected EOF from fread");
+                               default:
+                                       bp += cc, size -= cc;
+                                       break;
                                }
+                       *bp = 0;
+               }
 no_body:
-                               p->c_fp = NULL;
-                               p->c_end = p->c_begin;
+               p->c_fp = NULL;
+               p->c_end = p->c_begin;
 
-                               fclose(ct->c_fp);
-                               ct->c_fp = NULL;
+               fclose(ct->c_fp);
+               ct->c_fp = NULL;
 
-                               if (exresult == NOTOK)
-                                       return NOTOK;
-                               if (e->eb_flags == NOTOK)
-                                       return OK;
-
-                               switch (p->c_type) {
-                                       case CT_MULTIPART:
-                                               break;
+               if (exresult == NOTOK)
+                       return NOTOK;
+               if (e->eb_flags == NOTOK)
+                       return OK;
 
-                                       case CT_MESSAGE:
-                                               if (p->c_subtype != MESSAGE_RFC822)
-                                                       break;
-                                               /* else fall... */
-                                       default:
-                                               e->eb_partno = ct->c_partno;
-                                               if (p->c_ctinitfnx)
-                                                       (*p->c_ctinitfnx) (p);
-                                               break;
-                               }
-                       }
+               switch (p->c_type) {
+               case CT_MULTIPART:
                        break;
 
+               case CT_MESSAGE:
+                       if (p->c_subtype != MESSAGE_RFC822)
+                               break;
+                               /* else fall... */
                default:
+                       e->eb_partno = ct->c_partno;
+                       if (p->c_ctinitfnx)
+                               (*p->c_ctinitfnx) (p);
                        break;
+               }
+               }
+               break;
+
+       default:
+               break;
        }
 
        return OK;
@@ -2399,14 +2391,12 @@ openFile(CT ct, char **file)
        CE ce = ct->c_cefile;
 
        switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
-               case NOTOK:
-                       return NOTOK;
-
-               case OK:
-                       break;
-
-               case DONE:
-                       return fd;
+       case NOTOK:
+               return NOTOK;
+       case OK:
+               break;
+       case DONE:
+               return fd;
        }
 
        if (!e->eb_name) {
@@ -2482,6 +2472,8 @@ openFTP(CT ct, char **file)
        CE ce;
        static char *username = NULL;
        static char *password = NULL;
+       int child_id, vecp;
+       char *vec[9];
 
        e  = ct->c_ctexbody;
        ce = ct->c_cefile;
@@ -2489,20 +2481,16 @@ openFTP(CT ct, char **file)
        if ((ftp = context_find(nmhaccessftp)) && !*ftp)
                ftp = NULL;
 
-#ifndef BUILTIN_FTP
        if (!ftp)
                return NOTOK;
-#endif
 
        switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
-               case NOTOK:
-                       return NOTOK;
-
-               case OK:
-                       break;
-
-               case DONE:
-                       return fd;
+       case NOTOK:
+               return NOTOK;
+       case OK:
+               break;
+       case DONE:
+               return fd;
        }
 
        if (!e->eb_name || !e->eb_site) {
@@ -2595,61 +2583,42 @@ openFTP(CT ct, char **file)
                return NOTOK;
        }
 
-#ifdef BUILTIN_FTP
-       if (ftp)
-#endif
-       {
-               int child_id, i, vecp;
-               char *vec[9];
-
-               vecp = 0;
-               vec[vecp++] = mhbasename(ftp);
-               vec[vecp++] = e->eb_site;
-               vec[vecp++] = user;
-               vec[vecp++] = pass;
-               vec[vecp++] = e->eb_dir;
-               vec[vecp++] = e->eb_name;
-               vec[vecp++] = ce->ce_file,
-               vec[vecp++] = e->eb_mode &&
-                               !mh_strcasecmp(e->eb_mode, "ascii") ?
-                               "ascii" : "binary";
-               vec[vecp] = NULL;
-
-               fflush(stdout);
-
-               for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
-                       sleep(5);
-               switch (child_id) {
-                       case NOTOK:
-                               adios("fork", "unable to");
-                               /* NOTREACHED */
-
-                       case OK:
-                               close(fileno(ce->ce_fp));
-                               execvp(ftp, vec);
-                               fprintf(stderr, "unable to exec ");
-                               perror(ftp);
-                               _exit(-1);
-                               /* NOTREACHED */
-
-                       default:
-                               if (pidXwait(child_id, NULL)) {
-#ifdef BUILTIN_FTP
-losing_ftp:
-#endif
-                                       username = password = NULL;
-                                       ce->ce_unlink = 1;
-                                       return NOTOK;
-                               }
-                               break;
+       vecp = 0;
+       vec[vecp++] = mhbasename(ftp);
+       vec[vecp++] = e->eb_site;
+       vec[vecp++] = user;
+       vec[vecp++] = pass;
+       vec[vecp++] = e->eb_dir;
+       vec[vecp++] = e->eb_name;
+       vec[vecp++] = ce->ce_file,
+       vec[vecp++] = e->eb_mode &&
+                       !mh_strcasecmp(e->eb_mode, "ascii") ?
+                       "ascii" : "binary";
+       vec[vecp] = NULL;
+
+       fflush(stdout);
+
+       switch (child_id = fork()) {
+       case NOTOK:
+               adios("fork", "unable to");
+               /* NOTREACHED */
+
+       case OK:
+               close(fileno(ce->ce_fp));
+               execvp(ftp, vec);
+               fprintf(stderr, "unable to exec ");
+               perror(ftp);
+               _exit(-1);
+               /* NOTREACHED */
+
+       default:
+               if (pidXwait(child_id, NULL)) {
+                       username = password = NULL;
+                       ce->ce_unlink = 1;
+                       return NOTOK;
                }
+               break;
        }
-#ifdef BUILTIN_FTP
-       else if (ftp_get(e->eb_site, user, pass, e->eb_dir, e->eb_name,
-                       ce->ce_file, e->eb_mode && !mh_strcasecmp(e->eb_mode,
-                       "ascii"), 0) == NOTOK)
-               goto losing_ftp;
-#endif
 
        if (cachefile[0]) {
                if (caching)
@@ -2703,21 +2672,19 @@ InitMail(CT ct)
 static int
 openMail(CT ct, char **file)
 {
-       int child_id, fd, i, vecp;
+       int child_id, fd, vecp;
        int len, buflen;
        char *bp, buffer[BUFSIZ], *vec[7];
        struct exbody *e = ct->c_ctexbody;
        CE ce = ct->c_cefile;
 
        switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
-               case NOTOK:
-                       return NOTOK;
-
-               case OK:
-                       break;
-
-               case DONE:
-                       return fd;
+       case NOTOK:
+               return NOTOK;
+       case OK:
+               break;
+       case DONE:
+               return fd;
        }
 
        if (!e->eb_server) {
@@ -2757,7 +2724,7 @@ openMail(CT ct, char **file)
                return NOTOK;
 
        vecp = 0;
-       vec[vecp++] = mhbasename(mailproc);
+       vec[vecp++] = "mhmail";
        vec[vecp++] = e->eb_server;
        vec[vecp++] = "-subject";
        vec[vecp++] = e->eb_subject ? e->eb_subject : "mail-server request";
@@ -2765,24 +2732,22 @@ openMail(CT ct, char **file)
        vec[vecp++] = e->eb_body;
        vec[vecp] = NULL;
 
-       for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
-               sleep(5);
-       switch (child_id) {
-               case NOTOK:
-                       advise("fork", "unable to");
-                       return NOTOK;
+       switch (child_id = fork()) {
+       case NOTOK:
+               advise("fork", "unable to");
+               return NOTOK;
 
-               case OK:
-                       execvp(mailproc, vec);
-                       fprintf(stderr, "unable to exec ");
-                       perror(mailproc);
-                       _exit(-1);
-                       /* NOTREACHED */
+       case OK:
+               execvp(*vec, vec);
+               fprintf(stderr, "unable to exec ");
+               perror(*vec);
+               _exit(-1);
+               /* NOTREACHED */
 
-               default:
-                       if (pidXwait(child_id, NULL) == OK)
-                               advise(NULL, "request sent");
-                       break;
+       default:
+               if (pidXwait(child_id, NULL) == OK)
+                       advise(NULL, "request sent");
+               break;
        }
 
        if (*file == NULL) {
@@ -2833,36 +2798,36 @@ readDigest(CT ct, char *cp)
        for (ep = (dp = ct->c_digest)
                + sizeof(ct->c_digest) / sizeof(ct->c_digest[0]); *cp; cp++)
                switch (*cp) {
-                       default:
-                               if (skip || (*cp & 0x80) ||
-                                               (value = b642nib[*cp & 0x7f])
-                                               > 0x3f) {
-                                       if (debugsw)
-                                               fprintf(stderr, "invalid BASE64 encoding\n");
-                                       return NOTOK;
-                               }
+               default:
+                       if (skip || (*cp & 0x80) ||
+                                       (value = b642nib[*cp & 0x7f])
+                                       > 0x3f) {
+                               if (debugsw)
+                                       fprintf(stderr, "invalid BASE64 encoding\n");
+                               return NOTOK;
+                       }
 
-                               bits |= value << bitno;
+                       bits |= value << bitno;
 test_end:
-                               if ((bitno -= 6) < 0) {
-                                       if (dp + (3 - skip) > ep)
-                                               goto invalid_digest;
-                                       *dp++ = *b1;
-                                       if (skip < 2) {
-                                               *dp++ = *b2;
-                                               if (skip < 1)
-                                                       *dp++ = *b3;
-                                       }
-                                       bitno = 18;
-                                       bits = 0L;
-                                       skip = 0;
+                       if ((bitno -= 6) < 0) {
+                               if (dp + (3 - skip) > ep)
+                                       goto invalid_digest;
+                               *dp++ = *b1;
+                               if (skip < 2) {
+                                       *dp++ = *b2;
+                                       if (skip < 1)
+                                               *dp++ = *b3;
                                }
-                               break;
+                               bitno = 18;
+                               bits = 0L;
+                               skip = 0;
+                       }
+                       break;
 
-                       case '=':
-                               if (++skip > 3)
-                                       goto self_delimiting;
-                               goto test_end;
+               case '=':
+                       if (++skip > 3)
+                               goto self_delimiting;
+                       goto test_end;
                }
        if (bitno != 18) {
                if (debugsw)