Copyright 2017
[s-mailx.git] / nam_a_grp.c
blob88aa1d5285847a82bab5b2b42afea8a476b9771b
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Name lists, alternates and groups: aliases, mailing lists, shortcuts.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE nam_a_grp
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 enum group_type {
43 /* Main types (bits not values for easier testing only) */
44 GT_ALIAS = 1<< 0,
45 GT_MLIST = 1<< 1,
46 GT_SHORTCUT = 1<< 2,
47 GT_MASK = GT_ALIAS | GT_MLIST | GT_SHORTCUT,
49 /* Subtype bits and flags */
50 GT_SUBSCRIBE = 1<< 4,
51 GT_REGEX = 1<< 5,
53 /* Extended type mask to be able to reflect what we really have; i.e., mlist
54 * can have GT_REGEX if they are subscribed or not, but `mlsubscribe' should
55 * print out only GT_MLIST which have the GT_SUBSCRIBE attribute set */
56 GT_PRINT_MASK = GT_MASK | GT_SUBSCRIBE
59 struct group {
60 struct group *g_next;
61 size_t g_subclass_off; /* of "subclass" in .g_id (if any) */
62 ui8_t g_type; /* enum group_type */
63 /* Identifying name, of variable size. Dependent on actual "subtype" more
64 * data follows thereafter, but note this is always used (i.e., for regular
65 * expression entries this is still set to the plain string) */
66 char g_id[n_VFIELD_SIZE(-1)];
68 #define GP_TO_SUBCLASS(X,G) \
69 do {\
70 union __group_subclass {void *gs_vp; char *gs_cp;} __gs__;\
71 __gs__.gs_cp = (char*)n_UNCONST(G) + (G)->g_subclass_off;\
72 (X) = __gs__.gs_vp;\
73 } while (0)
75 struct grp_names_head {
76 struct grp_names *gnh_head;
79 struct grp_names {
80 struct grp_names *gn_next;
81 char gn_id[n_VFIELD_SIZE(0)];
84 #ifdef HAVE_REGEX
85 struct grp_regex {
86 struct grp_regex *gr_last;
87 struct grp_regex *gr_next;
88 struct group *gr_mygroup; /* xxx because lists use grp_regex*! ?? */
89 size_t gr_hits; /* Number of times this group matched */
90 regex_t gr_regex;
92 #endif
94 struct group_lookup {
95 struct group **gl_htable;
96 struct group **gl_slot;
97 struct group *gl_slot_last;
98 struct group *gl_group;
101 /* List of alternate names of user */
102 static char **_altnames;
104 /* `alias' */
105 static struct group *_alias_heads[HSHSIZE]; /* TODO dynamic hash */
107 /* `mlist', `mlsubscribe'. Anything is stored in the hashmap.. */
108 static struct group *_mlist_heads[HSHSIZE]; /* TODO dynamic hash */
110 /* ..but entries which have GT_REGEX set are false lookups and will really be
111 * accessed via sequential lists instead, which are type-specific for better
112 * performance, but also to make it possible to have ".*@xy.org" as a mlist
113 * and "(one|two)@xy.org" as a mlsubscription.
114 * These lists use a bit of QOS optimization in that a matching group will
115 * become relinked as the new list head if its hit count is
116 * (>= ((xy_hits / _xy_size) >> 2))
117 * Note that the hit counts only count currently linked in nodes.. */
118 #ifdef HAVE_REGEX
119 static struct grp_regex *_mlist_regex, *_mlsub_regex;
120 static size_t _mlist_size, _mlist_hits, _mlsub_size, _mlsub_hits;
121 #endif
123 /* `shortcut' */
124 static struct group *_shortcut_heads[HSHSIZE]; /* TODO dynamic hash */
126 /* Same name, while taking care for *allnet*? */
127 static bool_t _same_name(char const *n1, char const *n2);
129 /* Delete the given name from a namelist */
130 static struct name * delname(struct name *np, char const *name);
132 /* Put another node onto a list of names and return the list */
133 static struct name * put(struct name *list, struct name *node);
135 /* Grab a single name (liberal name) */
136 static char const * yankname(char const *ap, char *wbuf,
137 char const *separators, int keepcomms);
139 /* Extraction multiplexer that splits an input line to names */
140 static struct name * _extract1(char const *line, enum gfield ntype,
141 char const *separators, bool_t keepcomms);
143 /* Recursively expand a alias name. Limit expansion to some fixed level.
144 * Direct recursion is not expanded for convenience */
145 static struct name * _gexpand(size_t level, struct name *nlist,
146 struct group *gp, bool_t metoo, int ntype);
148 /* Lookup a group, return it or NULL, fill in glp anyway */
149 static struct group * _group_lookup(enum group_type gt,
150 struct group_lookup *glp, char const *id);
152 /* Easier-to-use wrapper around _group_lookup() */
153 static struct group * _group_find(enum group_type gt, char const *id);
155 /* Iteration: go to the first group, which also inits the iterator. A valid
156 * iterator can be stepped via _next(). A NULL return means no (more) groups
157 * to be iterated exist, in which case only glp->gl_group is set (NULL) */
158 static struct group * _group_go_first(enum group_type gt,
159 struct group_lookup *glp);
160 static struct group * _group_go_next(struct group_lookup *glp);
162 /* Fetch the group id, create it as necessary */
163 static struct group * _group_fetch(enum group_type gt, char const *id,
164 size_t addsz);
166 /* "Intelligent" delete which handles a "*" id, too;
167 * returns a true boolean if a group was deleted, and always succeeds for "*" */
168 static bool_t _group_del(enum group_type gt, char const *id);
170 static struct group * __group_del(struct group_lookup *glp);
171 static void __names_del(struct group *gp);
173 /* Print all groups of the given type, alphasorted */
174 static void _group_print_all(enum group_type gt);
176 static int __group_print_qsorter(void const *a, void const *b);
178 /* Really print a group, actually. Return number of written lines */
179 static size_t _group_print(struct group const *gp, FILE *fo);
181 /* Multiplexers for list and subscribe commands */
182 static int _mlmux(enum group_type gt, char **argv);
183 static int _unmlmux(enum group_type gt, char **argv);
185 /* Relinkers for the sequential match lists */
186 #ifdef HAVE_REGEX
187 static void _mlmux_linkin(struct group *gp);
188 static void _mlmux_linkout(struct group *gp);
189 # define _MLMUX_LINKIN(GP) \
190 do if ((GP)->g_type & GT_REGEX) _mlmux_linkin(GP); while (0)
191 # define _MLMUX_LINKOUT(GP) \
192 do if ((GP)->g_type & GT_REGEX) _mlmux_linkout(GP); while (0)
193 #else
194 # define _MLMUX_LINKIN(GP)
195 # define _MLMUX_LINKOUT(GP)
196 #endif
198 static bool_t
199 _same_name(char const *n1, char const *n2)
201 bool_t rv = FAL0;
202 char c1, c2;
203 NYD_ENTER;
205 if (ok_blook(allnet)) {
206 do {
207 c1 = *n1++;
208 c2 = *n2++;
209 c1 = lowerconv(c1);
210 c2 = lowerconv(c2);
211 if (c1 != c2)
212 goto jleave;
213 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
214 rv = 1;
215 } else
216 rv = !asccasecmp(n1, n2);
217 jleave:
218 NYD_LEAVE;
219 return rv;
222 static struct name *
223 delname(struct name *np, char const *name)
225 struct name *p;
226 NYD_ENTER;
228 for (p = np; p != NULL; p = p->n_flink)
229 if (_same_name(p->n_name, name)) {
230 if (p->n_blink == NULL) {
231 if (p->n_flink != NULL)
232 p->n_flink->n_blink = NULL;
233 np = p->n_flink;
234 continue;
236 if (p->n_flink == NULL) {
237 if (p->n_blink != NULL)
238 p->n_blink->n_flink = NULL;
239 continue;
241 p->n_blink->n_flink = p->n_flink;
242 p->n_flink->n_blink = p->n_blink;
244 NYD_LEAVE;
245 return np;
248 static struct name *
249 put(struct name *list, struct name *node)
251 NYD_ENTER;
252 node->n_flink = list;
253 node->n_blink = NULL;
254 if (list != NULL)
255 list->n_blink = node;
256 NYD_LEAVE;
257 return node;
260 static char const *
261 yankname(char const *ap, char *wbuf, char const *separators, int keepcomms)
263 char const *cp;
264 char *wp, c, inquote, lc, lastsp;
265 NYD_ENTER;
267 *(wp = wbuf) = '\0';
269 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
270 for (c = *ap; blankchar(c) || c == ','; c = *++ap)
272 if (c == '\0') {
273 cp = NULL;
274 goto jleave;
277 /* Parse a full name: TODO RFC 5322
278 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
279 * - Skip entire (nested) comments
280 * - In non-quote, non-comment, join adjacent space to a single SP
281 * - Understand separators only in non-quote, non-comment context,
282 * and only if not part of a *quoted-pair* (XXX too liberal) */
283 cp = ap;
284 for (inquote = lc = lastsp = 0;; lc = c, ++cp) {
285 c = *cp;
286 if (c == '\0')
287 break;
288 if (c == '\\')
289 goto jwpwc;
290 if (c == '"') {
291 if (lc != '\\')
292 inquote = !inquote;
293 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
294 else
295 --wp;
296 #endif
297 goto jwpwc;
299 if (inquote || lc == '\\') {
300 jwpwc:
301 *wp++ = c;
302 lastsp = 0;
303 continue;
305 if (c == '(') {
306 ap = cp;
307 cp = skip_comment(cp + 1);
308 if (keepcomms)
309 while (ap < cp)
310 *wp++ = *ap++;
311 --cp;
312 lastsp = 0;
313 continue;
315 if (strchr(separators, c) != NULL)
316 break;
318 lc = lastsp;
319 lastsp = blankchar(c);
320 if (!lastsp || !lc)
321 *wp++ = c;
323 if (blankchar(lc))
324 --wp;
326 *wp = '\0';
327 jleave:
328 NYD_LEAVE;
329 return cp;
332 static struct name *
333 _extract1(char const *line, enum gfield ntype, char const *separators,
334 bool_t keepcomms)
336 struct name *topp, *np, *t;
337 char const *cp;
338 char *nbuf;
339 NYD_ENTER;
341 topp = NULL;
342 if (line == NULL || *line == '\0')
343 goto jleave;
345 np = NULL;
346 cp = line;
347 nbuf = smalloc(strlen(line) +1);
348 while ((cp = yankname(cp, nbuf, separators, keepcomms)) != NULL) {
349 t = nalloc(nbuf, ntype);
350 if (topp == NULL)
351 topp = t;
352 else
353 np->n_flink = t;
354 t->n_blink = np;
355 np = t;
357 free(nbuf);
358 jleave:
359 NYD_LEAVE;
360 return topp;
363 static struct name *
364 _gexpand(size_t level, struct name *nlist, struct group *gp, bool_t metoo,
365 int ntype)
367 struct grp_names_head *gnhp;
368 struct grp_names *gnp;
369 NYD_ENTER;
371 if (UICMP(z, level++, >, MAXEXP)) {
372 n_err(_("Expanding alias to depth larger than %d\n"), MAXEXP);
373 goto jleave;
376 GP_TO_SUBCLASS(gnhp, gp);
377 for (gnp = gnhp->gnh_head; gnp != NULL; gnp = gnp->gn_next) {
378 char *cp;
379 struct group *ngp;
381 /* FIXME we do not really support leading backslash quoting do we??? */
382 if (*(cp = gnp->gn_id) == '\\' || !strcmp(cp, gp->g_id))
383 goto jquote;
385 if ((ngp = _group_find(GT_ALIAS, cp)) != NULL) {
386 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
387 * to a full address specification; aliases cannot be empty */
388 struct grp_names_head *ngnhp;
389 GP_TO_SUBCLASS(ngnhp, ngp);
391 assert(ngnhp->gnh_head != NULL);
392 if (metoo || ngnhp->gnh_head->gn_next != NULL ||
393 !_same_name(cp, myname))
394 nlist = _gexpand(level, nlist, ngp, metoo, ntype);
395 continue;
398 /* Here we should allow to expand to itself if only person in alias */
399 jquote:
400 if (metoo || gnhp->gnh_head->gn_next == NULL || !_same_name(cp, myname))
401 nlist = put(nlist, nalloc(cp, ntype | GFULL));
403 jleave:
404 NYD_LEAVE;
405 return nlist;
408 static struct group *
409 _group_lookup(enum group_type gt, struct group_lookup *glp, char const *id)
411 struct group *lgp, *gp;
412 NYD_ENTER;
414 gt &= GT_MASK;
415 lgp = NULL;
416 gp = *(glp->gl_htable = glp->gl_slot =
417 ((gt & GT_ALIAS ? _alias_heads :
418 (gt & GT_MLIST ? _mlist_heads :
419 (/*gt & GT_SHORTCUT ?*/ _shortcut_heads /*: NULL */))) +
420 torek_hash(id) % HSHSIZE));
422 for (; gp != NULL; lgp = gp, gp = gp->g_next)
423 if ((gp->g_type & gt) && *gp->g_id == *id && !strcmp(gp->g_id, id))
424 break;
426 glp->gl_slot_last = lgp;
427 glp->gl_group = gp;
428 NYD_LEAVE;
429 return gp;
432 static struct group *
433 _group_find(enum group_type gt, char const *id)
435 struct group_lookup gl;
436 struct group *gp;
437 NYD_ENTER;
439 gp = _group_lookup(gt, &gl, id);
440 NYD_LEAVE;
441 return gp;
444 static struct group *
445 _group_go_first(enum group_type gt, struct group_lookup *glp)
447 struct group **gpa, *gp;
448 size_t i;
449 NYD_ENTER;
451 for (glp->gl_htable = gpa = (gt & GT_ALIAS ? _alias_heads :
452 (gt & GT_MLIST ? _mlist_heads :
453 (/*gt & GT_SHORTCUT ?*/ _shortcut_heads /*: NULL */))), i = 0;
454 i < HSHSIZE; ++gpa, ++i)
455 if ((gp = *gpa) != NULL) {
456 glp->gl_slot = gpa;
457 glp->gl_group = gp;
458 goto jleave;
461 glp->gl_group = gp = NULL;
462 jleave:
463 glp->gl_slot_last = NULL;
464 NYD_LEAVE;
465 return gp;
468 static struct group *
469 _group_go_next(struct group_lookup *glp)
471 struct group *gp, **gpa;
472 NYD_ENTER;
474 if ((gp = glp->gl_group->g_next) != NULL)
475 glp->gl_slot_last = glp->gl_group;
476 else {
477 glp->gl_slot_last = NULL;
478 for (gpa = glp->gl_htable + HSHSIZE; ++glp->gl_slot < gpa;)
479 if ((gp = *glp->gl_slot) != NULL)
480 break;
482 glp->gl_group = gp;
483 NYD_LEAVE;
484 return gp;
487 static struct group *
488 _group_fetch(enum group_type gt, char const *id, size_t addsz)
490 struct group_lookup gl;
491 struct group *gp;
492 size_t l, i;
493 NYD_ENTER;
495 if ((gp = _group_lookup(gt, &gl, id)) != NULL)
496 goto jleave;
498 l = strlen(id) +1;
499 i = n_ALIGN(n_VSTRUCT_SIZEOF(struct group, g_id) + l);
500 switch (gt & GT_MASK) {
501 case GT_ALIAS:
502 addsz = sizeof(struct grp_names_head);
503 break;
504 case GT_MLIST:
505 #ifdef HAVE_REGEX
506 if (n_is_maybe_regex(id)) {
507 addsz = sizeof(struct grp_regex);
508 gt |= GT_REGEX;
510 #endif
511 case GT_SHORTCUT:
512 default:
513 break;
516 gp = smalloc(i + addsz);
517 gp->g_subclass_off = i;
518 gp->g_type = gt;
519 memcpy(gp->g_id, id, l);
521 if (gt & GT_ALIAS) {
522 struct grp_names_head *gnhp;
524 GP_TO_SUBCLASS(gnhp, gp);
525 gnhp->gnh_head = NULL;
527 #ifdef HAVE_REGEX
528 else if (/*(gt & GT_MLIST) &&*/ gt & GT_REGEX) {
529 struct grp_regex *grp;
530 GP_TO_SUBCLASS(grp, gp);
532 if (regcomp(&grp->gr_regex, id, REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
533 n_err(_("Invalid regular expression: %s\n"), id);
534 free(gp);
535 gp = NULL;
536 goto jleave;
538 grp->gr_mygroup = gp;
539 _mlmux_linkin(gp);
541 #endif
543 gp->g_next = *gl.gl_slot;
544 *gl.gl_slot = gp;
545 jleave:
546 NYD_LEAVE;
547 return gp;
550 static bool_t
551 _group_del(enum group_type gt, char const *id)
553 enum group_type xgt = gt & GT_MASK;
554 struct group_lookup gl;
555 struct group *gp;
556 NYD_ENTER;
558 /* Delete 'em all? */
559 if (id[0] == '*' && id[1] == '\0') {
560 for (gp = _group_go_first(gt, &gl); gp != NULL;)
561 gp = (gp->g_type & xgt) ? __group_del(&gl) : _group_go_next(&gl);
562 gp = (struct group*)TRU1;
563 } else if ((gp = _group_lookup(gt, &gl, id)) != NULL) {
564 if (gp->g_type & xgt)
565 __group_del(&gl);
566 else
567 gp = NULL;
569 NYD_LEAVE;
570 return (gp != NULL);
573 static struct group *
574 __group_del(struct group_lookup *glp)
576 struct group *x, *gp;
577 NYD_ENTER;
579 /* Overly complicated: link off this node, step ahead to next.. */
580 x = glp->gl_group;
581 if ((gp = glp->gl_slot_last) != NULL) {
582 gp = (gp->g_next = x->g_next);
583 } else {
584 glp->gl_slot_last = NULL;
585 gp = (*glp->gl_slot = x->g_next);
587 if (gp == NULL) {
588 struct group **gpa = glp->gl_htable + HSHSIZE;
590 while (++glp->gl_slot < gpa)
591 if ((gp = *glp->gl_slot) != NULL)
592 break;
595 glp->gl_group = gp;
597 if (x->g_type & GT_ALIAS)
598 __names_del(x);
599 #ifdef HAVE_REGEX
600 else if (/*(x->g_type & GT_MLIST) &&*/ x->g_type & GT_REGEX) {
601 struct grp_regex *grp;
602 GP_TO_SUBCLASS(grp, x);
604 regfree(&grp->gr_regex);
605 _mlmux_linkout(x);
607 #endif
609 free(x);
610 NYD_LEAVE;
611 return gp;
614 static void
615 __names_del(struct group *gp)
617 struct grp_names_head *gnhp;
618 struct grp_names *gnp;
619 NYD_ENTER;
621 GP_TO_SUBCLASS(gnhp, gp);
622 for (gnp = gnhp->gnh_head; gnp != NULL;) {
623 struct grp_names *x = gnp;
624 gnp = gnp->gn_next;
625 free(x);
627 NYD_LEAVE;
630 static void
631 _group_print_all(enum group_type gt)
633 enum group_type xgt;
634 struct group **gpa;
635 struct group const *gp;
636 ui32_t h, i;
637 char const **ida;
638 FILE *fp;
639 size_t lines;
640 NYD_ENTER;
642 xgt = gt & GT_PRINT_MASK;
643 gpa = (xgt & GT_ALIAS ? _alias_heads
644 : (xgt & GT_MLIST ? _mlist_heads
645 : (/*xgt & GT_SHORTCUT ?*/ _shortcut_heads
646 /*: NULL */)));
648 for (h = 0, i = 1; h < HSHSIZE; ++h)
649 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
650 if ((gp->g_type & xgt) == xgt)
651 ++i;
652 ida = salloc(i * sizeof *ida);
654 for (i = h = 0; h < HSHSIZE; ++h)
655 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
656 if ((gp->g_type & xgt) == xgt)
657 ida[i++] = gp->g_id;
658 ida[i] = NULL;
660 if (i > 1)
661 qsort(ida, i, sizeof *ida, &__group_print_qsorter);
663 if ((fp = Ftmp(NULL, "prgroup", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
664 fp = stdout;
665 lines = 0;
667 for (i = 0; ida[i] != NULL; ++i)
668 lines += _group_print(_group_find(gt, ida[i]), fp);
669 #ifdef HAVE_REGEX
670 if (gt & GT_MLIST) {
671 if (gt & GT_SUBSCRIBE)
672 i = (ui32_t)_mlsub_size, h = (ui32_t)_mlsub_hits;
673 else
674 i = (ui32_t)_mlist_size, h = (ui32_t)_mlist_hits;
675 if (i > 0 && (options & OPT_D_V)){
676 fprintf(fp, _("# %s list regex(7) total: %u entries, %u hits\n"),
677 (gt & GT_SUBSCRIBE ? _("Subscribed") : _("Non-subscribed")),
678 i, h);
679 ++lines;
682 #endif
684 if (fp != stdout) {
685 page_or_print(fp, lines);
686 Fclose(fp);
688 NYD_LEAVE;
691 static int
692 __group_print_qsorter(void const *a, void const *b)
694 int rv;
695 NYD_ENTER;
697 rv = strcmp(*(char**)n_UNCONST(a), *(char**)n_UNCONST(b));
698 NYD_LEAVE;
699 return rv;
702 static size_t
703 _group_print(struct group const *gp, FILE *fo)
705 char const *cp;
706 size_t rv;
707 NYD_ENTER;
709 rv = 1;
711 if (gp->g_type & GT_ALIAS) {
712 struct grp_names_head *gnhp;
713 struct grp_names *gnp;
715 fprintf(fo, "alias %s ", gp->g_id);
717 GP_TO_SUBCLASS(gnhp, gp);
718 if ((gnp = gnhp->gnh_head) != NULL) { /* xxx always 1+ entries */
719 do {
720 struct grp_names *x = gnp;
721 gnp = gnp->gn_next;
722 fprintf(fo, " \"%s\"", string_quote(x->gn_id)); /* TODO shexp */
723 } while (gnp != NULL);
725 putc('\n', fo);
726 } else if (gp->g_type & GT_MLIST) {
727 #ifdef HAVE_REGEX
728 if ((gp->g_type & GT_REGEX) && (options & OPT_D_V)){
729 size_t i;
730 struct grp_regex *grp,
731 *lp = (gp->g_type & GT_SUBSCRIBE ? _mlsub_regex : _mlist_regex);
733 GP_TO_SUBCLASS(grp, gp);
734 for (i = 1; lp != grp; lp = lp->gr_next)
735 ++i;
736 fprintf(fo, "# regex(7): hits %" PRIuZ ", sort %" PRIuZ ".\n ",
737 grp->gr_hits, i);
738 ++rv;
740 #endif
742 fprintf(fo, "wysh %s %s\n",
743 (gp->g_type & GT_SUBSCRIBE ? "mlsubscribe" : "mlist"),
744 n_shexp_quote_cp(gp->g_id, TRU1));
745 } else if (gp->g_type & GT_SHORTCUT) {
746 GP_TO_SUBCLASS(cp, gp);
747 fprintf(fo, "wysh shortcut %s %s\n",
748 gp->g_id, n_shexp_quote_cp(cp, TRU1));
751 NYD_LEAVE;
752 return rv;
755 static int
756 _mlmux(enum group_type gt, char **argv)
758 struct group *gp;
759 int rv = 0;
760 NYD_ENTER;
762 if (*argv == NULL)
763 _group_print_all(gt);
764 else do {
765 if ((gp = _group_find(gt, *argv)) != NULL) {
766 if (gt & GT_SUBSCRIBE) {
767 if (!(gp->g_type & GT_SUBSCRIBE)) {
768 _MLMUX_LINKOUT(gp);
769 gp->g_type |= GT_SUBSCRIBE;
770 _MLMUX_LINKIN(gp);
771 } else {
772 n_err(_("Mailing-list already `mlsubscribe'd: %s\n"),
773 *argv);
774 rv = 1;
776 } else {
777 n_err(_("Mailing-list already `mlist'ed: %s\n"), *argv);
778 rv = 1;
780 } else
781 _group_fetch(gt, *argv, 0);
782 } while (*++argv != NULL);
784 NYD_LEAVE;
785 return rv;
788 static int
789 _unmlmux(enum group_type gt, char **argv)
791 struct group *gp;
792 int rv = 0;
793 NYD_ENTER;
795 for (; *argv != NULL; ++argv) {
796 if (gt & GT_SUBSCRIBE) {
797 struct group_lookup gl;
798 bool_t isaster;
800 if (!(isaster = (**argv == '*')))
801 gp = _group_find(gt, *argv);
802 else if ((gp = _group_go_first(gt, &gl)) == NULL)
803 continue;
804 else if (gp != NULL && !(gp->g_type & GT_SUBSCRIBE))
805 goto jaster_entry;
807 if (gp != NULL) {
808 jaster_redo:
809 if (gp->g_type & GT_SUBSCRIBE) {
810 _MLMUX_LINKOUT(gp);
811 gp->g_type &= ~GT_SUBSCRIBE;
812 _MLMUX_LINKIN(gp);
813 if (isaster) {
814 jaster_entry:
815 while ((gp = _group_go_next(&gl)) != NULL &&
816 !(gp->g_type & GT_SUBSCRIBE))
818 if (gp != NULL)
819 goto jaster_redo;
821 } else {
822 n_err(_("Mailing-list not `mlsubscribe'd: %s\n"),
823 n_shexp_quote_cp(*argv, FAL0));
824 rv = 1;
826 continue;
828 } else if (_group_del(gt, *argv))
829 continue;
830 n_err(_("No such mailing-list: %s\n"), n_shexp_quote_cp(*argv, FAL0));
831 rv = 1;
833 NYD_LEAVE;
834 return rv;
837 #ifdef HAVE_REGEX
838 static void
839 _mlmux_linkin(struct group *gp)
841 struct grp_regex **lpp, *grp, *lhp;
842 NYD_ENTER;
844 if (gp->g_type & GT_SUBSCRIBE) {
845 lpp = &_mlsub_regex;
846 ++_mlsub_size;
847 } else {
848 lpp = &_mlist_regex;
849 ++_mlist_size;
852 GP_TO_SUBCLASS(grp, gp);
853 if ((lhp = *lpp) != NULL) {
854 (grp->gr_last = lhp->gr_last)->gr_next = grp;
855 (grp->gr_next = lhp)->gr_last = grp;
856 } else
857 *lpp = grp->gr_last = grp->gr_next = grp;
858 grp->gr_hits = 0;
859 NYD_LEAVE;
862 static void
863 _mlmux_linkout(struct group *gp)
865 struct grp_regex *grp, **lpp;
866 NYD_ENTER;
868 GP_TO_SUBCLASS(grp, gp);
870 if (gp->g_type & GT_SUBSCRIBE) {
871 lpp = &_mlsub_regex;
872 --_mlsub_size;
873 _mlsub_hits -= grp->gr_hits;
874 } else {
875 lpp = &_mlist_regex;
876 --_mlist_size;
877 _mlist_hits -= grp->gr_hits;
880 if (grp->gr_next == grp)
881 *lpp = NULL;
882 else {
883 (grp->gr_last->gr_next = grp->gr_next)->gr_last = grp->gr_last;
884 if (*lpp == grp)
885 *lpp = grp->gr_next;
887 NYD_LEAVE;
889 #endif /* HAVE_REGEX */
891 FL struct name *
892 nalloc(char const *str, enum gfield ntype)
894 struct n_addrguts ag;
895 struct str in, out;
896 struct name *np;
897 NYD_ENTER;
898 assert(!(ntype & GFULLEXTRA) || (ntype & GFULL) != 0);
900 str = n_addrspec_with_guts(&ag, str,
901 ((ntype & (GFULL | GSKIN | GREF)) != 0));
902 if(str == NULL){
904 np = NULL; TODO We cannot return NULL,
905 goto jleave; TODO thus handle failures in here!
907 str = ag.ag_input;
910 if (!(ag.ag_n_flags & NAME_NAME_SALLOC)) {
911 ag.ag_n_flags |= NAME_NAME_SALLOC;
912 np = salloc(sizeof(*np) + ag.ag_slen +1);
913 memcpy(np + 1, ag.ag_skinned, ag.ag_slen +1);
914 ag.ag_skinned = (char*)(np + 1);
915 } else
916 np = salloc(sizeof *np);
918 np->n_flink = NULL;
919 np->n_blink = NULL;
920 np->n_type = ntype;
921 np->n_flags = 0;
923 np->n_fullname = np->n_name = ag.ag_skinned;
924 np->n_fullextra = NULL;
925 np->n_flags = ag.ag_n_flags;
927 if (ntype & GFULL) {
928 if (ag.ag_ilen == ag.ag_slen
929 #ifdef HAVE_IDNA
930 && !(ag.ag_n_flags & NAME_IDNA)
931 #endif
933 goto jleave;
934 if (ag.ag_n_flags & NAME_ADDRSPEC_ISFILEORPIPE)
935 goto jleave;
937 /* n_fullextra is only the complete name part without address.
938 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
939 if ((ntype & GFULLEXTRA) && ag.ag_ilen > ag.ag_slen + 2) {
940 size_t s = ag.ag_iaddr_start, e = ag.ag_iaddr_aend, i;
941 char const *cp;
943 if (s == 0 || str[--s] != '<' || str[e++] != '>')
944 goto jskipfullextra;
945 i = ag.ag_ilen - e;
946 in.s = n_lofi_alloc(s + 1 + i +1);
947 while(s > 0 && blankchar(str[s - 1]))
948 --s;
949 memcpy(in.s, str, s);
950 if (i > 0) {
951 in.s[s++] = ' ';
952 while (blankchar(str[e])) {
953 ++e;
954 if (--i == 0)
955 break;
957 if (i > 0)
958 memcpy(&in.s[s], &str[e], i);
960 s += i;
961 in.s[in.l = s] = '\0';
962 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
964 for (cp = out.s, i = out.l; i > 0 && spacechar(*cp); --i, ++cp)
966 while (i > 0 && spacechar(cp[i - 1]))
967 --i;
968 np->n_fullextra = savestrbuf(cp, i);
970 n_lofi_free(in.s);
971 free(out.s);
973 jskipfullextra:
975 /* n_fullname depends on IDNA conversion */
976 #ifdef HAVE_IDNA
977 if (!(ag.ag_n_flags & NAME_IDNA)) {
978 #endif
979 in.s = n_UNCONST(str);
980 in.l = ag.ag_ilen;
981 #ifdef HAVE_IDNA
982 } else {
983 /* The domain name was IDNA and has been converted. We also have to
984 * ensure that the domain name in .n_fullname is replaced with the
985 * converted version, since MIME doesn't perform encoding of addrs */
986 /* TODO This definetely doesn't belong here! */
987 size_t l = ag.ag_iaddr_start,
988 lsuff = ag.ag_ilen - ag.ag_iaddr_aend;
989 in.s = ac_alloc(l + ag.ag_slen + lsuff +1);
990 memcpy(in.s, str, l);
991 memcpy(in.s + l, ag.ag_skinned, ag.ag_slen);
992 l += ag.ag_slen;
993 memcpy(in.s + l, str + ag.ag_iaddr_aend, lsuff);
994 l += lsuff;
995 in.s[l] = '\0';
996 in.l = l;
998 #endif
999 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1000 np->n_fullname = savestr(out.s);
1001 free(out.s);
1002 #ifdef HAVE_IDNA
1003 if (ag.ag_n_flags & NAME_IDNA)
1004 ac_free(in.s);
1005 #endif
1006 np->n_flags |= NAME_FULLNAME_SALLOC;
1008 jleave:
1009 NYD_LEAVE;
1010 return np;
1013 FL struct name *
1014 ndup(struct name *np, enum gfield ntype)
1016 struct name *nnp;
1017 NYD_ENTER;
1019 if ((ntype & (GFULL | GSKIN)) && !(np->n_flags & NAME_SKINNED)) {
1020 nnp = nalloc(np->n_name, ntype);
1021 goto jleave;
1024 nnp = salloc(sizeof *np);
1025 nnp->n_flink = nnp->n_blink = NULL;
1026 nnp->n_type = ntype;
1027 nnp->n_flags = (np->n_flags & ~(NAME_NAME_SALLOC | NAME_FULLNAME_SALLOC)) |
1028 NAME_NAME_SALLOC;
1029 nnp->n_name = savestr(np->n_name);
1030 if (np->n_name == np->n_fullname || !(ntype & (GFULL | GSKIN))) {
1031 nnp->n_fullname = nnp->n_name;
1032 nnp->n_fullextra = NULL;
1033 } else {
1034 nnp->n_flags |= NAME_FULLNAME_SALLOC;
1035 nnp->n_fullname = savestr(np->n_fullname);
1036 nnp->n_fullextra = (np->n_fullextra == NULL) ? NULL
1037 : savestr(np->n_fullextra);
1039 jleave:
1040 NYD_LEAVE;
1041 return nnp;
1044 FL struct name *
1045 cat(struct name *n1, struct name *n2)
1047 struct name *tail;
1048 NYD_ENTER;
1050 tail = n2;
1051 if (n1 == NULL)
1052 goto jleave;
1053 tail = n1;
1054 if (n2 == NULL)
1055 goto jleave;
1057 while (tail->n_flink != NULL)
1058 tail = tail->n_flink;
1059 tail->n_flink = n2;
1060 n2->n_blink = tail;
1061 tail = n1;
1062 jleave:
1063 NYD_LEAVE;
1064 return tail;
1067 FL struct name *
1068 namelist_dup(struct name const *np, enum gfield ntype)
1070 struct name *nnp;
1071 NYD_ENTER;
1073 for (nnp = NULL; np != NULL; np = np->n_flink) {
1074 struct name *x = ndup(n_UNCONST(np), (np->n_type & ~GMASK) | ntype);
1075 x->n_flink = nnp;
1076 nnp = x;
1078 NYD_LEAVE;
1079 return nnp;
1082 FL ui32_t
1083 count(struct name const *np)
1085 ui32_t c;
1086 NYD_ENTER;
1088 for (c = 0; np != NULL; np = np->n_flink)
1089 if (!(np->n_type & GDEL))
1090 ++c;
1091 NYD_LEAVE;
1092 return c;
1095 FL ui32_t
1096 count_nonlocal(struct name const *np)
1098 ui32_t c;
1099 NYD_ENTER;
1101 for (c = 0; np != NULL; np = np->n_flink)
1102 if (!(np->n_type & GDEL) && !(np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE))
1103 ++c;
1104 NYD_LEAVE;
1105 return c;
1108 FL struct name *
1109 extract(char const *line, enum gfield ntype)
1111 struct name *rv;
1112 NYD_ENTER;
1114 rv = _extract1(line, ntype, " \t,", 0);
1115 NYD_LEAVE;
1116 return rv;
1119 FL struct name *
1120 lextract(char const *line, enum gfield ntype)
1122 struct name *rv;
1123 NYD_ENTER;
1125 rv = ((line != NULL && strpbrk(line, ",\"\\(<|"))
1126 ? _extract1(line, ntype, ",", 1) : extract(line, ntype));
1127 NYD_LEAVE;
1128 return rv;
1131 FL char *
1132 detract(struct name *np, enum gfield ntype)
1134 char *topp, *cp;
1135 struct name *p;
1136 int flags, s;
1137 NYD_ENTER;
1139 topp = NULL;
1140 if (np == NULL)
1141 goto jleave;
1143 flags = ntype & (GCOMMA | GNAMEONLY);
1144 ntype &= ~(GCOMMA | GNAMEONLY);
1145 s = 0;
1147 for (p = np; p != NULL; p = p->n_flink) {
1148 if (ntype && (p->n_type & GMASK) != ntype)
1149 continue;
1150 s += strlen(flags & GNAMEONLY ? p->n_name : p->n_fullname) +1;
1151 if (flags & GCOMMA)
1152 ++s;
1154 if (s == 0)
1155 goto jleave;
1157 s += 2;
1158 topp = salloc(s);
1159 cp = topp;
1160 for (p = np; p != NULL; p = p->n_flink) {
1161 if (ntype && (p->n_type & GMASK) != ntype)
1162 continue;
1163 cp = sstpcpy(cp, (flags & GNAMEONLY ? p->n_name : p->n_fullname));
1164 if ((flags & GCOMMA) && p->n_flink != NULL)
1165 *cp++ = ',';
1166 *cp++ = ' ';
1168 *--cp = 0;
1169 if ((flags & GCOMMA) && *--cp == ',')
1170 *cp = 0;
1171 jleave:
1172 NYD_LEAVE;
1173 return topp;
1176 FL struct name *
1177 grab_names(enum n_lexinput_flags lif, char const *field, struct name *np,
1178 int comma, enum gfield gflags)
1180 struct name *nq;
1181 NYD_ENTER;
1183 jloop:
1184 np = lextract(n_lex_input_cp(lif, field, detract(np, comma)), gflags);
1185 for (nq = np; nq != NULL; nq = nq->n_flink)
1186 if (is_addr_invalid(nq, EACM_NONE))
1187 goto jloop;
1188 NYD_LEAVE;
1189 return np;
1192 FL bool_t
1193 name_is_same_domain(struct name const *n1, struct name const *n2)
1195 char const *d1, *d2;
1196 bool_t rv;
1197 NYD_ENTER;
1199 d1 = strrchr(n1->n_name, '@');
1200 d2 = strrchr(n2->n_name, '@');
1202 rv = (d1 != NULL && d2 != NULL) ? !asccasecmp(++d1, ++d2) : FAL0;
1204 NYD_LEAVE;
1205 return rv;
1208 FL struct name *
1209 checkaddrs(struct name *np, enum expand_addr_check_mode eacm,
1210 si8_t *set_on_error)
1212 struct name *n;
1213 NYD_ENTER;
1215 for (n = np; n != NULL;) {
1216 si8_t rv;
1218 if ((rv = is_addr_invalid(n, eacm)) != 0) {
1219 if (set_on_error != NULL)
1220 *set_on_error |= rv; /* don't loose -1! */
1221 if (n->n_blink)
1222 n->n_blink->n_flink = n->n_flink;
1223 if (n->n_flink)
1224 n->n_flink->n_blink = n->n_blink;
1225 if (n == np)
1226 np = n->n_flink;
1228 n = n->n_flink;
1230 NYD_LEAVE;
1231 return np;
1234 FL struct name *
1235 namelist_vaporise_head(struct header *hp, enum expand_addr_check_mode eacm,
1236 bool_t metoo, si8_t *set_on_error)
1238 struct name *tolist, *np, **npp;
1239 NYD_ENTER;
1241 tolist = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), metoo);
1242 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
1244 tolist = elide(checkaddrs(tolist, eacm, set_on_error));
1246 for (np = tolist; np != NULL; np = np->n_flink) {
1247 switch (np->n_type & (GDEL | GMASK)) {
1248 case GTO: npp = &hp->h_to; break;
1249 case GCC: npp = &hp->h_cc; break;
1250 case GBCC: npp = &hp->h_bcc; break;
1251 default: continue;
1253 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
1255 NYD_LEAVE;
1256 return tolist;
1259 FL struct name *
1260 usermap(struct name *names, bool_t force_metoo)
1262 struct name *new, *np, *cp;
1263 struct group *gp;
1264 int metoo;
1265 NYD_ENTER;
1267 new = NULL;
1268 np = names;
1269 metoo = (force_metoo || ok_blook(metoo));
1270 while (np != NULL) {
1271 assert(!(np->n_type & GDEL)); /* TODO legacy */
1272 if (is_fileorpipe_addr(np) || np->n_name[0] == '\\') {
1273 cp = np->n_flink;
1274 new = put(new, np);
1275 np = cp;
1276 continue;
1278 gp = _group_find(GT_ALIAS, np->n_name);
1279 cp = np->n_flink;
1280 if (gp != NULL)
1281 new = _gexpand(0, new, gp, metoo, np->n_type);
1282 else
1283 new = put(new, np);
1284 np = cp;
1286 NYD_LEAVE;
1287 return new;
1290 FL struct name *
1291 elide(struct name *names)
1293 struct name *np, *t, *newn, *x;
1294 NYD_ENTER;
1296 newn = NULL;
1297 if (names == NULL)
1298 goto jleave;
1300 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
1301 for (np = NULL; names != NULL; names = names->n_flink)
1302 if (!(names->n_type & GDEL)) {
1303 names->n_blink = np;
1304 if (np)
1305 np->n_flink = names;
1306 else
1307 newn = names;
1308 np = names;
1310 if (newn == NULL)
1311 goto jleave;
1313 np = newn->n_flink;
1314 if (np != NULL)
1315 np->n_blink = NULL;
1316 newn->n_flink = NULL;
1318 while (np != NULL) {
1319 int cmpres;
1321 t = newn;
1322 while ((cmpres = asccasecmp(t->n_name, np->n_name)) < 0) {
1323 if (t->n_flink == NULL)
1324 break;
1325 t = t->n_flink;
1328 /* If we ran out of t's, put new entry after the current value of t */
1329 if (cmpres < 0) {
1330 t->n_flink = np;
1331 np->n_blink = t;
1332 t = np;
1333 np = np->n_flink;
1334 t->n_flink = NULL;
1335 continue;
1338 /* Otherwise, put the new entry in front of the current t. If at the
1339 * front of the list, the new guy becomes the new head of the list */
1340 if (t == newn) {
1341 t = np;
1342 np = np->n_flink;
1343 t->n_flink = newn;
1344 newn->n_blink = t;
1345 t->n_blink = NULL;
1346 newn = t;
1347 continue;
1350 /* The normal case -- we are inserting into the middle of the list */
1351 x = np;
1352 np = np->n_flink;
1353 x->n_flink = t;
1354 x->n_blink = t->n_blink;
1355 t->n_blink->n_flink = x;
1356 t->n_blink = x;
1359 /* Now the list headed up by new is sorted. Remove duplicates */
1360 np = newn;
1361 while (np != NULL) {
1362 t = np;
1363 while (t->n_flink != NULL && !asccasecmp(np->n_name, t->n_flink->n_name))
1364 t = t->n_flink;
1365 if (t == np) {
1366 np = np->n_flink;
1367 continue;
1370 /* Now t points to the last entry with the same name as np.
1371 * Make np point beyond t */
1372 np->n_flink = t->n_flink;
1373 if (t->n_flink != NULL)
1374 t->n_flink->n_blink = np;
1375 np = np->n_flink;
1377 jleave:
1378 NYD_LEAVE;
1379 return newn;
1382 FL int
1383 c_alternates(void *v)
1385 size_t l;
1386 char **namelist = v, **ap, **ap2, *cp;
1387 NYD_ENTER;
1389 for (namelist = v, l = 0; namelist[l] != NULL; ++l)
1392 if (l == 0) {
1393 if (_altnames != NULL) {
1394 printf("alternates ");
1395 for (ap = _altnames; *ap != NULL; ++ap)
1396 printf("%s ", *ap);
1397 printf("\n");
1399 goto jleave;
1402 if (_altnames != NULL) {
1403 for (ap = _altnames; *ap != NULL; ++ap)
1404 free(*ap);
1405 free(_altnames);
1408 ++l;
1409 _altnames = smalloc(l * sizeof(*_altnames));
1410 for (ap = namelist, ap2 = _altnames; *ap != NULL; ++ap, ++ap2) {
1411 l = strlen(*ap) +1;
1412 cp = smalloc(l);
1413 memcpy(cp, *ap, l);
1414 *ap2 = cp;
1416 *ap2 = NULL;
1417 jleave:
1418 NYD_LEAVE;
1419 return 0;
1422 FL struct name *
1423 delete_alternates(struct name *np)
1425 struct name *xp;
1426 char **ap;
1427 NYD_ENTER;
1429 np = delname(np, myname);
1430 if (_altnames != NULL)
1431 for (ap = _altnames; *ap != '\0'; ++ap)
1432 np = delname(np, *ap);
1434 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1435 while (xp != NULL) {
1436 np = delname(np, xp->n_name);
1437 xp = xp->n_flink;
1440 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1441 while (xp != NULL) {
1442 np = delname(np, xp->n_name);
1443 xp = xp->n_flink;
1446 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1447 while (xp != NULL) {
1448 np = delname(np, xp->n_name);
1449 xp = xp->n_flink;
1451 NYD_LEAVE;
1452 return np;
1455 FL int
1456 is_myname(char const *name)
1458 int rv = 1;
1459 struct name *xp;
1460 char **ap;
1461 NYD_ENTER;
1463 if (_same_name(myname, name))
1464 goto jleave;
1465 if (_altnames != NULL)
1466 for (ap = _altnames; *ap != NULL; ++ap)
1467 if (_same_name(*ap, name))
1468 goto jleave;
1470 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1471 while (xp != NULL) {
1472 if (_same_name(xp->n_name, name))
1473 goto jleave;
1474 xp = xp->n_flink;
1477 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1478 while (xp != NULL) {
1479 if (_same_name(xp->n_name, name))
1480 goto jleave;
1481 xp = xp->n_flink;
1484 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1485 while (xp != NULL) {
1486 if (_same_name(xp->n_name, name))
1487 goto jleave;
1488 xp = xp->n_flink;
1490 rv = 0;
1491 jleave:
1492 NYD_LEAVE;
1493 return rv;
1496 FL int
1497 c_alias(void *v)
1499 char **argv = v;
1500 struct group *gp;
1501 int rv = 0;
1502 NYD_ENTER;
1504 if (*argv == NULL)
1505 _group_print_all(GT_ALIAS);
1506 else if (argv[1] == NULL) {
1507 if ((gp = _group_find(GT_ALIAS, *argv)) != NULL)
1508 _group_print(gp, stdout);
1509 else {
1510 n_err(_("No such alias: %s\n"), *argv);
1511 rv = 1;
1513 } else {
1514 struct grp_names_head *gnhp;
1516 gp = _group_fetch(GT_ALIAS, *argv, 0);
1517 GP_TO_SUBCLASS(gnhp, gp);
1519 for (++argv; *argv != NULL; ++argv) {
1520 size_t l = strlen(*argv) +1;
1521 struct grp_names *gnp = smalloc(n_VSTRUCT_SIZEOF(struct grp_names,
1522 gn_id) + l);
1523 gnp->gn_next = gnhp->gnh_head;
1524 gnhp->gnh_head = gnp;
1525 memcpy(gnp->gn_id, *argv, l);
1527 assert(gnhp->gnh_head != NULL);
1529 NYD_LEAVE;
1530 return rv;
1533 FL int
1534 c_unalias(void *v)
1536 char **argv = v;
1537 int rv = 0;
1538 NYD_ENTER;
1540 do if (!_group_del(GT_ALIAS, *argv)) {
1541 n_err(_("No such alias: %s\n"), *argv);
1542 rv = 1;
1543 } while (*++argv != NULL);
1544 NYD_LEAVE;
1545 return rv;
1548 FL int
1549 c_mlist(void *v)
1551 int rv;
1552 NYD_ENTER;
1554 rv = _mlmux(GT_MLIST, v);
1555 NYD_LEAVE;
1556 return rv;
1559 FL int
1560 c_unmlist(void *v)
1562 int rv;
1563 NYD_ENTER;
1565 rv = _unmlmux(GT_MLIST, v);
1566 NYD_LEAVE;
1567 return rv;
1570 FL int
1571 c_mlsubscribe(void *v)
1573 int rv;
1574 NYD_ENTER;
1576 rv = _mlmux(GT_MLIST | GT_SUBSCRIBE, v);
1577 NYD_LEAVE;
1578 return rv;
1581 FL int
1582 c_unmlsubscribe(void *v)
1584 int rv;
1585 NYD_ENTER;
1587 rv = _unmlmux(GT_MLIST | GT_SUBSCRIBE, v);
1588 NYD_LEAVE;
1589 return rv;
1592 FL enum mlist_state
1593 is_mlist(char const *name, bool_t subscribed_only)
1595 struct group *gp;
1596 #ifdef HAVE_REGEX
1597 struct grp_regex **lpp, *grp;
1598 bool_t re2;
1599 #endif
1600 enum mlist_state rv;
1601 NYD_ENTER;
1603 gp = _group_find(GT_MLIST, name);
1604 rv = (gp != NULL) ? MLIST_KNOWN : MLIST_OTHER;
1605 if (rv == MLIST_KNOWN) {
1606 if (gp->g_type & GT_SUBSCRIBE)
1607 rv = MLIST_SUBSCRIBED;
1608 else if (subscribed_only)
1609 rv = MLIST_OTHER;
1610 /* Of course, if that is a regular expression it doesn't mean a thing */
1611 #ifdef HAVE_REGEX
1612 if (gp->g_type & GT_REGEX)
1613 rv = MLIST_OTHER;
1614 else
1615 #endif
1616 goto jleave;
1619 /* Not in the hashmap (as something matchable), walk the lists */
1620 #ifdef HAVE_REGEX
1621 re2 = FAL0;
1622 lpp = &_mlsub_regex;
1623 jregex_redo:
1624 if ((grp = *lpp) != NULL) {
1625 do if (regexec(&grp->gr_regex, name, 0,NULL, 0) != REG_NOMATCH) {
1626 /* Relink as the head of this list if the hit count of this group is
1627 * >= 25% of the average hit count */
1628 size_t i;
1629 if (!re2)
1630 i = ++_mlsub_hits / _mlsub_size;
1631 else
1632 i = ++_mlist_hits / _mlist_size;
1633 i >>= 2;
1635 if (++grp->gr_hits >= i && *lpp != grp && grp->gr_next != grp) {
1636 grp->gr_last->gr_next = grp->gr_next;
1637 grp->gr_next->gr_last = grp->gr_last;
1638 (grp->gr_last = (*lpp)->gr_last)->gr_next = grp;
1639 (grp->gr_next = *lpp)->gr_last = grp;
1640 *lpp = grp;
1642 rv = !re2 ? MLIST_SUBSCRIBED : MLIST_KNOWN;
1643 goto jleave;
1644 } while ((grp = grp->gr_next) != *lpp);
1646 if (!re2 && !subscribed_only) {
1647 re2 = TRU1;
1648 lpp = &_mlist_regex;
1649 goto jregex_redo;
1651 assert(rv == MLIST_OTHER);
1652 #endif
1654 jleave:
1655 NYD_LEAVE;
1656 return rv;
1659 FL int
1660 c_shortcut(void *v)
1662 char **argv = v;
1663 int rv = 0;
1664 NYD_ENTER;
1666 if (*argv == NULL)
1667 _group_print_all(GT_SHORTCUT);
1668 else for (; *argv != NULL; argv += 2) {
1669 /* Because one hardly ever redefines, anything is stored in one chunk */
1670 size_t l;
1671 struct group *gp;
1672 char *cp;
1674 if (argv[1] == NULL) {
1675 n_err(_("Shortcut expansion is missing: %s\n"), *argv);
1676 rv = 1;
1677 break;
1679 if (_group_find(GT_SHORTCUT, *argv) != NULL)
1680 _group_del(GT_SHORTCUT, *argv);
1682 l = strlen(argv[1]) +1;
1683 gp = _group_fetch(GT_SHORTCUT, *argv, l);
1684 GP_TO_SUBCLASS(cp, gp);
1685 memcpy(cp, argv[1], l);
1687 NYD_LEAVE;
1688 return rv;
1691 FL int
1692 c_unshortcut(void *v)
1694 char **argv = v;
1695 int rv = 0;
1696 NYD_ENTER;
1698 do if (!_group_del(GT_SHORTCUT, *argv)) {
1699 n_err(_("No such shortcut: %s\n"), *argv);
1700 rv = 1;
1701 } while (*++argv != NULL);
1702 NYD_LEAVE;
1703 return rv;
1706 FL char const *
1707 shortcut_expand(char const *str)
1709 struct group *gp;
1710 NYD_ENTER;
1712 if ((gp = _group_find(GT_SHORTCUT, str)) != NULL)
1713 GP_TO_SUBCLASS(str, gp);
1714 else
1715 str = NULL;
1716 NYD_LEAVE;
1717 return str;
1720 /* s-it-mode */