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