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