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) },
64 { "mts smtp|sendmail/smtp|sendmail/pipe", 0 },
70 main (int argc, char **argv)
73 int i, status, isdf = 0;
74 int distsw = 0, vecp = 0;
75 char *cp, *dfolder = NULL, *dmsg = NULL;
76 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
77 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
80 setlocale(LC_ALL, "");
82 invo_name = r1bindex (argv[0], '/');
84 /* read user profile/context */
87 arguments = getarguments (invo_name, argc, argv, 1);
90 vec[vecp++] = invo_name;
91 vec[vecp++] = "-whom";
92 vec[vecp++] = "-library";
93 vec[vecp++] = getcpy (m_maildir (""));
95 /* Don't need to feed fileproc or mhlproc to post because
96 it doesn't use them when used for whom. */
98 while ((cp = *argp++)) {
100 switch (smatch (++cp, switches)) {
102 ambigsw (cp, switches);
105 adios (NULL, "-%s unknown", cp);
108 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
109 print_help (buf, switches, 1);
112 print_version(invo_name);
130 adios (NULL, "only one draft folder at a time!");
131 if (!(cp = *argp++) || *cp == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
134 *cp != '@' ? TFOLDER : TSUBCWF);
138 adios (NULL, "only one draft message at a time!");
139 if (!(dmsg = *argp++) || *dmsg == '-')
140 adios (NULL, "missing argument to %s", argp[-2]);
155 if (!(cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
162 adios (NULL, "only one draft at a time!");
164 vec[vecp++] = msg = cp;
167 /* allow Aliasfile: profile entry */
168 if ((cp = context_find ("Aliasfile"))) {
171 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
172 vec[vecp++] = "-alias";
179 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
181 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
182 msg = vec[vecp++] = cp;
184 if ((cp = getenv ("mhdist"))
186 && (distsw = atoi (cp))
187 && (cp = getenv ("mhaltmsg"))
189 if (distout (msg, cp, backup) == NOTOK)
191 vec[vecp++] = "-dist";
199 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
203 switch (distsw ? child_id : OK) {
205 advise (NULL, "unable to fork, so checking directly...");
207 execvp (postproc, vec);
208 fprintf (stderr, "unable to exec ");
213 SIGNAL (SIGHUP, SIG_IGN);
214 SIGNAL (SIGINT, SIG_IGN);
215 SIGNAL (SIGQUIT, SIG_IGN);
216 SIGNAL (SIGTERM, SIG_IGN);
218 status = pidwait(child_id, OK);
221 if (rename (backup, msg) == NOTOK)
222 adios (msg, "unable to rename %s to", backup);
226 return 0; /* dead code to satisfy the compiler */