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
){
234 char c1
, c2
, c1r
, c2r
;
237 if(ok_blook(allnet
)){
241 c1r
= (c1
== '\0' || c1
== '@');
244 c2r
= (c2
== '\0' || c2
== '@');
255 rv
= !asccasecmp(n1
, n2
);
261 a_nag_namelist_mark_name(struct name
*np
, char const *name
){
265 for(p
= np
; p
!= NULL
; p
= p
->n_flink
)
266 if(!(p
->n_type
& GDEL
) && !(p
->n_flags
& (ui32_t
)SI32_MIN
) &&
267 a_nag_is_same_name(p
->n_name
, name
))
268 p
->n_flags
|= (ui32_t
)SI32_MIN
;
274 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
277 char *wp
, c
, inquote
, lc
, lastsp
;
282 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
283 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
290 /* Parse a full name: TODO RFC 5322
291 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
292 * - Skip entire (nested) comments
293 * - In non-quote, non-comment, join adjacent space to a single SP
294 * - Understand separators only in non-quote, non-comment context,
295 * and only if not part of a *quoted-pair* (XXX too liberal) */
297 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
306 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
312 if (inquote
|| lc
== '\\') {
320 cp
= skip_comment(cp
+ 1);
328 if (strchr(separators
, c
) != NULL
)
332 lastsp
= blankchar(c
);
346 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
349 struct name
*topp
, *np
, *t
;
355 if (line
== NULL
|| *line
== '\0')
360 nbuf
= smalloc(strlen(line
) +1);
361 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
362 t
= nalloc(nbuf
, ntype
);
377 a_nag_gexpand(size_t level
, struct name
*nlist
, struct group
*gp
, bool_t metoo
,
379 struct grp_names
*gnp
;
380 struct name
*nlist_tail
;
382 struct grp_names_head
*gnhp
;
385 if(UICMP(z
, level
++, >, n_ALIAS_MAXEXP
)){
386 n_err(_("Expanding alias to depth larger than %d\n"), n_ALIAS_MAXEXP
);
390 GP_TO_SUBCLASS(gnhp
, gp
);
391 logname
= ok_vlook(LOGNAME
);
393 for(gnp
= gnhp
->gnh_head
; gnp
!= NULL
; gnp
= gnp
->gn_next
){
399 if(!strcmp(cp
, gp
->g_id
))
402 if((ngp
= _group_find(GT_ALIAS
, cp
)) != NULL
){
403 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
404 * to a full address specification; aliases cannot be empty */
405 struct grp_names_head
*ngnhp
;
406 GP_TO_SUBCLASS(ngnhp
, ngp
);
408 assert(ngnhp
->gnh_head
!= NULL
);
409 if(metoo
|| ngnhp
->gnh_head
->gn_next
!= NULL
||
410 !a_nag_is_same_name(cp
, logname
))
411 nlist
= a_nag_gexpand(level
, nlist
, ngp
, metoo
, ntype
);
415 /* Here we should allow to expand to itself if only person in alias */
417 if(metoo
|| gnhp
->gnh_head
->gn_next
== NULL
||
418 !a_nag_is_same_name(cp
, logname
)){
421 np
= nalloc(cp
, ntype
| GFULL
);
422 if((nlist_tail
= nlist
) != NULL
){
423 while(nlist_tail
->n_flink
!= NULL
)
424 nlist_tail
= nlist_tail
->n_flink
;
425 nlist_tail
->n_flink
= np
;
426 np
->n_blink
= nlist_tail
;
437 a_nag_elide_qsort(void const *s1
, void const *s2
){
438 struct name
const * const *np1
, * const *np2
;
444 rv
= asccasecmp((*np1
)->n_name
, (*np2
)->n_name
);
449 static struct group
*
450 _group_lookup(enum group_type gt
, struct group_lookup
*glp
, char const *id
){
452 struct group
*lgp
, *gp
;
458 ( gt
& GT_COMMANDALIAS
? _commandalias_heads
459 : (gt
& GT_ALIAS
? _alias_heads
460 : (gt
& GT_MLIST
? _mlist_heads
461 : (gt
& GT_SHORTCUT
? _shortcut_heads
462 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
463 : (/*gt & GT_FILETYPE ?*/ _filetype_heads
465 gp
= *(glp
->gl_slot
= &glp
->gl_htable
[
466 ((gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
))
467 ? n_torek_ihash(id
) : n_torek_hash(id
)) % HSHSIZE
]);
470 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
472 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
473 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&&
474 !asccasecmp(&gp
->g_id
[1], id
))
477 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
478 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&& !strcmp(&gp
->g_id
[1], id
))
482 glp
->gl_slot_last
= lgp
;
488 static struct group
*
489 _group_find(enum group_type gt
, char const *id
)
491 struct group_lookup gl
;
495 gp
= _group_lookup(gt
, &gl
, id
);
500 static struct group
*
501 _group_go_first(enum group_type gt
, struct group_lookup
*glp
)
503 struct group
**gpa
, *gp
;
507 for (glp
->gl_htable
= gpa
= (
508 gt
& GT_COMMANDALIAS
? _commandalias_heads
509 : (gt
& GT_ALIAS
? _alias_heads
510 : (gt
& GT_MLIST
? _mlist_heads
511 : (gt
& GT_SHORTCUT
? _shortcut_heads
512 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
513 : (gt
& GT_FILETYPE
? _filetype_heads
516 i
< HSHSIZE
; ++gpa
, ++i
)
517 if ((gp
= *gpa
) != NULL
) {
523 glp
->gl_group
= gp
= NULL
;
525 glp
->gl_slot_last
= NULL
;
530 static struct group
*
531 _group_go_next(struct group_lookup
*glp
)
533 struct group
*gp
, **gpa
;
536 if ((gp
= glp
->gl_group
->g_next
) != NULL
)
537 glp
->gl_slot_last
= glp
->gl_group
;
539 glp
->gl_slot_last
= NULL
;
540 for (gpa
= glp
->gl_htable
+ HSHSIZE
; ++glp
->gl_slot
< gpa
;)
541 if ((gp
= *glp
->gl_slot
) != NULL
)
549 static struct group
*
550 _group_fetch(enum group_type gt
, char const *id
, size_t addsz
)
552 struct group_lookup gl
;
557 if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
)
561 if (UIZ_MAX
- n_ALIGN(l
) <= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
)))
564 i
= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
) + l
);
565 switch (gt
& GT_MASK
) {
566 case GT_COMMANDALIAS
:
567 addsz
+= sizeof(struct a_nag_cmd_alias
);
570 addsz
+= sizeof(struct grp_names_head
);
573 addsz
+= sizeof(struct a_nag_file_type
);
577 if (n_is_maybe_regex(id
)) {
578 addsz
= sizeof(struct grp_regex
);
584 case GT_CHARSETALIAS
:
588 if (UIZ_MAX
- i
< addsz
|| UI32_MAX
<= i
|| UI16_MAX
< i
- l
)
591 gp
= smalloc(i
+ addsz
);
592 memcpy(gp
->g_id
, id
, l
);
593 gp
->g_subclass_off
= (ui32_t
)i
;
594 gp
->g_id_len_sub
= (ui16_t
)(i
- --l
);
596 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
599 for(cp
= gp
->g_id
; (c
= *cp
) != '\0'; ++cp
)
604 struct grp_names_head
*gnhp
;
606 GP_TO_SUBCLASS(gnhp
, gp
);
607 gnhp
->gnh_head
= NULL
;
610 else if (/*(gt & GT_MLIST) &&*/ gt
& GT_REGEX
) {
612 struct grp_regex
*grp
;
613 GP_TO_SUBCLASS(grp
, gp
);
615 if((s
= regcomp(&grp
->gr_regex
, id
,
616 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
617 n_err(_("Invalid regular expression: %s: %s\n"),
618 n_shexp_quote_cp(id
, FAL0
), n_regex_err_to_doc(&grp
->gr_regex
, s
));
623 grp
->gr_mygroup
= gp
;
628 gp
->g_next
= *gl
.gl_slot
;
636 _group_del(enum group_type gt
, char const *id
)
638 enum group_type xgt
= gt
& GT_MASK
;
639 struct group_lookup gl
;
643 /* Delete 'em all? */
644 if (id
[0] == '*' && id
[1] == '\0') {
645 for (gp
= _group_go_first(gt
, &gl
); gp
!= NULL
;)
646 gp
= (gp
->g_type
& xgt
) ? __group_del(&gl
) : _group_go_next(&gl
);
647 gp
= (struct group
*)TRU1
;
648 } else if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
) {
649 if (gp
->g_type
& xgt
)
658 static struct group
*
659 __group_del(struct group_lookup
*glp
)
661 struct group
*x
, *gp
;
664 /* Overly complicated: link off this node, step ahead to next.. */
666 if((gp
= glp
->gl_slot_last
) != NULL
)
667 gp
= (gp
->g_next
= x
->g_next
);
669 glp
->gl_slot_last
= NULL
;
670 gp
= (*glp
->gl_slot
= x
->g_next
);
675 for(gpa
= &glp
->gl_htable
[HSHSIZE
]; ++glp
->gl_slot
< gpa
;)
676 if((gp
= *glp
->gl_slot
) != NULL
)
682 if (x
->g_type
& GT_ALIAS
)
685 else if (/*(x->g_type & GT_MLIST) &&*/ x
->g_type
& GT_REGEX
) {
686 struct grp_regex
*grp
;
687 GP_TO_SUBCLASS(grp
, x
);
689 regfree(&grp
->gr_regex
);
700 __names_del(struct group
*gp
)
702 struct grp_names_head
*gnhp
;
703 struct grp_names
*gnp
;
706 GP_TO_SUBCLASS(gnhp
, gp
);
707 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
;) {
708 struct grp_names
*x
= gnp
;
716 _group_print_all(enum group_type gt
)
720 struct group
const *gp
;
727 xgt
= gt
& GT_PRINT_MASK
;
728 gpa
= ( xgt
& GT_COMMANDALIAS
? _commandalias_heads
729 : (xgt
& GT_ALIAS
? _alias_heads
730 : (xgt
& GT_MLIST
? _mlist_heads
731 : (xgt
& GT_SHORTCUT
? _shortcut_heads
732 : (xgt
& GT_CHARSETALIAS
? _charsetalias_heads
733 : (xgt
& GT_FILETYPE
? _filetype_heads
736 for (h
= 0, i
= 1; h
< HSHSIZE
; ++h
)
737 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
738 if ((gp
->g_type
& xgt
) == xgt
)
740 ida
= salloc(i
* sizeof *ida
);
742 for (i
= h
= 0; h
< HSHSIZE
; ++h
)
743 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
744 if ((gp
->g_type
& xgt
) == xgt
)
749 qsort(ida
, i
, sizeof *ida
, &__group_print_qsorter
);
751 if ((fp
= Ftmp(NULL
, "prgroup", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
755 for (i
= 0; ida
[i
] != NULL
; ++i
)
756 lines
+= _group_print(_group_find(gt
, ida
[i
]), fp
);
759 if (gt
& GT_SUBSCRIBE
)
760 i
= (ui32_t
)_mlsub_size
, h
= (ui32_t
)_mlsub_hits
;
762 i
= (ui32_t
)_mlist_size
, h
= (ui32_t
)_mlist_hits
;
763 if (i
> 0 && (n_poption
& n_PO_D_V
)){
764 fprintf(fp
, _("# %s list regex(7) total: %u entries, %u hits\n"),
765 (gt
& GT_SUBSCRIBE
? _("Subscribed") : _("Non-subscribed")),
772 if (fp
!= n_stdout
) {
773 page_or_print(fp
, lines
);
780 __group_print_qsorter(void const *a
, void const *b
)
785 rv
= strcmp(*(char**)n_UNCONST(a
), *(char**)n_UNCONST(b
));
791 _group_print(struct group
const *gp
, FILE *fo
)
799 if(gp
->g_type
& GT_COMMANDALIAS
){
800 struct a_nag_cmd_alias
*ncap
;
802 GP_TO_SUBCLASS(ncap
, gp
);
803 fprintf(fo
, "commandalias %s %s\n",
804 n_shexp_quote_cp(gp
->g_id
, TRU1
),
805 n_shexp_quote_cp(ncap
->ca_expand
.s
, TRU1
));
806 } else if (gp
->g_type
& GT_ALIAS
) {
807 struct grp_names_head
*gnhp
;
808 struct grp_names
*gnp
;
810 fprintf(fo
, "alias %s ", gp
->g_id
);
812 GP_TO_SUBCLASS(gnhp
, gp
);
813 if ((gnp
= gnhp
->gnh_head
) != NULL
) { /* xxx always 1+ entries */
815 struct grp_names
*x
= gnp
;
817 fprintf(fo
, " \"%s\"", string_quote(x
->gn_id
)); /* TODO shexp */
818 } while (gnp
!= NULL
);
821 } else if (gp
->g_type
& GT_MLIST
) {
823 if ((gp
->g_type
& GT_REGEX
) && (n_poption
& n_PO_D_V
)){
825 struct grp_regex
*grp
,
826 *lp
= (gp
->g_type
& GT_SUBSCRIBE
? _mlsub_regex
: _mlist_regex
);
828 GP_TO_SUBCLASS(grp
, gp
);
829 for (i
= 1; lp
!= grp
; lp
= lp
->gr_next
)
831 fprintf(fo
, "# regex(7): hits %" PRIuZ
", sort %" PRIuZ
".\n ",
837 fprintf(fo
, "wysh %s %s\n",
838 (gp
->g_type
& GT_SUBSCRIBE
? "mlsubscribe" : "mlist"),
839 n_shexp_quote_cp(gp
->g_id
, TRU1
));
840 } else if (gp
->g_type
& GT_SHORTCUT
) {
841 GP_TO_SUBCLASS(cp
, gp
);
842 fprintf(fo
, "wysh shortcut %s %s\n",
843 gp
->g_id
, n_shexp_quote_cp(cp
, TRU1
));
844 } else if (gp
->g_type
& GT_CHARSETALIAS
) {
845 GP_TO_SUBCLASS(cp
, gp
);
846 fprintf(fo
, "charsetalias %s %s\n",
847 n_shexp_quote_cp(gp
->g_id
, TRU1
), n_shexp_quote_cp(cp
, TRU1
));
848 } else if (gp
->g_type
& GT_FILETYPE
) {
849 struct a_nag_file_type
*nftp
;
851 GP_TO_SUBCLASS(nftp
, gp
);
852 fprintf(fo
, "filetype %s %s %s\n",
853 n_shexp_quote_cp(gp
->g_id
, TRU1
),
854 n_shexp_quote_cp(nftp
->nft_load
.s
, TRU1
),
855 n_shexp_quote_cp(nftp
->nft_save
.s
, TRU1
));
863 _mlmux(enum group_type gt
, char **argv
)
874 _group_print_all(gt
);
876 if ((gp
= _group_find(gt
, *argv
)) != NULL
) {
877 if (gt
& GT_SUBSCRIBE
) {
878 if (!(gp
->g_type
& GT_SUBSCRIBE
)) {
880 gp
->g_type
|= GT_SUBSCRIBE
;
883 ecp
= N_("Mailing-list already `mlsubscribe'd: %s\n");
887 ecp
= N_("Mailing-list already `mlist'ed: %s\n");
890 } else if(_group_fetch(gt
, *argv
, 0) == NULL
) {
891 ecp
= N_("Failed to create storage for mailing-list: %s\n");
893 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
896 } while (*++argv
!= NULL
);
903 _unmlmux(enum group_type gt
, char **argv
)
909 for (; *argv
!= NULL
; ++argv
) {
910 if (gt
& GT_SUBSCRIBE
) {
911 struct group_lookup gl
;
914 if (!(isaster
= (**argv
== '*')))
915 gp
= _group_find(gt
, *argv
);
916 else if ((gp
= _group_go_first(gt
, &gl
)) == NULL
)
918 else if (gp
!= NULL
&& !(gp
->g_type
& GT_SUBSCRIBE
))
923 if (gp
->g_type
& GT_SUBSCRIBE
) {
925 gp
->g_type
&= ~GT_SUBSCRIBE
;
929 while ((gp
= _group_go_next(&gl
)) != NULL
&&
930 !(gp
->g_type
& GT_SUBSCRIBE
))
936 n_err(_("Mailing-list not `mlsubscribe'd: %s\n"),
937 n_shexp_quote_cp(*argv
, FAL0
));
942 } else if (_group_del(gt
, *argv
))
944 n_err(_("No such mailing-list: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
953 _mlmux_linkin(struct group
*gp
)
955 struct grp_regex
**lpp
, *grp
, *lhp
;
958 if (gp
->g_type
& GT_SUBSCRIBE
) {
966 GP_TO_SUBCLASS(grp
, gp
);
967 if ((lhp
= *lpp
) != NULL
) {
968 (grp
->gr_last
= lhp
->gr_last
)->gr_next
= grp
;
969 (grp
->gr_next
= lhp
)->gr_last
= grp
;
971 *lpp
= grp
->gr_last
= grp
->gr_next
= grp
;
977 _mlmux_linkout(struct group
*gp
)
979 struct grp_regex
*grp
, **lpp
;
982 GP_TO_SUBCLASS(grp
, gp
);
984 if (gp
->g_type
& GT_SUBSCRIBE
) {
987 _mlsub_hits
-= grp
->gr_hits
;
991 _mlist_hits
-= grp
->gr_hits
;
994 if (grp
->gr_next
== grp
)
997 (grp
->gr_last
->gr_next
= grp
->gr_next
)->gr_last
= grp
->gr_last
;
1003 #endif /* HAVE_REGEX */
1006 nalloc(char const *str
, enum gfield ntype
)
1008 struct n_addrguts ag
;
1012 assert(!(ntype
& GFULLEXTRA
) || (ntype
& GFULL
) != 0);
1014 str
= n_addrspec_with_guts(&ag
, str
,
1015 ((ntype
& (GFULL
| GSKIN
| GREF
)) != 0), FAL0
);
1018 np = NULL; TODO We cannot return NULL,
1019 goto jleave; TODO thus handle failures in here!
1024 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
1025 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
1026 np
= salloc(sizeof(*np
) + ag
.ag_slen
+1);
1027 memcpy(np
+ 1, ag
.ag_skinned
, ag
.ag_slen
+1);
1028 ag
.ag_skinned
= (char*)(np
+ 1);
1030 np
= salloc(sizeof *np
);
1037 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
1038 np
->n_fullextra
= NULL
;
1039 np
->n_flags
= ag
.ag_n_flags
;
1041 if (ntype
& GFULL
) {
1042 if (ag
.ag_ilen
== ag
.ag_slen
1044 && !(ag
.ag_n_flags
& NAME_IDNA
)
1048 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
1051 /* n_fullextra is only the complete name part without address.
1052 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
1053 if ((ntype
& GFULLEXTRA
) && ag
.ag_ilen
> ag
.ag_slen
+ 2) {
1054 size_t s
= ag
.ag_iaddr_start
, e
= ag
.ag_iaddr_aend
, i
;
1057 if (s
== 0 || str
[--s
] != '<' || str
[e
++] != '>')
1058 goto jskipfullextra
;
1060 in
.s
= n_lofi_alloc(s
+ 1 + i
+1);
1061 while(s
> 0 && blankchar(str
[s
- 1]))
1063 memcpy(in
.s
, str
, s
);
1066 while (blankchar(str
[e
])) {
1072 memcpy(&in
.s
[s
], &str
[e
], i
);
1075 in
.s
[in
.l
= s
] = '\0';
1076 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1078 for (cp
= out
.s
, i
= out
.l
; i
> 0 && spacechar(*cp
); --i
, ++cp
)
1080 while (i
> 0 && spacechar(cp
[i
- 1]))
1082 np
->n_fullextra
= savestrbuf(cp
, i
);
1089 /* n_fullname depends on IDNA conversion */
1091 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
1093 in
.s
= n_UNCONST(str
);
1097 /* The domain name was IDNA and has been converted. We also have to
1098 * ensure that the domain name in .n_fullname is replaced with the
1099 * converted version, since MIME doesn't perform encoding of addrs */
1100 /* TODO This definetely doesn't belong here! */
1101 size_t l
= ag
.ag_iaddr_start
,
1102 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
1103 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
1104 memcpy(in
.s
, str
, l
);
1105 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
1107 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
1113 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1114 np
->n_fullname
= savestr(out
.s
);
1117 if (ag
.ag_n_flags
& NAME_IDNA
)
1120 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
1128 ndup(struct name
*np
, enum gfield ntype
)
1133 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
1134 nnp
= nalloc(np
->n_name
, ntype
);
1138 nnp
= salloc(sizeof *np
);
1139 nnp
->n_flink
= nnp
->n_blink
= NULL
;
1140 nnp
->n_type
= ntype
;
1141 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
1143 nnp
->n_name
= savestr(np
->n_name
);
1144 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
))) {
1145 nnp
->n_fullname
= nnp
->n_name
;
1146 nnp
->n_fullextra
= NULL
;
1148 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
1149 nnp
->n_fullname
= savestr(np
->n_fullname
);
1150 nnp
->n_fullextra
= (np
->n_fullextra
== NULL
) ? NULL
1151 : savestr(np
->n_fullextra
);
1159 cat(struct name
*n1
, struct name
*n2
)
1171 while (tail
->n_flink
!= NULL
)
1172 tail
= tail
->n_flink
;
1182 namelist_dup(struct name
const *np
, enum gfield ntype
){
1183 struct name
*nlist
, *xnp
;
1186 for(nlist
= xnp
= NULL
; np
!= NULL
; np
= np
->n_flink
){
1189 x
= ndup(n_UNCONST(np
), (np
->n_type
& ~GMASK
) | ntype
);
1190 if((x
->n_blink
= xnp
) == NULL
)
1201 count(struct name
const *np
)
1206 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1207 if (!(np
->n_type
& GDEL
))
1214 count_nonlocal(struct name
const *np
)
1219 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1220 if (!(np
->n_type
& GDEL
) && !(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
))
1227 extract(char const *line
, enum gfield ntype
)
1232 rv
= _extract1(line
, ntype
, " \t,", 0);
1238 lextract(char const *line
, enum gfield ntype
)
1243 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
1244 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
1250 detract(struct name
*np
, enum gfield ntype
)
1261 flags
= ntype
& (GCOMMA
| GNAMEONLY
);
1262 ntype
&= ~(GCOMMA
| GNAMEONLY
);
1265 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1266 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1268 s
+= strlen(flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
) +1;
1278 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1279 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1281 cp
= sstpcpy(cp
, (flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
));
1282 if ((flags
& GCOMMA
) && p
->n_flink
!= NULL
)
1287 if ((flags
& GCOMMA
) && *--cp
== ',')
1295 grab_names(enum n_go_input_flags gif
, char const *field
, struct name
*np
,
1296 int comma
, enum gfield gflags
)
1302 np
= lextract(n_go_input_cp(gif
, field
, detract(np
, comma
)), gflags
);
1303 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
1304 if (is_addr_invalid(nq
, EACM_NONE
))
1311 name_is_same_domain(struct name
const *n1
, struct name
const *n2
)
1313 char const *d1
, *d2
;
1317 d1
= strrchr(n1
->n_name
, '@');
1318 d2
= strrchr(n2
->n_name
, '@');
1320 rv
= (d1
!= NULL
&& d2
!= NULL
) ? !asccasecmp(++d1
, ++d2
) : FAL0
;
1327 checkaddrs(struct name
*np
, enum expand_addr_check_mode eacm
,
1328 si8_t
*set_on_error
)
1333 for (n
= np
; n
!= NULL
; n
= n
->n_flink
) {
1336 if ((rv
= is_addr_invalid(n
, eacm
)) != 0) {
1337 if (set_on_error
!= NULL
)
1338 *set_on_error
|= rv
; /* don't loose -1! */
1339 else if (eacm
& EAF_MAYKEEP
) /* TODO HACK! See definition! */
1342 n
->n_blink
->n_flink
= n
->n_flink
;
1344 n
->n_flink
->n_blink
= n
->n_blink
;
1354 namelist_vaporise_head(struct header
*hp
, enum expand_addr_check_mode eacm
,
1355 bool_t metoo
, si8_t
*set_on_error
)
1357 /* TODO namelist_vaporise_head() is incredibly expensive and redundant */
1358 struct name
*tolist
, *np
, **npp
;
1361 tolist
= cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
));
1362 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
1364 tolist
= usermap(tolist
, metoo
);
1365 tolist
= n_alternates_remove(tolist
, TRU1
);
1366 tolist
= elide(checkaddrs(tolist
, eacm
, set_on_error
));
1368 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
1369 switch (np
->n_type
& (GDEL
| GMASK
)) {
1370 case GTO
: npp
= &hp
->h_to
; break;
1371 case GCC
: npp
= &hp
->h_cc
; break;
1372 case GBCC
: npp
= &hp
->h_bcc
; break;
1375 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
1382 usermap(struct name
*names
, bool_t force_metoo
){
1384 struct name
*nlist
, *nlist_tail
, *np
, *cp
;
1388 metoo
= (force_metoo
|| ok_blook(metoo
));
1389 nlist
= nlist_tail
= NULL
;
1392 for(; np
!= NULL
; np
= cp
){
1393 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
1396 if(is_fileorpipe_addr(np
) ||
1397 (gp
= _group_find(GT_ALIAS
, np
->n_name
)) == NULL
){
1398 if((np
->n_blink
= nlist_tail
) != NULL
)
1399 nlist_tail
->n_flink
= np
;
1405 nlist
= a_nag_gexpand(0, nlist
, gp
, metoo
, np
->n_type
);
1406 if((nlist_tail
= nlist
) != NULL
)
1407 while(nlist_tail
->n_flink
!= NULL
)
1408 nlist_tail
= nlist_tail
->n_flink
;
1416 elide(struct name
*names
)
1419 struct name
*nlist
, *np
, **nparr
;
1427 /* Throw away all deleted nodes */
1428 for(np
= NULL
, i
= 0; names
!= NULL
; names
= names
->n_flink
)
1429 if(!(names
->n_type
& GDEL
)){
1430 names
->n_blink
= np
;
1432 np
->n_flink
= names
;
1438 if(nlist
== NULL
|| i
== 1)
1442 /* Create a temporay array and sort that */
1443 nparr
= n_lofi_alloc(sizeof(*nparr
) * i
);
1445 for(i
= 0, np
= nlist
; np
!= NULL
; np
= np
->n_flink
)
1448 qsort(nparr
, i
, sizeof *nparr
, &a_nag_elide_qsort
);
1450 /* Remove duplicates XXX speedup, or list_uniq()! */
1451 for(j
= 0, --i
; j
< i
;){
1452 if(asccasecmp(nparr
[j
]->n_name
, nparr
[k
= j
+ 1]->n_name
))
1456 nparr
[k
] = nparr
[k
+ 1];
1461 /* Throw away all list members which are not part of the array.
1462 * Note this keeps the original, possibly carefully crafted, order of the
1463 * addressees, thus */
1464 for(np
= nlist
; np
!= NULL
; np
= np
->n_flink
){
1465 for(j
= 0; j
<= i
; ++j
)
1472 nlist
= np
->n_flink
;
1475 np
->n_blink
->n_flink
= np
->n_flink
;
1476 if(np
->n_flink
!= NULL
)
1477 np
->n_flink
->n_blink
= np
->n_blink
;
1488 c_alternates(void *v
){ /* TODO use a hashmap!! */
1495 if(*(argv
= v
) == NULL
){
1498 if((ccp
= ok_vlook(alternates
)) != NULL
)
1499 fprintf(n_stdout
, "alternates %s\n", ccp
);
1501 fputs(_("# no alternates registered\n"), n_stdout
);
1505 struct n_strlist
*slp
, **slpa
;
1507 while((slp
= a_nag_altnames
) != NULL
){
1508 a_nag_altnames
= slp
->sl_next
;
1513 /* Extension: only clearance? */
1514 if(argv
[1] == NULL
&& argv
[0][0] == '-' && argv
[0][1] == '\0')
1516 else for(slpa
= &a_nag_altnames
; *argv
!= NULL
; ++argv
){
1520 if((np
= lextract(*argv
, GSKIN
)) == NULL
|| np
->n_flink
!= NULL
||
1521 (np
= checkaddrs(np
, EACM_STRICT
, NULL
)) == NULL
){
1522 n_err(_("Invalid `alternates' argument: %s\n"),
1523 n_shexp_quote_cp(*argv
, FAL0
));
1528 l
= strlen(np
->n_name
);
1529 if(UIZ_MAX
- l
<= vl
){
1530 n_err(_("Failed to create storage for alternate: %s\n"),
1531 n_shexp_quote_cp(*argv
, FAL0
));
1536 slp
= n_STRLIST_ALLOC(l
);
1537 slp
->sl_next
= NULL
;
1539 memcpy(slp
->sl_dat
, np
->n_name
, ++l
);
1541 slpa
= &slp
->sl_next
;
1546 /* And put it into *alternates* */
1548 cp
= n_autorec_alloc(vl
);
1549 for(vl
= 0, slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
){
1550 memcpy(&cp
[vl
], slp
->sl_dat
, slp
->sl_len
);
1551 cp
[vl
+= slp
->sl_len
] = ' ';
1557 n_PS_ROOT_BLOCK(vl
> 0 ? ok_vset(alternates
, cp
) : ok_vclear(alternates
));
1564 n_alternates_remove(struct name
*np
, bool_t keep_single
){
1565 struct name
*xp
, *newnp
;
1568 /* Delete the temporary bit from all */
1569 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1570 xp
->n_flags
&= ~(ui32_t
)SI32_MIN
;
1572 /* Mark all possible alternate names (xxx sic) */
1574 if(a_nag_altnames
!= NULL
){
1575 struct n_strlist
*slp
;
1577 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1578 np
= a_nag_namelist_mark_name(np
, slp
->sl_dat
);
1581 np
= a_nag_namelist_mark_name(np
, ok_vlook(LOGNAME
));
1583 for(xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
); xp
!= NULL
;
1585 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1587 for(xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
); xp
!= NULL
;
1589 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1591 for(xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
); xp
!= NULL
;
1593 np
= a_nag_namelist_mark_name(np
, xp
->n_name
);
1595 /* GDEL all (but a single) marked node(s) */
1596 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1597 if(xp
->n_flags
& (ui32_t
)SI32_MIN
){
1603 /* Clean the list by throwing away all deleted nodes */
1604 for(xp
= newnp
= NULL
; np
!= NULL
; np
= np
->n_flink
)
1605 if(!(np
->n_type
& GDEL
)){
1615 /* Delete the temporary bit from all remaining (again) */
1616 for(xp
= np
; xp
!= NULL
; xp
= xp
->n_flink
)
1617 xp
->n_flags
&= ~(ui32_t
)SI32_MIN
;
1624 n_is_myname(char const *name
){
1628 if(a_nag_is_same_name(ok_vlook(LOGNAME
), name
))
1631 if(a_nag_altnames
!= NULL
){
1632 struct n_strlist
*slp
;
1634 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1635 if(a_nag_is_same_name(slp
->sl_dat
, name
))
1639 for(xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
); xp
!= NULL
;
1641 if(a_nag_is_same_name(xp
->n_name
, name
))
1644 for(xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
); xp
!= NULL
;
1646 if(a_nag_is_same_name(xp
->n_name
, name
))
1649 for(xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
); xp
!= NULL
;
1651 if(a_nag_is_same_name(xp
->n_name
, name
))
1657 return (name
!= NULL
);
1661 c_addrcodec(void *vp
){
1662 struct n_addrguts ag
;
1663 struct n_string s_b
, *sp
;
1666 char const **argv
, *varname
, *act
, *cp
;
1669 sp
= n_string_creat_auto(&s_b
);
1671 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1674 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1685 alen
= PTR2SIZE(cp
- act
);
1693 if(i
<= UIZ_MAX
/ 4)
1695 sp
= n_string_reserve(sp
, i
);
1698 n_pstate_err_no
= n_ERR_NONE
;
1700 if(is_ascncaseprefix(act
, "encode", alen
)){
1701 /* This function cannot be a simple nalloc() wrapper even later on, since
1702 * we may need to turn any ", () or \ into quoted-pairs */
1705 while((c
= *cp
++) != '\0'){
1706 if(((c
== '(' || c
== ')') && mode
< 1) || (c
== '"' && mode
< 2) ||
1707 (c
== '\\' && mode
< 3))
1708 sp
= n_string_push_c(sp
, '\\');
1709 sp
= n_string_push_c(sp
, c
);
1712 if(n_addrspec_with_guts(&ag
, n_string_cp(sp
), TRU1
, TRU1
) == NULL
||
1713 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1714 ) != NAME_ADDRSPEC_ISADDR
){
1716 n_pstate_err_no
= n_ERR_INVAL
;
1721 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1722 cp
= np
->n_fullname
;
1724 }else if(mode
== 0){
1725 if(is_ascncaseprefix(act
, "decode", alen
)){
1728 while((c
= *cp
++) != '\0'){
1731 sp
= n_string_push_c(sp
, '(');
1732 act
= skip_comment(cp
);
1734 sp
= n_string_push_buf(sp
, cp
, PTR2SIZE(act
- cp
));
1735 sp
= n_string_push_c(sp
, ')');
1740 if((c
= *cp
++) == '"')
1742 if(c
== '\\' && (c
= *cp
) != '\0')
1744 sp
= n_string_push_c(sp
, c
);
1748 if(c
== '\\' && (c
= *cp
++) == '\0')
1750 sp
= n_string_push_c(sp
, c
);
1754 cp
= n_string_cp(sp
);
1755 }else if(is_ascncaseprefix(act
, "skin", alen
)){
1756 /* Let's just use the is-single-address hack for this one, too.. */
1757 if(n_addrspec_with_guts(&ag
, cp
, TRU1
, TRU1
) == NULL
||
1758 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1759 ) != NAME_ADDRSPEC_ISADDR
){
1760 n_pstate_err_no
= n_ERR_INVAL
;
1765 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1773 if(varname
== NULL
){
1774 if(fprintf(n_stdout
, "%s\n", cp
) < 0){
1775 n_pstate_err_no
= n_err_no
;
1778 }else if(!n_var_vset(varname
, (uintptr_t)cp
)){
1779 n_pstate_err_no
= n_ERR_NOTSUP
;
1785 return (vp
!= NULL
? 0 : 1);
1787 n_err(_("Synopsis: addrcodec: <[+[+[+]]]e[ncode]|d[ecode]|s[kin]> "
1788 "<rest-of-line>\n"));
1789 n_pstate_err_no
= n_ERR_INVAL
;
1795 c_commandalias(void *vp
){
1797 char const **argv
, *ccp
;
1804 if((ccp
= *argv
) == NULL
){
1805 _group_print_all(GT_COMMANDALIAS
);
1809 /* Verify the name is a valid one, and not a command modifier */
1810 if(*ccp
== '\0' || *n_cmd_isolate(ccp
) != '\0' ||
1811 !asccasecmp(ccp
, "ignerr") || !asccasecmp(ccp
, "wysh") ||
1812 !asccasecmp(ccp
, "vput")){
1813 n_err(_("`commandalias': not a valid command name: %s\n"),
1814 n_shexp_quote_cp(ccp
, FAL0
));
1819 if(argv
[1] == NULL
){
1820 if((gp
= _group_find(GT_COMMANDALIAS
, ccp
)) != NULL
)
1821 _group_print(gp
, n_stdout
);
1823 n_err(_("No such commandalias: %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
1827 /* Because one hardly ever redefines, anything is stored in one chunk */
1831 /* Delete the old one, if any; don't get fooled to remove them all */
1832 if(ccp
[0] != '*' || ccp
[1] != '\0')
1833 _group_del(GT_COMMANDALIAS
, ccp
);
1835 for(i
= len
= 0, ++argv
; argv
[i
] != NULL
; ++i
)
1836 len
+= strlen(argv
[i
]) + 1;
1840 if((gp
= _group_fetch(GT_COMMANDALIAS
, ccp
, len
)) == NULL
){
1841 n_err(_("Failed to create storage for commandalias: %s\n"),
1842 n_shexp_quote_cp(ccp
, FAL0
));
1845 struct a_nag_cmd_alias
*ncap
;
1847 GP_TO_SUBCLASS(ncap
, gp
);
1848 GP_TO_SUBCLASS(cp
, gp
);
1850 ncap
->ca_expand
.s
= cp
;
1851 ncap
->ca_expand
.l
= len
- 1;
1853 for(len
= 0; (ccp
= *argv
++) != NULL
;)
1854 if((i
= strlen(ccp
)) > 0){
1869 c_uncommandalias(void *vp
){
1877 do if(!_group_del(GT_COMMANDALIAS
, *argv
)){
1878 n_err(_("No such `commandalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
1880 }while(*++argv
!= NULL
);
1886 n_commandalias_exists(char const *name
, struct str
const **expansion_or_null
){
1890 if((gp
= _group_find(GT_COMMANDALIAS
, name
)) != NULL
){
1893 if(expansion_or_null
!= NULL
){
1894 struct a_nag_cmd_alias
*ncap
;
1896 GP_TO_SUBCLASS(ncap
, gp
);
1897 *expansion_or_null
= &ncap
->ca_expand
;
1906 n_alias_is_valid_name(char const *name
){
1912 for(rv
= TRU1
, cp
= name
++; (c
= *cp
++) != '\0';)
1913 /* User names, plus things explicitly mentioned in Postfix aliases(5).
1914 * As an extension, allow period: [[:alnum:]_#:@.-]+$? */
1915 if(!alnumchar(c
) && c
!= '_' && c
!= '-' &&
1916 c
!= '#' && c
!= ':' && c
!= '@' &&
1918 if(c
== '$' && cp
!= name
&& *cp
== '\0')
1938 n_UNINIT(ecp
, NULL
);
1941 _group_print_all(GT_ALIAS
);
1942 else if (!n_alias_is_valid_name(*argv
)) {
1943 ecp
= N_("Not a valid alias name: %s\n");
1945 } else if (argv
[1] == NULL
) {
1946 if ((gp
= _group_find(GT_ALIAS
, *argv
)) != NULL
)
1947 _group_print(gp
, n_stdout
);
1949 ecp
= N_("No such alias: %s\n");
1952 } else if ((gp
= _group_fetch(GT_ALIAS
, *argv
, 0)) == NULL
) {
1953 ecp
= N_("Failed to create alias storage for: %s\n");
1955 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
1958 struct grp_names
*gnp_tail
, *gnp
;
1959 struct grp_names_head
*gnhp
;
1961 GP_TO_SUBCLASS(gnhp
, gp
);
1963 if((gnp_tail
= gnhp
->gnh_head
) != NULL
)
1964 while((gnp
= gnp_tail
->gn_next
) != NULL
)
1967 for(++argv
; *argv
!= NULL
; ++argv
){
1970 i
= strlen(*argv
) +1;
1971 gnp
= smalloc(n_VSTRUCT_SIZEOF(struct grp_names
, gn_id
) + i
);
1972 if(gnp_tail
!= NULL
)
1973 gnp_tail
->gn_next
= gnp
;
1975 gnhp
->gnh_head
= gnp
;
1977 gnp
->gn_next
= NULL
;
1978 memcpy(gnp
->gn_id
, *argv
, i
);
1992 do if (!_group_del(GT_ALIAS
, *argv
)) {
1993 n_err(_("No such alias: %s\n"), *argv
);
1995 } while (*++argv
!= NULL
);
2006 rv
= _mlmux(GT_MLIST
, v
);
2017 rv
= _unmlmux(GT_MLIST
, v
);
2023 c_mlsubscribe(void *v
)
2028 rv
= _mlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2034 c_unmlsubscribe(void *v
)
2039 rv
= _unmlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2045 is_mlist(char const *name
, bool_t subscribed_only
)
2049 struct grp_regex
**lpp
, *grp
;
2052 enum mlist_state rv
;
2055 gp
= _group_find(GT_MLIST
, name
);
2056 rv
= (gp
!= NULL
) ? MLIST_KNOWN
: MLIST_OTHER
;
2057 if (rv
== MLIST_KNOWN
) {
2058 if (gp
->g_type
& GT_SUBSCRIBE
)
2059 rv
= MLIST_SUBSCRIBED
;
2060 else if (subscribed_only
)
2062 /* Of course, if that is a regular expression it doesn't mean a thing */
2064 if (gp
->g_type
& GT_REGEX
)
2071 /* Not in the hashmap (as something matchable), walk the lists */
2074 lpp
= &_mlsub_regex
;
2076 if ((grp
= *lpp
) != NULL
) {
2077 do if (regexec(&grp
->gr_regex
, name
, 0,NULL
, 0) != REG_NOMATCH
) {
2078 /* Relink as the head of this list if the hit count of this group is
2079 * >= 25% of the average hit count */
2082 i
= ++_mlsub_hits
/ _mlsub_size
;
2084 i
= ++_mlist_hits
/ _mlist_size
;
2087 if (++grp
->gr_hits
>= i
&& *lpp
!= grp
&& grp
->gr_next
!= grp
) {
2088 grp
->gr_last
->gr_next
= grp
->gr_next
;
2089 grp
->gr_next
->gr_last
= grp
->gr_last
;
2090 (grp
->gr_last
= (*lpp
)->gr_last
)->gr_next
= grp
;
2091 (grp
->gr_next
= *lpp
)->gr_last
= grp
;
2094 rv
= !re2
? MLIST_SUBSCRIBED
: MLIST_KNOWN
;
2096 } while ((grp
= grp
->gr_next
) != *lpp
);
2098 if (!re2
&& !subscribed_only
) {
2100 lpp
= &_mlist_regex
;
2103 assert(rv
== MLIST_OTHER
);
2123 _group_print_all(GT_SHORTCUT
);
2124 else if(argv
[1] == NULL
){
2125 if((gp
= _group_find(GT_SHORTCUT
, *argv
)) != NULL
)
2126 _group_print(gp
, n_stdout
);
2128 n_err(_("No such shortcut: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2131 }else for (; *argv
!= NULL
; argv
+= 2) {
2132 /* Because one hardly ever redefines, anything is stored in one chunk */
2136 if (argv
[1] == NULL
) {
2137 n_err(_("Synopsis: shortcut: <shortcut> <expansion>\n"));
2141 if (_group_find(GT_SHORTCUT
, *argv
) != NULL
)
2142 _group_del(GT_SHORTCUT
, *argv
);
2144 l
= strlen(argv
[1]) +1;
2145 if ((gp
= _group_fetch(GT_SHORTCUT
, *argv
, l
)) == NULL
) {
2146 n_err(_("Failed to create storage for shortcut: %s\n"),
2147 n_shexp_quote_cp(*argv
, FAL0
));
2150 GP_TO_SUBCLASS(cp
, gp
);
2151 memcpy(cp
, argv
[1], l
);
2159 c_unshortcut(void *v
)
2165 do if (!_group_del(GT_SHORTCUT
, *argv
)) {
2166 n_err(_("No such shortcut: %s\n"), *argv
);
2168 } while (*++argv
!= NULL
);
2174 shortcut_expand(char const *str
){
2178 if((gp
= _group_find(GT_SHORTCUT
, str
)) != NULL
)
2179 GP_TO_SUBCLASS(str
, gp
);
2187 c_charsetalias(void *vp
){
2197 _group_print_all(GT_CHARSETALIAS
);
2198 else if(argv
[1] == NULL
){
2199 if((gp
= _group_find(GT_CHARSETALIAS
, *argv
)) != NULL
)
2200 _group_print(gp
, n_stdout
);
2202 n_err(_("No such charsetalias: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2205 }else for(; *argv
!= NULL
; argv
+= 2){
2206 /* Because one hardly ever redefines, anything is stored in one chunk */
2211 if(argv
[1] == NULL
){
2212 n_err(_("Synopsis: charsetalias: <charset> <charset-alias>\n"));
2217 /* Delete the old one, if any; don't get fooled to remove them all */
2219 if(ccp
[0] != '*' || ccp
[1] != '\0')
2220 _group_del(GT_CHARSETALIAS
, ccp
);
2222 l
= strlen(argv
[1]) +1;
2223 if ((gp
= _group_fetch(GT_CHARSETALIAS
, ccp
, l
)) == NULL
) {
2224 n_err(_("Failed to create storage for charsetalias: %s\n"),
2225 n_shexp_quote_cp(ccp
, FAL0
));
2228 GP_TO_SUBCLASS(cp
, gp
);
2229 for(ccp
= argv
[1]; (c
= *ccp
++) != '\0';)
2230 *cp
++ = lowerconv(c
);
2239 c_uncharsetalias(void *vp
){
2247 do if(!_group_del(GT_CHARSETALIAS
, *argv
)){
2248 n_err(_("No such `charsetalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2250 }while(*++argv
!= NULL
);
2256 n_charsetalias_expand(char const *cp
){
2259 char const *cp_orig
;
2264 for(i
= 0; (gp
= _group_find(GT_CHARSETALIAS
, cp
)) != NULL
;){
2265 GP_TO_SUBCLASS(cp
, gp
);
2266 if(++i
== 8) /* XXX Magic (same as for `ghost' expansion) */
2277 c_filetype(void *vp
){ /* TODO support automatic chains: .tar.gz -> .gz + .tar */
2279 char **argv
; /* TODO While there: let ! prefix mean: direct execlp(2) */
2287 _group_print_all(GT_FILETYPE
);
2288 else if(argv
[1] == NULL
){
2289 if((gp
= _group_find(GT_FILETYPE
, *argv
)) != NULL
)
2290 _group_print(gp
, n_stdout
);
2292 n_err(_("No such filetype: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2295 }else for(; *argv
!= NULL
; argv
+= 3){
2296 /* Because one hardly ever redefines, anything is stored in one chunk */
2301 if(argv
[1] == NULL
|| argv
[2] == NULL
){
2302 n_err(_("Synopsis: filetype: <extension> <load-cmd> <save-cmd>\n"));
2307 /* Delete the old one, if any; don't get fooled to remove them all */
2309 if(ccp
[0] != '*' || ccp
[1] != '\0')
2310 _group_del(GT_FILETYPE
, ccp
);
2312 /* Lowercase it all (for display purposes) */
2315 while((c
= *cp
) != '\0')
2316 *cp
++ = lowerconv(c
);
2318 llc
= strlen(argv
[1]) +1;
2319 lsc
= strlen(argv
[2]) +1;
2320 if(UIZ_MAX
- llc
<= lsc
)
2323 if((gp
= _group_fetch(GT_FILETYPE
, ccp
, llc
+ lsc
)) == NULL
){
2325 n_err(_("Failed to create storage for filetype: %s\n"),
2326 n_shexp_quote_cp(argv
[0], FAL0
));
2329 struct a_nag_file_type
*nftp
;
2331 GP_TO_SUBCLASS(nftp
, gp
);
2332 GP_TO_SUBCLASS(cp
, gp
);
2334 memcpy(nftp
->nft_load
.s
= cp
, argv
[1], llc
);
2336 nftp
->nft_load
.l
= --llc
;
2337 memcpy(nftp
->nft_save
.s
= cp
, argv
[2], lsc
);
2339 nftp
->nft_save
.l
= --lsc
;
2347 c_unfiletype(void *vp
){
2355 do if(!_group_del(GT_FILETYPE
, *argv
)){
2356 n_err(_("No such `filetype': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2358 }while(*++argv
!= NULL
);
2364 n_filetype_trial(struct n_file_type
*res_or_null
, char const *file
){
2366 struct group_lookup gl
;
2367 struct n_string s
, *sp
;
2368 struct group
const *gp
;
2372 sp
= n_string_creat_auto(&s
);
2373 sp
= n_string_assign_cp(sp
, file
);
2374 sp
= n_string_push_c(sp
, '.');
2377 for(gp
= _group_go_first(GT_FILETYPE
, &gl
); gp
!= NULL
;
2378 gp
= _group_go_next(&gl
)){
2379 sp
= n_string_trunc(sp
, l
);
2380 sp
= n_string_push_buf(sp
, gp
->g_id
,
2381 gp
->g_subclass_off
- gp
->g_id_len_sub
);
2383 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2384 if(res_or_null
!= NULL
){
2385 struct a_nag_file_type
*nftp
;
2387 GP_TO_SUBCLASS(nftp
, gp
);
2388 res_or_null
->ft_ext_dat
= gp
->g_id
;
2389 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2390 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2391 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2392 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2393 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2395 goto jleave
; /* TODO after v15 legacy drop: break; */
2399 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2400 * TODO but NOT supporting *file-hook-{load,save}-EXTENSION* */
2401 gp
= (struct group
*)0x1;
2403 sp
= n_string_trunc(sp
, l
);
2404 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_xz
.ft_ext_dat
,
2405 a_nag_OBSOLETE_xz
.ft_ext_len
);
2406 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2407 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2408 if(res_or_null
!= NULL
)
2409 *res_or_null
= a_nag_OBSOLETE_xz
;
2413 sp
= n_string_trunc(sp
, l
);
2414 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_gz
.ft_ext_dat
,
2415 a_nag_OBSOLETE_gz
.ft_ext_len
);
2416 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2417 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2418 if(res_or_null
!= NULL
)
2419 *res_or_null
= a_nag_OBSOLETE_gz
;
2423 sp
= n_string_trunc(sp
, l
);
2424 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_bz2
.ft_ext_dat
,
2425 a_nag_OBSOLETE_bz2
.ft_ext_len
);
2426 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2427 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2428 if(res_or_null
!= NULL
)
2429 *res_or_null
= a_nag_OBSOLETE_bz2
;
2437 return (gp
!= NULL
);
2441 n_filetype_exists(struct n_file_type
*res_or_null
, char const *file
){
2442 char const *ext
, *lext
;
2445 if((ext
= strrchr(file
, '/')) != NULL
)
2448 for(lext
= NULL
; (ext
= strchr(file
, '.')) != NULL
; lext
= file
= ext
){
2449 struct group
const *gp
;
2451 if((gp
= _group_find(GT_FILETYPE
, ++ext
)) != NULL
){
2453 if(res_or_null
!= NULL
){
2454 struct a_nag_file_type
*nftp
;
2456 GP_TO_SUBCLASS(nftp
, gp
);
2457 res_or_null
->ft_ext_dat
= gp
->g_id
;
2458 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2459 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2460 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2461 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2462 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2464 goto jleave
; /* TODO after v15 legacy drop: break; */
2468 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2469 * TODO as well as supporting *file-hook-{load,save}-EXTENSION* */
2473 if(!asccasecmp(lext
, "xz")){
2474 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2475 if(res_or_null
!= NULL
)
2476 *res_or_null
= a_nag_OBSOLETE_xz
;
2478 }else if(!asccasecmp(lext
, "gz")){
2479 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2480 if(res_or_null
!= NULL
)
2481 *res_or_null
= a_nag_OBSOLETE_gz
;
2483 }else if(!asccasecmp(lext
, "bz2")){
2484 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2485 if(res_or_null
!= NULL
)
2486 *res_or_null
= a_nag_OBSOLETE_bz2
;
2489 char const *cload
, *csave
;
2494 #define a_X1 "file-hook-load-"
2496 #define a_X2 "file-hook-save-"
2498 vbuf
= n_lofi_alloc(l
+ n_MAX(sizeof(a_X1
), sizeof(a_X2
)));
2500 memcpy(vbuf
, a_X1
, sizeof(a_X1
) -1);
2501 memcpy(&vbuf
[sizeof(a_X1
) -1], lext
, l
);
2502 vbuf
[sizeof(a_X1
) -1 + l
] = '\0';
2503 cload
= n_var_vlook(vbuf
, FAL0
);
2505 memcpy(vbuf
, a_X2
, sizeof(a_X2
) -1);
2506 memcpy(&vbuf
[sizeof(a_X2
) -1], lext
, l
);
2507 vbuf
[sizeof(a_X2
) -1 + l
] = '\0';
2508 csave
= n_var_vlook(vbuf
, FAL0
);
2514 if((csave
!= NULL
) | (cload
!= NULL
)){
2515 n_OBSOLETE("*file-hook-{load,save}-EXTENSION* will vanish, "
2516 "please use the `filetype' command");
2518 if(((csave
!= NULL
) ^ (cload
!= NULL
)) == 0){
2519 if(res_or_null
!= NULL
){
2520 res_or_null
->ft_ext_dat
= lext
;
2521 res_or_null
->ft_ext_len
= l
;
2522 res_or_null
->ft_load_dat
= cload
;
2523 res_or_null
->ft_load_len
= strlen(cload
);
2524 res_or_null
->ft_save_dat
= csave
;
2525 res_or_null
->ft_save_len
= strlen(csave
);
2529 n_alert(_("Incomplete *file-hook-{load,save}-EXTENSION* for: .%s"),
2538 return (lext
!= NULL
);