3 * whom.c -- report to whom a message would be sent
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
15 # define SASLminc(a) (a)
16 #else /* CYRUS_SASL */
17 # define SASLminc(a) 0
18 #endif /* CYRUS_SASL */
21 # define TLSminc(a) (a)
22 #else /* TLS_SUPPORT */
24 #endif /* TLS_SUPPORT */
26 static struct swit switches[] = {
28 { "alias aliasfile", 0 },
36 { "draftfolder +folder", 6 },
38 { "draftmessage msg", 6 },
40 { "nodraftfolder", 0 },
46 { "client host", -6 },
48 { "server host", -6 },
52 { "sasl", SASLminc(4) },
54 { "saslmech mechanism", SASLminc(-5) },
56 { "user username", SASLminc(-4) },
58 { "port server port name/number", 4 },
60 { "tls", TLSminc(-3) },
62 { "notls", TLSminc(-5) },
68 main (int argc, char **argv)
71 int i, status, isdf = 0;
72 int distsw = 0, vecp = 0;
73 char *cp, *dfolder = NULL, *dmsg = NULL;
74 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
75 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
78 setlocale(LC_ALL, "");
80 invo_name = r1bindex (argv[0], '/');
82 /* read user profile/context */
85 arguments = getarguments (invo_name, argc, argv, 1);
88 vec[vecp++] = invo_name;
89 vec[vecp++] = "-whom";
90 vec[vecp++] = "-library";
91 vec[vecp++] = getcpy (m_maildir (""));
93 /* Don't need to feed fileproc or mhlproc to post because
94 it doesn't use them when used for whom. */
96 while ((cp = *argp++)) {
98 switch (smatch (++cp, switches)) {
100 ambigsw (cp, switches);
103 adios (NULL, "-%s unknown", cp);
106 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
107 print_help (buf, switches, 1);
110 print_version(invo_name);
128 adios (NULL, "only one draft folder at a time!");
129 if (!(cp = *argp++) || *cp == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
131 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
132 *cp != '@' ? TFOLDER : TSUBCWF);
136 adios (NULL, "only one draft message at a time!");
137 if (!(dmsg = *argp++) || *dmsg == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
152 if (!(cp = *argp++) || *cp == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
159 adios (NULL, "only one draft at a time!");
161 vec[vecp++] = msg = cp;
164 /* allow Aliasfile: profile entry */
165 if ((cp = context_find ("Aliasfile"))) {
168 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
169 vec[vecp++] = "-alias";
176 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
178 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
179 msg = vec[vecp++] = cp;
181 if ((cp = getenv ("mhdist"))
183 && (distsw = atoi (cp))
184 && (cp = getenv ("mhaltmsg"))
186 if (distout (msg, cp, backup) == NOTOK)
188 vec[vecp++] = "-dist";
196 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
200 switch (distsw ? child_id : OK) {
202 advise (NULL, "unable to fork, so checking directly...");
204 execvp (postproc, vec);
205 fprintf (stderr, "unable to exec ");
210 SIGNAL (SIGHUP, SIG_IGN);
211 SIGNAL (SIGINT, SIG_IGN);
212 SIGNAL (SIGQUIT, SIG_IGN);
213 SIGNAL (SIGTERM, SIG_IGN);
215 status = pidwait(child_id, OK);
218 if (rename (backup, msg) == NOTOK)
219 adios (msg, "unable to rename %s to", backup);
223 return 0; /* dead code to satisfy the compiler */