X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/352fe458a57061db81240c19fa4b356c7448463b..8699f1cc:/uip/whom.c?ds=inline diff --git a/uip/whom.c b/uip/whom.c index 51ce5fef..76f434e4 100644 --- a/uip/whom.c +++ b/uip/whom.c @@ -1,8 +1,4 @@ - -/* - * whom.c -- report to whom a message would be sent - * - * $Id$ +/* whom.c -- report to whom a message would be sent * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for @@ -10,8 +6,11 @@ */ #include +#include "h/done.h" +#include #include -#include +#include "sbr/m_maildir.h" +#include "sbr/m_mktemp.h" #ifndef CYRUS_SASL # define SASLminc(a) (a) @@ -19,60 +18,55 @@ # define SASLminc(a) 0 #endif /* CYRUS_SASL */ -static struct swit switches[] = { -#define ALIASW 0 - { "alias aliasfile", 0 }, -#define CHKSW 1 - { "check", 0 }, -#define NOCHKSW 2 - { "nocheck", 0 }, -#define DRAFTSW 3 - { "draft", 0 }, -#define DFOLDSW 4 - { "draftfolder +folder", 6 }, -#define DMSGSW 5 - { "draftmessage msg", 6 }, -#define NDFLDSW 6 - { "nodraftfolder", 0 }, -#define VERSIONSW 7 - { "version", 0 }, -#define HELPSW 8 - { "help", 0 }, -#define CLIESW 9 - { "client host", -6 }, -#define SERVSW 10 - { "server host", -6 }, -#define SNOOPSW 11 - { "snoop", -5 }, -#define SASLSW 12 - { "sasl", SASLminc(4) }, -#define SASLMECHSW 13 - { "saslmech mechanism", SASLminc(-5) }, -#define USERSW 14 - { "user username", SASLminc(-4) }, -#define PORTSW 15 - { "port server port name/number", 4 }, - { NULL, 0 } -}; +#ifndef TLS_SUPPORT +# define TLSminc(a) (a) +#else /* TLS_SUPPORT */ +# define TLSminc(a) 0 +#endif /* TLS_SUPPORT */ + +#define WHOM_SWITCHES \ + X("alias aliasfile", 0, ALIASW) \ + X("check", 0, CHKSW) \ + X("nocheck", 0, NOCHKSW) \ + X("draft", 0, DRAFTSW) \ + X("draftfolder +folder", 6, DFOLDSW) \ + X("draftmessage msg", 6, DMSGSW) \ + X("nodraftfolder", 0, NDFLDSW) \ + X("version", 0, VERSIONSW) \ + X("help", 0, HELPSW) \ + X("client host", -6, CLIESW) \ + X("server host", 0, SERVSW) \ + X("snoop", 0, SNOOPSW) \ + X("sasl", SASLminc(4), SASLSW) \ + X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \ + X("user username", SASLminc(-4), USERSW) \ + X("port server port name/number", 4, PORTSW) \ + X("tls", TLSminc(-3), TLSSW) \ + X("initialtls", TLSminc(-10), INITTLSSW) \ + X("notls", TLSminc(-5), NTLSSW) \ + X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \ + +#define X(sw, minchars, id) id, +DEFINE_SWITCH_ENUM(WHOM); +#undef X + +#define X(sw, minchars, id) { sw, minchars, id }, +DEFINE_SWITCH_ARRAY(WHOM, switches); +#undef X int main (int argc, char **argv) { - pid_t child_id; - int i, status, isdf = 0; - int distsw = 0, vecp = 0; + pid_t child_id = OK; + int status, isdf = 0; + int vecp = 0; + bool distsw; char *cp, *dfolder = NULL, *dmsg = NULL; char *msg = NULL, **ap, **argp, backup[BUFSIZ]; char buf[BUFSIZ], **arguments, *vec[MAXARGS]; -#ifdef LOCALE - setlocale(LC_ALL, ""); -#endif - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 2)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -82,6 +76,15 @@ main (int argc, char **argv) vec[vecp++] = "-library"; vec[vecp++] = getcpy (m_maildir ("")); + if ((cp = context_find ("credentials"))) { + /* post doesn't read context so need to pass credentials. */ + vec[vecp++] = "-credentials"; + vec[vecp++] = cp; + } + + /* Don't need to feed fileproc or mhlproc to post because + it doesn't use them when used for whom. */ + while ((cp = *argp++)) { if (*cp == '-') { switch (smatch (++cp, switches)) { @@ -94,15 +97,18 @@ main (int argc, char **argv) case HELPSW: snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name); print_help (buf, switches, 1); - done (1); + done (0); case VERSIONSW: print_version(invo_name); - done (1); + done (0); case CHKSW: case NOCHKSW: case SNOOPSW: case SASLSW: + case TLSSW: + case INITTLSSW: + case NTLSSW: vec[vecp++] = --cp; continue; @@ -135,6 +141,7 @@ main (int argc, char **argv) case USERSW: case PORTSW: case SASLMECHSW: + case MTSSW: vec[vecp++] = --cp; if (!(cp = *argp++) || *cp == '-') adios (NULL, "missing argument to %s", argp[-2]); @@ -144,68 +151,57 @@ main (int argc, char **argv) } if (msg) adios (NULL, "only one draft at a time!"); - else - vec[vecp++] = msg = cp; + vec[vecp++] = msg = cp; } /* allow Aliasfile: profile entry */ if ((cp = context_find ("Aliasfile"))) { char *dp = NULL; - for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) { + for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) { vec[vecp++] = "-alias"; vec[vecp++] = *ap; } } if (msg == NULL) { -#ifdef WHATNOW - if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0') -#endif /* WHATNOW */ cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf)); msg = vec[vecp++] = cp; } - if ((cp = getenv ("mhdist")) - && *cp - && (distsw = atoi (cp)) - && (cp = getenv ("mhaltmsg")) - && *cp) { + + distsw = (cp = getenv("mhdist")) && *cp && atoi(cp) && + (cp = getenv("mhaltmsg")) && *cp; + if (distsw) { if (distout (msg, cp, backup) == NOTOK) done (1); vec[vecp++] = "-dist"; - distsw++; } vec[vecp] = NULL; closefds (3); if (distsw) { - for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) - sleep (5); - } - - switch (distsw ? child_id : OK) { - case NOTOK: - advise (NULL, "unable to fork, so checking directly..."); - case OK: - execvp (postproc, vec); - fprintf (stderr, "unable to exec "); - perror (postproc); - _exit (-1); - - default: - SIGNAL (SIGHUP, SIG_IGN); - SIGNAL (SIGINT, SIG_IGN); - SIGNAL (SIGQUIT, SIG_IGN); - SIGNAL (SIGTERM, SIG_IGN); - - status = pidwait(child_id, OK); - - unlink (msg); - if (rename (backup, msg) == NOTOK) - adios (msg, "unable to rename %s to", backup); - done (status); + if ((child_id = fork()) == -1) + adios("fork", "failed:"); + + if (child_id) { + SIGNAL (SIGHUP, SIG_IGN); + SIGNAL (SIGINT, SIG_IGN); + SIGNAL (SIGQUIT, SIG_IGN); + SIGNAL (SIGTERM, SIG_IGN); + + status = pidwait(child_id, OK); + + (void) m_unlink (msg); + if (rename (backup, msg) == NOTOK) + adios (msg, "unable to rename %s to", backup); + done (status); + } } - return 0; /* dead code to satisfy the compiler */ + /* Either the child, or no fork occurred. */ + execvp (postproc, vec); + fprintf (stderr, "unable to exec "); + perror (postproc); + _exit(1); }