2 * whom.c -- report to whom a message would be sent
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/signals.h>
13 static struct swit switches[] = {
15 { "alias aliasfile", 0 },
23 { "draftfolder +folder", 6 },
25 { "draftmessage msg", 6 },
27 { "nodraftfolder", 0 },
33 { "client host", -6 },
35 { "server host", -6 },
39 { "port server port name/number", 4 },
45 main (int argc, char **argv)
48 int i, status, isdf = 0;
49 int distsw = 0, vecp = 0;
50 char *cp, *dfolder = NULL, *dmsg = NULL;
51 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
52 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
55 setlocale(LC_ALL, "");
57 invo_name = r1bindex (argv[0], '/');
59 /* read user profile/context */
62 arguments = getarguments (invo_name, argc, argv, 1);
65 vec[vecp++] = invo_name;
66 vec[vecp++] = "-whom";
67 vec[vecp++] = "-library";
68 vec[vecp++] = getcpy (m_maildir (""));
70 while ((cp = *argp++)) {
72 switch (smatch (++cp, switches)) {
74 ambigsw (cp, switches);
77 adios (NULL, "-%s unknown", cp);
80 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
81 print_help (buf, switches, 1);
84 print_version(invo_name);
99 adios (NULL, "only one draft folder at a time!");
100 if (!(cp = *argp++) || *cp == '-')
101 adios (NULL, "missing argument to %s", argp[-2]);
102 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
103 *cp != '@' ? TFOLDER : TSUBCWF);
107 adios (NULL, "only one draft message at a time!");
108 if (!(dmsg = *argp++) || *dmsg == '-')
109 adios (NULL, "missing argument to %s", argp[-2]);
121 if (!(cp = *argp++) || *cp == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
128 adios (NULL, "only one draft at a time!");
130 vec[vecp++] = msg = cp;
133 /* allow Aliasfile: profile entry */
134 if ((cp = context_find ("Aliasfile"))) {
137 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
138 vec[vecp++] = "-alias";
145 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
147 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
148 msg = vec[vecp++] = cp;
150 if ((cp = getenv ("mhdist"))
152 && (distsw = atoi (cp))
153 && (cp = getenv ("mhaltmsg"))
155 if (distout (msg, cp, backup) == NOTOK)
157 vec[vecp++] = "-dist";
165 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
169 switch (distsw ? child_id : OK) {
171 advise (NULL, "unable to fork, so checking directly...");
173 execvp (postproc, vec);
174 fprintf (stderr, "unable to exec ");
179 SIGNAL (SIGHUP, SIG_IGN);
180 SIGNAL (SIGINT, SIG_IGN);
181 SIGNAL (SIGQUIT, SIG_IGN);
182 SIGNAL (SIGTERM, SIG_IGN);
184 status = pidwait(child_id, OK);
187 if (rename (backup, msg) == NOTOK)
188 adios (msg, "unable to rename %s to", backup);
192 return 0; /* dead code to satisfy the compiler */