]> diplodocus.org Git - nmh/blob - uip/whom.c
39a64703233689185336359d57fcdacc86bbd1ee
[nmh] / uip / whom.c
1 /* whom.c -- report to whom a message would be sent
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include <h/signals.h>
11 #include "sbr/m_maildir.h"
12 #include "sbr/m_mktemp.h"
13
14 #ifndef CYRUS_SASL
15 # define SASLminc(a) (a)
16 #else /* CYRUS_SASL */
17 # define SASLminc(a) 0
18 #endif /* CYRUS_SASL */
19
20 #ifndef TLS_SUPPORT
21 # define TLSminc(a) (a)
22 #else /* TLS_SUPPORT */
23 # define TLSminc(a) 0
24 #endif /* TLS_SUPPORT */
25
26 #define WHOM_SWITCHES \
27 X("alias aliasfile", 0, ALIASW) \
28 X("check", 0, CHKSW) \
29 X("nocheck", 0, NOCHKSW) \
30 X("draft", 0, DRAFTSW) \
31 X("draftfolder +folder", 6, DFOLDSW) \
32 X("draftmessage msg", 6, DMSGSW) \
33 X("nodraftfolder", 0, NDFLDSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("client host", -6, CLIESW) \
37 X("server host", 0, SERVSW) \
38 X("snoop", 0, SNOOPSW) \
39 X("sasl", SASLminc(4), SASLSW) \
40 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
41 X("user username", SASLminc(-4), USERSW) \
42 X("port server port name/number", 4, PORTSW) \
43 X("tls", TLSminc(-3), TLSSW) \
44 X("initialtls", TLSminc(-10), INITTLSSW) \
45 X("notls", TLSminc(-5), NTLSSW) \
46 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
47
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(WHOM);
50 #undef X
51
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(WHOM, switches);
54 #undef X
55
56
57 int
58 main (int argc, char **argv)
59 {
60 pid_t child_id = OK;
61 int status, isdf = 0;
62 int vecp = 0;
63 bool distsw;
64 char *cp, *dfolder = NULL, *dmsg = NULL;
65 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
66 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
67
68 if (nmh_init(argv[0], 2)) { return 1; }
69
70 arguments = getarguments (invo_name, argc, argv, 1);
71 argp = arguments;
72
73 vec[vecp++] = invo_name;
74 vec[vecp++] = "-whom";
75 vec[vecp++] = "-library";
76 vec[vecp++] = getcpy (m_maildir (""));
77
78 if ((cp = context_find ("credentials"))) {
79 /* post doesn't read context so need to pass credentials. */
80 vec[vecp++] = "-credentials";
81 vec[vecp++] = cp;
82 }
83
84 /* Don't need to feed fileproc or mhlproc to post because
85 it doesn't use them when used for whom. */
86
87 while ((cp = *argp++)) {
88 if (*cp == '-') {
89 switch (smatch (++cp, switches)) {
90 case AMBIGSW:
91 ambigsw (cp, switches);
92 done (1);
93 case UNKWNSW:
94 adios (NULL, "-%s unknown", cp);
95
96 case HELPSW:
97 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
98 print_help (buf, switches, 1);
99 done (0);
100 case VERSIONSW:
101 print_version(invo_name);
102 done (0);
103
104 case CHKSW:
105 case NOCHKSW:
106 case SNOOPSW:
107 case SASLSW:
108 case TLSSW:
109 case INITTLSSW:
110 case NTLSSW:
111 vec[vecp++] = --cp;
112 continue;
113
114 case DRAFTSW:
115 msg = draft;
116 continue;
117
118 case DFOLDSW:
119 if (dfolder)
120 adios (NULL, "only one draft folder at a time!");
121 if (!(cp = *argp++) || *cp == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
123 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
124 *cp != '@' ? TFOLDER : TSUBCWF);
125 continue;
126 case DMSGSW:
127 if (dmsg)
128 adios (NULL, "only one draft message at a time!");
129 if (!(dmsg = *argp++) || *dmsg == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
131 continue;
132 case NDFLDSW:
133 dfolder = NULL;
134 isdf = NOTOK;
135 continue;
136
137 case ALIASW:
138 case CLIESW:
139 case SERVSW:
140 case USERSW:
141 case PORTSW:
142 case SASLMECHSW:
143 case MTSSW:
144 vec[vecp++] = --cp;
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 vec[vecp++] = cp;
148 continue;
149 }
150 }
151 if (msg)
152 adios (NULL, "only one draft at a time!");
153 vec[vecp++] = msg = cp;
154 }
155
156 /* allow Aliasfile: profile entry */
157 if ((cp = context_find ("Aliasfile"))) {
158 char *dp = NULL;
159
160 for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) {
161 vec[vecp++] = "-alias";
162 vec[vecp++] = *ap;
163 }
164 }
165
166 if (msg == NULL) {
167 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
168 msg = vec[vecp++] = cp;
169 }
170
171 distsw = (cp = getenv("mhdist")) && *cp && atoi(cp) &&
172 (cp = getenv("mhaltmsg")) && *cp;
173 if (distsw) {
174 if (distout (msg, cp, backup) == NOTOK)
175 done (1);
176 vec[vecp++] = "-dist";
177 }
178 vec[vecp] = NULL;
179
180 closefds (3);
181
182 if (distsw) {
183 if ((child_id = fork()) == -1)
184 adios("fork", "failed:");
185
186 if (child_id) {
187 SIGNAL (SIGHUP, SIG_IGN);
188 SIGNAL (SIGINT, SIG_IGN);
189 SIGNAL (SIGQUIT, SIG_IGN);
190 SIGNAL (SIGTERM, SIG_IGN);
191
192 status = pidwait(child_id, OK);
193
194 (void) m_unlink (msg);
195 if (rename (backup, msg) == NOTOK)
196 adios (msg, "unable to rename %s to", backup);
197 done (status);
198 }
199 }
200
201 /* Either the child, or no fork occurred. */
202 execvp (postproc, vec);
203 fprintf (stderr, "unable to exec ");
204 perror (postproc);
205 _exit(1);
206 }