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
_same_name(char const *n1
, char const *n2
);
162 /* Delete the given name from a namelist */
163 static struct name
* delname(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 _same_name(char const *n1
, char const *n2
)
238 if (ok_blook(allnet
)) {
246 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
249 rv
= !asccasecmp(n1
, n2
);
256 delname(struct name
*np
, char const *name
)
261 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
262 if (_same_name(p
->n_name
, name
)) {
263 if (p
->n_blink
== NULL
) {
264 if (p
->n_flink
!= NULL
)
265 p
->n_flink
->n_blink
= NULL
;
269 if (p
->n_flink
== NULL
) {
270 if (p
->n_blink
!= NULL
)
271 p
->n_blink
->n_flink
= NULL
;
274 p
->n_blink
->n_flink
= p
->n_flink
;
275 p
->n_flink
->n_blink
= p
->n_blink
;
282 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
285 char *wp
, c
, inquote
, lc
, lastsp
;
290 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
291 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
298 /* Parse a full name: TODO RFC 5322
299 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
300 * - Skip entire (nested) comments
301 * - In non-quote, non-comment, join adjacent space to a single SP
302 * - Understand separators only in non-quote, non-comment context,
303 * and only if not part of a *quoted-pair* (XXX too liberal) */
305 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
314 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
320 if (inquote
|| lc
== '\\') {
328 cp
= skip_comment(cp
+ 1);
336 if (strchr(separators
, c
) != NULL
)
340 lastsp
= blankchar(c
);
354 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
357 struct name
*topp
, *np
, *t
;
363 if (line
== NULL
|| *line
== '\0')
368 nbuf
= smalloc(strlen(line
) +1);
369 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
370 t
= nalloc(nbuf
, ntype
);
385 a_nag_gexpand(size_t level
, struct name
*nlist
, struct group
*gp
, bool_t metoo
,
387 struct grp_names
*gnp
;
388 struct name
*nlist_tail
;
390 struct grp_names_head
*gnhp
;
393 if(UICMP(z
, level
++, >, n_ALIAS_MAXEXP
)){
394 n_err(_("Expanding alias to depth larger than %d\n"), n_ALIAS_MAXEXP
);
398 GP_TO_SUBCLASS(gnhp
, gp
);
399 logname
= ok_vlook(LOGNAME
);
401 for(gnp
= gnhp
->gnh_head
; gnp
!= NULL
; gnp
= gnp
->gn_next
){
407 if(!strcmp(cp
, gp
->g_id
))
410 if((ngp
= _group_find(GT_ALIAS
, cp
)) != NULL
){
411 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
412 * to a full address specification; aliases cannot be empty */
413 struct grp_names_head
*ngnhp
;
414 GP_TO_SUBCLASS(ngnhp
, ngp
);
416 assert(ngnhp
->gnh_head
!= NULL
);
417 if(metoo
|| ngnhp
->gnh_head
->gn_next
!= NULL
||
418 !_same_name(cp
, logname
))
419 nlist
= a_nag_gexpand(level
, nlist
, ngp
, metoo
, ntype
);
423 /* Here we should allow to expand to itself if only person in alias */
425 if(metoo
|| gnhp
->gnh_head
->gn_next
== NULL
|| !_same_name(cp
, logname
)){
428 np
= nalloc(cp
, ntype
| GFULL
);
429 if((nlist_tail
= nlist
) != NULL
){
430 while(nlist_tail
->n_flink
!= NULL
)
431 nlist_tail
= nlist_tail
->n_flink
;
432 nlist_tail
->n_flink
= np
;
433 np
->n_blink
= nlist_tail
;
444 a_nag_elide_qsort(void const *s1
, void const *s2
){
445 struct name
const * const *np1
, * const *np2
;
451 rv
= asccasecmp((*np1
)->n_name
, (*np2
)->n_name
);
456 static struct group
*
457 _group_lookup(enum group_type gt
, struct group_lookup
*glp
, char const *id
){
459 struct group
*lgp
, *gp
;
465 ( gt
& GT_COMMANDALIAS
? _commandalias_heads
466 : (gt
& GT_ALIAS
? _alias_heads
467 : (gt
& GT_MLIST
? _mlist_heads
468 : (gt
& GT_SHORTCUT
? _shortcut_heads
469 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
470 : (/*gt & GT_FILETYPE ?*/ _filetype_heads
472 gp
= *(glp
->gl_slot
= &glp
->gl_htable
[
473 ((gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
))
474 ? n_torek_ihash(id
) : n_torek_hash(id
)) % HSHSIZE
]);
477 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
479 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
480 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&&
481 !asccasecmp(&gp
->g_id
[1], id
))
484 for(; gp
!= NULL
; lgp
= gp
, gp
= gp
->g_next
)
485 if((gp
->g_type
& gt
) && *gp
->g_id
== c1
&& !strcmp(&gp
->g_id
[1], id
))
489 glp
->gl_slot_last
= lgp
;
495 static struct group
*
496 _group_find(enum group_type gt
, char const *id
)
498 struct group_lookup gl
;
502 gp
= _group_lookup(gt
, &gl
, id
);
507 static struct group
*
508 _group_go_first(enum group_type gt
, struct group_lookup
*glp
)
510 struct group
**gpa
, *gp
;
514 for (glp
->gl_htable
= gpa
= (
515 gt
& GT_COMMANDALIAS
? _commandalias_heads
516 : (gt
& GT_ALIAS
? _alias_heads
517 : (gt
& GT_MLIST
? _mlist_heads
518 : (gt
& GT_SHORTCUT
? _shortcut_heads
519 : (gt
& GT_CHARSETALIAS
? _charsetalias_heads
520 : (gt
& GT_FILETYPE
? _filetype_heads
523 i
< HSHSIZE
; ++gpa
, ++i
)
524 if ((gp
= *gpa
) != NULL
) {
530 glp
->gl_group
= gp
= NULL
;
532 glp
->gl_slot_last
= NULL
;
537 static struct group
*
538 _group_go_next(struct group_lookup
*glp
)
540 struct group
*gp
, **gpa
;
543 if ((gp
= glp
->gl_group
->g_next
) != NULL
)
544 glp
->gl_slot_last
= glp
->gl_group
;
546 glp
->gl_slot_last
= NULL
;
547 for (gpa
= glp
->gl_htable
+ HSHSIZE
; ++glp
->gl_slot
< gpa
;)
548 if ((gp
= *glp
->gl_slot
) != NULL
)
556 static struct group
*
557 _group_fetch(enum group_type gt
, char const *id
, size_t addsz
)
559 struct group_lookup gl
;
564 if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
)
568 if (UIZ_MAX
- n_ALIGN(l
) <= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
)))
571 i
= n_ALIGN(n_VSTRUCT_SIZEOF(struct group
, g_id
) + l
);
572 switch (gt
& GT_MASK
) {
573 case GT_COMMANDALIAS
:
574 addsz
+= sizeof(struct a_nag_cmd_alias
);
577 addsz
+= sizeof(struct grp_names_head
);
580 addsz
+= sizeof(struct a_nag_file_type
);
584 if (n_is_maybe_regex(id
)) {
585 addsz
= sizeof(struct grp_regex
);
591 case GT_CHARSETALIAS
:
595 if (UIZ_MAX
- i
< addsz
|| UI32_MAX
<= i
|| UI16_MAX
< i
- l
)
598 gp
= smalloc(i
+ addsz
);
599 memcpy(gp
->g_id
, id
, l
);
600 gp
->g_subclass_off
= (ui32_t
)i
;
601 gp
->g_id_len_sub
= (ui16_t
)(i
- --l
);
603 if(gt
& (GT_MLIST
| GT_CHARSETALIAS
| GT_FILETYPE
)){
606 for(cp
= gp
->g_id
; (c
= *cp
) != '\0'; ++cp
)
611 struct grp_names_head
*gnhp
;
613 GP_TO_SUBCLASS(gnhp
, gp
);
614 gnhp
->gnh_head
= NULL
;
617 else if (/*(gt & GT_MLIST) &&*/ gt
& GT_REGEX
) {
619 struct grp_regex
*grp
;
620 GP_TO_SUBCLASS(grp
, gp
);
622 if((s
= regcomp(&grp
->gr_regex
, id
,
623 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
624 n_err(_("Invalid regular expression: %s: %s\n"),
625 n_shexp_quote_cp(id
, FAL0
), n_regex_err_to_doc(&grp
->gr_regex
, s
));
630 grp
->gr_mygroup
= gp
;
635 gp
->g_next
= *gl
.gl_slot
;
643 _group_del(enum group_type gt
, char const *id
)
645 enum group_type xgt
= gt
& GT_MASK
;
646 struct group_lookup gl
;
650 /* Delete 'em all? */
651 if (id
[0] == '*' && id
[1] == '\0') {
652 for (gp
= _group_go_first(gt
, &gl
); gp
!= NULL
;)
653 gp
= (gp
->g_type
& xgt
) ? __group_del(&gl
) : _group_go_next(&gl
);
654 gp
= (struct group
*)TRU1
;
655 } else if ((gp
= _group_lookup(gt
, &gl
, id
)) != NULL
) {
656 if (gp
->g_type
& xgt
)
665 static struct group
*
666 __group_del(struct group_lookup
*glp
)
668 struct group
*x
, *gp
;
671 /* Overly complicated: link off this node, step ahead to next.. */
673 if((gp
= glp
->gl_slot_last
) != NULL
)
674 gp
= (gp
->g_next
= x
->g_next
);
676 glp
->gl_slot_last
= NULL
;
677 gp
= (*glp
->gl_slot
= x
->g_next
);
682 for(gpa
= &glp
->gl_htable
[HSHSIZE
]; ++glp
->gl_slot
< gpa
;)
683 if((gp
= *glp
->gl_slot
) != NULL
)
689 if (x
->g_type
& GT_ALIAS
)
692 else if (/*(x->g_type & GT_MLIST) &&*/ x
->g_type
& GT_REGEX
) {
693 struct grp_regex
*grp
;
694 GP_TO_SUBCLASS(grp
, x
);
696 regfree(&grp
->gr_regex
);
707 __names_del(struct group
*gp
)
709 struct grp_names_head
*gnhp
;
710 struct grp_names
*gnp
;
713 GP_TO_SUBCLASS(gnhp
, gp
);
714 for (gnp
= gnhp
->gnh_head
; gnp
!= NULL
;) {
715 struct grp_names
*x
= gnp
;
723 _group_print_all(enum group_type gt
)
727 struct group
const *gp
;
734 xgt
= gt
& GT_PRINT_MASK
;
735 gpa
= ( xgt
& GT_COMMANDALIAS
? _commandalias_heads
736 : (xgt
& GT_ALIAS
? _alias_heads
737 : (xgt
& GT_MLIST
? _mlist_heads
738 : (xgt
& GT_SHORTCUT
? _shortcut_heads
739 : (xgt
& GT_CHARSETALIAS
? _charsetalias_heads
740 : (xgt
& GT_FILETYPE
? _filetype_heads
743 for (h
= 0, i
= 1; h
< HSHSIZE
; ++h
)
744 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
745 if ((gp
->g_type
& xgt
) == xgt
)
747 ida
= salloc(i
* sizeof *ida
);
749 for (i
= h
= 0; h
< HSHSIZE
; ++h
)
750 for (gp
= gpa
[h
]; gp
!= NULL
; gp
= gp
->g_next
)
751 if ((gp
->g_type
& xgt
) == xgt
)
756 qsort(ida
, i
, sizeof *ida
, &__group_print_qsorter
);
758 if ((fp
= Ftmp(NULL
, "prgroup", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
762 for (i
= 0; ida
[i
] != NULL
; ++i
)
763 lines
+= _group_print(_group_find(gt
, ida
[i
]), fp
);
766 if (gt
& GT_SUBSCRIBE
)
767 i
= (ui32_t
)_mlsub_size
, h
= (ui32_t
)_mlsub_hits
;
769 i
= (ui32_t
)_mlist_size
, h
= (ui32_t
)_mlist_hits
;
770 if (i
> 0 && (n_poption
& n_PO_D_V
)){
771 fprintf(fp
, _("# %s list regex(7) total: %u entries, %u hits\n"),
772 (gt
& GT_SUBSCRIBE
? _("Subscribed") : _("Non-subscribed")),
779 if (fp
!= n_stdout
) {
780 page_or_print(fp
, lines
);
787 __group_print_qsorter(void const *a
, void const *b
)
792 rv
= strcmp(*(char**)n_UNCONST(a
), *(char**)n_UNCONST(b
));
798 _group_print(struct group
const *gp
, FILE *fo
)
806 if(gp
->g_type
& GT_COMMANDALIAS
){
807 struct a_nag_cmd_alias
*ncap
;
809 GP_TO_SUBCLASS(ncap
, gp
);
810 fprintf(fo
, "commandalias %s %s\n",
811 n_shexp_quote_cp(gp
->g_id
, TRU1
),
812 n_shexp_quote_cp(ncap
->ca_expand
.s
, TRU1
));
813 } else if (gp
->g_type
& GT_ALIAS
) {
814 struct grp_names_head
*gnhp
;
815 struct grp_names
*gnp
;
817 fprintf(fo
, "alias %s ", gp
->g_id
);
819 GP_TO_SUBCLASS(gnhp
, gp
);
820 if ((gnp
= gnhp
->gnh_head
) != NULL
) { /* xxx always 1+ entries */
822 struct grp_names
*x
= gnp
;
824 fprintf(fo
, " \"%s\"", string_quote(x
->gn_id
)); /* TODO shexp */
825 } while (gnp
!= NULL
);
828 } else if (gp
->g_type
& GT_MLIST
) {
830 if ((gp
->g_type
& GT_REGEX
) && (n_poption
& n_PO_D_V
)){
832 struct grp_regex
*grp
,
833 *lp
= (gp
->g_type
& GT_SUBSCRIBE
? _mlsub_regex
: _mlist_regex
);
835 GP_TO_SUBCLASS(grp
, gp
);
836 for (i
= 1; lp
!= grp
; lp
= lp
->gr_next
)
838 fprintf(fo
, "# regex(7): hits %" PRIuZ
", sort %" PRIuZ
".\n ",
844 fprintf(fo
, "wysh %s %s\n",
845 (gp
->g_type
& GT_SUBSCRIBE
? "mlsubscribe" : "mlist"),
846 n_shexp_quote_cp(gp
->g_id
, TRU1
));
847 } else if (gp
->g_type
& GT_SHORTCUT
) {
848 GP_TO_SUBCLASS(cp
, gp
);
849 fprintf(fo
, "wysh shortcut %s %s\n",
850 gp
->g_id
, n_shexp_quote_cp(cp
, TRU1
));
851 } else if (gp
->g_type
& GT_CHARSETALIAS
) {
852 GP_TO_SUBCLASS(cp
, gp
);
853 fprintf(fo
, "charsetalias %s %s\n",
854 n_shexp_quote_cp(gp
->g_id
, TRU1
), n_shexp_quote_cp(cp
, TRU1
));
855 } else if (gp
->g_type
& GT_FILETYPE
) {
856 struct a_nag_file_type
*nftp
;
858 GP_TO_SUBCLASS(nftp
, gp
);
859 fprintf(fo
, "filetype %s %s %s\n",
860 n_shexp_quote_cp(gp
->g_id
, TRU1
),
861 n_shexp_quote_cp(nftp
->nft_load
.s
, TRU1
),
862 n_shexp_quote_cp(nftp
->nft_save
.s
, TRU1
));
870 _mlmux(enum group_type gt
, char **argv
)
881 _group_print_all(gt
);
883 if ((gp
= _group_find(gt
, *argv
)) != NULL
) {
884 if (gt
& GT_SUBSCRIBE
) {
885 if (!(gp
->g_type
& GT_SUBSCRIBE
)) {
887 gp
->g_type
|= GT_SUBSCRIBE
;
890 ecp
= N_("Mailing-list already `mlsubscribe'd: %s\n");
894 ecp
= N_("Mailing-list already `mlist'ed: %s\n");
897 } else if(_group_fetch(gt
, *argv
, 0) == NULL
) {
898 ecp
= N_("Failed to create storage for mailing-list: %s\n");
900 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
903 } while (*++argv
!= NULL
);
910 _unmlmux(enum group_type gt
, char **argv
)
916 for (; *argv
!= NULL
; ++argv
) {
917 if (gt
& GT_SUBSCRIBE
) {
918 struct group_lookup gl
;
921 if (!(isaster
= (**argv
== '*')))
922 gp
= _group_find(gt
, *argv
);
923 else if ((gp
= _group_go_first(gt
, &gl
)) == NULL
)
925 else if (gp
!= NULL
&& !(gp
->g_type
& GT_SUBSCRIBE
))
930 if (gp
->g_type
& GT_SUBSCRIBE
) {
932 gp
->g_type
&= ~GT_SUBSCRIBE
;
936 while ((gp
= _group_go_next(&gl
)) != NULL
&&
937 !(gp
->g_type
& GT_SUBSCRIBE
))
943 n_err(_("Mailing-list not `mlsubscribe'd: %s\n"),
944 n_shexp_quote_cp(*argv
, FAL0
));
949 } else if (_group_del(gt
, *argv
))
951 n_err(_("No such mailing-list: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
960 _mlmux_linkin(struct group
*gp
)
962 struct grp_regex
**lpp
, *grp
, *lhp
;
965 if (gp
->g_type
& GT_SUBSCRIBE
) {
973 GP_TO_SUBCLASS(grp
, gp
);
974 if ((lhp
= *lpp
) != NULL
) {
975 (grp
->gr_last
= lhp
->gr_last
)->gr_next
= grp
;
976 (grp
->gr_next
= lhp
)->gr_last
= grp
;
978 *lpp
= grp
->gr_last
= grp
->gr_next
= grp
;
984 _mlmux_linkout(struct group
*gp
)
986 struct grp_regex
*grp
, **lpp
;
989 GP_TO_SUBCLASS(grp
, gp
);
991 if (gp
->g_type
& GT_SUBSCRIBE
) {
994 _mlsub_hits
-= grp
->gr_hits
;
998 _mlist_hits
-= grp
->gr_hits
;
1001 if (grp
->gr_next
== grp
)
1004 (grp
->gr_last
->gr_next
= grp
->gr_next
)->gr_last
= grp
->gr_last
;
1006 *lpp
= grp
->gr_next
;
1010 #endif /* HAVE_REGEX */
1013 nalloc(char const *str
, enum gfield ntype
)
1015 struct n_addrguts ag
;
1019 assert(!(ntype
& GFULLEXTRA
) || (ntype
& GFULL
) != 0);
1021 str
= n_addrspec_with_guts(&ag
, str
,
1022 ((ntype
& (GFULL
| GSKIN
| GREF
)) != 0), FAL0
);
1025 np = NULL; TODO We cannot return NULL,
1026 goto jleave; TODO thus handle failures in here!
1031 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
1032 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
1033 np
= salloc(sizeof(*np
) + ag
.ag_slen
+1);
1034 memcpy(np
+ 1, ag
.ag_skinned
, ag
.ag_slen
+1);
1035 ag
.ag_skinned
= (char*)(np
+ 1);
1037 np
= salloc(sizeof *np
);
1044 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
1045 np
->n_fullextra
= NULL
;
1046 np
->n_flags
= ag
.ag_n_flags
;
1048 if (ntype
& GFULL
) {
1049 if (ag
.ag_ilen
== ag
.ag_slen
1051 && !(ag
.ag_n_flags
& NAME_IDNA
)
1055 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
1058 /* n_fullextra is only the complete name part without address.
1059 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
1060 if ((ntype
& GFULLEXTRA
) && ag
.ag_ilen
> ag
.ag_slen
+ 2) {
1061 size_t s
= ag
.ag_iaddr_start
, e
= ag
.ag_iaddr_aend
, i
;
1064 if (s
== 0 || str
[--s
] != '<' || str
[e
++] != '>')
1065 goto jskipfullextra
;
1067 in
.s
= n_lofi_alloc(s
+ 1 + i
+1);
1068 while(s
> 0 && blankchar(str
[s
- 1]))
1070 memcpy(in
.s
, str
, s
);
1073 while (blankchar(str
[e
])) {
1079 memcpy(&in
.s
[s
], &str
[e
], i
);
1082 in
.s
[in
.l
= s
] = '\0';
1083 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1085 for (cp
= out
.s
, i
= out
.l
; i
> 0 && spacechar(*cp
); --i
, ++cp
)
1087 while (i
> 0 && spacechar(cp
[i
- 1]))
1089 np
->n_fullextra
= savestrbuf(cp
, i
);
1096 /* n_fullname depends on IDNA conversion */
1098 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
1100 in
.s
= n_UNCONST(str
);
1104 /* The domain name was IDNA and has been converted. We also have to
1105 * ensure that the domain name in .n_fullname is replaced with the
1106 * converted version, since MIME doesn't perform encoding of addrs */
1107 /* TODO This definetely doesn't belong here! */
1108 size_t l
= ag
.ag_iaddr_start
,
1109 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
1110 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
1111 memcpy(in
.s
, str
, l
);
1112 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
1114 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
1120 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1121 np
->n_fullname
= savestr(out
.s
);
1124 if (ag
.ag_n_flags
& NAME_IDNA
)
1127 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
1135 ndup(struct name
*np
, enum gfield ntype
)
1140 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
1141 nnp
= nalloc(np
->n_name
, ntype
);
1145 nnp
= salloc(sizeof *np
);
1146 nnp
->n_flink
= nnp
->n_blink
= NULL
;
1147 nnp
->n_type
= ntype
;
1148 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
1150 nnp
->n_name
= savestr(np
->n_name
);
1151 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
))) {
1152 nnp
->n_fullname
= nnp
->n_name
;
1153 nnp
->n_fullextra
= NULL
;
1155 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
1156 nnp
->n_fullname
= savestr(np
->n_fullname
);
1157 nnp
->n_fullextra
= (np
->n_fullextra
== NULL
) ? NULL
1158 : savestr(np
->n_fullextra
);
1166 cat(struct name
*n1
, struct name
*n2
)
1178 while (tail
->n_flink
!= NULL
)
1179 tail
= tail
->n_flink
;
1189 namelist_dup(struct name
const *np
, enum gfield ntype
){
1190 struct name
*nlist
, *xnp
;
1193 for(nlist
= xnp
= NULL
; np
!= NULL
; np
= np
->n_flink
){
1196 x
= ndup(n_UNCONST(np
), (np
->n_type
& ~GMASK
) | ntype
);
1197 if((x
->n_blink
= xnp
) == NULL
)
1208 count(struct name
const *np
)
1213 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1214 if (!(np
->n_type
& GDEL
))
1221 count_nonlocal(struct name
const *np
)
1226 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
1227 if (!(np
->n_type
& GDEL
) && !(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
))
1234 extract(char const *line
, enum gfield ntype
)
1239 rv
= _extract1(line
, ntype
, " \t,", 0);
1245 lextract(char const *line
, enum gfield ntype
)
1250 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
1251 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
1257 detract(struct name
*np
, enum gfield ntype
)
1268 flags
= ntype
& (GCOMMA
| GNAMEONLY
);
1269 ntype
&= ~(GCOMMA
| GNAMEONLY
);
1272 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1273 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1275 s
+= strlen(flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
) +1;
1285 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
1286 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
1288 cp
= sstpcpy(cp
, (flags
& GNAMEONLY
? p
->n_name
: p
->n_fullname
));
1289 if ((flags
& GCOMMA
) && p
->n_flink
!= NULL
)
1294 if ((flags
& GCOMMA
) && *--cp
== ',')
1302 grab_names(enum n_go_input_flags gif
, char const *field
, struct name
*np
,
1303 int comma
, enum gfield gflags
)
1309 np
= lextract(n_go_input_cp(gif
, field
, detract(np
, comma
)), gflags
);
1310 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
1311 if (is_addr_invalid(nq
, EACM_NONE
))
1318 name_is_same_domain(struct name
const *n1
, struct name
const *n2
)
1320 char const *d1
, *d2
;
1324 d1
= strrchr(n1
->n_name
, '@');
1325 d2
= strrchr(n2
->n_name
, '@');
1327 rv
= (d1
!= NULL
&& d2
!= NULL
) ? !asccasecmp(++d1
, ++d2
) : FAL0
;
1334 checkaddrs(struct name
*np
, enum expand_addr_check_mode eacm
,
1335 si8_t
*set_on_error
)
1340 for (n
= np
; n
!= NULL
; n
= n
->n_flink
) {
1343 if ((rv
= is_addr_invalid(n
, eacm
)) != 0) {
1344 if (set_on_error
!= NULL
)
1345 *set_on_error
|= rv
; /* don't loose -1! */
1346 else if (eacm
& EAF_MAYKEEP
) /* TODO HACK! See definition! */
1349 n
->n_blink
->n_flink
= n
->n_flink
;
1351 n
->n_flink
->n_blink
= n
->n_blink
;
1361 namelist_vaporise_head(struct header
*hp
, enum expand_addr_check_mode eacm
,
1362 bool_t metoo
, si8_t
*set_on_error
)
1364 /* TODO namelist_vaporise_head() is incredibly expensive and redundant */
1365 struct name
*tolist
, *np
, **npp
;
1368 tolist
= cat(hp
->h_to
, cat(hp
->h_cc
, hp
->h_bcc
));
1369 hp
->h_to
= hp
->h_cc
= hp
->h_bcc
= NULL
;
1371 tolist
= usermap(tolist
, metoo
);
1372 tolist
= elide(checkaddrs(tolist
, eacm
, set_on_error
));
1374 for (np
= tolist
; np
!= NULL
; np
= np
->n_flink
) {
1375 switch (np
->n_type
& (GDEL
| GMASK
)) {
1376 case GTO
: npp
= &hp
->h_to
; break;
1377 case GCC
: npp
= &hp
->h_cc
; break;
1378 case GBCC
: npp
= &hp
->h_bcc
; break;
1381 *npp
= cat(*npp
, ndup(np
, np
->n_type
| GFULL
));
1388 usermap(struct name
*names
, bool_t force_metoo
){
1390 struct name
*nlist
, *nlist_tail
, *np
, *cp
;
1394 metoo
= (force_metoo
|| ok_blook(metoo
));
1395 nlist
= nlist_tail
= NULL
;
1398 for(; np
!= NULL
; np
= cp
){
1399 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
1402 if(is_fileorpipe_addr(np
) ||
1403 (gp
= _group_find(GT_ALIAS
, np
->n_name
)) == NULL
){
1404 if((np
->n_blink
= nlist_tail
) != NULL
)
1405 nlist_tail
->n_flink
= np
;
1411 nlist
= a_nag_gexpand(0, nlist
, gp
, metoo
, np
->n_type
);
1412 if((nlist_tail
= nlist
) != NULL
)
1413 while(nlist_tail
->n_flink
!= NULL
)
1414 nlist_tail
= nlist_tail
->n_flink
;
1422 elide(struct name
*names
)
1425 struct name
*nlist
, *np
, **nparr
;
1433 /* Throw away all deleted nodes */
1434 for(np
= NULL
, i
= 0; names
!= NULL
; names
= names
->n_flink
)
1435 if(!(names
->n_type
& GDEL
)){
1436 names
->n_blink
= np
;
1438 np
->n_flink
= names
;
1445 if(nlist
== NULL
|| i
== 1)
1448 /* Create a temporay array and sort that */
1449 nparr
= n_lofi_alloc(sizeof(*nparr
) * i
);
1451 for(i
= 0, np
= nlist
; np
!= NULL
; np
= np
->n_flink
)
1454 qsort(nparr
, i
, sizeof *nparr
, &a_nag_elide_qsort
);
1456 /* Remove duplicates XXX speedup, or list_uniq()! */
1457 for(j
= 0, --i
; j
< i
;){
1458 if(asccasecmp(nparr
[j
]->n_name
, nparr
[k
= j
+ 1]->n_name
))
1462 nparr
[k
] = nparr
[k
+ 1];
1467 /* Throw away all list members which are not part of the array.
1468 * Note this keeps the original, possibly carefully crafted, order of the
1469 * addressees, thus */
1470 for(np
= nlist
; np
!= NULL
; np
= np
->n_flink
){
1471 for(j
= 0; j
<= i
; ++j
)
1478 nlist
= np
->n_flink
;
1481 np
->n_blink
->n_flink
= np
->n_flink
;
1482 if(np
->n_flink
!= NULL
)
1483 np
->n_flink
->n_blink
= np
->n_blink
;
1494 c_alternates(void *v
){
1501 if(*(argv
= v
) == NULL
){
1504 if((ccp
= ok_vlook(alternates
)) != NULL
)
1505 fprintf(n_stdout
, "alternates %s\n", ccp
);
1507 fputs(_("# no alternates registered\n"), n_stdout
);
1511 struct n_strlist
*slp
, **slpa
;
1513 while((slp
= a_nag_altnames
) != NULL
){
1514 a_nag_altnames
= slp
->sl_next
;
1519 /* Extension: only clearance? */
1520 if(argv
[1] == NULL
&& argv
[0][0] == '-' && argv
[0][1] == '\0')
1522 else for(slpa
= &a_nag_altnames
; *argv
!= NULL
; ++argv
){
1526 if((np
= lextract(*argv
, GSKIN
)) == NULL
|| np
->n_flink
!= NULL
||
1527 (np
= checkaddrs(np
, EACM_STRICT
, NULL
)) == NULL
){
1528 n_err(_("Invalid `alternates' argument: %s\n"),
1529 n_shexp_quote_cp(*argv
, FAL0
));
1534 l
= strlen(np
->n_name
);
1535 if(UIZ_MAX
- l
<= vl
){
1536 n_err(_("Failed to create storage for alternate: %s\n"),
1537 n_shexp_quote_cp(*argv
, FAL0
));
1542 slp
= n_STRLIST_ALLOC(l
);
1543 slp
->sl_next
= NULL
;
1545 memcpy(slp
->sl_dat
, np
->n_name
, ++l
);
1547 slpa
= &slp
->sl_next
;
1552 /* And put it into *alternates* */
1554 cp
= n_autorec_alloc(vl
);
1555 for(vl
= 0, slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
){
1556 memcpy(&cp
[vl
], slp
->sl_dat
, slp
->sl_len
);
1557 cp
[vl
+= slp
->sl_len
] = ' ';
1563 n_PS_ROOT_BLOCK(vl
> 0 ? ok_vset(alternates
, cp
) : ok_vclear(alternates
));
1570 delete_alternates(struct name
*np
)
1575 np
= delname(np
, ok_vlook(LOGNAME
));
1577 if(a_nag_altnames
!= NULL
){
1578 struct n_strlist
*slp
;
1580 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1581 np
= delname(np
, slp
->sl_dat
);
1584 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1585 while (xp
!= NULL
) {
1586 np
= delname(np
, xp
->n_name
);
1590 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1591 while (xp
!= NULL
) {
1592 np
= delname(np
, xp
->n_name
);
1596 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1597 while (xp
!= NULL
) {
1598 np
= delname(np
, xp
->n_name
);
1606 is_myname(char const *name
)
1612 if (_same_name(ok_vlook(LOGNAME
), name
))
1615 if(a_nag_altnames
!= NULL
){
1616 struct n_strlist
*slp
;
1618 for(slp
= a_nag_altnames
; slp
!= NULL
; slp
= slp
->sl_next
)
1619 if(_same_name(slp
->sl_dat
, name
))
1623 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
1624 while (xp
!= NULL
) {
1625 if (_same_name(xp
->n_name
, name
))
1630 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
1631 while (xp
!= NULL
) {
1632 if (_same_name(xp
->n_name
, name
))
1637 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
1638 while (xp
!= NULL
) {
1639 if (_same_name(xp
->n_name
, name
))
1650 c_addrcodec(void *vp
){
1651 struct n_addrguts ag
;
1652 struct n_string s_b
, *sp
;
1655 char const **argv
, *varname
, *act
, *cp
;
1658 sp
= n_string_creat_auto(&s_b
);
1660 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1663 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1674 alen
= PTR2SIZE(cp
- act
);
1682 if(i
<= UIZ_MAX
/ 4)
1684 sp
= n_string_reserve(sp
, i
);
1687 n_pstate_err_no
= n_ERR_NONE
;
1689 if(is_ascncaseprefix(act
, "encode", alen
)){
1690 /* This function cannot be a simple nalloc() wrapper even later on, since
1691 * we may need to turn any ", () or \ into quoted-pairs */
1694 while((c
= *cp
++) != '\0'){
1695 if(((c
== '(' || c
== ')') && mode
< 1) || (c
== '"' && mode
< 2) ||
1696 (c
== '\\' && mode
< 3))
1697 sp
= n_string_push_c(sp
, '\\');
1698 sp
= n_string_push_c(sp
, c
);
1701 if(n_addrspec_with_guts(&ag
, n_string_cp(sp
), TRU1
, TRU1
) == NULL
||
1702 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1703 ) != NAME_ADDRSPEC_ISADDR
){
1705 n_pstate_err_no
= n_ERR_INVAL
;
1710 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1711 cp
= np
->n_fullname
;
1713 }else if(mode
== 0){
1714 if(is_ascncaseprefix(act
, "decode", alen
)){
1717 while((c
= *cp
++) != '\0'){
1720 sp
= n_string_push_c(sp
, '(');
1721 act
= skip_comment(cp
);
1723 sp
= n_string_push_buf(sp
, cp
, PTR2SIZE(act
- cp
));
1724 sp
= n_string_push_c(sp
, ')');
1729 if((c
= *cp
++) == '"')
1731 if(c
== '\\' && (c
= *cp
) != '\0')
1733 sp
= n_string_push_c(sp
, c
);
1737 if(c
== '\\' && (c
= *cp
++) == '\0')
1739 sp
= n_string_push_c(sp
, c
);
1743 cp
= n_string_cp(sp
);
1744 }else if(is_ascncaseprefix(act
, "skin", alen
)){
1745 /* Let's just use the is-single-address hack for this one, too.. */
1746 if(n_addrspec_with_guts(&ag
, cp
, TRU1
, TRU1
) == NULL
||
1747 (ag
.ag_n_flags
& (NAME_ADDRSPEC_ISADDR
| NAME_ADDRSPEC_INVALID
)
1748 ) != NAME_ADDRSPEC_ISADDR
){
1749 n_pstate_err_no
= n_ERR_INVAL
;
1754 np
= nalloc(ag
.ag_input
, GTO
| GFULL
| GSKIN
);
1762 if(varname
== NULL
){
1763 if(fprintf(n_stdout
, "%s\n", cp
) < 0){
1764 n_pstate_err_no
= n_err_no
;
1767 }else if(!n_var_vset(varname
, (uintptr_t)cp
)){
1768 n_pstate_err_no
= n_ERR_NOTSUP
;
1774 return (vp
!= NULL
? 0 : 1);
1776 n_err(_("Synopsis: addrcodec: <[+[+[+]]]e[ncode]|d[ecode]|s[kin]> "
1777 "<rest-of-line>\n"));
1778 n_pstate_err_no
= n_ERR_INVAL
;
1784 c_commandalias(void *vp
){
1786 char const **argv
, *ccp
;
1793 if((ccp
= *argv
) == NULL
){
1794 _group_print_all(GT_COMMANDALIAS
);
1798 /* Verify the name is a valid one, and not a command modifier */
1799 if(*ccp
== '\0' || *n_cmd_isolate(ccp
) != '\0' ||
1800 !asccasecmp(ccp
, "ignerr") || !asccasecmp(ccp
, "wysh") ||
1801 !asccasecmp(ccp
, "vput")){
1802 n_err(_("`commandalias': not a valid command name: %s\n"),
1803 n_shexp_quote_cp(ccp
, FAL0
));
1808 if(argv
[1] == NULL
){
1809 if((gp
= _group_find(GT_COMMANDALIAS
, ccp
)) != NULL
)
1810 _group_print(gp
, n_stdout
);
1812 n_err(_("No such commandalias: %s\n"), n_shexp_quote_cp(ccp
, FAL0
));
1816 /* Because one hardly ever redefines, anything is stored in one chunk */
1820 /* Delete the old one, if any; don't get fooled to remove them all */
1821 if(ccp
[0] != '*' || ccp
[1] != '\0')
1822 _group_del(GT_COMMANDALIAS
, ccp
);
1824 for(i
= len
= 0, ++argv
; argv
[i
] != NULL
; ++i
)
1825 len
+= strlen(argv
[i
]) + 1;
1829 if((gp
= _group_fetch(GT_COMMANDALIAS
, ccp
, len
)) == NULL
){
1830 n_err(_("Failed to create storage for commandalias: %s\n"),
1831 n_shexp_quote_cp(ccp
, FAL0
));
1834 struct a_nag_cmd_alias
*ncap
;
1836 GP_TO_SUBCLASS(ncap
, gp
);
1837 GP_TO_SUBCLASS(cp
, gp
);
1839 ncap
->ca_expand
.s
= cp
;
1840 ncap
->ca_expand
.l
= len
- 1;
1842 for(len
= 0; (ccp
= *argv
++) != NULL
;)
1843 if((i
= strlen(ccp
)) > 0){
1858 c_uncommandalias(void *vp
){
1866 do if(!_group_del(GT_COMMANDALIAS
, *argv
)){
1867 n_err(_("No such `commandalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
1869 }while(*++argv
!= NULL
);
1875 n_commandalias_exists(char const *name
, struct str
const **expansion_or_null
){
1879 if((gp
= _group_find(GT_COMMANDALIAS
, name
)) != NULL
){
1882 if(expansion_or_null
!= NULL
){
1883 struct a_nag_cmd_alias
*ncap
;
1885 GP_TO_SUBCLASS(ncap
, gp
);
1886 *expansion_or_null
= &ncap
->ca_expand
;
1895 n_alias_is_valid_name(char const *name
){
1901 for(rv
= TRU1
, cp
= name
++; (c
= *cp
++) != '\0';)
1902 /* User names, plus things explicitly mentioned in Postfix aliases(5).
1903 * As an extension, allow period: [[:alnum:]_#:@.-]+$? */
1904 if(!alnumchar(c
) && c
!= '_' && c
!= '-' &&
1905 c
!= '#' && c
!= ':' && c
!= '@' &&
1907 if(c
== '$' && cp
!= name
&& *cp
== '\0')
1927 n_UNINIT(ecp
, NULL
);
1930 _group_print_all(GT_ALIAS
);
1931 else if (!n_alias_is_valid_name(*argv
)) {
1932 ecp
= N_("Not a valid alias name: %s\n");
1934 } else if (argv
[1] == NULL
) {
1935 if ((gp
= _group_find(GT_ALIAS
, *argv
)) != NULL
)
1936 _group_print(gp
, n_stdout
);
1938 ecp
= N_("No such alias: %s\n");
1941 } else if ((gp
= _group_fetch(GT_ALIAS
, *argv
, 0)) == NULL
) {
1942 ecp
= N_("Failed to create alias storage for: %s\n");
1944 n_err(V_(ecp
), n_shexp_quote_cp(*argv
, FAL0
));
1947 struct grp_names
*gnp_tail
, *gnp
;
1948 struct grp_names_head
*gnhp
;
1950 GP_TO_SUBCLASS(gnhp
, gp
);
1952 if((gnp_tail
= gnhp
->gnh_head
) != NULL
)
1953 while((gnp
= gnp_tail
->gn_next
) != NULL
)
1956 for(++argv
; *argv
!= NULL
; ++argv
){
1959 i
= strlen(*argv
) +1;
1960 gnp
= smalloc(n_VSTRUCT_SIZEOF(struct grp_names
, gn_id
) + i
);
1961 if(gnp_tail
!= NULL
)
1962 gnp_tail
->gn_next
= gnp
;
1964 gnhp
->gnh_head
= gnp
;
1966 gnp
->gn_next
= NULL
;
1967 memcpy(gnp
->gn_id
, *argv
, i
);
1981 do if (!_group_del(GT_ALIAS
, *argv
)) {
1982 n_err(_("No such alias: %s\n"), *argv
);
1984 } while (*++argv
!= NULL
);
1995 rv
= _mlmux(GT_MLIST
, v
);
2006 rv
= _unmlmux(GT_MLIST
, v
);
2012 c_mlsubscribe(void *v
)
2017 rv
= _mlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2023 c_unmlsubscribe(void *v
)
2028 rv
= _unmlmux(GT_MLIST
| GT_SUBSCRIBE
, v
);
2034 is_mlist(char const *name
, bool_t subscribed_only
)
2038 struct grp_regex
**lpp
, *grp
;
2041 enum mlist_state rv
;
2044 gp
= _group_find(GT_MLIST
, name
);
2045 rv
= (gp
!= NULL
) ? MLIST_KNOWN
: MLIST_OTHER
;
2046 if (rv
== MLIST_KNOWN
) {
2047 if (gp
->g_type
& GT_SUBSCRIBE
)
2048 rv
= MLIST_SUBSCRIBED
;
2049 else if (subscribed_only
)
2051 /* Of course, if that is a regular expression it doesn't mean a thing */
2053 if (gp
->g_type
& GT_REGEX
)
2060 /* Not in the hashmap (as something matchable), walk the lists */
2063 lpp
= &_mlsub_regex
;
2065 if ((grp
= *lpp
) != NULL
) {
2066 do if (regexec(&grp
->gr_regex
, name
, 0,NULL
, 0) != REG_NOMATCH
) {
2067 /* Relink as the head of this list if the hit count of this group is
2068 * >= 25% of the average hit count */
2071 i
= ++_mlsub_hits
/ _mlsub_size
;
2073 i
= ++_mlist_hits
/ _mlist_size
;
2076 if (++grp
->gr_hits
>= i
&& *lpp
!= grp
&& grp
->gr_next
!= grp
) {
2077 grp
->gr_last
->gr_next
= grp
->gr_next
;
2078 grp
->gr_next
->gr_last
= grp
->gr_last
;
2079 (grp
->gr_last
= (*lpp
)->gr_last
)->gr_next
= grp
;
2080 (grp
->gr_next
= *lpp
)->gr_last
= grp
;
2083 rv
= !re2
? MLIST_SUBSCRIBED
: MLIST_KNOWN
;
2085 } while ((grp
= grp
->gr_next
) != *lpp
);
2087 if (!re2
&& !subscribed_only
) {
2089 lpp
= &_mlist_regex
;
2092 assert(rv
== MLIST_OTHER
);
2112 _group_print_all(GT_SHORTCUT
);
2113 else if(argv
[1] == NULL
){
2114 if((gp
= _group_find(GT_SHORTCUT
, *argv
)) != NULL
)
2115 _group_print(gp
, n_stdout
);
2117 n_err(_("No such shortcut: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2120 }else for (; *argv
!= NULL
; argv
+= 2) {
2121 /* Because one hardly ever redefines, anything is stored in one chunk */
2125 if (argv
[1] == NULL
) {
2126 n_err(_("Synopsis: shortcut: <shortcut> <expansion>\n"));
2130 if (_group_find(GT_SHORTCUT
, *argv
) != NULL
)
2131 _group_del(GT_SHORTCUT
, *argv
);
2133 l
= strlen(argv
[1]) +1;
2134 if ((gp
= _group_fetch(GT_SHORTCUT
, *argv
, l
)) == NULL
) {
2135 n_err(_("Failed to create storage for shortcut: %s\n"),
2136 n_shexp_quote_cp(*argv
, FAL0
));
2139 GP_TO_SUBCLASS(cp
, gp
);
2140 memcpy(cp
, argv
[1], l
);
2148 c_unshortcut(void *v
)
2154 do if (!_group_del(GT_SHORTCUT
, *argv
)) {
2155 n_err(_("No such shortcut: %s\n"), *argv
);
2157 } while (*++argv
!= NULL
);
2163 shortcut_expand(char const *str
){
2167 if((gp
= _group_find(GT_SHORTCUT
, str
)) != NULL
)
2168 GP_TO_SUBCLASS(str
, gp
);
2176 c_charsetalias(void *vp
){
2186 _group_print_all(GT_CHARSETALIAS
);
2187 else if(argv
[1] == NULL
){
2188 if((gp
= _group_find(GT_CHARSETALIAS
, *argv
)) != NULL
)
2189 _group_print(gp
, n_stdout
);
2191 n_err(_("No such charsetalias: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2194 }else for(; *argv
!= NULL
; argv
+= 2){
2195 /* Because one hardly ever redefines, anything is stored in one chunk */
2200 if(argv
[1] == NULL
){
2201 n_err(_("Synopsis: charsetalias: <charset> <charset-alias>\n"));
2206 /* Delete the old one, if any; don't get fooled to remove them all */
2208 if(ccp
[0] != '*' || ccp
[1] != '\0')
2209 _group_del(GT_CHARSETALIAS
, ccp
);
2211 l
= strlen(argv
[1]) +1;
2212 if ((gp
= _group_fetch(GT_CHARSETALIAS
, ccp
, l
)) == NULL
) {
2213 n_err(_("Failed to create storage for charsetalias: %s\n"),
2214 n_shexp_quote_cp(ccp
, FAL0
));
2217 GP_TO_SUBCLASS(cp
, gp
);
2218 for(ccp
= argv
[1]; (c
= *ccp
++) != '\0';)
2219 *cp
++ = lowerconv(c
);
2228 c_uncharsetalias(void *vp
){
2236 do if(!_group_del(GT_CHARSETALIAS
, *argv
)){
2237 n_err(_("No such `charsetalias': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2239 }while(*++argv
!= NULL
);
2245 n_charsetalias_expand(char const *cp
){
2248 char const *cp_orig
;
2253 for(i
= 0; (gp
= _group_find(GT_CHARSETALIAS
, cp
)) != NULL
;){
2254 GP_TO_SUBCLASS(cp
, gp
);
2255 if(++i
== 8) /* XXX Magic (same as for `ghost' expansion) */
2266 c_filetype(void *vp
){ /* TODO support automatic chains: .tar.gz -> .gz + .tar */
2268 char **argv
; /* TODO While there: let ! prefix mean: direct execlp(2) */
2276 _group_print_all(GT_FILETYPE
);
2277 else if(argv
[1] == NULL
){
2278 if((gp
= _group_find(GT_FILETYPE
, *argv
)) != NULL
)
2279 _group_print(gp
, n_stdout
);
2281 n_err(_("No such filetype: %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2284 }else for(; *argv
!= NULL
; argv
+= 3){
2285 /* Because one hardly ever redefines, anything is stored in one chunk */
2290 if(argv
[1] == NULL
|| argv
[2] == NULL
){
2291 n_err(_("Synopsis: filetype: <extension> <load-cmd> <save-cmd>\n"));
2296 /* Delete the old one, if any; don't get fooled to remove them all */
2298 if(ccp
[0] != '*' || ccp
[1] != '\0')
2299 _group_del(GT_FILETYPE
, ccp
);
2301 /* Lowercase it all (for display purposes) */
2304 while((c
= *cp
) != '\0')
2305 *cp
++ = lowerconv(c
);
2307 llc
= strlen(argv
[1]) +1;
2308 lsc
= strlen(argv
[2]) +1;
2309 if(UIZ_MAX
- llc
<= lsc
)
2312 if((gp
= _group_fetch(GT_FILETYPE
, ccp
, llc
+ lsc
)) == NULL
){
2314 n_err(_("Failed to create storage for filetype: %s\n"),
2315 n_shexp_quote_cp(argv
[0], FAL0
));
2318 struct a_nag_file_type
*nftp
;
2320 GP_TO_SUBCLASS(nftp
, gp
);
2321 GP_TO_SUBCLASS(cp
, gp
);
2323 memcpy(nftp
->nft_load
.s
= cp
, argv
[1], llc
);
2325 nftp
->nft_load
.l
= --llc
;
2326 memcpy(nftp
->nft_save
.s
= cp
, argv
[2], lsc
);
2328 nftp
->nft_save
.l
= --lsc
;
2336 c_unfiletype(void *vp
){
2344 do if(!_group_del(GT_FILETYPE
, *argv
)){
2345 n_err(_("No such `filetype': %s\n"), n_shexp_quote_cp(*argv
, FAL0
));
2347 }while(*++argv
!= NULL
);
2353 n_filetype_trial(struct n_file_type
*res_or_null
, char const *file
){
2355 struct group_lookup gl
;
2356 struct n_string s
, *sp
;
2357 struct group
const *gp
;
2361 sp
= n_string_creat_auto(&s
);
2362 sp
= n_string_assign_cp(sp
, file
);
2363 sp
= n_string_push_c(sp
, '.');
2366 for(gp
= _group_go_first(GT_FILETYPE
, &gl
); gp
!= NULL
;
2367 gp
= _group_go_next(&gl
)){
2368 sp
= n_string_trunc(sp
, l
);
2369 sp
= n_string_push_buf(sp
, gp
->g_id
,
2370 gp
->g_subclass_off
- gp
->g_id_len_sub
);
2372 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2373 if(res_or_null
!= NULL
){
2374 struct a_nag_file_type
*nftp
;
2376 GP_TO_SUBCLASS(nftp
, gp
);
2377 res_or_null
->ft_ext_dat
= gp
->g_id
;
2378 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2379 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2380 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2381 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2382 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2384 goto jleave
; /* TODO after v15 legacy drop: break; */
2388 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2389 * TODO but NOT supporting *file-hook-{load,save}-EXTENSION* */
2390 gp
= (struct group
*)0x1;
2392 sp
= n_string_trunc(sp
, l
);
2393 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_xz
.ft_ext_dat
,
2394 a_nag_OBSOLETE_xz
.ft_ext_len
);
2395 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2396 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2397 if(res_or_null
!= NULL
)
2398 *res_or_null
= a_nag_OBSOLETE_xz
;
2402 sp
= n_string_trunc(sp
, l
);
2403 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_gz
.ft_ext_dat
,
2404 a_nag_OBSOLETE_gz
.ft_ext_len
);
2405 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2406 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2407 if(res_or_null
!= NULL
)
2408 *res_or_null
= a_nag_OBSOLETE_gz
;
2412 sp
= n_string_trunc(sp
, l
);
2413 sp
= n_string_push_buf(sp
, a_nag_OBSOLETE_bz2
.ft_ext_dat
,
2414 a_nag_OBSOLETE_bz2
.ft_ext_len
);
2415 if(!stat(n_string_cp(sp
), &stb
) && S_ISREG(stb
.st_mode
)){
2416 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2417 if(res_or_null
!= NULL
)
2418 *res_or_null
= a_nag_OBSOLETE_bz2
;
2426 return (gp
!= NULL
);
2430 n_filetype_exists(struct n_file_type
*res_or_null
, char const *file
){
2431 char const *ext
, *lext
;
2434 if((ext
= strrchr(file
, '/')) != NULL
)
2437 for(lext
= NULL
; (ext
= strchr(file
, '.')) != NULL
; lext
= file
= ext
){
2438 struct group
const *gp
;
2440 if((gp
= _group_find(GT_FILETYPE
, ++ext
)) != NULL
){
2442 if(res_or_null
!= NULL
){
2443 struct a_nag_file_type
*nftp
;
2445 GP_TO_SUBCLASS(nftp
, gp
);
2446 res_or_null
->ft_ext_dat
= gp
->g_id
;
2447 res_or_null
->ft_ext_len
= gp
->g_subclass_off
- gp
->g_id_len_sub
;
2448 res_or_null
->ft_load_dat
= nftp
->nft_load
.s
;
2449 res_or_null
->ft_load_len
= nftp
->nft_load
.l
;
2450 res_or_null
->ft_save_dat
= nftp
->nft_save
.s
;
2451 res_or_null
->ft_save_len
= nftp
->nft_save
.l
;
2453 goto jleave
; /* TODO after v15 legacy drop: break; */
2457 /* TODO v15 legacy code: automatic file hooks for .{bz2,gz,xz},
2458 * TODO as well as supporting *file-hook-{load,save}-EXTENSION* */
2462 if(!asccasecmp(lext
, "xz")){
2463 n_OBSOLETE(".xz support will vanish, please use the `filetype' command");
2464 if(res_or_null
!= NULL
)
2465 *res_or_null
= a_nag_OBSOLETE_xz
;
2467 }else if(!asccasecmp(lext
, "gz")){
2468 n_OBSOLETE(".gz support will vanish, please use the `filetype' command");
2469 if(res_or_null
!= NULL
)
2470 *res_or_null
= a_nag_OBSOLETE_gz
;
2472 }else if(!asccasecmp(lext
, "bz2")){
2473 n_OBSOLETE(".bz2 support will vanish, please use the `filetype' command");
2474 if(res_or_null
!= NULL
)
2475 *res_or_null
= a_nag_OBSOLETE_bz2
;
2478 char const *cload
, *csave
;
2483 #define a_X1 "file-hook-load-"
2485 #define a_X2 "file-hook-save-"
2487 vbuf
= n_lofi_alloc(l
+ n_MAX(sizeof(a_X1
), sizeof(a_X2
)));
2489 memcpy(vbuf
, a_X1
, sizeof(a_X1
) -1);
2490 memcpy(&vbuf
[sizeof(a_X1
) -1], lext
, l
);
2491 vbuf
[sizeof(a_X1
) -1 + l
] = '\0';
2492 cload
= n_var_vlook(vbuf
, FAL0
);
2494 memcpy(vbuf
, a_X2
, sizeof(a_X2
) -1);
2495 memcpy(&vbuf
[sizeof(a_X2
) -1], lext
, l
);
2496 vbuf
[sizeof(a_X2
) -1 + l
] = '\0';
2497 csave
= n_var_vlook(vbuf
, FAL0
);
2503 if((csave
!= NULL
) | (cload
!= NULL
)){
2504 n_OBSOLETE("*file-hook-{load,save}-EXTENSION* will vanish, "
2505 "please use the `filetype' command");
2507 if(((csave
!= NULL
) ^ (cload
!= NULL
)) == 0){
2508 if(res_or_null
!= NULL
){
2509 res_or_null
->ft_ext_dat
= lext
;
2510 res_or_null
->ft_ext_len
= l
;
2511 res_or_null
->ft_load_dat
= cload
;
2512 res_or_null
->ft_load_len
= strlen(cload
);
2513 res_or_null
->ft_save_dat
= csave
;
2514 res_or_null
->ft_save_len
= strlen(csave
);
2518 n_alert(_("Incomplete *file-hook-{load,save}-EXTENSION* for: .%s"),
2527 return (lext
!= NULL
);