1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Name lists, alternates and groups: aliases, mailing lists, shortcuts.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define n_FILE nam_a_grp
38 #ifndef HAVE_AMALGAMATION
43 /* Main types (bits not values for easier testing only) */
47 GT_MASK
= GT_ALIAS
| GT_MLIST
| GT_SHORTCUT
,
49 /* Subtype bits and flags */
53 /* Extended type mask to be able to reflect what we really have; i.e., mlist
54 * can have GT_REGEX if they are subscribed or not, but `mlsubscribe' should
55 * print out only GT_MLIST which have the GT_SUBSCRIBE attribute set */
56 GT_PRINT_MASK
= GT_MASK
| GT_SUBSCRIBE
61 size_t g_subclass_off
; /* of "subclass" in .g_id (if any) */
62 ui8_t g_type
; /* enum group_type */
63 /* Identifying name, of variable size. Dependent on actual "subtype" more
64 * data follows thereafter, but note this is always used (i.e., for regular
65 * expression entries this is still set to the plain string) */
66 char g_id
[n_VFIELD_SIZE(-1)];
68 #define GP_TO_SUBCLASS(X,G) \
70 union __group_subclass {void *gs_vp; char *gs_cp;} __gs__;\
71 __gs__.gs_cp = (char*)n_UNCONST(G) + (G)->g_subclass_off;\
75 struct grp_names_head
{
76 struct grp_names
*gnh_head
;
80 struct grp_names
*gn_next
;
81 char gn_id
[n_VFIELD_SIZE(0)];
86 struct grp_regex
*gr_last
;
87 struct grp_regex
*gr_next
;
88 struct group
*gr_mygroup
; /* xxx because lists use grp_regex*! ?? */
89 size_t gr_hits
; /* Number of times this group matched */
95 struct group
**gl_htable
;
96 struct group
**gl_slot
;
97 struct group
*gl_slot_last
;
98 struct group
*gl_group
;
101 /* List of alternate names of user */
102 static char **_altnames
;
105 static struct group
*_alias_heads
[HSHSIZE
]; /* TODO dynamic hash */
107 /* `mlist', `mlsubscribe'. Anything is stored in the hashmap.. */
108 static struct group
*_mlist_heads
[HSHSIZE
]; /* TODO dynamic hash */
110 /* ..but entries which have GT_REGEX set are false lookups and will really be
111 * accessed via sequential lists instead, which are type-specific for better
112 * performance, but also to make it possible to have ".*@xy.org" as a mlist
113 * and "(one|two)@xy.org" as a mlsubscription.
114 * These lists use a bit of QOS optimization in that a matching group will
115 * become relinked as the new list head if its hit count is
116 * (>= ((xy_hits / _xy_size) >> 2))
117 * Note that the hit counts only count currently linked in nodes.. */
119 static struct grp_regex
*_mlist_regex
, *_mlsub_regex
;
120 static size_t _mlist_size
, _mlist_hits
, _mlsub_size
, _mlsub_hits
;
124 static struct group
*_shortcut_heads
[HSHSIZE
]; /* TODO dynamic hash */
126 /* Same name, while taking care for *allnet*? */
127 static bool_t
_same_name(char const *n1
, char const *n2
);
129 /* Delete the given name from a namelist */
130 static struct name
* delname(struct name
*np
, char const *name
);
132 /* Put another node onto a list of names and return the list */
133 static struct name
* put(struct name
*list
, struct name
*node
);
135 /* Grab a single name (liberal name) */
136 static char const * yankname(char const *ap
, char *wbuf
,
137 char const *separators
, int keepcomms
);
139 /* Extraction multiplexer that splits an input line to names */
140 static struct name
* _extract1(char const *line
, enum gfield ntype
,
141 char const *separators
, bool_t keepcomms
);
143 /* Recursively expand a alias name. Limit expansion to some fixed level.
144 * Direct recursion is not expanded for convenience */
145 static struct name
* _gexpand(size_t level
, struct name
*nlist
,
146 struct group
*gp
, bool_t metoo
, int ntype
);
148 /* Lookup a group, return it or NULL, fill in glp anyway */
149 static struct group
* _group_lookup(enum group_type gt
,
150 struct group_lookup
*glp
, char const *id
);
152 /* Easier-to-use wrapper around _group_lookup() */
153 static struct group
* _group_find(enum group_type gt
, char const *id
);
155 /* Iteration: go to the first group, which also inits the iterator. A valid
156 * iterator can be stepped via _next(). A NULL return means no (more) groups
157 * to be iterated exist, in which case only glp->gl_group is set (NULL) */
158 static struct group
* _group_go_first(enum group_type gt
,
159 struct group_lookup
*glp
);
160 static struct group
* _group_go_next(struct group_lookup
*glp
);
162 /* Fetch the group id, create it as necessary */
163 static struct group
* _group_fetch(enum group_type gt
, char const *id
,
166 /* "Intelligent" delete which handles a "*" id, too;
167 * returns a true boolean if a group was deleted, and always succeeds for "*" */
168 static bool_t
_group_del(enum group_type gt
, char const *id
);
170 static struct group
* __group_del(struct group_lookup
*glp
);
171 static void __names_del(struct group
*gp
);
173 /* Print all groups of the given type, alphasorted */
174 static void _group_print_all(enum group_type gt
);
176 static int __group_print_qsorter(void const *a
, void const *b
);
178 /* Really print a group, actually. Return number of written lines */
179 static size_t _group_print(struct group
const *gp
, FILE *fo
);
181 /* Multiplexers for list and subscribe commands */
182 static int _mlmux(enum group_type gt
, char **argv
);
183 static int _unmlmux(enum group_type gt
, char **argv
);
185 /* Relinkers for the sequential match lists */
187 static void _mlmux_linkin(struct group
*gp
);
188 static void _mlmux_linkout(struct group
*gp
);
189 # define _MLMUX_LINKIN(GP) \
190 do if ((GP)->g_type & GT_REGEX) _mlmux_linkin(GP); while (0)
191 # define _MLMUX_LINKOUT(GP) \
192 do if ((GP)->g_type & GT_REGEX) _mlmux_linkout(GP); while (0)
194 # define _MLMUX_LINKIN(GP)
195 # define _MLMUX_LINKOUT(GP)
199 _same_name(char const *n1
, char const *n2
)
205 if (ok_blook(allnet
)) {
213 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
216 rv
= !asccasecmp(n1
, n2
);
223 delname(struct name
*np
, char const *name
)
228 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
229 if (_same_name(p
->n_name
, name
)) {
230 if (p
->n_blink
== NULL
) {
231 if (p
->n_flink
!= NULL
)
232 p
->n_flink
->n_blink
= NULL
;
236 if (p
->n_flink
== NULL
) {
237 if (p
->n_blink
!= NULL
)
238 p
->n_blink
->n_flink
= NULL
;
241 p
->n_blink
->n_flink
= p
->n_flink
;
242 p
->n_flink
->n_blink
= p
->n_blink
;
249 put(struct name
*list
, struct name
*node
)
252 node
->n_flink
= list
;
253 node
->n_blink
= NULL
;
255 list
->n_blink
= node
;
261 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
264 char *wp
, c
, inquote
, lc
, lastsp
;
269 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
270 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
277 /* Parse a full name: TODO RFC 5322
278 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
279 * - Skip entire (nested) comments
280 * - In non-quote, non-comment, join adjacent space to a single SP
281 * - Understand separators only in non-quote, non-comment context,
282 * and only if not part of a *quoted-pair* (XXX too liberal) */
284 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
293 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
299 if (inquote
|| lc
== '\\') {
307 cp
= skip_comment(cp
+ 1);
315 if (strchr(separators
, c
) != NULL
)
319 lastsp
= blankchar(c
);
333 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
336 struct name
*topp
, *np
, *t
;
342 if (line
== NULL
|| *line
== '\0')
347 nbuf
= smalloc(strlen(line
) +1);
348 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
349 t
= nalloc(nbuf
, ntype
);
364 _gexpand(size_t level
, struct name
*nlist
, struct group
*gp
, bool_t metoo
,
368 struct grp_names_head
*gnhp
;
369 struct grp_names
*gnp
;
372 if (UICMP(z
, level
++, >, MAXEXP
)) {
373 n_err(_("Expanding alias to depth larger than %d\n"), MAXEXP
);
377 GP_TO_SUBCLASS(gnhp
, gp
);
378 logname
= ok_vlook(LOGNAME
);
379 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
; gnp
= gnp
->gn_next
) {
383 /* FIXME we do not really support leading backslash quoting do we??? */
384 if (*(cp
= gnp
->gn_id
) == '\\' || !strcmp(cp
, gp
->g_id
))
387 if ((ngp
= _group_find(GT_ALIAS
, cp
)) != NULL
) {
388 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
389 * to a full address specification; aliases cannot be empty */
390 struct grp_names_head
*ngnhp
;
391 GP_TO_SUBCLASS(ngnhp
, ngp
);
393 assert(ngnhp
->gnh_head
!= NULL
);
394 if (metoo
|| ngnhp
->gnh_head
->gn_next
!= NULL
||
395 !_same_name(cp
, logname
))
396 nlist
= _gexpand(level
, nlist
, ngp
, metoo
, ntype
);
400 /* Here we should allow to expand to itself if only person in alias */
402 if (metoo
|| gnhp
->gnh_head
->gn_next
== NULL
|| !_same_name(cp
, logname
))
403 nlist
= put(nlist
, nalloc(cp
, ntype
| GFULL
));
410 static struct group
*
411 _group_lookup(enum group_type gt
, struct group_lookup
*glp
, char const *id
)
413 struct group
*lgp
, *gp
;
418 gp
= *(glp
->gl_htable
= glp
->gl_slot
=
419 ((gt
& GT_ALIAS
? _alias_heads
:
420 (gt
& GT_MLIST
? _mlist_heads
:
421 (/*gt & GT_SHORTCUT ?*/ _shortcut_heads
/*: NULL */))) +
422 torek_hash(id
) % HSHSIZE
));
424 for (; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
425 if ((gp
->g_type
& gt
) && *gp
->g_id
== *id
&& !strcmp(gp
->g_id
, id
))
428 glp
->gl_slot_last
= lgp
;
434 static struct group
*
435 _group_find(enum group_type gt
, char const *id
)
437 struct group_lookup gl
;
441 gp
= _group_lookup(gt
, &gl
, id
);
446 static struct group
*
447 _group_go_first(enum group_type gt
, struct group_lookup
*glp
)
449 struct group
**gpa
, *gp
;
453 for (glp
->gl_htable
= gpa
= (gt
& GT_ALIAS
? _alias_heads
:
454 (gt
& GT_MLIST
? _mlist_heads
:
455 (/*gt & GT_SHORTCUT ?*/ _shortcut_heads
/*: NULL */))), i
= 0;
456 i
< HSHSIZE
; ++gpa
, ++i
)
457 if ((gp
= *gpa
) != NULL
) {
463 glp
->gl_group
= gp
= NULL
;
465 glp
->gl_slot_last
= NULL
;
470 static struct group
*
471 _group_go_next(struct group_lookup
*glp
)
473 struct group
*gp
, **gpa
;
476 if ((gp
= glp
->gl_group
->g_next
) != NULL
)
477 glp
->gl_slot_last
= glp
->gl_group
;
479 glp
->gl_slot_last
= NULL
;
480 for (gpa
= glp
->gl_htable
+ HSHSIZE
; ++glp
->gl_slot
< gpa
;)
481 if ((gp
= *glp
->gl_slot
) != NULL
)
489 static struct group
*
490 _group_fetch(enum group_type gt
, char const *id
, size_t addsz
)
492 struct group_lookup gl
;
497 if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
)
501 i
= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
) + l
);
502 switch (gt
& GT_MASK
) {
504 addsz
= sizeof(struct grp_names_head
);
508 if (n_is_maybe_regex(id
)) {
509 addsz
= sizeof(struct grp_regex
);
518 gp
= smalloc(i
+ addsz
);
519 gp
->g_subclass_off
= i
;
521 memcpy(gp
->g_id
, id
, l
);
524 struct grp_names_head
*gnhp
;
526 GP_TO_SUBCLASS(gnhp
, gp
);
527 gnhp
->gnh_head
= NULL
;
530 else if (/*(gt & GT_MLIST) &&*/ gt
& GT_REGEX
) {
531 struct grp_regex
*grp
;
532 GP_TO_SUBCLASS(grp
, gp
);
534 if (regcomp(&grp
->gr_regex
, id
, REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) {
535 n_err(_("Invalid regular expression: %s\n"), id
);
540 grp
->gr_mygroup
= gp
;
545 gp
->g_next
= *gl
.gl_slot
;
553 _group_del(enum group_type gt
, char const *id
)
555 enum group_type xgt
= gt
& GT_MASK
;
556 struct group_lookup gl
;
560 /* Delete 'em all? */
561 if (id
[0] == '*' && id
[1] == '\0') {
562 for (gp
= _group_go_first(gt
, &gl
); gp
!= NULL
;)
563 gp
= (gp
->g_type
& xgt
) ? __group_del(&gl
) : _group_go_next(&gl
);
564 gp
= (struct group
*)TRU1
;
565 } else if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
) {
566 if (gp
->g_type
& xgt
)
575 static struct group
*
576 __group_del(struct group_lookup
*glp
)
578 struct group
*x
, *gp
;
581 /* Overly complicated: link off this node, step ahead to next.. */
583 if ((gp
= glp
->gl_slot_last
) != NULL
) {
584 gp
= (gp
->g_next
= x
->g_next
);
586 glp
->gl_slot_last
= NULL
;
587 gp
= (*glp
->gl_slot
= x
->g_next
);
590 struct group
**gpa
= glp
->gl_htable
+ HSHSIZE
;
592 while (++glp
->gl_slot
< gpa
)
593 if ((gp
= *glp
->gl_slot
) != NULL
)
599 if (x
->g_type
& GT_ALIAS
)
602 else if (/*(x->g_type & GT_MLIST) &&*/ x
->g_type
& GT_REGEX
) {
603 struct grp_regex
*grp
;
604 GP_TO_SUBCLASS(grp
, x
);
606 regfree(&grp
->gr_regex
);
617 __names_del(struct group
*gp
)
619 struct grp_names_head
*gnhp
;
620 struct grp_names
*gnp
;
623 GP_TO_SUBCLASS(gnhp
, gp
);
624 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
;) {
625 struct grp_names
*x
= gnp
;
633 _group_print_all(enum group_type gt
)
637 struct group
const *gp
;
644 xgt
= gt
& GT_PRINT_MASK
;
645 gpa
= (xgt
& GT_ALIAS
? _alias_heads
646 : (xgt
& GT_MLIST
? _mlist_heads
647 : (/*xgt & GT_SHORTCUT ?*/ _shortcut_heads
650 for (h
= 0, i
= 1; h
< HSHSIZE
; ++h
)
651 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
652 if ((gp
->g_type
& xgt
) == xgt
)
654 ida
= salloc(i
* sizeof *ida
);
656 for (i
= h
= 0; h
< HSHSIZE
; ++h
)
657 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
658 if ((gp
->g_type
& xgt
) == xgt
)
663 qsort(ida
, i
, sizeof *ida
, &__group_print_qsorter
);
665 if ((fp
= Ftmp(NULL
, "prgroup", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
669 for (i
= 0; ida
[i
] != NULL
; ++i
)
670 lines
+= _group_print(_group_find(gt
, ida
[i
]), fp
);
673 if (gt
& GT_SUBSCRIBE
)
674 i
= (ui32_t
)_mlsub_size
, h
= (ui32_t
)_mlsub_hits
;
676 i
= (ui32_t
)_mlist_size
, h
= (ui32_t
)_mlist_hits
;
677 if (i
> 0 && (options
& OPT_D_V
)){
678 fprintf(fp
, _("# %s list regex(7) total: %u entries, %u hits\n"),
679 (gt
& GT_SUBSCRIBE
? _("Subscribed") : _("Non-subscribed")),
687 page_or_print(fp
, lines
);
694 __group_print_qsorter(void const *a
, void const *b
)
699 rv
= strcmp(*(char**)n_UNCONST(a
), *(char**)n_UNCONST(b
));
705 _group_print(struct group
const *gp
, FILE *fo
)
713 if (gp
->g_type
& GT_ALIAS
) {
714 struct grp_names_head
*gnhp
;
715 struct grp_names
*gnp
;
717 fprintf(fo
, "alias %s ", gp
->g_id
);
719 GP_TO_SUBCLASS(gnhp
, gp
);
720 if ((gnp
= gnhp
->gnh_head
) != NULL
) { /* xxx always 1+ entries */
722 struct grp_names
*x
= gnp
;
724 fprintf(fo
, " \"%s\"", string_quote(x
->gn_id
)); /* TODO shexp */
725 } while (gnp
!= NULL
);
728 } else if (gp
->g_type
& GT_MLIST
) {
730 if ((gp
->g_type
& GT_REGEX
) && (options
& OPT_D_V
)){
732 struct grp_regex
*grp
,
733 *lp
= (gp
->g_type
& GT_SUBSCRIBE
? _mlsub_regex
: _mlist_regex
);
735 GP_TO_SUBCLASS(grp
, gp
);
736 for (i
= 1; lp
!= grp
; lp
= lp
->gr_next
)
738 fprintf(fo
, "# regex(7): hits %" PRIuZ
", sort %" PRIuZ
".\n ",
744 fprintf(fo
, "wysh %s %s\n",
745 (gp
->g_type
& GT_SUBSCRIBE
? "mlsubscribe" : "mlist"),
746 n_shexp_quote_cp(gp
->g_id
, TRU1
));
747 } else if (gp
->g_type
& GT_SHORTCUT
) {
748 GP_TO_SUBCLASS(cp
, gp
);
749 fprintf(fo
, "wysh shortcut %s %s\n",
750 gp
->g_id
, n_shexp_quote_cp(cp
, TRU1
));
758 _mlmux(enum group_type gt
, char **argv
)
765 _group_print_all(gt
);
767 if ((gp
= _group_find(gt
, *argv
)) != NULL
) {
768 if (gt
& GT_SUBSCRIBE
) {
769 if (!(gp
->g_type
& GT_SUBSCRIBE
)) {
771 gp
->g_type
|= GT_SUBSCRIBE
;
774 n_err(_("Mailing-list already `mlsubscribe'd: %s\n"),
779 n_err(_("Mailing-list already `mlist'ed: %s\n"), *argv
);
783 _group_fetch(gt
, *argv
, 0);
784 } while (*++argv
!= NULL
);
791 _unmlmux(enum group_type gt
, char **argv
)
797 for (; *argv
!= NULL
; ++argv
) {
798 if (gt
& GT_SUBSCRIBE
) {
799 struct group_lookup gl
;
802 if (!(isaster
= (**argv
== '*')))
803 gp
= _group_find(gt
, *argv
);
804 else if ((gp
= _group_go_first(gt
, &gl
)) == NULL
)
806 else if (gp
!= NULL
&& !(gp
->g_type
& GT_SUBSCRIBE
))
811 if (gp
->g_type
& GT_SUBSCRIBE
) {
813 gp
->g_type
&= ~GT_SUBSCRIBE
;
817 while ((gp
= _group_go_next(&gl
)) != NULL
&&
818 !(gp
->g_type
& GT_SUBSCRIBE
))
824 n_err(_("Mailing-list not `mlsubscribe'd: %s\n"),
825 n_shexp_quote_cp(*argv
, FAL0
));
830 } else if (_group_del(gt
, *argv
))
832 n_err(_("No such mailing-list: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
841 _mlmux_linkin(struct group
*gp
)
843 struct grp_regex
**lpp
, *grp
, *lhp
;
846 if (gp
->g_type
& GT_SUBSCRIBE
) {
854 GP_TO_SUBCLASS(grp
, gp
);
855 if ((lhp
= *lpp
) != NULL
) {
856 (grp
->gr_last
= lhp
->gr_last
)->gr_next
= grp
;
857 (grp
->gr_next
= lhp
)->gr_last
= grp
;
859 *lpp
= grp
->gr_last
= grp
->gr_next
= grp
;
865 _mlmux_linkout(struct group
*gp
)
867 struct grp_regex
*grp
, **lpp
;
870 GP_TO_SUBCLASS(grp
, gp
);
872 if (gp
->g_type
& GT_SUBSCRIBE
) {
875 _mlsub_hits
-= grp
->gr_hits
;
879 _mlist_hits
-= grp
->gr_hits
;
882 if (grp
->gr_next
== grp
)
885 (grp
->gr_last
->gr_next
= grp
->gr_next
)->gr_last
= grp
->gr_last
;
891 #endif /* HAVE_REGEX */
894 nalloc(char const *str
, enum gfield ntype
)
896 struct n_addrguts ag
;
900 assert(!(ntype
& GFULLEXTRA
) || (ntype
& GFULL
) != 0);
902 str
= n_addrspec_with_guts(&ag
, str
,
903 ((ntype
& (GFULL
| GSKIN
| GREF
)) != 0));
906 np = NULL; TODO We cannot return NULL,
907 goto jleave; TODO thus handle failures in here!
912 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
913 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
914 np
= salloc(sizeof(*np
) + ag
.ag_slen
+1);
915 memcpy(np
+ 1, ag
.ag_skinned
, ag
.ag_slen
+1);
916 ag
.ag_skinned
= (char*)(np
+ 1);
918 np
= salloc(sizeof *np
);
925 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
926 np
->n_fullextra
= NULL
;
927 np
->n_flags
= ag
.ag_n_flags
;
930 if (ag
.ag_ilen
== ag
.ag_slen
932 && !(ag
.ag_n_flags
& NAME_IDNA
)
936 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
939 /* n_fullextra is only the complete name part without address.
940 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
941 if ((ntype
& GFULLEXTRA
) && ag
.ag_ilen
> ag
.ag_slen
+ 2) {
942 size_t s
= ag
.ag_iaddr_start
, e
= ag
.ag_iaddr_aend
, i
;
945 if (s
== 0 || str
[--s
] != '<' || str
[e
++] != '>')
948 in
.s
= n_lofi_alloc(s
+ 1 + i
+1);
949 while(s
> 0 && blankchar(str
[s
- 1]))
951 memcpy(in
.s
, str
, s
);
954 while (blankchar(str
[e
])) {
960 memcpy(&in
.s
[s
], &str
[e
], i
);
963 in
.s
[in
.l
= s
] = '\0';
964 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
966 for (cp
= out
.s
, i
= out
.l
; i
> 0 && spacechar(*cp
); --i
, ++cp
)
968 while (i
> 0 && spacechar(cp
[i
- 1]))
970 np
->n_fullextra
= savestrbuf(cp
, i
);
977 /* n_fullname depends on IDNA conversion */
979 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
981 in
.s
= n_UNCONST(str
);
985 /* The domain name was IDNA and has been converted. We also have to
986 * ensure that the domain name in .n_fullname is replaced with the
987 * converted version, since MIME doesn't perform encoding of addrs */
988 /* TODO This definetely doesn't belong here! */
989 size_t l
= ag
.ag_iaddr_start
,
990 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
991 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
992 memcpy(in
.s
, str
, l
);
993 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
995 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
1001 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1002 np
->n_fullname
= savestr(out
.s
);
1005 if (ag
.ag_n_flags
& NAME_IDNA
)
1008 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
1016 ndup(struct name
*np
, enum gfield ntype
)
1021 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
1022 nnp
= nalloc(np
->n_name
, ntype
);
1026 nnp
= salloc(sizeof *np
);
1027 nnp
->n_flink
= nnp
->n_blink
= NULL
;
1028 nnp
->n_type
= ntype
;
1029 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
1031 nnp
->n_name
= savestr(np
->n_name
);
1032 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
))) {
1033 nnp
->n_fullname
= nnp
->n_name
;
1034 nnp
->n_fullextra
= NULL
;
1036 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
1037 nnp
->n_fullname
= savestr(np
->n_fullname
);
1038 nnp
->n_fullextra
= (np
->n_fullextra
== NULL
) ? NULL
1039 : savestr(np
->n_fullextra
);
1047 cat(struct name
*n1
, struct name
*n2
)
1059 while (tail
->n_flink
!= NULL
)
1060 tail
= tail
->n_flink
;
1070 namelist_dup(struct name
const *np
, enum gfield ntype
)
1075 for (nnp
= NULL
; np
!= NULL
; np
= np
->n_flink
) {
1076 struct name
*x
= ndup(n_UNCONST(np
), (np
->n_type
& ~GMASK
) | ntype
);
1085 count(struct name
const *np
)
1090 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1091 if (!(np
->n_type
& GDEL
))
1098 count_nonlocal(struct name
const *np
)
1103 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1104 if (!(np
->n_type
& GDEL
) && !(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
))
1111 extract(char const *line
, enum gfield ntype
)
1116 rv
= _extract1(line
, ntype
, " \t,", 0);
1122 lextract(char const *line
, enum gfield ntype
)
1127 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
1128 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
1134 detract(struct name
*np
, enum gfield ntype
)
1145 flags
= ntype
& (GCOMMA
| GNAMEONLY
);
1146 ntype
&= ~(GCOMMA
| GNAMEONLY
);
1149 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1150 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1152 s
+= strlen(flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
) +1;
1162 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1163 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1165 cp
= sstpcpy(cp
, (flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
));
1166 if ((flags
& GCOMMA
) && p
->n_flink
!= NULL
)
1171 if ((flags
& GCOMMA
) && *--cp
== ',')
1179 grab_names(enum n_lexinput_flags lif
, char const *field
, struct name
*np
,
1180 int comma
, enum gfield gflags
)
1186 np
= lextract(n_lex_input_cp(lif
, field
, detract(np
, comma
)), gflags
);
1187 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
1188 if (is_addr_invalid(nq
, EACM_NONE
))
1195 name_is_same_domain(struct name
const *n1
, struct name
const *n2
)
1197 char const *d1
, *d2
;
1201 d1
= strrchr(n1
->n_name
, '@');
1202 d2
= strrchr(n2
->n_name
, '@');
1204 rv
= (d1
!= NULL
&& d2
!= NULL
) ? !asccasecmp(++d1
, ++d2
) : FAL0
;
1211 checkaddrs(struct name
*np
, enum expand_addr_check_mode eacm
,
1212 si8_t
*set_on_error
)
1217 for (n
= np
; n
!= NULL
;) {
1220 if ((rv
= is_addr_invalid(n
, eacm
)) != 0) {
1221 if (set_on_error
!= NULL
)
1222 *set_on_error
|= rv
; /* don't loose -1! */
1224 n
->n_blink
->n_flink
= n
->n_flink
;
1226 n
->n_flink
->n_blink
= n
->n_blink
;
1237 namelist_vaporise_head(struct header
*hp
, enum expand_addr_check_mode eacm
,
1238 bool_t metoo
, si8_t
*set_on_error
)
1240 struct name
*tolist
, *np
, **npp
;
1243 tolist
= usermap(cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
)), metoo
);
1244 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
1246 tolist
= elide(checkaddrs(tolist
, eacm
, set_on_error
));
1248 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
1249 switch (np
->n_type
& (GDEL
| GMASK
)) {
1250 case GTO
: npp
= &hp
->h_to
; break;
1251 case GCC
: npp
= &hp
->h_cc
; break;
1252 case GBCC
: npp
= &hp
->h_bcc
; break;
1255 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
1262 usermap(struct name
*names
, bool_t force_metoo
)
1264 struct name
*new, *np
, *cp
;
1271 metoo
= (force_metoo
|| ok_blook(metoo
));
1272 while (np
!= NULL
) {
1273 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
1274 if (is_fileorpipe_addr(np
) || np
->n_name
[0] == '\\') {
1280 gp
= _group_find(GT_ALIAS
, np
->n_name
);
1283 new = _gexpand(0, new, gp
, metoo
, np
->n_type
);
1293 elide(struct name
*names
)
1295 struct name
*np
, *t
, *newn
, *x
;
1302 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
1303 for (np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
1304 if (!(names
->n_type
& GDEL
)) {
1305 names
->n_blink
= np
;
1307 np
->n_flink
= names
;
1318 newn
->n_flink
= NULL
;
1320 while (np
!= NULL
) {
1324 while ((cmpres
= asccasecmp(t
->n_name
, np
->n_name
)) < 0) {
1325 if (t
->n_flink
== NULL
)
1330 /* If we ran out of t's, put new entry after the current value of t */
1340 /* Otherwise, put the new entry in front of the current t. If at the
1341 * front of the list, the new guy becomes the new head of the list */
1352 /* The normal case -- we are inserting into the middle of the list */
1356 x
->n_blink
= t
->n_blink
;
1357 t
->n_blink
->n_flink
= x
;
1361 /* Now the list headed up by new is sorted. Remove duplicates */
1363 while (np
!= NULL
) {
1365 while (t
->n_flink
!= NULL
&& !asccasecmp(np
->n_name
, t
->n_flink
->n_name
))
1372 /* Now t points to the last entry with the same name as np.
1373 * Make np point beyond t */
1374 np
->n_flink
= t
->n_flink
;
1375 if (t
->n_flink
!= NULL
)
1376 t
->n_flink
->n_blink
= np
;
1385 c_alternates(void *v
)
1388 char **namelist
= v
, **ap
, **ap2
, *cp
;
1391 for (namelist
= v
, l
= 0; namelist
[l
] != NULL
; ++l
)
1395 if (_altnames
!= NULL
) {
1396 printf("alternates ");
1397 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1404 if (_altnames
!= NULL
) {
1405 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1411 _altnames
= smalloc(l
* sizeof(*_altnames
));
1412 for (ap
= namelist
, ap2
= _altnames
; *ap
!= NULL
; ++ap
, ++ap2
) {
1425 delete_alternates(struct name
*np
)
1431 np
= delname(np
, ok_vlook(LOGNAME
));
1432 if (_altnames
!= NULL
)
1433 for (ap
= _altnames
; *ap
!= '\0'; ++ap
)
1434 np
= delname(np
, *ap
);
1436 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1437 while (xp
!= NULL
) {
1438 np
= delname(np
, xp
->n_name
);
1442 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1443 while (xp
!= NULL
) {
1444 np
= delname(np
, xp
->n_name
);
1448 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1449 while (xp
!= NULL
) {
1450 np
= delname(np
, xp
->n_name
);
1458 is_myname(char const *name
)
1465 if (_same_name(ok_vlook(LOGNAME
), name
))
1467 if (_altnames
!= NULL
)
1468 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1469 if (_same_name(*ap
, name
))
1472 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1473 while (xp
!= NULL
) {
1474 if (_same_name(xp
->n_name
, name
))
1479 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1480 while (xp
!= NULL
) {
1481 if (_same_name(xp
->n_name
, name
))
1486 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1487 while (xp
!= NULL
) {
1488 if (_same_name(xp
->n_name
, name
))
1507 _group_print_all(GT_ALIAS
);
1508 else if (argv
[1] == NULL
) {
1509 if ((gp
= _group_find(GT_ALIAS
, *argv
)) != NULL
)
1510 _group_print(gp
, stdout
);
1512 n_err(_("No such alias: %s\n"), *argv
);
1516 struct grp_names_head
*gnhp
;
1518 gp
= _group_fetch(GT_ALIAS
, *argv
, 0);
1519 GP_TO_SUBCLASS(gnhp
, gp
);
1521 for (++argv
; *argv
!= NULL
; ++argv
) {
1522 size_t l
= strlen(*argv
) +1;
1523 struct grp_names
*gnp
= smalloc(n_VSTRUCT_SIZEOF(struct grp_names
,
1525 gnp
->gn_next
= gnhp
->gnh_head
;
1526 gnhp
->gnh_head
= gnp
;
1527 memcpy(gnp
->gn_id
, *argv
, l
);
1529 assert(gnhp
->gnh_head
!= NULL
);
1542 do if (!_group_del(GT_ALIAS
, *argv
)) {
1543 n_err(_("No such alias: %s\n"), *argv
);
1545 } while (*++argv
!= NULL
);
1556 rv
= _mlmux(GT_MLIST
, v
);
1567 rv
= _unmlmux(GT_MLIST
, v
);
1573 c_mlsubscribe(void *v
)
1578 rv
= _mlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
1584 c_unmlsubscribe(void *v
)
1589 rv
= _unmlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
1595 is_mlist(char const *name
, bool_t subscribed_only
)
1599 struct grp_regex
**lpp
, *grp
;
1602 enum mlist_state rv
;
1605 gp
= _group_find(GT_MLIST
, name
);
1606 rv
= (gp
!= NULL
) ? MLIST_KNOWN
: MLIST_OTHER
;
1607 if (rv
== MLIST_KNOWN
) {
1608 if (gp
->g_type
& GT_SUBSCRIBE
)
1609 rv
= MLIST_SUBSCRIBED
;
1610 else if (subscribed_only
)
1612 /* Of course, if that is a regular expression it doesn't mean a thing */
1614 if (gp
->g_type
& GT_REGEX
)
1621 /* Not in the hashmap (as something matchable), walk the lists */
1624 lpp
= &_mlsub_regex
;
1626 if ((grp
= *lpp
) != NULL
) {
1627 do if (regexec(&grp
->gr_regex
, name
, 0,NULL
, 0) != REG_NOMATCH
) {
1628 /* Relink as the head of this list if the hit count of this group is
1629 * >= 25% of the average hit count */
1632 i
= ++_mlsub_hits
/ _mlsub_size
;
1634 i
= ++_mlist_hits
/ _mlist_size
;
1637 if (++grp
->gr_hits
>= i
&& *lpp
!= grp
&& grp
->gr_next
!= grp
) {
1638 grp
->gr_last
->gr_next
= grp
->gr_next
;
1639 grp
->gr_next
->gr_last
= grp
->gr_last
;
1640 (grp
->gr_last
= (*lpp
)->gr_last
)->gr_next
= grp
;
1641 (grp
->gr_next
= *lpp
)->gr_last
= grp
;
1644 rv
= !re2
? MLIST_SUBSCRIBED
: MLIST_KNOWN
;
1646 } while ((grp
= grp
->gr_next
) != *lpp
);
1648 if (!re2
&& !subscribed_only
) {
1650 lpp
= &_mlist_regex
;
1653 assert(rv
== MLIST_OTHER
);
1669 _group_print_all(GT_SHORTCUT
);
1670 else for (; *argv
!= NULL
; argv
+= 2) {
1671 /* Because one hardly ever redefines, anything is stored in one chunk */
1676 if (argv
[1] == NULL
) {
1677 n_err(_("Shortcut expansion is missing: %s\n"), *argv
);
1681 if (_group_find(GT_SHORTCUT
, *argv
) != NULL
)
1682 _group_del(GT_SHORTCUT
, *argv
);
1684 l
= strlen(argv
[1]) +1;
1685 gp
= _group_fetch(GT_SHORTCUT
, *argv
, l
);
1686 GP_TO_SUBCLASS(cp
, gp
);
1687 memcpy(cp
, argv
[1], l
);
1694 c_unshortcut(void *v
)
1700 do if (!_group_del(GT_SHORTCUT
, *argv
)) {
1701 n_err(_("No such shortcut: %s\n"), *argv
);
1703 } while (*++argv
!= NULL
);
1709 shortcut_expand(char const *str
)
1714 if ((gp
= _group_find(GT_SHORTCUT
, str
)) != NULL
)
1715 GP_TO_SUBCLASS(str
, gp
);