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