]>
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.
9 #include "sbr/vfgets.h"
10 #include "sbr/getcpy.h"
11 #include "h/aliasbr.h"
12 #include "h/addrsbr.h"
19 struct aka
*akahead
= NULL
;
20 struct aka
*akatail
= NULL
;
25 static char *akval (struct aka
*, char *);
26 static bool aleq (char *, char *) PURE
;
27 static char *scanp (char *) PURE
;
28 static char *getp (char *);
29 static char *seekp (char *, char *, char **);
30 static int addfile (struct aka
*, char *);
31 static char *getalias (char *);
32 static void add_aka (struct aka
*, char *);
33 static struct aka
*akalloc (char *);
36 /* Do mh alias substitution on 's' and return the results. */
46 v
= akval (akahead
, s
);
61 akresult (struct aka
*ak
)
63 char *cp
= NULL
, *dp
, *pp
;
66 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
67 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
68 : getcpy (ad
->ad_text
);
72 cp
= concat (cp
, ",", pp
, NULL
);
81 akvis
= ak
->ak_visible
;
87 akval (struct aka
*ak
, char *s
)
92 /* It'd be tempting to check for a trailing semicolon and remove
93 it. But that would break the EXMH alias parser on what would
94 then be valid expressions:
95 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
98 for (; ak
; ak
= ak
->ak_next
) {
99 if (aleq (s
, ak
->ak_name
)) {
100 return akresult (ak
);
103 if (strchr (s
, ':')) {
104 /* The first address in a blind list will contain the
105 alias name, so try to match, but just with just the
106 address (not including the list name). If there's a
107 match, then replace the alias part with its
110 char *name
= getname (s
);
114 /* s is of the form "Blind list: address". If address
115 is an alias, expand it. */
116 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
118 if (mp
&& mp
->m_ingrp
) {
119 char *gname
= mh_xstrdup(FENDNULL(mp
->m_gname
));
121 /* FIXME: gname must be true; add() never returns NULL.
122 * Is some other test required? */
123 if (gname
&& aleq (name
, ak
->ak_name
)) {
125 cp
= concat (gname
, akresult (ak
), NULL
);
133 /* Need to flush getname after use. */
134 while (getname ("")) continue;
142 return mh_xstrdup(s
);
147 aleq (char *string
, char *aliasent
)
151 while ((c
= *string
++)) {
152 if (*aliasent
== '*')
154 if (tolower((unsigned char)c
) != tolower((unsigned char)*aliasent
))
159 return *aliasent
== 0 || *aliasent
== '*';
169 struct aka
*ak
= NULL
;
173 && !has_prefix(file
, "./") && !has_prefix(file
, "../"))
174 file
= etcpath (file
);
175 if ((fp
= fopen (file
, "r")) == NULL
) {
180 while (vfgets (fp
, &ap
) == OK
) {
182 switch (*(pp
= scanp (bp
))) {
183 case '<': /* recurse a level */
184 if (!*(cp
= getp (pp
+ 1))) {
185 akerrst
= "'<' without alias-file";
189 if ((i
= alias (cp
)) != AK_OK
) {
194 case ':': /* comment */
202 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
206 if (!(ak
= akalloc (cp
))) {
212 ak
->ak_visible
= false;
216 ak
->ak_visible
= true;
224 switch (*(pp
= scanp (ap
))) {
229 case '<': /* read values from file */
230 if (!*(cp
= getp (pp
+ 1))) {
234 if (!addfile (ak
, cp
)) {
241 while ((cp
= getalias (pp
)))
255 static char buffer
[BUFSIZ
];
259 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
263 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
267 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
271 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
282 while (isspace ((unsigned char) *p
))
291 char *cp
= scanp (p
);
294 while (!isspace ((unsigned char) *cp
) && *cp
)
303 seekp (char *p
, char *c
, char **a
)
308 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
319 addfile (struct aka
*ak
, char *file
)
325 if (!(fp
= fopen (etcpath (file
), "r"))) {
330 while (fgets (buffer
, sizeof buffer
, fp
))
331 while ((cp
= getalias (buffer
)))
340 getalias (char *addrs
)
343 static char *cp
= NULL
;
350 /* Remove leading any space from the address. */
351 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
355 /* Find the end of the address. */
356 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
358 /* Set cp to point to the remainder of the addresses. */
361 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
363 if (isspace ((unsigned char) *qp
))
374 add_aka (struct aka
*ak
, char *pp
)
378 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
379 if (!strcmp (pp
, ad
->ad_text
))
383 ad
->ad_text
= mh_xstrdup(pp
);
384 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
399 p
->ak_name
= getcpy (id
);
400 p
->ak_visible
= false;
404 akatail
->ak_next
= p
;