]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
b5935f6072fdbc571097f79b5eea0ab6dbcb54a2
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/getcpy.h"
10 #include "h/aliasbr.h"
11 #include "h/addrsbr.h"
18 struct aka
*akahead
= NULL
;
19 struct aka
*akatail
= NULL
;
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 *);
35 /* Do mh alias substitution on 's' and return the results. */
45 v
= akval (akahead
, s
);
60 akresult (struct aka
*ak
)
62 char *cp
= NULL
, *dp
, *pp
;
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
);
71 cp
= concat (cp
, ",", pp
, NULL
);
80 akvis
= ak
->ak_visible
;
86 akval (struct aka
*ak
, char *s
)
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
97 for (; ak
; ak
= ak
->ak_next
) {
98 if (aleq (s
, ak
->ak_name
)) {
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
109 char *name
= getname (s
);
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);
117 if (mp
&& mp
->m_ingrp
) {
118 char *gname
= mh_xstrdup(FENDNULL(mp
->m_gname
));
120 /* FIXME: gname must be true; add() never returns NULL.
121 * Is some other test required? */
122 if (gname
&& aleq (name
, ak
->ak_name
)) {
124 cp
= concat (gname
, akresult (ak
), NULL
);
132 /* Need to flush getname after use. */
133 while (getname ("")) continue;
141 return mh_xstrdup(s
);
146 aleq (char *string
, char *aliasent
)
150 while ((c
= *string
++)) {
151 if (*aliasent
== '*')
153 if (tolower((unsigned char)c
) != tolower((unsigned char)*aliasent
))
158 return *aliasent
== 0 || *aliasent
== '*';
168 struct aka
*ak
= NULL
;
172 && !has_prefix(file
, "./") && !has_prefix(file
, "../"))
173 file
= etcpath (file
);
174 if ((fp
= fopen (file
, "r")) == NULL
) {
179 while (vfgets (fp
, &ap
) == OK
) {
181 switch (*(pp
= scanp (bp
))) {
182 case '<': /* recurse a level */
183 if (!*(cp
= getp (pp
+ 1))) {
184 akerrst
= "'<' without alias-file";
188 if ((i
= alias (cp
)) != AK_OK
) {
193 case ':': /* comment */
201 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
205 if (!(ak
= akalloc (cp
))) {
211 ak
->ak_visible
= false;
215 ak
->ak_visible
= true;
223 switch (*(pp
= scanp (ap
))) {
228 case '<': /* read values from file */
229 if (!*(cp
= getp (pp
+ 1))) {
233 if (!addfile (ak
, cp
)) {
240 while ((cp
= getalias (pp
)))
254 static char buffer
[BUFSIZ
];
258 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
262 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
266 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
270 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
281 while (isspace ((unsigned char) *p
))
290 char *cp
= scanp (p
);
293 while (!isspace ((unsigned char) *cp
) && *cp
)
302 seekp (char *p
, char *c
, char **a
)
307 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
318 addfile (struct aka
*ak
, char *file
)
324 if (!(fp
= fopen (etcpath (file
), "r"))) {
329 while (fgets (buffer
, sizeof buffer
, fp
))
330 while ((cp
= getalias (buffer
)))
339 getalias (char *addrs
)
342 static char *cp
= NULL
;
349 /* Remove leading any space from the address. */
350 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
354 /* Find the end of the address. */
355 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
357 /* Set cp to point to the remainder of the addresses. */
360 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
362 if (isspace ((unsigned char) *qp
))
373 add_aka (struct aka
*ak
, char *pp
)
377 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
378 if (!strcmp (pp
, ad
->ad_text
))
382 ad
->ad_text
= mh_xstrdup(pp
);
383 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
398 p
->ak_name
= getcpy (id
);
399 p
->ak_visible
= false;
403 akatail
->ak_next
= p
;