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