]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
1 /* aliasbr.c -- new aliasing mechanism
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.
10 #include <h/addrsbr.h>
17 struct aka
*akahead
= NULL
;
18 struct aka
*akatail
= NULL
;
23 static char *akval (struct aka
*, char *);
24 static bool aleq (char *, char *) PURE
;
25 static char *scanp (char *) PURE
;
26 static char *getp (char *);
27 static char *seekp (char *, char *, char **);
28 static int addfile (struct aka
*, char *);
29 static char *getalias (char *);
30 static void add_aka (struct aka
*, char *);
31 static struct aka
*akalloc (char *);
34 /* Do mh alias substitution on 's' and return the results. */
44 v
= akval (akahead
, s
);
59 akresult (struct aka
*ak
)
61 char *cp
= NULL
, *dp
, *pp
;
64 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
65 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
66 : getcpy (ad
->ad_text
);
70 cp
= concat (cp
, ",", pp
, NULL
);
79 akvis
= ak
->ak_visible
;
85 akval (struct aka
*ak
, char *s
)
90 /* It'd be tempting to check for a trailing semicolon and remove
91 it. But that would break the EXMH alias parser on what would
92 then be valid expressions:
93 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
96 for (; ak
; ak
= ak
->ak_next
) {
97 if (aleq (s
, ak
->ak_name
)) {
101 if (strchr (s
, ':')) {
102 /* The first address in a blind list will contain the
103 alias name, so try to match, but just with just the
104 address (not including the list name). If there's a
105 match, then replace the alias part with its
108 char *name
= getname (s
);
112 /* s is of the form "Blind list: address". If address
113 is an alias, expand it. */
114 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
116 if (mp
&& mp
->m_ingrp
) {
117 char *gname
= mh_xstrdup(FENDNULL(mp
->m_gname
));
119 /* FIXME: gname must be true; add() never returns NULL.
120 * Is some other test required? */
121 if (gname
&& aleq (name
, ak
->ak_name
)) {
123 cp
= concat (gname
, akresult (ak
), NULL
);
131 /* Need to flush getname after use. */
132 while (getname ("")) continue;
140 return mh_xstrdup(s
);
145 aleq (char *string
, char *aliasent
)
149 while ((c
= *string
++)) {
150 if (*aliasent
== '*')
152 if (tolower((unsigned char)c
) != tolower((unsigned char)*aliasent
))
157 return (*aliasent
== 0 || *aliasent
== '*');
167 struct aka
*ak
= NULL
;
171 && !has_prefix(file
, "./") && !has_prefix(file
, "../"))
172 file
= etcpath (file
);
173 if ((fp
= fopen (file
, "r")) == NULL
) {
178 while (vfgets (fp
, &ap
) == OK
) {
180 switch (*(pp
= scanp (bp
))) {
181 case '<': /* recurse a level */
182 if (!*(cp
= getp (pp
+ 1))) {
183 akerrst
= "'<' without alias-file";
187 if ((i
= alias (cp
)) != AK_OK
) {
192 case ':': /* comment */
200 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
204 if (!(ak
= akalloc (cp
))) {
210 ak
->ak_visible
= false;
214 ak
->ak_visible
= true;
222 switch (*(pp
= scanp (ap
))) {
227 case '<': /* read values from file */
228 if (!*(cp
= getp (pp
+ 1))) {
232 if (!addfile (ak
, cp
)) {
239 while ((cp
= getalias (pp
)))
253 static char buffer
[BUFSIZ
];
257 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
261 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
265 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
269 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
280 while (isspace ((unsigned char) *p
))
289 char *cp
= scanp (p
);
292 while (!isspace ((unsigned char) *cp
) && *cp
)
301 seekp (char *p
, char *c
, char **a
)
306 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
317 addfile (struct aka
*ak
, char *file
)
323 if (!(fp
= fopen (etcpath (file
), "r"))) {
328 while (fgets (buffer
, sizeof buffer
, fp
))
329 while ((cp
= getalias (buffer
)))
338 getalias (char *addrs
)
341 static char *cp
= NULL
;
348 /* Remove leading any space from the address. */
349 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
353 /* Find the end of the address. */
354 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
356 /* Set cp to point to the remainder of the addresses. */
359 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
361 if (isspace ((unsigned char) *qp
))
372 add_aka (struct aka
*ak
, char *pp
)
376 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
377 if (!strcmp (pp
, ad
->ad_text
))
381 ad
->ad_text
= mh_xstrdup(pp
);
382 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
397 p
->ak_name
= getcpy (id
);
398 p
->ak_visible
= false;
402 akatail
->ak_next
= p
;