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>
14 static struct swit switches[] = {
16 { "alias aliasfile", 0 },
24 { "draftfolder +folder", 6 },
26 { "draftmessage msg", 6 },
28 { "nodraftfolder", 0 },
34 { "client host", -6 },
36 { "server host", -6 },
40 { "port server port name/number", 4 },
46 main (int argc, char **argv)
49 int i, status, isdf = 0;
50 int distsw = 0, vecp = 0;
51 char *cp, *dfolder = NULL, *dmsg = NULL;
52 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
53 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
56 setlocale(LC_ALL, "");
58 invo_name = r1bindex (argv[0], '/');
60 /* read user profile/context */
63 arguments = getarguments (invo_name, argc, argv, 1);
66 vec[vecp++] = invo_name;
67 vec[vecp++] = "-whom";
68 vec[vecp++] = "-library";
69 vec[vecp++] = getcpy (m_maildir (""));
71 while ((cp = *argp++)) {
73 switch (smatch (++cp, switches)) {
75 ambigsw (cp, switches);
78 adios (NULL, "-%s unknown", cp);
81 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
82 print_help (buf, switches, 1);
85 print_version(invo_name);
100 adios (NULL, "only one draft folder at a time!");
101 if (!(cp = *argp++) || *cp == '-')
102 adios (NULL, "missing argument to %s", argp[-2]);
103 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
104 *cp != '@' ? TFOLDER : TSUBCWF);
108 adios (NULL, "only one draft message at a time!");
109 if (!(dmsg = *argp++) || *dmsg == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
122 if (!(cp = *argp++) || *cp == '-')
123 adios (NULL, "missing argument to %s", argp[-2]);
129 adios (NULL, "only one draft at a time!");
131 vec[vecp++] = msg = cp;
134 /* allow Aliasfile: profile entry */
135 if ((cp = context_find ("Aliasfile"))) {
138 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
139 vec[vecp++] = "-alias";
146 if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
148 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
149 msg = vec[vecp++] = cp;
151 if ((cp = getenv ("mhdist"))
153 && (distsw = atoi (cp))
154 && (cp = getenv ("mhaltmsg"))
156 if (distout (msg, cp, backup) == NOTOK)
158 vec[vecp++] = "-dist";
166 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
170 switch (distsw ? child_id : OK) {
172 advise (NULL, "unable to fork, so checking directly...");
174 execvp (postproc, vec);
175 fprintf (stderr, "unable to exec ");
180 SIGNAL (SIGHUP, SIG_IGN);
181 SIGNAL (SIGINT, SIG_IGN);
182 SIGNAL (SIGQUIT, SIG_IGN);
183 SIGNAL (SIGTERM, SIG_IGN);
185 status = pidwait(child_id, OK);
188 if (rename (backup, msg) == NOTOK)
189 adios (msg, "unable to rename %s to", backup);
193 return 0; /* dead code to satisfy the compiler */