3 * whom.c -- report to whom a message would be sent
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/signals.h>
17 # define SASLminc(a) (a)
18 #else /* CYRUS_SASL */
19 # define SASLminc(a) 0
20 #endif /* CYRUS_SASL */
23 # define TLSminc(a) (a)
24 #else /* TLS_SUPPORT */
26 #endif /* TLS_SUPPORT */
28 static struct swit switches[] = {
30 { "alias aliasfile", 0 },
38 { "draftfolder +folder", 6 },
40 { "draftmessage msg", 6 },
42 { "nodraftfolder", 0 },
48 { "client host", -6 },
50 { "server host", -6 },
54 { "sasl", SASLminc(4) },
56 { "saslmech mechanism", SASLminc(-5) },
58 { "user username", SASLminc(-4) },
60 { "port server port name/number", 4 },
62 { "tls", TLSminc(-3) },
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 while ((cp = *argp++)) {
95 switch (smatch (++cp, switches)) {
97 ambigsw (cp, switches);
100 adios (NULL, "-%s unknown", cp);
103 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
104 print_help (buf, switches, 1);
107 print_version(invo_name);
123 adios (NULL, "only one draft folder at a time!");
124 if (!(cp = *argp++) || *cp == '-')
125 adios (NULL, "missing argument to %s", argp[-2]);
126 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
127 *cp != '@' ? TFOLDER : TSUBCWF);
131 adios (NULL, "only one draft message at a time!");
132 if (!(dmsg = *argp++) || *dmsg == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
147 if (!(cp = *argp++) || *cp == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
154 adios (NULL, "only one draft at a time!");
156 vec[vecp++] = msg = cp;
159 /* allow Aliasfile: profile entry */
160 if ((cp = context_find ("Aliasfile"))) {
163 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
164 vec[vecp++] = "-alias";
171 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
173 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
174 msg = vec[vecp++] = cp;
176 if ((cp = getenv ("mhdist"))
178 && (distsw = atoi (cp))
179 && (cp = getenv ("mhaltmsg"))
181 if (distout (msg, cp, backup) == NOTOK)
183 vec[vecp++] = "-dist";
191 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
195 switch (distsw ? child_id : OK) {
197 advise (NULL, "unable to fork, so checking directly...");
199 execvp (postproc, vec);
200 fprintf (stderr, "unable to exec ");
205 SIGNAL (SIGHUP, SIG_IGN);
206 SIGNAL (SIGINT, SIG_IGN);
207 SIGNAL (SIGQUIT, SIG_IGN);
208 SIGNAL (SIGTERM, SIG_IGN);
210 status = pidwait(child_id, OK);
213 if (rename (backup, msg) == NOTOK)
214 adios (msg, "unable to rename %s to", backup);
218 return 0; /* dead code to satisfy the compiler */