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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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) */
48 GT_MASK
= GT_ALIAS
| GT_MLIST
| GT_SHORTCUT
| GT_CUSTOMHDR
,
50 /* Subtype bits and flags */
54 /* Extended type mask to be able to reflect what we really have; i.e., mlist
55 * can have GT_REGEX if they are subscribed or not, but `mlsubscribe' should
56 * print out only GT_MLIST which have the GT_SUBSCRIBE attribute set */
57 GT_PRINT_MASK
= GT_MASK
| GT_SUBSCRIBE
62 size_t g_subclass_off
; /* of "subclass" in .g_id (if any) */
63 ui8_t g_type
; /* enum group_type */
64 /* Identifying name, of variable size. Dependent on actual "subtype" more
65 * data follows thereafter, but note this is always used (i.e., for regular
66 * expression entries this is still set to the plain string) */
67 char g_id
[VFIELD_SIZE(-1)];
69 #define GP_TO_SUBCLASS(X,G) \
71 union __group_subclass {void *gs_vp; char *gs_cp;} __gs__;\
72 __gs__.gs_cp = (char*)UNCONST(G) + (G)->g_subclass_off;\
76 struct grp_names_head
{
77 struct grp_names
*gnh_head
;
81 struct grp_names
*gn_next
;
82 char gn_id
[VFIELD_SIZE(0)];
87 struct grp_regex
*gr_last
;
88 struct grp_regex
*gr_next
;
89 struct group
*gr_mygroup
; /* xxx because lists use grp_regex*! ?? */
90 size_t gr_hits
; /* Number of times this group matched */
96 struct group
**gl_htable
;
97 struct group
**gl_slot
;
98 struct group
*gl_slot_last
;
99 struct group
*gl_group
;
102 /* List of alternate names of user */
103 static char **_altnames
;
106 static struct group
*_alias_heads
[HSHSIZE
]; /* TODO dynamic hash */
108 /* `mlist', `mlsubscribe'. Anything is stored in the hashmap.. */
109 static struct group
*_mlist_heads
[HSHSIZE
]; /* TODO dynamic hash */
111 /* ..but entries which have GT_REGEX set are false lookups and will really be
112 * accessed via sequential lists instead, which are type-specific for better
113 * performance, but also to make it possible to have ".*@xy.org" as a mlist
114 * and "(one|two)@xy.org" as a mlsubscription.
115 * These lists use a bit of QOS optimization in that a matching group will
116 * become relinked as the new list head if its hit count is
117 * (>= ((xy_hits / _xy_size) >> 2))
118 * Note that the hit counts only count currently linked in nodes.. */
120 static struct grp_regex
*_mlist_regex
, *_mlsub_regex
;
121 static size_t _mlist_size
, _mlist_hits
, _mlsub_size
, _mlsub_hits
;
125 static struct group
*_shortcut_heads
[HSHSIZE
]; /* TODO dynamic hash */
128 static struct group
*_customhdr_heads
[HSHSIZE
]; /* TODO dynamic hash */
130 /* Same name, while taking care for *allnet*? */
131 static bool_t
_same_name(char const *n1
, char const *n2
);
133 /* Delete the given name from a namelist */
134 static struct name
* delname(struct name
*np
, char const *name
);
136 /* Put another node onto a list of names and return the list */
137 static struct name
* put(struct name
*list
, struct name
*node
);
139 /* Grab a single name (liberal name) */
140 static char const * yankname(char const *ap
, char *wbuf
,
141 char const *separators
, int keepcomms
);
143 /* Extraction multiplexer that splits an input line to names */
144 static struct name
* _extract1(char const *line
, enum gfield ntype
,
145 char const *separators
, bool_t keepcomms
);
147 /* Recursively expand a alias name. Limit expansion to some fixed level.
148 * Direct recursion is not expanded for convenience */
149 static struct name
* _gexpand(size_t level
, struct name
*nlist
,
150 struct group
*gp
, bool_t metoo
, int ntype
);
152 /* Lookup a group, return it or NULL, fill in glp anyway */
153 static struct group
* _group_lookup(enum group_type gt
,
154 struct group_lookup
*glp
, char const *id
);
156 /* Easier-to-use wrapper around _group_lookup() */
157 static struct group
* _group_find(enum group_type gt
, char const *id
);
159 /* Iteration: go to the first group, which also inits the iterator. A valid
160 * iterator can be stepped via _next(). A NULL return means no (more) groups
161 * to be iterated exist, in which case only glp->gl_group is set (NULL) */
162 static struct group
* _group_go_first(enum group_type gt
,
163 struct group_lookup
*glp
);
164 static struct group
* _group_go_next(struct group_lookup
*glp
);
166 /* Fetch the group id, create it as necessary */
167 static struct group
* _group_fetch(enum group_type gt
, char const *id
,
170 /* "Intelligent" delete which handles a "*" id, too;
171 * returns a true boolean if a group was deleted, and always succeeds for "*" */
172 static bool_t
_group_del(enum group_type gt
, char const *id
);
174 static struct group
* __group_del(struct group_lookup
*glp
);
175 static void __names_del(struct group
*gp
);
177 /* Print all groups of the given type, alphasorted */
178 static void _group_print_all(enum group_type gt
);
180 static int __group_print_qsorter(void const *a
, void const *b
);
182 /* Really print a group, actually. Return number of written lines */
183 static size_t _group_print(struct group
const *gp
, FILE *fo
);
185 /* Multiplexers for list and subscribe commands */
186 static int _mlmux(enum group_type gt
, char **argv
);
187 static int _unmlmux(enum group_type gt
, char **argv
);
189 /* Relinkers for the sequential match lists */
191 static void _mlmux_linkin(struct group
*gp
);
192 static void _mlmux_linkout(struct group
*gp
);
193 # define _MLMUX_LINKIN(GP) \
194 do if ((GP)->g_type & GT_REGEX) _mlmux_linkin(GP); while (0)
195 # define _MLMUX_LINKOUT(GP) \
196 do if ((GP)->g_type & GT_REGEX) _mlmux_linkout(GP); while (0)
198 # define _MLMUX_LINKIN(GP)
199 # define _MLMUX_LINKOUT(GP)
203 _same_name(char const *n1
, char const *n2
)
209 if (ok_blook(allnet
)) {
217 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
220 rv
= !asccasecmp(n1
, n2
);
227 delname(struct name
*np
, char const *name
)
232 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
233 if (_same_name(p
->n_name
, name
)) {
234 if (p
->n_blink
== NULL
) {
235 if (p
->n_flink
!= NULL
)
236 p
->n_flink
->n_blink
= NULL
;
240 if (p
->n_flink
== NULL
) {
241 if (p
->n_blink
!= NULL
)
242 p
->n_blink
->n_flink
= NULL
;
245 p
->n_blink
->n_flink
= p
->n_flink
;
246 p
->n_flink
->n_blink
= p
->n_blink
;
253 put(struct name
*list
, struct name
*node
)
256 node
->n_flink
= list
;
257 node
->n_blink
= NULL
;
259 list
->n_blink
= node
;
265 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
268 char *wp
, c
, inquote
, lc
, lastsp
;
273 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
274 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
281 /* Parse a full name: TODO RFC 5322
282 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
283 * - Skip entire (nested) comments
284 * - In non-quote, non-comment, join adjacent space to a single SP
285 * - Understand separators only in non-quote, non-comment context,
286 * and only if not part of a *quoted-pair* (XXX too liberal) */
288 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
299 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
305 if (inquote
|| lc
== '\\') {
313 cp
= skip_comment(cp
+ 1);
321 if (strchr(separators
, c
) != NULL
)
325 lastsp
= blankchar(c
);
339 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
342 struct name
*topp
, *np
, *t
;
348 if (line
== NULL
|| *line
== '\0')
353 nbuf
= smalloc(strlen(line
) +1);
354 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
355 t
= nalloc(nbuf
, ntype
);
370 _gexpand(size_t level
, struct name
*nlist
, struct group
*gp
, bool_t metoo
,
373 struct grp_names_head
*gnhp
;
374 struct grp_names
*gnp
;
377 if (UICMP(z
, level
++, >, MAXEXP
)) {
378 n_err(_("Expanding alias to depth larger than %d\n"), MAXEXP
);
382 GP_TO_SUBCLASS(gnhp
, gp
);
383 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
; gnp
= gnp
->gn_next
) {
387 /* FIXME we do not really support leading backslash quoting do we??? */
388 if (*(cp
= gnp
->gn_id
) == '\\' || !strcmp(cp
, gp
->g_id
))
391 if ((ngp
= _group_find(GT_ALIAS
, cp
)) != NULL
) {
392 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
393 * to a full address specification; aliases cannot be empty */
394 struct grp_names_head
*ngnhp
;
395 GP_TO_SUBCLASS(ngnhp
, ngp
);
397 assert(ngnhp
->gnh_head
!= NULL
);
398 if (metoo
|| ngnhp
->gnh_head
->gn_next
!= NULL
||
399 !_same_name(cp
, myname
))
400 nlist
= _gexpand(level
, nlist
, ngp
, metoo
, ntype
);
404 /* Here we should allow to expand to itself if only person in alias */
406 if (metoo
|| gnhp
->gnh_head
->gn_next
== NULL
|| !_same_name(cp
, myname
))
407 nlist
= put(nlist
, nalloc(cp
, ntype
| GFULL
));
414 static struct group
*
415 _group_lookup(enum group_type gt
, struct group_lookup
*glp
, char const *id
)
417 struct group
*lgp
, *gp
;
422 gp
= *(glp
->gl_htable
= glp
->gl_slot
=
423 ((gt
& GT_ALIAS
? _alias_heads
:
424 (gt
& GT_MLIST
? _mlist_heads
:
425 (gt
& GT_SHORTCUT
? _shortcut_heads
: _customhdr_heads
))) +
426 torek_hash(id
) % HSHSIZE
));
428 for (; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
429 if ((gp
->g_type
& gt
) && *gp
->g_id
== *id
&& !strcmp(gp
->g_id
, id
))
432 glp
->gl_slot_last
= lgp
;
438 static struct group
*
439 _group_find(enum group_type gt
, char const *id
)
441 struct group_lookup gl
;
445 gp
= _group_lookup(gt
, &gl
, id
);
450 static struct group
*
451 _group_go_first(enum group_type gt
, struct group_lookup
*glp
)
453 struct group
**gpa
, *gp
;
457 for (glp
->gl_htable
= gpa
= (gt
& GT_ALIAS
? _alias_heads
:
458 (gt
& GT_MLIST
? _mlist_heads
:
459 (gt
& GT_SHORTCUT
? _shortcut_heads
: _customhdr_heads
))), i
= 0;
460 i
< HSHSIZE
; ++gpa
, ++i
)
461 if ((gp
= *gpa
) != NULL
) {
467 glp
->gl_group
= gp
= NULL
;
469 glp
->gl_slot_last
= NULL
;
474 static struct group
*
475 _group_go_next(struct group_lookup
*glp
)
477 struct group
*gp
, **gpa
;
480 if ((gp
= glp
->gl_group
->g_next
) != NULL
)
481 glp
->gl_slot_last
= glp
->gl_group
;
483 glp
->gl_slot_last
= NULL
;
484 for (gpa
= glp
->gl_htable
+ HSHSIZE
; ++glp
->gl_slot
< gpa
;)
485 if ((gp
= *glp
->gl_slot
) != NULL
)
493 static struct group
*
494 _group_fetch(enum group_type gt
, char const *id
, size_t addsz
)
496 struct group_lookup gl
;
501 if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
)
505 i
= n_ALIGN(sizeof(*gp
) - VFIELD_SIZEOF(struct group
, g_id
) + l
);
506 switch (gt
& GT_MASK
) {
508 addsz
= sizeof(struct grp_names_head
);
512 if (is_maybe_regex(id
)) {
513 addsz
= sizeof(struct grp_regex
);
522 gp
= smalloc(i
+ addsz
);
523 gp
->g_subclass_off
= i
;
525 memcpy(gp
->g_id
, id
, l
);
528 struct grp_names_head
*gnhp
;
529 GP_TO_SUBCLASS(gnhp
, gp
);
530 gnhp
->gnh_head
= NULL
;
533 else if (/*(gt & GT_MLIST) &&*/ gt
& GT_REGEX
) {
534 struct grp_regex
*grp
;
535 GP_TO_SUBCLASS(grp
, gp
);
537 if (regcomp(&grp
->gr_regex
, id
, REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) {
538 n_err(_("Invalid regular expression: \"%s\"\n"), id
);
543 grp
->gr_mygroup
= gp
;
548 gp
->g_next
= *gl
.gl_slot
;
556 _group_del(enum group_type gt
, char const *id
)
558 enum group_type xgt
= gt
& GT_MASK
;
559 struct group_lookup gl
;
563 /* Delete 'em all? */
564 if (id
[0] == '*' && id
[1] == '\0') {
565 for (gp
= _group_go_first(gt
, &gl
); gp
!= NULL
;)
566 gp
= (gp
->g_type
& xgt
) ? __group_del(&gl
) : _group_go_next(&gl
);
567 gp
= (struct group
*)TRU1
;
568 } else if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
) {
569 if (gp
->g_type
& xgt
)
578 static struct group
*
579 __group_del(struct group_lookup
*glp
)
581 struct group
*x
, *gp
;
584 /* Overly complicated: link off this node, step ahead to next.. */
586 if ((gp
= glp
->gl_slot_last
) != NULL
) {
587 gp
= (gp
->g_next
= x
->g_next
);
589 glp
->gl_slot_last
= NULL
;
590 gp
= (*glp
->gl_slot
= x
->g_next
);
593 struct group
**gpa
= glp
->gl_htable
+ HSHSIZE
;
595 while (++glp
->gl_slot
< gpa
)
596 if ((gp
= *glp
->gl_slot
) != NULL
)
602 if (x
->g_type
& GT_ALIAS
)
605 else if (/*(x->g_type & GT_MLIST) &&*/ x
->g_type
& GT_REGEX
) {
606 struct grp_regex
*grp
;
607 GP_TO_SUBCLASS(grp
, x
);
609 regfree(&grp
->gr_regex
);
620 __names_del(struct group
*gp
)
622 struct grp_names_head
*gnhp
;
623 struct grp_names
*gnp
;
626 GP_TO_SUBCLASS(gnhp
, gp
);
627 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
;) {
628 struct grp_names
*x
= gnp
;
636 _group_print_all(enum group_type gt
)
640 struct group
const *gp
;
647 xgt
= gt
& GT_PRINT_MASK
;
648 gpa
= (xgt
& GT_ALIAS
? _alias_heads
649 : (xgt
& GT_MLIST
? _mlist_heads
650 : (xgt
& GT_SHORTCUT
? _shortcut_heads
: _customhdr_heads
)));
652 for (h
= 0, i
= 1; h
< HSHSIZE
; ++h
)
653 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
654 if ((gp
->g_type
& xgt
) == xgt
)
656 ida
= salloc(i
* sizeof *ida
);
658 for (i
= h
= 0; h
< HSHSIZE
; ++h
)
659 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
660 if ((gp
->g_type
& xgt
) == xgt
)
665 qsort(ida
, i
, sizeof *ida
, &__group_print_qsorter
);
667 if ((fp
= Ftmp(NULL
, "prgroup", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
671 for (i
= 0; ida
[i
] != NULL
; ++i
)
672 lines
+= _group_print(_group_find(gt
, ida
[i
]), fp
);
675 if (gt
& GT_SUBSCRIBE
)
676 i
= (ui32_t
)_mlsub_size
, h
= (ui32_t
)_mlsub_hits
;
678 i
= (ui32_t
)_mlist_size
, h
= (ui32_t
)_mlist_hits
;
679 if (i
> 0 && (options
& OPT_D_V
)){
680 fprintf(fp
, _("# %s list regex(7) total: %u entries, %u hits\n"),
681 (gt
& GT_SUBSCRIBE
? _("Subscribed") : _("Non-subscribed")),
689 page_or_print(fp
, lines
);
696 __group_print_qsorter(void const *a
, void const *b
)
701 rv
= strcmp(*(char**)UNCONST(a
), *(char**)UNCONST(b
));
707 _group_print(struct group
const *gp
, FILE *fo
)
715 if (gp
->g_type
& GT_ALIAS
) {
716 struct grp_names_head
*gnhp
;
717 struct grp_names
*gnp
;
719 fprintf(fo
, "alias %s", gp
->g_id
);
721 GP_TO_SUBCLASS(gnhp
, gp
);
722 if ((gnp
= gnhp
->gnh_head
) != NULL
) { /* xxx always 1+ entries */
724 struct grp_names
*x
= gnp
;
726 fprintf(fo
, " \"%s\"", string_quote(x
->gn_id
));
727 } while (gnp
!= NULL
);
729 } else if (gp
->g_type
& GT_MLIST
) {
731 if ((gp
->g_type
& GT_REGEX
) && (options
& OPT_D_V
)){
733 struct grp_regex
*grp
,
734 *lp
= (gp
->g_type
& GT_SUBSCRIBE
? _mlsub_regex
: _mlist_regex
);
736 GP_TO_SUBCLASS(grp
, gp
);
737 for (i
= 1; lp
!= grp
; lp
= lp
->gr_next
)
739 fprintf(fo
, "# regex(7): hits %" PRIuZ
", sort %" PRIuZ
".\n ",
745 fprintf(fo
, "wysh %s %s",
746 (gp
->g_type
& GT_SUBSCRIBE
? "mlsubscribe" : "mlist"),
747 n_shell_quote_cp(gp
->g_id
));
748 } else if (gp
->g_type
& GT_SHORTCUT
) {
749 GP_TO_SUBCLASS(cp
, gp
);
750 fprintf(fo
, "wysh shortcut %s %s", gp
->g_id
, n_shell_quote_cp(cp
));
751 } else if (gp
->g_type
& GT_CUSTOMHDR
) {
752 GP_TO_SUBCLASS(cp
, gp
);
753 fprintf(fo
, "customhdr %s %s", gp
->g_id
, n_shell_quote_cp(cp
));
762 _mlmux(enum group_type gt
, char **argv
)
769 _group_print_all(gt
);
771 if ((gp
= _group_find(gt
, *argv
)) != NULL
) {
772 if (gt
& GT_SUBSCRIBE
) {
773 if (!(gp
->g_type
& GT_SUBSCRIBE
)) {
775 gp
->g_type
|= GT_SUBSCRIBE
;
778 n_err(_("Mailing-list already `mlsubscribe'd: %s\n"),
783 n_err(_("Mailing-list already `mlist'ed: %s\n"), *argv
);
787 _group_fetch(gt
, *argv
, 0);
788 } while (*++argv
!= NULL
);
795 _unmlmux(enum group_type gt
, char **argv
)
801 for (; *argv
!= NULL
; ++argv
) {
802 if (gt
& GT_SUBSCRIBE
) {
803 struct group_lookup gl
;
806 if (!(isaster
= (**argv
== '*')))
807 gp
= _group_find(gt
, *argv
);
808 else if ((gp
= _group_go_first(gt
, &gl
)) == NULL
)
810 else if (gp
!= NULL
&& !(gp
->g_type
& GT_SUBSCRIBE
))
815 if (gp
->g_type
& GT_SUBSCRIBE
) {
817 gp
->g_type
&= ~GT_SUBSCRIBE
;
821 while ((gp
= _group_go_next(&gl
)) != NULL
&&
822 !(gp
->g_type
& GT_SUBSCRIBE
))
828 n_err(_("Mailing-list not `mlsubscribe'd: \"%s\"\n"), *argv
);
833 } else if (_group_del(gt
, *argv
))
835 n_err(_("No such mailing-list: \"%s\"\n"), *argv
);
844 _mlmux_linkin(struct group
*gp
)
846 struct grp_regex
**lpp
, *grp
, *lhp
;
849 if (gp
->g_type
& GT_SUBSCRIBE
) {
857 GP_TO_SUBCLASS(grp
, gp
);
858 if ((lhp
= *lpp
) != NULL
) {
859 (grp
->gr_last
= lhp
->gr_last
)->gr_next
= grp
;
860 (grp
->gr_next
= lhp
)->gr_last
= grp
;
862 *lpp
= grp
->gr_last
= grp
->gr_next
= grp
;
868 _mlmux_linkout(struct group
*gp
)
870 struct grp_regex
*grp
, **lpp
;
873 GP_TO_SUBCLASS(grp
, gp
);
875 if (gp
->g_type
& GT_SUBSCRIBE
) {
878 _mlsub_hits
-= grp
->gr_hits
;
882 _mlist_hits
-= grp
->gr_hits
;
885 if (grp
->gr_next
== grp
)
888 (grp
->gr_last
->gr_next
= grp
->gr_next
)->gr_last
= grp
->gr_last
;
894 #endif /* HAVE_REGEX */
897 nalloc(char *str
, enum gfield ntype
)
903 assert(!(ntype
& GFULLEXTRA
) || (ntype
& GFULL
) != 0);
905 addrspec_with_guts(((ntype
& (GFULL
| GSKIN
| GREF
)) != 0), str
, &ag
);
906 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
907 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
908 np
= salloc(sizeof(*np
) + ag
.ag_slen
+1);
909 memcpy(np
+ 1, ag
.ag_skinned
, ag
.ag_slen
+1);
910 ag
.ag_skinned
= (char*)(np
+ 1);
912 np
= salloc(sizeof *np
);
919 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
920 np
->n_fullextra
= NULL
;
921 np
->n_flags
= ag
.ag_n_flags
;
924 if (ag
.ag_ilen
== ag
.ag_slen
926 && !(ag
.ag_n_flags
& NAME_IDNA
)
930 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
933 /* n_fullextra is only the complete name part without address.
934 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
935 if ((ntype
& GFULLEXTRA
) && ag
.ag_ilen
> ag
.ag_slen
+ 2) {
936 size_t s
= ag
.ag_iaddr_start
, e
= ag
.ag_iaddr_aend
, i
;
939 if (s
== 0 || str
[--s
] != '<' || str
[e
++] != '>')
942 in
.s
= ac_alloc(s
+ i
+1);
943 memcpy(in
.s
, str
, s
);
945 memcpy(in
.s
+ s
, str
+ e
, i
);
947 in
.s
[in
.l
= s
] = '\0';
948 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
950 for (cp
= out
.s
, i
= out
.l
; i
> 0 && spacechar(*cp
); --i
, ++cp
)
952 while (i
> 0 && spacechar(cp
[i
- 1]))
954 np
->n_fullextra
= savestrbuf(cp
, i
);
961 /* n_fullname depends on IDNA conversion */
963 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
969 /* The domain name was IDNA and has been converted. We also have to
970 * ensure that the domain name in .n_fullname is replaced with the
971 * converted version, since MIME doesn't perform encoding of addrs */
972 size_t l
= ag
.ag_iaddr_start
,
973 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
974 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
975 memcpy(in
.s
, str
, l
);
976 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
978 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
984 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
985 np
->n_fullname
= savestr(out
.s
);
988 if (ag
.ag_n_flags
& NAME_IDNA
)
991 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
999 ndup(struct name
*np
, enum gfield ntype
)
1004 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
1005 nnp
= nalloc(np
->n_name
, ntype
);
1009 nnp
= salloc(sizeof *np
);
1010 nnp
->n_flink
= nnp
->n_blink
= NULL
;
1011 nnp
->n_type
= ntype
;
1012 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
1014 nnp
->n_name
= savestr(np
->n_name
);
1015 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
))) {
1016 nnp
->n_fullname
= nnp
->n_name
;
1017 nnp
->n_fullextra
= NULL
;
1019 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
1020 nnp
->n_fullname
= savestr(np
->n_fullname
);
1021 nnp
->n_fullextra
= (np
->n_fullextra
== NULL
) ? NULL
1022 : savestr(np
->n_fullextra
);
1030 cat(struct name
*n1
, struct name
*n2
)
1042 while (tail
->n_flink
!= NULL
)
1043 tail
= tail
->n_flink
;
1053 namelist_dup(struct name
const *np
, enum gfield ntype
)
1058 for (nnp
= NULL
; np
!= NULL
; np
= np
->n_flink
) {
1059 struct name
*x
= ndup(UNCONST(np
), (np
->n_type
& ~GMASK
) | ntype
);
1068 count(struct name
const *np
)
1073 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1074 if (!(np
->n_type
& GDEL
))
1081 count_nonlocal(struct name
const *np
)
1086 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1087 if (!(np
->n_type
& GDEL
) && !(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
))
1094 extract(char const *line
, enum gfield ntype
)
1099 rv
= _extract1(line
, ntype
, " \t,", 0);
1105 lextract(char const *line
, enum gfield ntype
)
1110 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
1111 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
1117 detract(struct name
*np
, enum gfield ntype
)
1128 flags
= ntype
& (GCOMMA
| GNAMEONLY
);
1129 ntype
&= ~(GCOMMA
| GNAMEONLY
);
1132 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1133 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1135 s
+= strlen(flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
) +1;
1145 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1146 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1148 cp
= sstpcpy(cp
, (flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
));
1149 if ((flags
& GCOMMA
) && p
->n_flink
!= NULL
)
1154 if ((flags
& GCOMMA
) && *--cp
== ',')
1162 grab_names(char const *field
, struct name
*np
, int comma
, enum gfield gflags
)
1168 np
= lextract(n_lex_input_cp_addhist(field
, detract(np
, comma
), TRU1
),
1170 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
1171 if (is_addr_invalid(nq
, EACM_NONE
))
1178 name_is_same_domain(struct name
const *n1
, struct name
const *n2
)
1180 char const *d1
, *d2
;
1184 d1
= strrchr(n1
->n_name
, '@');
1185 d2
= strrchr(n2
->n_name
, '@');
1187 rv
= (d1
!= NULL
&& d2
!= NULL
) ? !asccasecmp(++d1
, ++d2
) : FAL0
;
1194 checkaddrs(struct name
*np
, enum expand_addr_check_mode eacm
,
1195 si8_t
*set_on_error
)
1200 for (n
= np
; n
!= NULL
;) {
1203 if ((rv
= is_addr_invalid(n
, eacm
)) != 0) {
1204 if (set_on_error
!= NULL
)
1205 *set_on_error
|= rv
; /* don't loose -1! */
1207 n
->n_blink
->n_flink
= n
->n_flink
;
1209 n
->n_flink
->n_blink
= n
->n_blink
;
1220 namelist_vaporise_head(struct header
*hp
, enum expand_addr_check_mode eacm
,
1221 bool_t metoo
, si8_t
*set_on_error
)
1223 struct name
*tolist
, *np
, **npp
;
1226 tolist
= usermap(cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
)), metoo
);
1227 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
1229 tolist
= elide(checkaddrs(tolist
, eacm
, set_on_error
));
1231 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
1232 switch (np
->n_type
& (GDEL
| GMASK
)) {
1233 case GTO
: npp
= &hp
->h_to
; break;
1234 case GCC
: npp
= &hp
->h_cc
; break;
1235 case GBCC
: npp
= &hp
->h_bcc
; break;
1238 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
1245 usermap(struct name
*names
, bool_t force_metoo
)
1247 struct name
*new, *np
, *cp
;
1254 metoo
= (force_metoo
|| ok_blook(metoo
));
1255 while (np
!= NULL
) {
1256 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
1257 if (is_fileorpipe_addr(np
) || np
->n_name
[0] == '\\') {
1263 gp
= _group_find(GT_ALIAS
, np
->n_name
);
1266 new = _gexpand(0, new, gp
, metoo
, np
->n_type
);
1276 elide(struct name
*names
)
1278 struct name
*np
, *t
, *newn
, *x
;
1285 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
1286 for (np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
1287 if (!(names
->n_type
& GDEL
)) {
1288 names
->n_blink
= np
;
1290 np
->n_flink
= names
;
1301 newn
->n_flink
= NULL
;
1303 while (np
!= NULL
) {
1307 while ((cmpres
= asccasecmp(t
->n_name
, np
->n_name
)) < 0) {
1308 if (t
->n_flink
== NULL
)
1313 /* If we ran out of t's, put new entry after the current value of t */
1323 /* Otherwise, put the new entry in front of the current t. If at the
1324 * front of the list, the new guy becomes the new head of the list */
1335 /* The normal case -- we are inserting into the middle of the list */
1339 x
->n_blink
= t
->n_blink
;
1340 t
->n_blink
->n_flink
= x
;
1344 /* Now the list headed up by new is sorted. Remove duplicates */
1346 while (np
!= NULL
) {
1348 while (t
->n_flink
!= NULL
&& !asccasecmp(np
->n_name
, t
->n_flink
->n_name
))
1355 /* Now t points to the last entry with the same name as np.
1356 * Make np point beyond t */
1357 np
->n_flink
= t
->n_flink
;
1358 if (t
->n_flink
!= NULL
)
1359 t
->n_flink
->n_blink
= np
;
1368 c_alternates(void *v
)
1371 char **namelist
= v
, **ap
, **ap2
, *cp
;
1374 l
= argcount(namelist
) +1;
1377 if (_altnames
!= NULL
) {
1378 printf("alternates ");
1379 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1386 if (_altnames
!= NULL
) {
1387 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1392 _altnames
= smalloc(l
* sizeof(*_altnames
));
1393 for (ap
= namelist
, ap2
= _altnames
; *ap
!= NULL
; ++ap
, ++ap2
) {
1406 delete_alternates(struct name
*np
)
1412 np
= delname(np
, myname
);
1413 if (_altnames
!= NULL
)
1414 for (ap
= _altnames
; *ap
!= '\0'; ++ap
)
1415 np
= delname(np
, *ap
);
1417 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1418 while (xp
!= NULL
) {
1419 np
= delname(np
, xp
->n_name
);
1423 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1424 while (xp
!= NULL
) {
1425 np
= delname(np
, xp
->n_name
);
1429 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1430 while (xp
!= NULL
) {
1431 np
= delname(np
, xp
->n_name
);
1439 is_myname(char const *name
)
1446 if (_same_name(myname
, name
))
1448 if (_altnames
!= NULL
)
1449 for (ap
= _altnames
; *ap
!= NULL
; ++ap
)
1450 if (_same_name(*ap
, name
))
1453 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1454 while (xp
!= NULL
) {
1455 if (_same_name(xp
->n_name
, name
))
1460 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1461 while (xp
!= NULL
) {
1462 if (_same_name(xp
->n_name
, name
))
1467 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1468 while (xp
!= NULL
) {
1469 if (_same_name(xp
->n_name
, name
))
1488 _group_print_all(GT_ALIAS
);
1489 else if (argv
[1] == NULL
) {
1490 if ((gp
= _group_find(GT_ALIAS
, *argv
)) != NULL
)
1491 _group_print(gp
, stdout
);
1493 n_err(_("No such alias: \"%s\"\n"), *argv
);
1497 struct grp_names_head
*gnhp
;
1499 gp
= _group_fetch(GT_ALIAS
, *argv
, 0);
1500 GP_TO_SUBCLASS(gnhp
, gp
);
1502 for (++argv
; *argv
!= NULL
; ++argv
) {
1503 size_t l
= strlen(*argv
) +1;
1504 struct grp_names
*gnp
= smalloc(sizeof(*gnp
) -
1505 VFIELD_SIZEOF(struct grp_names
, gn_id
) + l
);
1506 gnp
->gn_next
= gnhp
->gnh_head
;
1507 gnhp
->gnh_head
= gnp
;
1508 memcpy(gnp
->gn_id
, *argv
, l
);
1510 assert(gnhp
->gnh_head
!= NULL
);
1523 do if (!_group_del(GT_ALIAS
, *argv
)) {
1524 n_err(_("No such alias: \"%s\"\n"), *argv
);
1526 } while (*++argv
!= NULL
);
1537 rv
= _mlmux(GT_MLIST
, v
);
1548 rv
= _unmlmux(GT_MLIST
, v
);
1554 c_mlsubscribe(void *v
)
1559 rv
= _mlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
1565 c_unmlsubscribe(void *v
)
1570 rv
= _unmlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
1576 is_mlist(char const *name
, bool_t subscribed_only
)
1580 struct grp_regex
**lpp
, *grp
;
1583 enum mlist_state rv
;
1586 gp
= _group_find(GT_MLIST
, name
);
1587 rv
= (gp
!= NULL
) ? MLIST_KNOWN
: MLIST_OTHER
;
1588 if (rv
== MLIST_KNOWN
) {
1589 if (gp
->g_type
& GT_SUBSCRIBE
)
1590 rv
= MLIST_SUBSCRIBED
;
1591 else if (subscribed_only
)
1593 /* Of course, if that is a regular expression it doesn't mean a thing */
1595 if (gp
->g_type
& GT_REGEX
)
1602 /* Not in the hashmap (as something matchable), walk the lists */
1605 lpp
= &_mlsub_regex
;
1607 if ((grp
= *lpp
) != NULL
) {
1608 do if (regexec(&grp
->gr_regex
, name
, 0,NULL
, 0) != REG_NOMATCH
) {
1609 /* Relink as the head of this list if the hit count of this group is
1610 * >= 25% of the average hit count */
1613 i
= ++_mlsub_hits
/ _mlsub_size
;
1615 i
= ++_mlist_hits
/ _mlist_size
;
1618 if (++grp
->gr_hits
>= i
&& *lpp
!= grp
&& grp
->gr_next
!= grp
) {
1619 grp
->gr_last
->gr_next
= grp
->gr_next
;
1620 grp
->gr_next
->gr_last
= grp
->gr_last
;
1621 (grp
->gr_last
= (*lpp
)->gr_last
)->gr_next
= grp
;
1622 (grp
->gr_next
= *lpp
)->gr_last
= grp
;
1625 rv
= !re2
? MLIST_SUBSCRIBED
: MLIST_KNOWN
;
1627 } while ((grp
= grp
->gr_next
) != *lpp
);
1629 if (!re2
&& !subscribed_only
) {
1631 lpp
= &_mlist_regex
;
1634 assert(rv
== MLIST_OTHER
);
1650 _group_print_all(GT_SHORTCUT
);
1651 else for (; *argv
!= NULL
; argv
+= 2) {
1652 /* Because one hardly ever redefines, anything is stored in one chunk */
1657 if (argv
[1] == NULL
) {
1658 n_err(_("Shortcut expansion is missing: \"%s\"\n"), *argv
);
1662 if (_group_find(GT_SHORTCUT
, *argv
) != NULL
)
1663 _group_del(GT_SHORTCUT
, *argv
);
1665 l
= strlen(argv
[1]) +1;
1666 gp
= _group_fetch(GT_SHORTCUT
, *argv
, l
);
1667 GP_TO_SUBCLASS(cp
, gp
);
1668 memcpy(cp
, argv
[1], l
);
1675 c_unshortcut(void *v
)
1681 do if (!_group_del(GT_SHORTCUT
, *argv
)) {
1682 n_err(_("No such shortcut: \"%s\"\n"), *argv
);
1684 } while (*++argv
!= NULL
);
1690 shortcut_expand(char const *str
)
1695 if ((gp
= _group_find(GT_SHORTCUT
, str
)) != NULL
)
1696 GP_TO_SUBCLASS(str
, gp
);
1704 c_customhdr(void *v
){
1706 char const **argv
, *hcp
;
1712 if((hcp
= *(argv
= v
)) == NULL
){
1713 _group_print_all(GT_CUSTOMHDR
);
1717 /* Verify the header field name */
1718 for(rv
= 1;; ++hcp
){
1719 if(fieldnamechar(*hcp
))
1723 n_err(_("`customhdr': no name specified\n"));
1729 n_err(_("`customhdr': invalid name: \"%s\"\n"), *argv
);
1734 if(*++argv
== NULL
){
1735 if((gp
= _group_find(GT_CUSTOMHDR
, hcp
)) != NULL
)
1736 _group_print(gp
, stdout
);
1738 n_err(_("No such `customhdr': \"%s\"\n"), hcp
);
1742 /* Because one hardly ever redefines, anything is stored in one chunk */
1746 if(pstate
& PS_WYSHLIST_SAW_CONTROL
){
1747 n_err(_("`customhdr': \"%s\": control characters not allowed: %s%s\n"),
1748 hcp
, n_shell_quote_cp(*argv
), (argv
[1] != NULL
? "..." : ""));
1753 if(_group_find(GT_CUSTOMHDR
, hcp
) != NULL
)
1754 _group_del(GT_CUSTOMHDR
, hcp
);
1756 for(l
= i
= 0; argv
[i
] != NULL
; ++i
)
1757 l
+= strlen(argv
[i
]) + 1;
1758 gp
= _group_fetch(GT_CUSTOMHDR
, hcp
, l
+1);
1759 GP_TO_SUBCLASS(cp
, gp
);
1760 for(i
= 0; argv
[i
] != NULL
; ++i
){
1763 l
= strlen(argv
[i
]);
1764 memcpy(cp
, argv
[i
], l
);
1775 c_uncustomhdr(void *v
){
1784 if(!_group_del(GT_CUSTOMHDR
, *argv
)){
1785 n_err(_("No such custom header: \"%s\"\n"), *argv
);
1788 }while(*++argv
!= NULL
);
1793 FL
struct n_header_field
*
1794 n_customhdr_query(void){ /* XXX Uses salloc()! */
1795 struct group
const *gp
;
1797 struct n_header_field
*rv
, **tail
, *hfp
;
1803 for(h
= 0; h
< HSHSIZE
; ++h
)
1804 for(gp
= _customhdr_heads
[h
]; gp
!= NULL
; gp
= gp
->g_next
){
1808 GP_TO_SUBCLASS(cp
, gp
);
1809 nl
= (ui32_t
)strlen(gp
->g_id
) +1;
1810 bl
= (ui32_t
)strlen(cp
) +1;
1812 *tail
= hfp
= salloc(VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
) +
1814 tail
= &hfp
->hf_next
;
1815 hfp
->hf_next
= NULL
;
1816 hfp
->hf_nl
= nl
- 1;
1817 hfp
->hf_bl
= bl
- 1;
1818 memcpy(hfp
->hf_dat
, gp
->g_id
, nl
);
1819 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);
1822 /* TODO We have no copy-on-write environments yet, and since custom headers
1823 * TODO are a perfect subject for localization, add the OBSOLETE variable
1824 * TODO *customhdr*, too */
1826 char const *vp
= ok_vlook(customhdr
);
1829 char *buf
= savestr(vp
);
1831 while((vp
= n_strescsep(&buf
, ',', TRU1
)) != NULL
){
1833 char const *nstart
, *cp
;
1835 for(nstart
= cp
= vp
;; ++cp
){
1836 if(fieldnamechar(*cp
))
1840 n_err(_("Invalid nameless *customhdr* entry\n"));
1843 }else if(*cp
!= ':' && !blankchar(*cp
)){
1845 n_err(_("Invalid *customhdr* entry: \"%s\"\n"), vp
);
1850 nl
= (ui32_t
)PTR2SIZE(cp
- nstart
);
1852 while(blankchar(*cp
))
1856 while(blankchar(*cp
))
1858 bl
= (ui32_t
)strlen(cp
) +1;
1860 *tail
= hfp
= salloc(VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
) +
1862 tail
= &hfp
->hf_next
;
1863 hfp
->hf_next
= NULL
;
1865 hfp
->hf_bl
= bl
- 1;
1866 memcpy(hfp
->hf_dat
, nstart
, nl
);
1867 hfp
->hf_dat
[nl
++] = '\0';
1868 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);