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) },
66 main (int argc, char **argv)
69 int i, status, isdf = 0;
70 int distsw = 0, vecp = 0;
71 char *cp, *dfolder = NULL, *dmsg = NULL;
72 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
73 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
76 setlocale(LC_ALL, "");
78 invo_name = r1bindex (argv[0], '/');
80 /* read user profile/context */
83 arguments = getarguments (invo_name, argc, argv, 1);
86 vec[vecp++] = invo_name;
87 vec[vecp++] = "-whom";
88 vec[vecp++] = "-library";
89 vec[vecp++] = getcpy (m_maildir (""));
91 while ((cp = *argp++)) {
93 switch (smatch (++cp, switches)) {
95 ambigsw (cp, switches);
98 adios (NULL, "-%s unknown", cp);
101 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
102 print_help (buf, switches, 1);
105 print_version(invo_name);
121 adios (NULL, "only one draft folder at a time!");
122 if (!(cp = *argp++) || *cp == '-')
123 adios (NULL, "missing argument to %s", argp[-2]);
124 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
125 *cp != '@' ? TFOLDER : TSUBCWF);
129 adios (NULL, "only one draft message at a time!");
130 if (!(dmsg = *argp++) || *dmsg == '-')
131 adios (NULL, "missing argument to %s", argp[-2]);
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
152 adios (NULL, "only one draft at a time!");
154 vec[vecp++] = msg = cp;
157 /* allow Aliasfile: profile entry */
158 if ((cp = context_find ("Aliasfile"))) {
161 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
162 vec[vecp++] = "-alias";
169 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
171 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
172 msg = vec[vecp++] = cp;
174 if ((cp = getenv ("mhdist"))
176 && (distsw = atoi (cp))
177 && (cp = getenv ("mhaltmsg"))
179 if (distout (msg, cp, backup) == NOTOK)
181 vec[vecp++] = "-dist";
189 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
193 switch (distsw ? child_id : OK) {
195 advise (NULL, "unable to fork, so checking directly...");
197 execvp (postproc, vec);
198 fprintf (stderr, "unable to exec ");
203 SIGNAL (SIGHUP, SIG_IGN);
204 SIGNAL (SIGINT, SIG_IGN);
205 SIGNAL (SIGQUIT, SIG_IGN);
206 SIGNAL (SIGTERM, SIG_IGN);
208 status = pidwait(child_id, OK);
211 if (rename (backup, msg) == NOTOK)
212 adios (msg, "unable to rename %s to", backup);
216 return 0; /* dead code to satisfy the compiler */