]> diplodocus.org Git - nmh/blob - uip/aliasbr.c
b5935f6072fdbc571097f79b5eea0ab6dbcb54a2
[nmh] / uip / aliasbr.c
1 /* aliasbr.c -- new aliasing mechanism
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/getcpy.h"
10 #include "h/aliasbr.h"
11 #include "h/addrsbr.h"
12 #include "h/utils.h"
13 #include <pwd.h>
14
15 static int akvis;
16 static char *akerrst;
17
18 struct aka *akahead = NULL;
19 struct aka *akatail = NULL;
20
21 /*
22 * prototypes
23 */
24 static char *akval (struct aka *, char *);
25 static bool aleq (char *, char *) PURE;
26 static char *scanp (char *) PURE;
27 static char *getp (char *);
28 static char *seekp (char *, char *, char **);
29 static int addfile (struct aka *, char *);
30 static char *getalias (char *);
31 static void add_aka (struct aka *, char *);
32 static struct aka *akalloc (char *);
33
34
35 /* Do mh alias substitution on 's' and return the results. */
36 char *
37 akvalue (char *s)
38 {
39 char *v;
40
41 if (akahead == NULL)
42 alias (AliasFile);
43
44 akvis = -1;
45 v = akval (akahead, s);
46 if (akvis == -1)
47 akvis = 0;
48 return v;
49 }
50
51
52 int
53 akvisible (void)
54 {
55 return akvis;
56 }
57
58
59 char *
60 akresult (struct aka *ak)
61 {
62 char *cp = NULL, *dp, *pp;
63 struct adr *ad;
64
65 for (ad = ak->ak_addr; ad; ad = ad->ad_next) {
66 pp = ad->ad_local ? akval (ak->ak_next, ad->ad_text)
67 : getcpy (ad->ad_text);
68
69 if (cp) {
70 dp = cp;
71 cp = concat (cp, ",", pp, NULL);
72 free (dp);
73 free (pp);
74 }
75 else
76 cp = pp;
77 }
78
79 if (akvis == -1)
80 akvis = ak->ak_visible;
81 return cp;
82 }
83
84
85 static char *
86 akval (struct aka *ak, char *s)
87 {
88 if (!s)
89 return s; /* XXX */
90
91 /* It'd be tempting to check for a trailing semicolon and remove
92 it. But that would break the EXMH alias parser on what would
93 then be valid expressions:
94 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
95 */
96
97 for (; ak; ak = ak->ak_next) {
98 if (aleq (s, ak->ak_name)) {
99 return akresult (ak);
100 }
101
102 if (strchr (s, ':')) {
103 /* The first address in a blind list will contain the
104 alias name, so try to match, but just with just the
105 address (not including the list name). If there's a
106 match, then replace the alias part with its
107 expansion. */
108
109 char *name = getname (s);
110 char *cp = NULL;
111
112 if (name) {
113 /* s is of the form "Blind list: address". If address
114 is an alias, expand it. */
115 struct mailname *mp = getm (name, NULL, 0, NULL, 0);
116
117 if (mp && mp->m_ingrp) {
118 char *gname = mh_xstrdup(FENDNULL(mp->m_gname));
119
120 /* FIXME: gname must be true; add() never returns NULL.
121 * Is some other test required? */
122 if (gname && aleq (name, ak->ak_name)) {
123 /* Will leak cp. */
124 cp = concat (gname, akresult (ak), NULL);
125 free (gname);
126 }
127 }
128
129 mnfree (mp);
130 }
131
132 /* Need to flush getname after use. */
133 while (getname ("")) continue;
134
135 if (cp) {
136 return cp;
137 }
138 }
139 }
140
141 return mh_xstrdup(s);
142 }
143
144
145 static bool
146 aleq (char *string, char *aliasent)
147 {
148 char c;
149
150 while ((c = *string++)) {
151 if (*aliasent == '*')
152 return true;
153 if (tolower((unsigned char)c) != tolower((unsigned char)*aliasent))
154 return false;
155 aliasent++;
156 }
157
158 return *aliasent == 0 || *aliasent == '*';
159 }
160
161
162 int
163 alias (char *file)
164 {
165 int i;
166 char *bp, *cp, *pp;
167 char lc, *ap;
168 struct aka *ak = NULL;
169 FILE *fp;
170
171 if (*file != '/'
172 && !has_prefix(file, "./") && !has_prefix(file, "../"))
173 file = etcpath (file);
174 if ((fp = fopen (file, "r")) == NULL) {
175 akerrst = file;
176 return AK_NOFILE;
177 }
178
179 while (vfgets (fp, &ap) == OK) {
180 bp = ap;
181 switch (*(pp = scanp (bp))) {
182 case '<': /* recurse a level */
183 if (!*(cp = getp (pp + 1))) {
184 akerrst = "'<' without alias-file";
185 fclose (fp);
186 return AK_ERROR;
187 }
188 if ((i = alias (cp)) != AK_OK) {
189 fclose (fp);
190 return i;
191 }
192 continue;
193 case ':': /* comment */
194 case ';':
195 case '#':
196 case 0:
197 continue;
198 }
199
200 akerrst = bp;
201 if (!*(cp = seekp (pp, &lc, &ap))) {
202 fclose (fp);
203 return AK_ERROR;
204 }
205 if (!(ak = akalloc (cp))) {
206 fclose (fp);
207 return AK_LIMIT;
208 }
209 switch (lc) {
210 case ':':
211 ak->ak_visible = false;
212 break;
213
214 case ';':
215 ak->ak_visible = true;
216 break;
217
218 default:
219 fclose (fp);
220 return AK_ERROR;
221 }
222
223 switch (*(pp = scanp (ap))) {
224 case 0: /* EOL */
225 fclose (fp);
226 return AK_ERROR;
227
228 case '<': /* read values from file */
229 if (!*(cp = getp (pp + 1))) {
230 fclose (fp);
231 return AK_ERROR;
232 }
233 if (!addfile (ak, cp)) {
234 fclose (fp);
235 return AK_NOFILE;
236 }
237 break;
238
239 default: /* list */
240 while ((cp = getalias (pp)))
241 add_aka (ak, cp);
242 break;
243 }
244 }
245
246 fclose (fp);
247 return AK_OK;
248 }
249
250
251 char *
252 akerror (int i)
253 {
254 static char buffer[BUFSIZ];
255
256 switch (i) {
257 case AK_NOFILE:
258 snprintf (buffer, sizeof(buffer), "unable to read '%s'", akerrst);
259 break;
260
261 case AK_ERROR:
262 snprintf (buffer, sizeof(buffer), "error in line '%s'", akerrst);
263 break;
264
265 case AK_LIMIT:
266 snprintf (buffer, sizeof(buffer), "out of memory while on '%s'", akerrst);
267 break;
268
269 default:
270 snprintf (buffer, sizeof(buffer), "unknown error (%d)", i);
271 break;
272 }
273
274 return buffer;
275 }
276
277
278 static char *
279 scanp (char *p)
280 {
281 while (isspace ((unsigned char) *p))
282 p++;
283 return p;
284 }
285
286
287 static char *
288 getp (char *p)
289 {
290 char *cp = scanp (p);
291
292 p = cp;
293 while (!isspace ((unsigned char) *cp) && *cp)
294 cp++;
295 *cp = 0;
296
297 return p;
298 }
299
300
301 static char *
302 seekp (char *p, char *c, char **a)
303 {
304 char *cp;
305
306 p = cp = scanp (p);
307 while (!isspace ((unsigned char) *cp) && *cp && *cp != ':' && *cp != ';')
308 cp++;
309 *c = *cp;
310 *cp++ = 0;
311 *a = cp;
312
313 return p;
314 }
315
316
317 static int
318 addfile (struct aka *ak, char *file)
319 {
320 char *cp;
321 char buffer[BUFSIZ];
322 FILE *fp;
323
324 if (!(fp = fopen (etcpath (file), "r"))) {
325 akerrst = file;
326 return 0;
327 }
328
329 while (fgets (buffer, sizeof buffer, fp))
330 while ((cp = getalias (buffer)))
331 add_aka (ak, cp);
332
333 fclose (fp);
334 return 1;
335 }
336
337
338 static char *
339 getalias (char *addrs)
340 {
341 char *pp, *qp;
342 static char *cp = NULL;
343
344 if (cp == NULL)
345 cp = addrs;
346 else if (*cp == 0)
347 return cp = NULL;
348
349 /* Remove leading any space from the address. */
350 for (pp = cp; isspace ((unsigned char) *pp); pp++)
351 continue;
352 if (*pp == 0)
353 return cp = NULL;
354 /* Find the end of the address. */
355 for (qp = pp; *qp != 0 && *qp != ','; qp++)
356 continue;
357 /* Set cp to point to the remainder of the addresses. */
358 if (*qp == ',')
359 *qp++ = 0;
360 for (cp = qp, qp--; qp > pp; qp--)
361 if (*qp != 0) {
362 if (isspace ((unsigned char) *qp))
363 *qp = 0;
364 else
365 break;
366 }
367
368 return pp;
369 }
370
371
372 static void
373 add_aka (struct aka *ak, char *pp)
374 {
375 struct adr *ad, *ld;
376
377 for (ad = ak->ak_addr, ld = NULL; ad; ld = ad, ad = ad->ad_next)
378 if (!strcmp (pp, ad->ad_text))
379 return;
380
381 NEW(ad);
382 ad->ad_text = mh_xstrdup(pp);
383 ad->ad_local = strchr(pp, '@') == NULL && strchr(pp, '!') == NULL;
384 ad->ad_next = NULL;
385 if (ak->ak_addr)
386 ld->ad_next = ad;
387 else
388 ak->ak_addr = ad;
389 }
390
391
392 static struct aka *
393 akalloc (char *id)
394 {
395 struct aka *p;
396
397 NEW(p);
398 p->ak_name = getcpy (id);
399 p->ak_visible = false;
400 p->ak_addr = NULL;
401 p->ak_next = NULL;
402 if (akatail != NULL)
403 akatail->ak_next = p;
404 if (akahead == NULL)
405 akahead = p;
406 akatail = p;
407
408 return p;
409 }