1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Name lists, alternates and groups: aliases, mailing lists, shortcuts.
3 *@ TODO Dynamic hashmaps; names and (these) groups have _nothing_ in common!
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define n_FILE nam_a_grp
39 #ifndef HAVE_AMALGAMATION
44 /* Main types (bits not values for easier testing only) */
45 GT_COMMANDALIAS
= 1u<<0,
49 GT_CHARSETALIAS
= 1u<<4,
51 GT_MASK
= GT_COMMANDALIAS
| GT_ALIAS
| GT_MLIST
| GT_SHORTCUT
|
52 GT_CHARSETALIAS
| GT_FILETYPE
,
54 /* Subtype bits and flags */
58 /* Extended type mask to be able to reflect what we really have; i.e., mlist
59 * can have GT_REGEX if they are subscribed or not, but `mlsubscribe' should
60 * print out only GT_MLIST which have the GT_SUBSCRIBE attribute set */
61 GT_PRINT_MASK
= GT_MASK
| GT_SUBSCRIBE
66 ui32_t g_subclass_off
; /* of "subclass" in .g_id (if any) */
67 ui16_t g_id_len_sub
; /* length of .g_id: _subclass_off - this */
68 ui8_t g_type
; /* enum group_type */
69 /* Identifying name, of variable size. Dependent on actual "subtype" more
70 * data follows thereafter, but note this is always used (i.e., for regular
71 * expression entries this is still set to the plain string) */
72 char g_id
[n_VFIELD_SIZE(1)];
74 #define GP_TO_SUBCLASS(X,G) \
76 union __group_subclass {void *gs_vp; char *gs_cp;} __gs__;\
77 __gs__.gs_cp = (char*)n_UNCONST(G) + (G)->g_subclass_off;\
81 struct grp_names_head
{
82 struct grp_names
*gnh_head
;
86 struct grp_names
*gn_next
;
87 char gn_id
[n_VFIELD_SIZE(0)];
92 struct grp_regex
*gr_last
;
93 struct grp_regex
*gr_next
;
94 struct group
*gr_mygroup
; /* xxx because lists use grp_regex*! ?? */
95 size_t gr_hits
; /* Number of times this group matched */
100 struct a_nag_cmd_alias
{
101 struct str ca_expand
;
104 struct a_nag_file_type
{
109 struct group_lookup
{
110 struct group
**gl_htable
;
111 struct group
**gl_slot
;
112 struct group
*gl_slot_last
;
113 struct group
*gl_group
;
116 static struct n_file_type
const a_nag_OBSOLETE_xz
= { /* TODO v15 compat */
117 "xz", 2, "xz -cd", sizeof("xz -cd") -1, "xz -cz", sizeof("xz -cz") -1
118 }, a_nag_OBSOLETE_gz
= {
119 "gz", 2, "gzip -cd", sizeof("gzip -cd") -1, "gzip -cz", sizeof("gzip -cz") -1
120 }, a_nag_OBSOLETE_bz2
= {
121 "bz2", 3, "bzip2 -cd", sizeof("bzip2 -cd") -1,
122 "bzip2 -cz", sizeof("bzip2 -cz") -1
125 /* List of alternate names of user */
126 struct n_strlist
*a_nag_altnames
;
129 static struct group
*_commandalias_heads
[HSHSIZE
]; /* TODO dynamic hash */
132 static struct group
*_alias_heads
[HSHSIZE
]; /* TODO dynamic hash */
134 /* `mlist', `mlsubscribe'. Anything is stored in the hashmap.. */
135 static struct group
*_mlist_heads
[HSHSIZE
]; /* TODO dynamic hash */
137 /* ..but entries which have GT_REGEX set are false lookups and will really be
138 * accessed via sequential lists instead, which are type-specific for better
139 * performance, but also to make it possible to have ".*@xy.org" as a mlist
140 * and "(one|two)@xy.org" as a mlsubscription.
141 * These lists use a bit of QOS optimization in that a matching group will
142 * become relinked as the new list head if its hit count is
143 * (>= ((xy_hits / _xy_size) >> 2))
144 * Note that the hit counts only count currently linked in nodes.. */
146 static struct grp_regex
*_mlist_regex
, *_mlsub_regex
;
147 static size_t _mlist_size
, _mlist_hits
, _mlsub_size
, _mlsub_hits
;
151 static struct group
*_shortcut_heads
[HSHSIZE
]; /* TODO dynamic hashmaps! */
154 static struct group
*_charsetalias_heads
[HSHSIZE
];
157 static struct group
*_filetype_heads
[HSHSIZE
];
159 /* Same name, while taking care for *allnet*? */
160 static bool_t
a_nag_is_same_name(char const *n1
, char const *n2
);
162 /* Mark all nodes with the given name */
163 static struct name
*a_nag_namelist_mark_name(struct name
*np
, char const *name
);
165 /* Grab a single name (liberal name) */
166 static char const * yankname(char const *ap
, char *wbuf
,
167 char const *separators
, int keepcomms
);
169 /* Extraction multiplexer that splits an input line to names */
170 static struct name
* _extract1(char const *line
, enum gfield ntype
,
171 char const *separators
, bool_t keepcomms
);
173 /* Recursively expand a alias name. Limit expansion to some fixed level.
174 * Direct recursion is not expanded for convenience */
175 static struct name
*a_nag_gexpand(size_t level
, struct name
*nlist
,
176 struct group
*gp
, bool_t metoo
, int ntype
);
179 static int a_nag_elide_qsort(void const *s1
, void const *s2
);
181 /* Lookup a group, return it or NULL, fill in glp anyway */
182 static struct group
* _group_lookup(enum group_type gt
,
183 struct group_lookup
*glp
, char const *id
);
185 /* Easier-to-use wrapper around _group_lookup() */
186 static struct group
* _group_find(enum group_type gt
, char const *id
);
188 /* Iteration: go to the first group, which also inits the iterator. A valid
189 * iterator can be stepped via _next(). A NULL return means no (more) groups
190 * to be iterated exist, in which case only glp->gl_group is set (NULL) */
191 static struct group
* _group_go_first(enum group_type gt
,
192 struct group_lookup
*glp
);
193 static struct group
* _group_go_next(struct group_lookup
*glp
);
195 /* Fetch the group id, create it as necessary, fail with NULL if impossible */
196 static struct group
* _group_fetch(enum group_type gt
, char const *id
,
199 /* "Intelligent" delete which handles a "*" id, too;
200 * returns a true boolean if a group was deleted, and always succeeds for "*" */
201 static bool_t
_group_del(enum group_type gt
, char const *id
);
203 static struct group
* __group_del(struct group_lookup
*glp
);
204 static void __names_del(struct group
*gp
);
206 /* Print all groups of the given type, alphasorted */
207 static void _group_print_all(enum group_type gt
);
209 static int __group_print_qsorter(void const *a
, void const *b
);
211 /* Really print a group, actually. Return number of written lines */
212 static size_t _group_print(struct group
const *gp
, FILE *fo
);
214 /* Multiplexers for list and subscribe commands */
215 static int _mlmux(enum group_type gt
, char **argv
);
216 static int _unmlmux(enum group_type gt
, char **argv
);
218 /* Relinkers for the sequential match lists */
220 static void _mlmux_linkin(struct group
*gp
);
221 static void _mlmux_linkout(struct group
*gp
);
222 # define _MLMUX_LINKIN(GP) \
223 do if ((GP)->g_type & GT_REGEX) _mlmux_linkin(GP); while (0)
224 # define _MLMUX_LINKOUT(GP) \
225 do if ((GP)->g_type & GT_REGEX) _mlmux_linkout(GP); while (0)
227 # define _MLMUX_LINKIN(GP)
228 # define _MLMUX_LINKOUT(GP)
232 a_nag_is_same_name(char const *n1
, char const *n2
){
237 if(ok_blook(allnet
)){
248 }while(c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
250 rv
= !asccasecmp(n1
, n2
);
256 a_nag_namelist_mark_name(struct name
*np
, char const *name
){
260 for(p
= np
; p
!= NULL
; p
= p
->n_flink
)
261 if(!(p
->n_type
& GDEL
) && !(p
->n_flags
& (ui32_t
)SI32_MIN
) &&
262 a_nag_is_same_name(p
->n_name
, name
))
263 p
->n_flags
|= (ui32_t
)SI32_MIN
;
269 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
272 char *wp
, c
, inquote
, lc
, lastsp
;
277 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
278 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
285 /* Parse a full name: TODO RFC 5322
286 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
287 * - Skip entire (nested) comments
288 * - In non-quote, non-comment, join adjacent space to a single SP
289 * - Understand separators only in non-quote, non-comment context,
290 * and only if not part of a *quoted-pair* (XXX too liberal) */
292 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
301 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
307 if (inquote
|| lc
== '\\') {
315 cp
= skip_comment(cp
+ 1);
323 if (strchr(separators
, c
) != NULL
)
327 lastsp
= blankchar(c
);
341 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
344 struct name
*topp
, *np
, *t
;
350 if (line
== NULL
|| *line
== '\0')
355 nbuf
= smalloc(strlen(line
) +1);
356 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
357 t
= nalloc(nbuf
, ntype
);
372 a_nag_gexpand(size_t level
, struct name
*nlist
, struct group
*gp
, bool_t metoo
,
374 struct grp_names
*gnp
;
375 struct name
*nlist_tail
;
377 struct grp_names_head
*gnhp
;
380 if(UICMP(z
, level
++, >, n_ALIAS_MAXEXP
)){
381 n_err(_("Expanding alias to depth larger than %d\n"), n_ALIAS_MAXEXP
);
385 GP_TO_SUBCLASS(gnhp
, gp
);
386 logname
= ok_vlook(LOGNAME
);
388 for(gnp
= gnhp
->gnh_head
; gnp
!= NULL
; gnp
= gnp
->gn_next
){
394 if(!strcmp(cp
, gp
->g_id
))
397 if((ngp
= _group_find(GT_ALIAS
, cp
)) != NULL
){
398 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
399 * to a full address specification; aliases cannot be empty */
400 struct grp_names_head
*ngnhp
;
401 GP_TO_SUBCLASS(ngnhp
, ngp
);
403 assert(ngnhp
->gnh_head
!= NULL
);
404 if(metoo
|| ngnhp
->gnh_head
->gn_next
!= NULL
||
405 !a_nag_is_same_name(cp
, logname
))
406 nlist
= a_nag_gexpand(level
, nlist
, ngp
, metoo
, ntype
);
410 /* Here we should allow to expand to itself if only person in alias */
412 if(metoo
|| gnhp
->gnh_head
->gn_next
== NULL
||
413 !a_nag_is_same_name(cp
, logname
)){
416 np
= nalloc(cp
, ntype
| GFULL
);
417 if((nlist_tail
= nlist
) != NULL
){
418 while(nlist_tail
->n_flink
!= NULL
)
419 nlist_tail
= nlist_tail
->n_flink
;
420 nlist_tail
->n_flink
= np
;
421 np
->n_blink
= nlist_tail
;
432 a_nag_elide_qsort(void const *s1
, void const *s2
){
433 struct name
const * const *np1
, * const *np2
;
439 rv
= asccasecmp((*np1
)->n_name
, (*np2
)->n_name
);
444 static struct group
*
445 _group_lookup(enum group_type gt
, struct group_lookup
*glp
, char const *id
){
447 struct group
*lgp
, *gp
;
453 ( gt
& GT_COMMANDALIAS
? _commandalias_heads
454 : (gt
& GT_ALIAS
? _alias_heads
455 : (gt
& GT_MLIST
? _mlist_heads
456 : (gt
& GT_SHORTCUT
? _shortcut_heads
457 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
458 : (/*gt & GT_FILETYPE ?*/ _filetype_heads
460 gp
= *(glp
->gl_slot
= &glp
->gl_htable
[
461 ((gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
))
462 ? n_torek_ihash(id
) : n_torek_hash(id
)) % HSHSIZE
]);
465 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
467 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
468 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&&
469 !asccasecmp(&gp
->g_id
[1], id
))
472 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
473 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&& !strcmp(&gp
->g_id
[1], id
))
477 glp
->gl_slot_last
= lgp
;
483 static struct group
*
484 _group_find(enum group_type gt
, char const *id
)
486 struct group_lookup gl
;
490 gp
= _group_lookup(gt
, &gl
, id
);
495 static struct group
*
496 _group_go_first(enum group_type gt
, struct group_lookup
*glp
)
498 struct group
**gpa
, *gp
;
502 for (glp
->gl_htable
= gpa
= (
503 gt
& GT_COMMANDALIAS
? _commandalias_heads
504 : (gt
& GT_ALIAS
? _alias_heads
505 : (gt
& GT_MLIST
? _mlist_heads
506 : (gt
& GT_SHORTCUT
? _shortcut_heads
507 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
508 : (gt
& GT_FILETYPE
? _filetype_heads
511 i
< HSHSIZE
; ++gpa
, ++i
)
512 if ((gp
= *gpa
) != NULL
) {
518 glp
->gl_group
= gp
= NULL
;
520 glp
->gl_slot_last
= NULL
;
525 static struct group
*
526 _group_go_next(struct group_lookup
*glp
)
528 struct group
*gp
, **gpa
;
531 if ((gp
= glp
->gl_group
->g_next
) != NULL
)
532 glp
->gl_slot_last
= glp
->gl_group
;
534 glp
->gl_slot_last
= NULL
;
535 for (gpa
= glp
->gl_htable
+ HSHSIZE
; ++glp
->gl_slot
< gpa
;)
536 if ((gp
= *glp
->gl_slot
) != NULL
)
544 static struct group
*
545 _group_fetch(enum group_type gt
, char const *id
, size_t addsz
)
547 struct group_lookup gl
;
552 if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
)
556 if (UIZ_MAX
- n_ALIGN(l
) <= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
)))
559 i
= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
) + l
);
560 switch (gt
& GT_MASK
) {
561 case GT_COMMANDALIAS
:
562 addsz
+= sizeof(struct a_nag_cmd_alias
);
565 addsz
+= sizeof(struct grp_names_head
);
568 addsz
+= sizeof(struct a_nag_file_type
);
572 if (n_is_maybe_regex(id
)) {
573 addsz
= sizeof(struct grp_regex
);
579 case GT_CHARSETALIAS
:
583 if (UIZ_MAX
- i
< addsz
|| UI32_MAX
<= i
|| UI16_MAX
< i
- l
)
586 gp
= smalloc(i
+ addsz
);
587 memcpy(gp
->g_id
, id
, l
);
588 gp
->g_subclass_off
= (ui32_t
)i
;
589 gp
->g_id_len_sub
= (ui16_t
)(i
- --l
);
591 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
594 for(cp
= gp
->g_id
; (c
= *cp
) != '\0'; ++cp
)
599 struct grp_names_head
*gnhp
;
601 GP_TO_SUBCLASS(gnhp
, gp
);
602 gnhp
->gnh_head
= NULL
;
605 else if (/*(gt & GT_MLIST) &&*/ gt
& GT_REGEX
) {
607 struct grp_regex
*grp
;
608 GP_TO_SUBCLASS(grp
, gp
);
610 if((s
= regcomp(&grp
->gr_regex
, id
,
611 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
612 n_err(_("Invalid regular expression: %s: %s\n"),
613 n_shexp_quote_cp(id
, FAL0
), n_regex_err_to_doc(&grp
->gr_regex
, s
));
618 grp
->gr_mygroup
= gp
;
623 gp
->g_next
= *gl
.gl_slot
;
631 _group_del(enum group_type gt
, char const *id
)
633 enum group_type xgt
= gt
& GT_MASK
;
634 struct group_lookup gl
;
638 /* Delete 'em all? */
639 if (id
[0] == '*' && id
[1] == '\0') {
640 for (gp
= _group_go_first(gt
, &gl
); gp
!= NULL
;)
641 gp
= (gp
->g_type
& xgt
) ? __group_del(&gl
) : _group_go_next(&gl
);
642 gp
= (struct group
*)TRU1
;
643 } else if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
) {
644 if (gp
->g_type
& xgt
)
653 static struct group
*
654 __group_del(struct group_lookup
*glp
)
656 struct group
*x
, *gp
;
659 /* Overly complicated: link off this node, step ahead to next.. */
661 if((gp
= glp
->gl_slot_last
) != NULL
)
662 gp
= (gp
->g_next
= x
->g_next
);
664 glp
->gl_slot_last
= NULL
;
665 gp
= (*glp
->gl_slot
= x
->g_next
);
670 for(gpa
= &glp
->gl_htable
[HSHSIZE
]; ++glp
->gl_slot
< gpa
;)
671 if((gp
= *glp
->gl_slot
) != NULL
)
677 if (x
->g_type
& GT_ALIAS
)
680 else if (/*(x->g_type & GT_MLIST) &&*/ x
->g_type
& GT_REGEX
) {
681 struct grp_regex
*grp
;
682 GP_TO_SUBCLASS(grp
, x
);
684 regfree(&grp
->gr_regex
);
695 __names_del(struct group
*gp
)
697 struct grp_names_head
*gnhp
;
698 struct grp_names
*gnp
;
701 GP_TO_SUBCLASS(gnhp
, gp
);
702 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
;) {
703 struct grp_names
*x
= gnp
;
711 _group_print_all(enum group_type gt
)
715 struct group
const *gp
;
722 xgt
= gt
& GT_PRINT_MASK
;
723 gpa
= ( xgt
& GT_COMMANDALIAS
? _commandalias_heads
724 : (xgt
& GT_ALIAS
? _alias_heads
725 : (xgt
& GT_MLIST
? _mlist_heads
726 : (xgt
& GT_SHORTCUT
? _shortcut_heads
727 : (xgt
& GT_CHARSETALIAS
? _charsetalias_heads
728 : (xgt
& GT_FILETYPE
? _filetype_heads
731 for (h
= 0, i
= 1; h
< HSHSIZE
; ++h
)
732 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
733 if ((gp
->g_type
& xgt
) == xgt
)
735 ida
= salloc(i
* sizeof *ida
);
737 for (i
= h
= 0; h
< HSHSIZE
; ++h
)
738 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
739 if ((gp
->g_type
& xgt
) == xgt
)
744 qsort(ida
, i
, sizeof *ida
, &__group_print_qsorter
);
746 if ((fp
= Ftmp(NULL
, "prgroup", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
750 for (i
= 0; ida
[i
] != NULL
; ++i
)
751 lines
+= _group_print(_group_find(gt
, ida
[i
]), fp
);
754 if (gt
& GT_SUBSCRIBE
)
755 i
= (ui32_t
)_mlsub_size
, h
= (ui32_t
)_mlsub_hits
;
757 i
= (ui32_t
)_mlist_size
, h
= (ui32_t
)_mlist_hits
;
758 if (i
> 0 && (n_poption
& n_PO_D_V
)){
759 fprintf(fp
, _("# %s list regex(7) total: %u entries, %u hits\n"),
760 (gt
& GT_SUBSCRIBE
? _("Subscribed") : _("Non-subscribed")),
767 if (fp
!= n_stdout
) {
768 page_or_print(fp
, lines
);
775 __group_print_qsorter(void const *a
, void const *b
)
780 rv
= strcmp(*(char**)n_UNCONST(a
), *(char**)n_UNCONST(b
));
786 _group_print(struct group
const *gp
, FILE *fo
)
794 if(gp
->g_type
& GT_COMMANDALIAS
){
795 struct a_nag_cmd_alias
*ncap
;
797 GP_TO_SUBCLASS(ncap
, gp
);
798 fprintf(fo
, "commandalias %s %s\n",
799 n_shexp_quote_cp(gp
->g_id
, TRU1
),
800 n_shexp_quote_cp(ncap
->ca_expand
.s
, TRU1
));
801 } else if (gp
->g_type
& GT_ALIAS
) {
802 struct grp_names_head
*gnhp
;
803 struct grp_names
*gnp
;
805 fprintf(fo
, "alias %s ", gp
->g_id
);
807 GP_TO_SUBCLASS(gnhp
, gp
);
808 if ((gnp
= gnhp
->gnh_head
) != NULL
) { /* xxx always 1+ entries */
810 struct grp_names
*x
= gnp
;
812 fprintf(fo
, " \"%s\"", string_quote(x
->gn_id
)); /* TODO shexp */
813 } while (gnp
!= NULL
);
816 } else if (gp
->g_type
& GT_MLIST
) {
818 if ((gp
->g_type
& GT_REGEX
) && (n_poption
& n_PO_D_V
)){
820 struct grp_regex
*grp
,
821 *lp
= (gp
->g_type
& GT_SUBSCRIBE
? _mlsub_regex
: _mlist_regex
);
823 GP_TO_SUBCLASS(grp
, gp
);
824 for (i
= 1; lp
!= grp
; lp
= lp
->gr_next
)
826 fprintf(fo
, "# regex(7): hits %" PRIuZ
", sort %" PRIuZ
".\n ",
832 fprintf(fo
, "wysh %s %s\n",
833 (gp
->g_type
& GT_SUBSCRIBE
? "mlsubscribe" : "mlist"),
834 n_shexp_quote_cp(gp
->g_id
, TRU1
));
835 } else if (gp
->g_type
& GT_SHORTCUT
) {
836 GP_TO_SUBCLASS(cp
, gp
);
837 fprintf(fo
, "wysh shortcut %s %s\n",
838 gp
->g_id
, n_shexp_quote_cp(cp
, TRU1
));
839 } else if (gp
->g_type
& GT_CHARSETALIAS
) {
840 GP_TO_SUBCLASS(cp
, gp
);
841 fprintf(fo
, "charsetalias %s %s\n",
842 n_shexp_quote_cp(gp
->g_id
, TRU1
), n_shexp_quote_cp(cp
, TRU1
));
843 } else if (gp
->g_type
& GT_FILETYPE
) {
844 struct a_nag_file_type
*nftp
;
846 GP_TO_SUBCLASS(nftp
, gp
);
847 fprintf(fo
, "filetype %s %s %s\n",
848 n_shexp_quote_cp(gp
->g_id
, TRU1
),
849 n_shexp_quote_cp(nftp
->nft_load
.s
, TRU1
),
850 n_shexp_quote_cp(nftp
->nft_save
.s
, TRU1
));
858 _mlmux(enum group_type gt
, char **argv
)
869 _group_print_all(gt
);
871 if ((gp
= _group_find(gt
, *argv
)) != NULL
) {
872 if (gt
& GT_SUBSCRIBE
) {
873 if (!(gp
->g_type
& GT_SUBSCRIBE
)) {
875 gp
->g_type
|= GT_SUBSCRIBE
;
878 ecp
= N_("Mailing-list already `mlsubscribe'd: %s\n");
882 ecp
= N_("Mailing-list already `mlist'ed: %s\n");
885 } else if(_group_fetch(gt
, *argv
, 0) == NULL
) {
886 ecp
= N_("Failed to create storage for mailing-list: %s\n");
888 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
891 } while (*++argv
!= NULL
);
898 _unmlmux(enum group_type gt
, char **argv
)
904 for (; *argv
!= NULL
; ++argv
) {
905 if (gt
& GT_SUBSCRIBE
) {
906 struct group_lookup gl
;
909 if (!(isaster
= (**argv
== '*')))
910 gp
= _group_find(gt
, *argv
);
911 else if ((gp
= _group_go_first(gt
, &gl
)) == NULL
)
913 else if (gp
!= NULL
&& !(gp
->g_type
& GT_SUBSCRIBE
))
918 if (gp
->g_type
& GT_SUBSCRIBE
) {
920 gp
->g_type
&= ~GT_SUBSCRIBE
;
924 while ((gp
= _group_go_next(&gl
)) != NULL
&&
925 !(gp
->g_type
& GT_SUBSCRIBE
))
931 n_err(_("Mailing-list not `mlsubscribe'd: %s\n"),
932 n_shexp_quote_cp(*argv
, FAL0
));
937 } else if (_group_del(gt
, *argv
))
939 n_err(_("No such mailing-list: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
948 _mlmux_linkin(struct group
*gp
)
950 struct grp_regex
**lpp
, *grp
, *lhp
;
953 if (gp
->g_type
& GT_SUBSCRIBE
) {
961 GP_TO_SUBCLASS(grp
, gp
);
962 if ((lhp
= *lpp
) != NULL
) {
963 (grp
->gr_last
= lhp
->gr_last
)->gr_next
= grp
;
964 (grp
->gr_next
= lhp
)->gr_last
= grp
;
966 *lpp
= grp
->gr_last
= grp
->gr_next
= grp
;
972 _mlmux_linkout(struct group
*gp
)
974 struct grp_regex
*grp
, **lpp
;
977 GP_TO_SUBCLASS(grp
, gp
);
979 if (gp
->g_type
& GT_SUBSCRIBE
) {
982 _mlsub_hits
-= grp
->gr_hits
;
986 _mlist_hits
-= grp
->gr_hits
;
989 if (grp
->gr_next
== grp
)
992 (grp
->gr_last
->gr_next
= grp
->gr_next
)->gr_last
= grp
->gr_last
;
998 #endif /* HAVE_REGEX */
1001 nalloc(char const *str
, enum gfield ntype
)
1003 struct n_addrguts ag
;
1007 assert(!(ntype
& GFULLEXTRA
) || (ntype
& GFULL
) != 0);
1009 str
= n_addrspec_with_guts(&ag
, str
,
1010 ((ntype
& (GFULL
| GSKIN
| GREF
)) != 0), FAL0
);
1013 np = NULL; TODO We cannot return NULL,
1014 goto jleave; TODO thus handle failures in here!
1019 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
1020 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
1021 np
= salloc(sizeof(*np
) + ag
.ag_slen
+1);
1022 memcpy(np
+ 1, ag
.ag_skinned
, ag
.ag_slen
+1);
1023 ag
.ag_skinned
= (char*)(np
+ 1);
1025 np
= salloc(sizeof *np
);
1032 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
1033 np
->n_fullextra
= NULL
;
1034 np
->n_flags
= ag
.ag_n_flags
;
1036 if (ntype
& GFULL
) {
1037 if (ag
.ag_ilen
== ag
.ag_slen
1039 && !(ag
.ag_n_flags
& NAME_IDNA
)
1043 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
1046 /* n_fullextra is only the complete name part without address.
1047 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
1048 if ((ntype
& GFULLEXTRA
) && ag
.ag_ilen
> ag
.ag_slen
+ 2) {
1049 size_t s
= ag
.ag_iaddr_start
, e
= ag
.ag_iaddr_aend
, i
;
1052 if (s
== 0 || str
[--s
] != '<' || str
[e
++] != '>')
1053 goto jskipfullextra
;
1055 in
.s
= n_lofi_alloc(s
+ 1 + i
+1);
1056 while(s
> 0 && blankchar(str
[s
- 1]))
1058 memcpy(in
.s
, str
, s
);
1061 while (blankchar(str
[e
])) {
1067 memcpy(&in
.s
[s
], &str
[e
], i
);
1070 in
.s
[in
.l
= s
] = '\0';
1071 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1073 for (cp
= out
.s
, i
= out
.l
; i
> 0 && spacechar(*cp
); --i
, ++cp
)
1075 while (i
> 0 && spacechar(cp
[i
- 1]))
1077 np
->n_fullextra
= savestrbuf(cp
, i
);
1084 /* n_fullname depends on IDNA conversion */
1086 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
1088 in
.s
= n_UNCONST(str
);
1092 /* The domain name was IDNA and has been converted. We also have to
1093 * ensure that the domain name in .n_fullname is replaced with the
1094 * converted version, since MIME doesn't perform encoding of addrs */
1095 /* TODO This definetely doesn't belong here! */
1096 size_t l
= ag
.ag_iaddr_start
,
1097 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
1098 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
1099 memcpy(in
.s
, str
, l
);
1100 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
1102 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
1108 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1109 np
->n_fullname
= savestr(out
.s
);
1112 if (ag
.ag_n_flags
& NAME_IDNA
)
1115 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
1123 ndup(struct name
*np
, enum gfield ntype
)
1128 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
1129 nnp
= nalloc(np
->n_name
, ntype
);
1133 nnp
= salloc(sizeof *np
);
1134 nnp
->n_flink
= nnp
->n_blink
= NULL
;
1135 nnp
->n_type
= ntype
;
1136 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
1138 nnp
->n_name
= savestr(np
->n_name
);
1139 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
))) {
1140 nnp
->n_fullname
= nnp
->n_name
;
1141 nnp
->n_fullextra
= NULL
;
1143 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
1144 nnp
->n_fullname
= savestr(np
->n_fullname
);
1145 nnp
->n_fullextra
= (np
->n_fullextra
== NULL
) ? NULL
1146 : savestr(np
->n_fullextra
);
1154 cat(struct name
*n1
, struct name
*n2
)
1166 while (tail
->n_flink
!= NULL
)
1167 tail
= tail
->n_flink
;
1177 namelist_dup(struct name
const *np
, enum gfield ntype
){
1178 struct name
*nlist
, *xnp
;
1181 for(nlist
= xnp
= NULL
; np
!= NULL
; np
= np
->n_flink
){
1184 x
= ndup(n_UNCONST(np
), (np
->n_type
& ~GMASK
) | ntype
);
1185 if((x
->n_blink
= xnp
) == NULL
)
1196 count(struct name
const *np
)
1201 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1202 if (!(np
->n_type
& GDEL
))
1209 count_nonlocal(struct name
const *np
)
1214 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1215 if (!(np
->n_type
& GDEL
) && !(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
))
1222 extract(char const *line
, enum gfield ntype
)
1227 rv
= _extract1(line
, ntype
, " \t,", 0);
1233 lextract(char const *line
, enum gfield ntype
)
1238 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
1239 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
1245 detract(struct name
*np
, enum gfield ntype
)
1256 flags
= ntype
& (GCOMMA
| GNAMEONLY
);
1257 ntype
&= ~(GCOMMA
| GNAMEONLY
);
1260 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1261 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1263 s
+= strlen(flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
) +1;
1273 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1274 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1276 cp
= sstpcpy(cp
, (flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
));
1277 if ((flags
& GCOMMA
) && p
->n_flink
!= NULL
)
1282 if ((flags
& GCOMMA
) && *--cp
== ',')
1290 grab_names(enum n_go_input_flags gif
, char const *field
, struct name
*np
,
1291 int comma
, enum gfield gflags
)
1297 np
= lextract(n_go_input_cp(gif
, field
, detract(np
, comma
)), gflags
);
1298 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
1299 if (is_addr_invalid(nq
, EACM_NONE
))
1306 name_is_same_domain(struct name
const *n1
, struct name
const *n2
)
1308 char const *d1
, *d2
;
1312 d1
= strrchr(n1
->n_name
, '@');
1313 d2
= strrchr(n2
->n_name
, '@');
1315 rv
= (d1
!= NULL
&& d2
!= NULL
) ? !asccasecmp(++d1
, ++d2
) : FAL0
;
1322 checkaddrs(struct name
*np
, enum expand_addr_check_mode eacm
,
1323 si8_t
*set_on_error
)
1328 for (n
= np
; n
!= NULL
; n
= n
->n_flink
) {
1331 if ((rv
= is_addr_invalid(n
, eacm
)) != 0) {
1332 if (set_on_error
!= NULL
)
1333 *set_on_error
|= rv
; /* don't loose -1! */
1334 else if (eacm
& EAF_MAYKEEP
) /* TODO HACK! See definition! */
1337 n
->n_blink
->n_flink
= n
->n_flink
;
1339 n
->n_flink
->n_blink
= n
->n_blink
;
1349 namelist_vaporise_head(struct header
*hp
, enum expand_addr_check_mode eacm
,
1350 bool_t metoo
, si8_t
*set_on_error
)
1352 /* TODO namelist_vaporise_head() is incredibly expensive and redundant */
1353 struct name
*tolist
, *np
, **npp
;
1356 tolist
= cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
));
1357 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
1359 tolist
= usermap(tolist
, metoo
);
1360 tolist
= n_alternates_remove(tolist
, TRU1
);
1361 tolist
= elide(checkaddrs(tolist
, eacm
, set_on_error
));
1363 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
1364 switch (np
->n_type
& (GDEL
| GMASK
)) {
1365 case GTO
: npp
= &hp
->h_to
; break;
1366 case GCC
: npp
= &hp
->h_cc
; break;
1367 case GBCC
: npp
= &hp
->h_bcc
; break;
1370 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
1377 usermap(struct name
*names
, bool_t force_metoo
){
1379 struct name
*nlist
, *nlist_tail
, *np
, *cp
;
1383 metoo
= (force_metoo
|| ok_blook(metoo
));
1384 nlist
= nlist_tail
= NULL
;
1387 for(; np
!= NULL
; np
= cp
){
1388 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
1391 if(is_fileorpipe_addr(np
) ||
1392 (gp
= _group_find(GT_ALIAS
, np
->n_name
)) == NULL
){
1393 if((np
->n_blink
= nlist_tail
) != NULL
)
1394 nlist_tail
->n_flink
= np
;
1400 nlist
= a_nag_gexpand(0, nlist
, gp
, metoo
, np
->n_type
);
1401 if((nlist_tail
= nlist
) != NULL
)
1402 while(nlist_tail
->n_flink
!= NULL
)
1403 nlist_tail
= nlist_tail
->n_flink
;
1411 elide(struct name
*names
)
1414 struct name
*nlist
, *np
, **nparr
;
1422 /* Throw away all deleted nodes */
1423 for(np
= NULL
, i
= 0; names
!= NULL
; names
= names
->n_flink
)
1424 if(!(names
->n_type
& GDEL
)){
1425 names
->n_blink
= np
;
1427 np
->n_flink
= names
;
1433 if(nlist
== NULL
|| i
== 1)
1437 /* Create a temporay array and sort that */
1438 nparr
= n_lofi_alloc(sizeof(*nparr
) * i
);
1440 for(i
= 0, np
= nlist
; np
!= NULL
; np
= np
->n_flink
)
1443 qsort(nparr
, i
, sizeof *nparr
, &a_nag_elide_qsort
);
1445 /* Remove duplicates XXX speedup, or list_uniq()! */
1446 for(j
= 0, --i
; j
< i
;){
1447 if(asccasecmp(nparr
[j
]->n_name
, nparr
[k
= j
+ 1]->n_name
))
1451 nparr
[k
] = nparr
[k
+ 1];
1456 /* Throw away all list members which are not part of the array.
1457 * Note this keeps the original, possibly carefully crafted, order of the
1458 * addressees, thus */
1459 for(np
= nlist
; np
!= NULL
; np
= np
->n_flink
){
1460 for(j
= 0; j
<= i
; ++j
)
1467 nlist
= np
->n_flink
;
1470 np
->n_blink
->n_flink
= np
->n_flink
;
1471 if(np
->n_flink
!= NULL
)
1472 np
->n_flink
->n_blink
= np
->n_blink
;
1483 c_alternates(void *v
){ /* TODO use a hashmap!! */
1490 if(*(argv
= v
) == NULL
){
1493 if((ccp
= ok_vlook(alternates
)) != NULL
)
1494 fprintf(n_stdout
, "alternates %s\n", ccp
);
1496 fputs(_("# no alternates registered\n"), n_stdout
);
1500 struct n_strlist
*slp
, **slpa
;
1502 while((slp
= a_nag_altnames
) != NULL
){
1503 a_nag_altnames
= slp
->sl_next
;
1508 /* Extension: only clearance? */
1509 if(argv
[1] == NULL
&& argv
[0][0] == '-' && argv
[0][1] == '\0')
1511 else for(slpa
= &a_nag_altnames
; *argv
!= NULL
; ++argv
){
1515 if((np
= lextract(*argv
, GSKIN
)) == NULL
|| np
->n_flink
!= NULL
||
1516 (np
= checkaddrs(np
, EACM_STRICT
, NULL
)) == NULL
){
1517 n_err(_("Invalid `alternates' argument: %s\n"),
1518 n_shexp_quote_cp(*argv
, FAL0
));
1523 l
= strlen(np
->n_name
);
1524 if(UIZ_MAX
- l
<= vl
){
1525 n_err(_("Failed to create storage for alternate: %s\n"),
1526 n_shexp_quote_cp(*argv
, FAL0
));
1531 slp
= n_STRLIST_ALLOC(l
);
1532 slp
->sl_next
= NULL
;
1534 memcpy(slp
->sl_dat
, np
->n_name
, ++l
);
1536 slpa
= &slp
->sl_next
;
1541 /* And put it into *alternates* */
1543 cp
= n_autorec_alloc(vl
);
1544 for(vl
= 0, slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
){
1545 memcpy(&cp
[vl
], slp
->sl_dat
, slp
->sl_len
);
1546 cp
[vl
+= slp
->sl_len
] = ' ';
1552 n_PS_ROOT_BLOCK(vl
> 0 ? ok_vset(alternates
, cp
) : ok_vclear(alternates
));
1559 n_alternates_remove(struct name
*np
, bool_t keep_single
){
1560 struct name
*xp
, *newnp
;
1563 /* Delete the temporary bit from all */
1564 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1565 xp
->n_flags
&= ~(ui32_t
)SI32_MIN
;
1567 /* Mark all possible alternate names (xxx sic) */
1569 if(a_nag_altnames
!= NULL
){
1570 struct n_strlist
*slp
;
1572 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1573 np
= a_nag_namelist_mark_name(np
, slp
->sl_dat
);
1576 np
= a_nag_namelist_mark_name(np
, ok_vlook(LOGNAME
));
1578 for(xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
); xp
!= NULL
;
1580 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1582 for(xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
); xp
!= NULL
;
1584 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1586 for(xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
); xp
!= NULL
;
1588 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1590 /* GDEL all (but a single) marked node(s) */
1591 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1592 if(xp
->n_flags
& (ui32_t
)SI32_MIN
){
1598 /* Clean the list by throwing away all deleted nodes */
1599 for(xp
= newnp
= NULL
; np
!= NULL
; np
= np
->n_flink
)
1600 if(!(np
->n_type
& GDEL
)){
1610 /* Delete the temporary bit from all remaining (again) */
1611 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1612 xp
->n_flags
&= ~(ui32_t
)SI32_MIN
;
1619 n_is_myname(char const *name
){
1623 if(a_nag_is_same_name(ok_vlook(LOGNAME
), name
))
1626 if(a_nag_altnames
!= NULL
){
1627 struct n_strlist
*slp
;
1629 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1630 if(a_nag_is_same_name(slp
->sl_dat
, name
))
1634 for(xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
); xp
!= NULL
;
1636 if(a_nag_is_same_name(xp
->n_name
, name
))
1639 for(xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
); xp
!= NULL
;
1641 if(a_nag_is_same_name(xp
->n_name
, name
))
1644 for(xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
); xp
!= NULL
;
1646 if(a_nag_is_same_name(xp
->n_name
, name
))
1652 return (name
!= NULL
);
1656 c_addrcodec(void *vp
){
1657 struct n_addrguts ag
;
1658 struct n_string s_b
, *sp
;
1661 char const **argv
, *varname
, *act
, *cp
;
1664 sp
= n_string_creat_auto(&s_b
);
1666 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1669 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1680 alen
= PTR2SIZE(cp
- act
);
1688 if(i
<= UIZ_MAX
/ 4)
1690 sp
= n_string_reserve(sp
, i
);
1693 n_pstate_err_no
= n_ERR_NONE
;
1695 if(is_ascncaseprefix(act
, "encode", alen
)){
1696 /* This function cannot be a simple nalloc() wrapper even later on, since
1697 * we may need to turn any ", () or \ into quoted-pairs */
1700 while((c
= *cp
++) != '\0'){
1701 if(((c
== '(' || c
== ')') && mode
< 1) || (c
== '"' && mode
< 2) ||
1702 (c
== '\\' && mode
< 3))
1703 sp
= n_string_push_c(sp
, '\\');
1704 sp
= n_string_push_c(sp
, c
);
1707 if(n_addrspec_with_guts(&ag
, n_string_cp(sp
), TRU1
, TRU1
) == NULL
||
1708 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1709 ) != NAME_ADDRSPEC_ISADDR
){
1711 n_pstate_err_no
= n_ERR_INVAL
;
1716 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1717 cp
= np
->n_fullname
;
1719 }else if(mode
== 0){
1720 if(is_ascncaseprefix(act
, "decode", alen
)){
1723 while((c
= *cp
++) != '\0'){
1726 sp
= n_string_push_c(sp
, '(');
1727 act
= skip_comment(cp
);
1729 sp
= n_string_push_buf(sp
, cp
, PTR2SIZE(act
- cp
));
1730 sp
= n_string_push_c(sp
, ')');
1735 if((c
= *cp
++) == '"')
1737 if(c
== '\\' && (c
= *cp
) != '\0')
1739 sp
= n_string_push_c(sp
, c
);
1743 if(c
== '\\' && (c
= *cp
++) == '\0')
1745 sp
= n_string_push_c(sp
, c
);
1749 cp
= n_string_cp(sp
);
1750 }else if(is_ascncaseprefix(act
, "skin", alen
)){
1751 /* Let's just use the is-single-address hack for this one, too.. */
1752 if(n_addrspec_with_guts(&ag
, cp
, TRU1
, TRU1
) == NULL
||
1753 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1754 ) != NAME_ADDRSPEC_ISADDR
){
1755 n_pstate_err_no
= n_ERR_INVAL
;
1760 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1768 if(varname
== NULL
){
1769 if(fprintf(n_stdout
, "%s\n", cp
) < 0){
1770 n_pstate_err_no
= n_err_no
;
1773 }else if(!n_var_vset(varname
, (uintptr_t)cp
)){
1774 n_pstate_err_no
= n_ERR_NOTSUP
;
1780 return (vp
!= NULL
? 0 : 1);
1782 n_err(_("Synopsis: addrcodec: <[+[+[+]]]e[ncode]|d[ecode]|s[kin]> "
1783 "<rest-of-line>\n"));
1784 n_pstate_err_no
= n_ERR_INVAL
;
1790 c_commandalias(void *vp
){
1792 char const **argv
, *ccp
;
1799 if((ccp
= *argv
) == NULL
){
1800 _group_print_all(GT_COMMANDALIAS
);
1804 /* Verify the name is a valid one, and not a command modifier */
1805 if(*ccp
== '\0' || *n_cmd_isolate(ccp
) != '\0' ||
1806 !asccasecmp(ccp
, "ignerr") || !asccasecmp(ccp
, "wysh") ||
1807 !asccasecmp(ccp
, "vput")){
1808 n_err(_("`commandalias': not a valid command name: %s\n"),
1809 n_shexp_quote_cp(ccp
, FAL0
));
1814 if(argv
[1] == NULL
){
1815 if((gp
= _group_find(GT_COMMANDALIAS
, ccp
)) != NULL
)
1816 _group_print(gp
, n_stdout
);
1818 n_err(_("No such commandalias: %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
1822 /* Because one hardly ever redefines, anything is stored in one chunk */
1826 /* Delete the old one, if any; don't get fooled to remove them all */
1827 if(ccp
[0] != '*' || ccp
[1] != '\0')
1828 _group_del(GT_COMMANDALIAS
, ccp
);
1830 for(i
= len
= 0, ++argv
; argv
[i
] != NULL
; ++i
)
1831 len
+= strlen(argv
[i
]) + 1;
1835 if((gp
= _group_fetch(GT_COMMANDALIAS
, ccp
, len
)) == NULL
){
1836 n_err(_("Failed to create storage for commandalias: %s\n"),
1837 n_shexp_quote_cp(ccp
, FAL0
));
1840 struct a_nag_cmd_alias
*ncap
;
1842 GP_TO_SUBCLASS(ncap
, gp
);
1843 GP_TO_SUBCLASS(cp
, gp
);
1845 ncap
->ca_expand
.s
= cp
;
1846 ncap
->ca_expand
.l
= len
- 1;
1848 for(len
= 0; (ccp
= *argv
++) != NULL
;)
1849 if((i
= strlen(ccp
)) > 0){
1864 c_uncommandalias(void *vp
){
1872 do if(!_group_del(GT_COMMANDALIAS
, *argv
)){
1873 n_err(_("No such `commandalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
1875 }while(*++argv
!= NULL
);
1881 n_commandalias_exists(char const *name
, struct str
const **expansion_or_null
){
1885 if((gp
= _group_find(GT_COMMANDALIAS
, name
)) != NULL
){
1888 if(expansion_or_null
!= NULL
){
1889 struct a_nag_cmd_alias
*ncap
;
1891 GP_TO_SUBCLASS(ncap
, gp
);
1892 *expansion_or_null
= &ncap
->ca_expand
;
1901 n_alias_is_valid_name(char const *name
){
1907 for(rv
= TRU1
, cp
= name
++; (c
= *cp
++) != '\0';)
1908 /* User names, plus things explicitly mentioned in Postfix aliases(5).
1909 * As an extension, allow period: [[:alnum:]_#:@.-]+$? */
1910 if(!alnumchar(c
) && c
!= '_' && c
!= '-' &&
1911 c
!= '#' && c
!= ':' && c
!= '@' &&
1913 if(c
== '$' && cp
!= name
&& *cp
== '\0')
1933 n_UNINIT(ecp
, NULL
);
1936 _group_print_all(GT_ALIAS
);
1937 else if (!n_alias_is_valid_name(*argv
)) {
1938 ecp
= N_("Not a valid alias name: %s\n");
1940 } else if (argv
[1] == NULL
) {
1941 if ((gp
= _group_find(GT_ALIAS
, *argv
)) != NULL
)
1942 _group_print(gp
, n_stdout
);
1944 ecp
= N_("No such alias: %s\n");
1947 } else if ((gp
= _group_fetch(GT_ALIAS
, *argv
, 0)) == NULL
) {
1948 ecp
= N_("Failed to create alias storage for: %s\n");
1950 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
1953 struct grp_names
*gnp_tail
, *gnp
;
1954 struct grp_names_head
*gnhp
;
1956 GP_TO_SUBCLASS(gnhp
, gp
);
1958 if((gnp_tail
= gnhp
->gnh_head
) != NULL
)
1959 while((gnp
= gnp_tail
->gn_next
) != NULL
)
1962 for(++argv
; *argv
!= NULL
; ++argv
){
1965 i
= strlen(*argv
) +1;
1966 gnp
= smalloc(n_VSTRUCT_SIZEOF(struct grp_names
, gn_id
) + i
);
1967 if(gnp_tail
!= NULL
)
1968 gnp_tail
->gn_next
= gnp
;
1970 gnhp
->gnh_head
= gnp
;
1972 gnp
->gn_next
= NULL
;
1973 memcpy(gnp
->gn_id
, *argv
, i
);
1987 do if (!_group_del(GT_ALIAS
, *argv
)) {
1988 n_err(_("No such alias: %s\n"), *argv
);
1990 } while (*++argv
!= NULL
);
2001 rv
= _mlmux(GT_MLIST
, v
);
2012 rv
= _unmlmux(GT_MLIST
, v
);
2018 c_mlsubscribe(void *v
)
2023 rv
= _mlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2029 c_unmlsubscribe(void *v
)
2034 rv
= _unmlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2040 is_mlist(char const *name
, bool_t subscribed_only
)
2044 struct grp_regex
**lpp
, *grp
;
2047 enum mlist_state rv
;
2050 gp
= _group_find(GT_MLIST
, name
);
2051 rv
= (gp
!= NULL
) ? MLIST_KNOWN
: MLIST_OTHER
;
2052 if (rv
== MLIST_KNOWN
) {
2053 if (gp
->g_type
& GT_SUBSCRIBE
)
2054 rv
= MLIST_SUBSCRIBED
;
2055 else if (subscribed_only
)
2057 /* Of course, if that is a regular expression it doesn't mean a thing */
2059 if (gp
->g_type
& GT_REGEX
)
2066 /* Not in the hashmap (as something matchable), walk the lists */
2069 lpp
= &_mlsub_regex
;
2071 if ((grp
= *lpp
) != NULL
) {
2072 do if (regexec(&grp
->gr_regex
, name
, 0,NULL
, 0) != REG_NOMATCH
) {
2073 /* Relink as the head of this list if the hit count of this group is
2074 * >= 25% of the average hit count */
2077 i
= ++_mlsub_hits
/ _mlsub_size
;
2079 i
= ++_mlist_hits
/ _mlist_size
;
2082 if (++grp
->gr_hits
>= i
&& *lpp
!= grp
&& grp
->gr_next
!= grp
) {
2083 grp
->gr_last
->gr_next
= grp
->gr_next
;
2084 grp
->gr_next
->gr_last
= grp
->gr_last
;
2085 (grp
->gr_last
= (*lpp
)->gr_last
)->gr_next
= grp
;
2086 (grp
->gr_next
= *lpp
)->gr_last
= grp
;
2089 rv
= !re2
? MLIST_SUBSCRIBED
: MLIST_KNOWN
;
2091 } while ((grp
= grp
->gr_next
) != *lpp
);
2093 if (!re2
&& !subscribed_only
) {
2095 lpp
= &_mlist_regex
;
2098 assert(rv
== MLIST_OTHER
);
2118 _group_print_all(GT_SHORTCUT
);
2119 else if(argv
[1] == NULL
){
2120 if((gp
= _group_find(GT_SHORTCUT
, *argv
)) != NULL
)
2121 _group_print(gp
, n_stdout
);
2123 n_err(_("No such shortcut: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2126 }else for (; *argv
!= NULL
; argv
+= 2) {
2127 /* Because one hardly ever redefines, anything is stored in one chunk */
2131 if (argv
[1] == NULL
) {
2132 n_err(_("Synopsis: shortcut: <shortcut> <expansion>\n"));
2136 if (_group_find(GT_SHORTCUT
, *argv
) != NULL
)
2137 _group_del(GT_SHORTCUT
, *argv
);
2139 l
= strlen(argv
[1]) +1;
2140 if ((gp
= _group_fetch(GT_SHORTCUT
, *argv
, l
)) == NULL
) {
2141 n_err(_("Failed to create storage for shortcut: %s\n"),
2142 n_shexp_quote_cp(*argv
, FAL0
));
2145 GP_TO_SUBCLASS(cp
, gp
);
2146 memcpy(cp
, argv
[1], l
);
2154 c_unshortcut(void *v
)
2160 do if (!_group_del(GT_SHORTCUT
, *argv
)) {
2161 n_err(_("No such shortcut: %s\n"), *argv
);
2163 } while (*++argv
!= NULL
);
2169 shortcut_expand(char const *str
){
2173 if((gp
= _group_find(GT_SHORTCUT
, str
)) != NULL
)
2174 GP_TO_SUBCLASS(str
, gp
);
2182 c_charsetalias(void *vp
){
2192 _group_print_all(GT_CHARSETALIAS
);
2193 else if(argv
[1] == NULL
){
2194 if((gp
= _group_find(GT_CHARSETALIAS
, *argv
)) != NULL
)
2195 _group_print(gp
, n_stdout
);
2197 n_err(_("No such charsetalias: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2200 }else for(; *argv
!= NULL
; argv
+= 2){
2201 /* Because one hardly ever redefines, anything is stored in one chunk */
2206 if(argv
[1] == NULL
){
2207 n_err(_("Synopsis: charsetalias: <charset> <charset-alias>\n"));
2212 /* Delete the old one, if any; don't get fooled to remove them all */
2214 if(ccp
[0] != '*' || ccp
[1] != '\0')
2215 _group_del(GT_CHARSETALIAS
, ccp
);
2217 l
= strlen(argv
[1]) +1;
2218 if ((gp
= _group_fetch(GT_CHARSETALIAS
, ccp
, l
)) == NULL
) {
2219 n_err(_("Failed to create storage for charsetalias: %s\n"),
2220 n_shexp_quote_cp(ccp
, FAL0
));
2223 GP_TO_SUBCLASS(cp
, gp
);
2224 for(ccp
= argv
[1]; (c
= *ccp
++) != '\0';)
2225 *cp
++ = lowerconv(c
);
2234 c_uncharsetalias(void *vp
){
2242 do if(!_group_del(GT_CHARSETALIAS
, *argv
)){
2243 n_err(_("No such `charsetalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2245 }while(*++argv
!= NULL
);
2251 n_charsetalias_expand(char const *cp
){
2254 char const *cp_orig
;
2259 for(i
= 0; (gp
= _group_find(GT_CHARSETALIAS
, cp
)) != NULL
;){
2260 GP_TO_SUBCLASS(cp
, gp
);
2261 if(++i
== 8) /* XXX Magic (same as for `ghost' expansion) */
2272 c_filetype(void *vp
){ /* TODO support automatic chains: .tar.gz -> .gz + .tar */
2274 char **argv
; /* TODO While there: let ! prefix mean: direct execlp(2) */
2282 _group_print_all(GT_FILETYPE
);
2283 else if(argv
[1] == NULL
){
2284 if((gp
= _group_find(GT_FILETYPE
, *argv
)) != NULL
)
2285 _group_print(gp
, n_stdout
);
2287 n_err(_("No such filetype: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2290 }else for(; *argv
!= NULL
; argv
+= 3){
2291 /* Because one hardly ever redefines, anything is stored in one chunk */
2296 if(argv
[1] == NULL
|| argv
[2] == NULL
){
2297 n_err(_("Synopsis: filetype: <extension> <load-cmd> <save-cmd>\n"));
2302 /* Delete the old one, if any; don't get fooled to remove them all */
2304 if(ccp
[0] != '*' || ccp
[1] != '\0')
2305 _group_del(GT_FILETYPE
, ccp
);
2307 /* Lowercase it all (for display purposes) */
2310 while((c
= *cp
) != '\0')
2311 *cp
++ = lowerconv(c
);
2313 llc
= strlen(argv
[1]) +1;
2314 lsc
= strlen(argv
[2]) +1;
2315 if(UIZ_MAX
- llc
<= lsc
)
2318 if((gp
= _group_fetch(GT_FILETYPE
, ccp
, llc
+ lsc
)) == NULL
){
2320 n_err(_("Failed to create storage for filetype: %s\n"),
2321 n_shexp_quote_cp(argv
[0], FAL0
));
2324 struct a_nag_file_type
*nftp
;
2326 GP_TO_SUBCLASS(nftp
, gp
);
2327 GP_TO_SUBCLASS(cp
, gp
);
2329 memcpy(nftp
->nft_load
.s
= cp
, argv
[1], llc
);
2331 nftp
->nft_load
.l
= --llc
;
2332 memcpy(nftp
->nft_save
.s
= cp
, argv
[2], lsc
);
2334 nftp
->nft_save
.l
= --lsc
;
2342 c_unfiletype(void *vp
){
2350 do if(!_group_del(GT_FILETYPE
, *argv
)){
2351 n_err(_("No such `filetype': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2353 }while(*++argv
!= NULL
);
2359 n_filetype_trial(struct n_file_type
*res_or_null
, char const *file
){
2361 struct group_lookup gl
;
2362 struct n_string s
, *sp
;
2363 struct group
const *gp
;
2367 sp
= n_string_creat_auto(&s
);
2368 sp
= n_string_assign_cp(sp
, file
);
2369 sp
= n_string_push_c(sp
, '.');
2372 for(gp
= _group_go_first(GT_FILETYPE
, &gl
); gp
!= NULL
;
2373 gp
= _group_go_next(&gl
)){
2374 sp
= n_string_trunc(sp
, l
);
2375 sp
= n_string_push_buf(sp
, gp
->g_id
,
2376 gp
->g_subclass_off
- gp
->g_id_len_sub
);
2378 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2379 if(res_or_null
!= NULL
){
2380 struct a_nag_file_type
*nftp
;
2382 GP_TO_SUBCLASS(nftp
, gp
);
2383 res_or_null
->ft_ext_dat
= gp
->g_id
;
2384 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2385 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2386 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2387 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2388 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2390 goto jleave
; /* TODO after v15 legacy drop: break; */
2394 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2395 * TODO but NOT supporting *file-hook-{load,save}-EXTENSION* */
2396 gp
= (struct group
*)0x1;
2398 sp
= n_string_trunc(sp
, l
);
2399 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_xz
.ft_ext_dat
,
2400 a_nag_OBSOLETE_xz
.ft_ext_len
);
2401 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2402 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2403 if(res_or_null
!= NULL
)
2404 *res_or_null
= a_nag_OBSOLETE_xz
;
2408 sp
= n_string_trunc(sp
, l
);
2409 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_gz
.ft_ext_dat
,
2410 a_nag_OBSOLETE_gz
.ft_ext_len
);
2411 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2412 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2413 if(res_or_null
!= NULL
)
2414 *res_or_null
= a_nag_OBSOLETE_gz
;
2418 sp
= n_string_trunc(sp
, l
);
2419 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_bz2
.ft_ext_dat
,
2420 a_nag_OBSOLETE_bz2
.ft_ext_len
);
2421 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2422 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2423 if(res_or_null
!= NULL
)
2424 *res_or_null
= a_nag_OBSOLETE_bz2
;
2432 return (gp
!= NULL
);
2436 n_filetype_exists(struct n_file_type
*res_or_null
, char const *file
){
2437 char const *ext
, *lext
;
2440 if((ext
= strrchr(file
, '/')) != NULL
)
2443 for(lext
= NULL
; (ext
= strchr(file
, '.')) != NULL
; lext
= file
= ext
){
2444 struct group
const *gp
;
2446 if((gp
= _group_find(GT_FILETYPE
, ++ext
)) != NULL
){
2448 if(res_or_null
!= NULL
){
2449 struct a_nag_file_type
*nftp
;
2451 GP_TO_SUBCLASS(nftp
, gp
);
2452 res_or_null
->ft_ext_dat
= gp
->g_id
;
2453 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2454 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2455 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2456 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2457 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2459 goto jleave
; /* TODO after v15 legacy drop: break; */
2463 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2464 * TODO as well as supporting *file-hook-{load,save}-EXTENSION* */
2468 if(!asccasecmp(lext
, "xz")){
2469 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2470 if(res_or_null
!= NULL
)
2471 *res_or_null
= a_nag_OBSOLETE_xz
;
2473 }else if(!asccasecmp(lext
, "gz")){
2474 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2475 if(res_or_null
!= NULL
)
2476 *res_or_null
= a_nag_OBSOLETE_gz
;
2478 }else if(!asccasecmp(lext
, "bz2")){
2479 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2480 if(res_or_null
!= NULL
)
2481 *res_or_null
= a_nag_OBSOLETE_bz2
;
2484 char const *cload
, *csave
;
2489 #define a_X1 "file-hook-load-"
2491 #define a_X2 "file-hook-save-"
2493 vbuf
= n_lofi_alloc(l
+ n_MAX(sizeof(a_X1
), sizeof(a_X2
)));
2495 memcpy(vbuf
, a_X1
, sizeof(a_X1
) -1);
2496 memcpy(&vbuf
[sizeof(a_X1
) -1], lext
, l
);
2497 vbuf
[sizeof(a_X1
) -1 + l
] = '\0';
2498 cload
= n_var_vlook(vbuf
, FAL0
);
2500 memcpy(vbuf
, a_X2
, sizeof(a_X2
) -1);
2501 memcpy(&vbuf
[sizeof(a_X2
) -1], lext
, l
);
2502 vbuf
[sizeof(a_X2
) -1 + l
] = '\0';
2503 csave
= n_var_vlook(vbuf
, FAL0
);
2509 if((csave
!= NULL
) | (cload
!= NULL
)){
2510 n_OBSOLETE("*file-hook-{load,save}-EXTENSION* will vanish, "
2511 "please use the `filetype' command");
2513 if(((csave
!= NULL
) ^ (cload
!= NULL
)) == 0){
2514 if(res_or_null
!= NULL
){
2515 res_or_null
->ft_ext_dat
= lext
;
2516 res_or_null
->ft_ext_len
= l
;
2517 res_or_null
->ft_load_dat
= cload
;
2518 res_or_null
->ft_load_len
= strlen(cload
);
2519 res_or_null
->ft_save_dat
= csave
;
2520 res_or_null
->ft_save_len
= strlen(csave
);
2524 n_alert(_("Incomplete *file-hook-{load,save}-EXTENSION* for: .%s"),
2533 return (lext
!= NULL
);