]> diplodocus.org Git - nmh/blobdiff - uip/whatnowsbr.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / whatnowsbr.c
index 005095a8a9818e409d3f99663e209c3928e2d0a9..69cbf795d8bebab4c26db28b480f83baeaa25990 100644 (file)
 #include <h/mime.h>
 #include <h/utils.h>
 
+#ifdef OAUTH_SUPPORT
+# include <h/oauth.h>
+#endif
+
 #define WHATNOW_SWITCHES \
     X("draftfolder +folder", 0, DFOLDSW) \
     X("draftmessage msg", 0, DMSGSW) \
@@ -106,6 +110,7 @@ static void sendit (char *, char **, char *, int);
 static int buildfile (char **, char *);
 static int whomfile (char **, char *);
 static int removefile (char *);
+static int checkmimeheader (char *);
 static void writelscmd(char *, int, char *, char **);
 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
@@ -129,11 +134,9 @@ WhatNow (int argc, char **argv)
     char       *l;                     /* set on -l to alist  command */
     int                n;                      /* set on -n to alist command */
 
+    /* Need this if called from what_now(). */
     invo_name = r1bindex (argv[0], '/');
 
-    /* read user profile/context */
-    context_read();
-
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
@@ -141,7 +144,7 @@ WhatNow (int argc, char **argv)
      * Get the initial current working directory.
      */
 
-    if (getcwd(cwd, sizeof (cwd)) == (char *)0) {
+    if (getcwd(cwd, sizeof (cwd)) == NULL) {
        adios("getcwd", "could not get working directory");
     }
 
@@ -213,7 +216,7 @@ WhatNow (int argc, char **argv)
     if ((drft == NULL && (drft = getenv ("mhdraft")) == NULL) || *drft == 0)
        drft = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
 
-    msgnam = (cp = getenv ("mhaltmsg")) && *cp ? getcpy (cp) : NULL;
+    msgnam = (cp = getenv ("mhaltmsg")) && *cp ? mh_xstrdup(cp) : NULL;
 
     if ((cp = getenv ("mhatfile")) && *cp)
        atfile = atoi(cp);
@@ -234,11 +237,11 @@ WhatNow (int argc, char **argv)
     snprintf (prompt, sizeof(prompt), myprompt, invo_name);
     for (;;) {
 #ifdef READLINE_SUPPORT
-       if (!(argp = getans_via_readline (prompt, aleqs))) {
+       if (!(argp = read_switch_multiword_via_readline (prompt, aleqs))) {
 #else /* ! READLINE_SUPPORT */
-       if (!(argp = getans (prompt, aleqs))) {
+       if (!(argp = read_switch_multiword (prompt, aleqs))) {
 #endif /* READLINE_SUPPORT */
-           unlink (LINK);
+           (void) m_unlink (LINK);
            done (1);
        }
        switch (smatch (*argp, aleqs)) {
@@ -315,18 +318,17 @@ WhatNow (int argc, char **argv)
             *  is accustomed to.  Read back the absolute path.
             */
 
-           if (*(argp+1) == (char *)0) {
-               (void)sprintf(buf, "$SHELL -c \"cd&&pwd\"");
+           if (*(argp+1) == NULL) {
+               strcpy(buf, "$SHELL -c \"cd&&pwd\"");
            }
            else {
                writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
            }
-           if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
-               fgets(cwd, sizeof (cwd), f);
-
-               if (strchr(cwd, '\n') != (char *)0)
-                       *strchr(cwd, '\n') = '\0';
-
+           if ((f = popen_in_dir(cwd, buf, "r")) != NULL) {
+               if (fgets(cwd, sizeof (cwd), f) == NULL) {
+                   advise (buf, "fgets");
+               }
+                TrimSuffixC(cwd, '\n');
                pclose(f);
            }
            else {
@@ -358,10 +360,13 @@ WhatNow (int argc, char **argv)
             *   -n     numbers listing
             */
 
-           l = (char *)0;
+           if (checkmimeheader(drft))
+               break;
+
+           l = NULL;
            n = 0;
 
-           while (*++argp != (char *)0) {
+           while (*++argp != NULL) {
                if (strcmp(*argp, "-l") == 0)
                    l = "/";
 
@@ -380,7 +385,7 @@ WhatNow (int argc, char **argv)
            }
 
            if (n == -1)
-               advise((char *)0, "usage is alist [-ln].");
+               advise(NULL, "usage is alist [-ln].");
 
            else
                annolist(drft, ATTACH_FIELD, l, n);
@@ -395,6 +400,9 @@ WhatNow (int argc, char **argv)
             int verbose = 0;
             char **ap;
 
+           if (checkmimeheader(drft))
+               break;
+
            for (ap = argp+1; *ap; ++ap) {
                if (strcmp(*ap, "-v") == 0) {
                    ++argp;
@@ -404,7 +412,7 @@ WhatNow (int argc, char **argv)
                }
            }
 
-           if (*(argp+1) == (char *)0) {
+           if (*(argp+1) == NULL) {
                advise(NULL, "attach command requires file argument(s).");
                break;
            }
@@ -423,11 +431,11 @@ WhatNow (int argc, char **argv)
             *  draft.
             */
 
-           if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
-               while (fgets(shell, sizeof (shell), f) != (char *)0) {
+           if ((f = popen_in_dir(cwd, buf, "r")) != NULL) {
+               while (fgets(shell, sizeof (shell), f) != NULL) {
                    char *ctype;
 
-                   *(strchr(shell, '\n')) = '\0';
+                    TrimSuffixC(shell, '\n');
 
                    if (*shell == '/') {
                        strncpy(file, shell, sizeof(file));
@@ -439,9 +447,6 @@ WhatNow (int argc, char **argv)
                    annotate(drft, ATTACH_FIELD, file, 1, 0, -2, 1);
                    if (verbose) {
                        ctype = mime_type(file);
-                   }
-
-                   if (verbose) {
                        printf ("Attaching %s as a %s\n", file, ctype);
                        free (ctype);
                    }
@@ -465,7 +470,10 @@ WhatNow (int argc, char **argv)
             *  so this catches a -n anywhere in the argument list.
             */
 
-           for (n = 0, arguments = argp + 1; *arguments != (char *)0; arguments++) {
+           if (checkmimeheader(drft))
+               break;
+
+           for (n = 0, arguments = argp + 1; *arguments != NULL; arguments++) {
                if (strcmp(*arguments, "-n") == 0) {
                        n = 1;
                        break;
@@ -480,15 +488,15 @@ WhatNow (int argc, char **argv)
             */
 
            if (n == 1) {
-               for (arguments = argp + 1; *arguments != (char *)0; arguments++) {
+               for (arguments = argp + 1; *arguments != NULL; arguments++) {
                    if (strcmp(*arguments, "-n") == 0)
                        continue;
 
                    if (**arguments != '\0') {
                        n = atoi(*arguments);
-                       annotate(drft, ATTACH_FIELD, (char *)0, 1, 0, n, 1);
+                       annotate(drft, ATTACH_FIELD, NULL, 1, 0, n, 1);
 
-                       for (argp = arguments + 1; *argp != (char *)0; argp++) {
+                       for (argp = arguments + 1; *argp != NULL; argp++) {
                            if (atoi(*argp) > n) {
                                if (atoi(*argp) == 1)
                                    *argp = "";
@@ -510,9 +518,9 @@ WhatNow (int argc, char **argv)
             * provide a file name with a space in it.
             */
            writelscmd(buf, sizeof(buf), "-d --", argp);
-           if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
-               while (fgets(shell, sizeof (shell), f) != (char *)0) {
-                   *(strchr(shell, '\n')) = '\0';
+           if ((f = popen_in_dir(cwd, buf, "r")) != NULL) {
+               while (fgets(shell, sizeof (shell), f) != NULL) {
+                    TrimSuffixC(shell, '\n');
                    annotate(drft, ATTACH_FIELD, shell, 1, 0, 0, 1);
                }
                pclose(f);
@@ -554,7 +562,7 @@ writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
      */
     int trailln = strlen(trailcmd) + 4;
     if (ln < 0 || ln + trailln > bufsz)
-       adios((char *)0, "arguments too long");
+       adios(NULL, "arguments too long");
 
     cp = buf + ln;
 
@@ -562,7 +570,7 @@ writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
        ln = strlen(*argp);
        /* +1 for leading space */
        if (ln + trailln + 1 > bufsz - (cp-buf))
-           adios((char *)0, "arguments too long");
+           adios(NULL, "arguments too long");
        *cp++ = ' ';
        memcpy(cp, *argp, ln+1);
        cp += ln;
@@ -653,7 +661,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
     int        slinked = 0;
 
     /* Was there a previous edit session? */
-    if (reedit) {
+    if (reedit && (*ed || edsave)) {
        if (!*ed) {             /* no explicit editor      */
            *ed = edsave;       /* so use the previous one */
            if ((cp = r1bindex (*ed, '/')) == NULL)
@@ -681,9 +689,11 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
            snprintf (linkpath, sizeof(linkpath), "%s/%s", cwd, LINK);
 
        if (atfile) {
-           unlink (linkpath);
+           (void) m_unlink (linkpath);
            if (link (altpath, linkpath) == NOTOK) {
-               symlink (altpath, linkpath);
+               if (symlink (altpath, linkpath) < 0) {
+                   adios (linkpath, "symlink");
+               }
                slinked = 1;
            } else {
                slinked = 0;
@@ -701,8 +711,11 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
            break;
 
        case OK:
-           if (cwd)
-               chdir (cwd);
+           if (cwd) {
+               if (chdir (cwd) < 0) {
+                   advise (cwd, "chdir");
+               }
+           }
            if (altmsg) {
                if (mp)
                    m_putenv ("mhfolder", mp->foldpath);
@@ -747,7 +760,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
                                && copyf (linkpath, altpath) == NOTOK
                           : stat (linkpath, &st) != NOTOK
                                && st.st_nlink == 1
-                               && (unlink (altpath) == NOTOK
+                               && (m_unlink (altpath) == NOTOK
                                        || link (linkpath, altpath) == NOTOK)))
                advise (linkpath, "unable to update %s from", altmsg);
     }
@@ -758,7 +771,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp,
 
     *ed = NULL;
     if (altmsg && atfile)
-       unlink (linkpath);
+       (void) m_unlink (linkpath);
 
     return status;
 }
@@ -926,14 +939,14 @@ buildfile (char **argp, char *file)
     X("client host", -6, CLIESW) \
     X("server host", 6, SERVSW) \
     X("snoop", -5, SNOOPSW) \
-    X("draftfolder +folder", -6, SDRFSW) \
-    X("draftmessage msg", -6, SDRMSW) \
-    X("nodraftfolder", -3, SNDRFSW) \
-    X("sasl", SASLminc(-4), SASLSW) \
-    X("nosasl", SASLminc(-6), NOSASLSW) \
-    X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
-    X("saslmech", SASLminc(-5), SASLMECHSW) \
-    X("user", SASLminc(-4), USERSW) \
+    X("draftfolder +folder", 0, SDRFSW) \
+    X("draftmessage msg", 0, SDRMSW) \
+    X("nodraftfolder", 0, SNDRFSW) \
+    X("sasl", SASLminc(4), SASLSW) \
+    X("nosasl", SASLminc(6), NOSASLSW) \
+    X("saslmech", SASLminc(5), SASLMECHSW) \
+    X("authservice", SASLminc(0), AUTHSERVICESW) \
+    X("user username", SASLminc(4), USERSW) \
     X("attach fieldname", 6, SNDATTACHSW) \
     X("noattach", 0, SNDNOATTACHSW) \
     X("attachformat", 7, SNDATTACHFORMAT) \
@@ -941,6 +954,7 @@ buildfile (char **argp, char *file)
     X("tls", TLSminc(-3), TLSSW) \
     X("initialtls", TLSminc(-10), INITTLSSW) \
     X("notls", TLSminc(-5), NTLSSW) \
+    X("sendmail program", 0, MTSSM) \
     X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
     X("messageid localname|random", 2, MESSAGEIDSW) \
 
@@ -972,6 +986,9 @@ sendit (char *sp, char **arg, char *file, int pushed)
     int        vecp, n = 1;
     char *cp, buf[BUFSIZ], **argp, *program;
     char **arguments, *savearg[MAXARGS], **vec;
+    const char *user = NULL, *saslmech = NULL;
+    char *auth_svc = NULL;
+    int snoop = 0;
     struct stat st;
 
 #ifndef        lint
@@ -1112,7 +1129,6 @@ sendit (char *sp, char **arg, char *file, int pushed)
                case NMSGDSW:
                case WATCSW:
                case NWATCSW:
-               case SNOOPSW:
                case SASLSW:
                case NOSASLSW:
                case TLSSW:
@@ -1121,15 +1137,33 @@ sendit (char *sp, char **arg, char *file, int pushed)
                    vec[vecp++] = --cp;
                    continue;
 
+               case SNOOPSW:
+                    snoop++;
+                   vec[vecp++] = --cp;
+                   continue;
+
+               case AUTHSERVICESW:
+#ifdef OAUTH_SUPPORT
+                   if (!(auth_svc = *argp++) || *auth_svc == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+#else
+                    NMH_UNUSED (user);
+                    NMH_UNUSED (auth_svc);
+                   adios (NULL, "not built with OAuth support");
+#endif
+                   continue;
+
+               case SASLMECHSW:
+                    saslmech = *argp;
+                   /* fall thru */
                case ALIASW:
                case FILTSW:
                case WIDTHSW:
                case CLIESW:
                case SERVSW:
-               case SASLMXSSFSW:
-               case SASLMECHSW:
                case USERSW:
                case PORTSW:
+               case MTSSM:
                case MTSSW:
                case MESSAGEIDSW:
                    vec[vecp++] = --cp;
@@ -1138,6 +1172,7 @@ sendit (char *sp, char **arg, char *file, int pushed)
                        return;
                    }
                    vec[vecp++] = cp;
+                    user = cp;
                    continue;
 
                case SDRFSW:
@@ -1169,7 +1204,7 @@ sendit (char *sp, char **arg, char *file, int pushed)
     if ((cp = context_find ("Aliasfile"))) {
        char **ap, *dp;
 
-       dp = getcpy (cp);
+       dp = mh_xstrdup(cp);
        for (ap = brkstring (dp, " ", "\n"); ap && *ap; ap++) {
            vec[vecp++] = "-alias";
            vec[vecp++] = *ap;
@@ -1194,14 +1229,32 @@ sendit (char *sp, char **arg, char *file, int pushed)
 #endif /* not lint */
            && altmsg) {
        vec[vecp++] = "-dist";
-       distfile = getcpy (m_mktemp2(altmsg, invo_name, NULL, NULL));
-       unlink(distfile);
+       if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
+           adios(NULL, "unable to create temporary file in %s",
+                 get_temp_dir());
+       }
+       distfile = mh_xstrdup(cp);
+       (void) m_unlink(distfile);
        if (link (altmsg, distfile) == NOTOK)
            adios (distfile, "unable to link %s to", altmsg);
     } else {
        distfile = NULL;
     }
 
+#ifdef OAUTH_SUPPORT
+    if (auth_svc == NULL) {
+        if (saslmech  &&  ! strcasecmp(saslmech, "xoauth2")) {
+            adios (NULL, "must specify -authservice with -saslmech xoauth2");
+        }
+    } else {
+        if (user == NULL) {
+            adios (NULL, "must specify -user with -saslmech xoauth2");
+        }
+    }
+#else
+    NMH_UNUSED(saslmech);
+#endif /* OAUTH_SUPPORT */
+
     if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
        st.st_mtime = 0;
        st.st_dev = 0;
@@ -1212,7 +1265,7 @@ sendit (char *sp, char **arg, char *file, int pushed)
 
     closefds (3);
 
-    if (sendsbr (vec, vecp, program, file, &st, 1) == OK)
+    if (sendsbr (vec, vecp, program, file, &st, 1, auth_svc) == OK)
        done (0);
 }
 
@@ -1237,10 +1290,10 @@ whomfile (char **arg, char *file)
 
        case OK:
            vec = argsplit(whomproc, &program, &vecp);
-           vec[vecp++] = file;
            if (arg)
                while (*arg)
                    vec[vecp++] = *arg++;
+           vec[vecp++] = file;
            vec[vecp] = NULL;
 
            execvp (program, vec);
@@ -1261,8 +1314,50 @@ whomfile (char **arg, char *file)
 static int
 removefile (char *drft)
 {
-    if (unlink (drft) == NOTOK)
+    if (m_unlink (drft) == NOTOK)
        adios (drft, "unable to unlink");
 
     return OK;
 }
+
+
+/*
+ * Return 1 if we already have a MIME-Version header, 0 otherwise.
+ */
+
+static int
+checkmimeheader (char *drft)
+{
+    FILE *f;
+    m_getfld_state_t gstate = 0;
+    char buf[BUFSIZ], name[NAMESZ];
+    int state, retval = 0;
+
+    if ((f = fopen(drft, "r")) == NULL) {
+       admonish(drft, "unable to read draft");
+       return (0);
+    }
+
+    for (;;) {
+       int bufsz = sizeof(buf);
+       switch (state = m_getfld(&gstate, name, buf, &bufsz, f)) {
+       case FLD:
+       case FLDPLUS:
+           if (strcasecmp(name, VRSN_FIELD) == 0) {
+               advise(NULL, "Cannot use attach commands with already-"
+                      "formatted MIME message \"%s\"", drft);
+               retval = 1;
+               break;
+           }
+           continue;
+       default:
+           break;
+       }
+       break;
+    }
+
+    m_getfld_state_destroy(&gstate);
+    fclose(f);
+
+    return retval;
+}