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