]>
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
;
25 char *akresult (struct aka
*);
26 char *akvalue (char *);
29 static char *akval (struct aka
*, char *);
30 static int aleq (char *, char *);
31 static char *scanp (char *);
32 static char *getp (char *);
33 static char *seekp (char *, char *, char **);
34 static int addfile (struct aka
*, char *);
35 static char *getalias (char *);
36 static void add_aka (struct aka
*, char *);
37 static struct aka
*akalloc (char *);
40 /* Do mh alias substitution on 's' and return the results. */
50 v
= akval (akahead
, s
);
65 akresult (struct aka
*ak
)
67 char *cp
= NULL
, *dp
, *pp
;
70 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
71 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
72 : getcpy (ad
->ad_text
);
76 cp
= concat (cp
, ",", pp
, NULL
);
85 akvis
= ak
->ak_visible
;
91 akval (struct aka
*ak
, char *s
)
96 /* It'd be tempting to check for a trailing semicolon and remove
97 it. But that would break the EXMH alias parser on what would
98 then be valid expressions:
99 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
102 for (; ak
; ak
= ak
->ak_next
) {
103 if (aleq (s
, ak
->ak_name
)) {
104 return akresult (ak
);
107 if (strchr (s
, ':')) {
108 /* The first address in a blind list will contain the
109 alias name, so try to match, but just with just the
110 address (not including the list name). If there's a
111 match, then replace the alias part with its
114 char *name
= getname (s
);
118 /* s is of the form "Blind list: address". If address
119 is an alias, expand it. */
120 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
122 if (mp
&& mp
->m_ingrp
) {
123 char *gname
= add (mp
->m_gname
, NULL
);
125 /* FIXME: gname must be true; add() never returns NULL.
126 * Is some other test required? */
127 if (gname
&& aleq (name
, ak
->ak_name
)) {
129 cp
= concat (gname
, akresult (ak
), NULL
);
137 /* Need to flush getname after use. */
138 while (getname ("")) continue;
146 return mh_xstrdup(s
);
151 aleq (char *string
, char *aliasent
)
155 while ((c
= *string
++)) {
156 if (*aliasent
== '*')
158 if (tolower((unsigned char)c
) != tolower((unsigned char)*aliasent
))
163 return (*aliasent
== 0 || *aliasent
== '*');
173 struct aka
*ak
= NULL
;
177 && !has_prefix(file
, "./") && !has_prefix(file
, "../"))
178 file
= etcpath (file
);
179 if ((fp
= fopen (file
, "r")) == NULL
) {
184 while (vfgets (fp
, &ap
) == OK
) {
186 switch (*(pp
= scanp (bp
))) {
187 case '<': /* recurse a level */
188 if (!*(cp
= getp (pp
+ 1))) {
189 akerrst
= "'<' without alias-file";
193 if ((i
= alias (cp
)) != AK_OK
) {
198 case ':': /* comment */
206 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
210 if (!(ak
= akalloc (cp
))) {
228 switch (*(pp
= scanp (ap
))) {
233 case '<': /* read values from file */
234 if (!*(cp
= getp (pp
+ 1))) {
238 if (!addfile (ak
, cp
)) {
245 while ((cp
= getalias (pp
)))
259 static char buffer
[BUFSIZ
];
263 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
267 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
271 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
275 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
286 while (isspace ((unsigned char) *p
))
295 char *cp
= scanp (p
);
298 while (!isspace ((unsigned char) *cp
) && *cp
)
307 seekp (char *p
, char *c
, char **a
)
312 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
323 addfile (struct aka
*ak
, char *file
)
329 if (!(fp
= fopen (etcpath (file
), "r"))) {
334 while (fgets (buffer
, sizeof buffer
, fp
))
335 while ((cp
= getalias (buffer
)))
344 getalias (char *addrs
)
347 static char *cp
= NULL
;
355 /* Remove leading any space from the address. */
356 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
360 /* Find the end of the address. */
361 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
363 /* Set cp to point to the remainder of the addresses. */
366 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
368 if (isspace ((unsigned char) *qp
))
379 add_aka (struct aka
*ak
, char *pp
)
383 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
384 if (!strcmp (pp
, ad
->ad_text
))
388 ad
->ad_text
= mh_xstrdup(pp
);
389 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
404 p
->ak_name
= getcpy (id
);
409 akatail
->ak_next
= p
;