Move socket code to new socket.c
[s-mailx.git] / nam_a_grp.c
blob04d4012ed05457ef43cd9307f27372cf24400c32
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Name lists, alternates and groups: aliases, mailing lists, shortcuts.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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_CUSTOMHDR = 1<< 3,
48 GT_MASK = GT_ALIAS | GT_MLIST | GT_SHORTCUT | GT_CUSTOMHDR,
50 /* Subtype bits and flags */
51 GT_SUBSCRIBE = 1<< 4,
52 GT_REGEX = 1<< 5,
54 /* Extended type mask to be able to reflect what we really have; i.e., mlist
55 * can have GT_REGEX if they are subscribed or not, but `mlsubscribe' should
56 * print out only GT_MLIST which have the GT_SUBSCRIBE attribute set */
57 GT_PRINT_MASK = GT_MASK | GT_SUBSCRIBE
60 struct group {
61 struct group *g_next;
62 size_t g_subclass_off; /* of "subclass" in .g_id (if any) */
63 ui8_t g_type; /* enum group_type */
64 /* Identifying name, of variable size. Dependent on actual "subtype" more
65 * data follows thereafter, but note this is always used (i.e., for regular
66 * expression entries this is still set to the plain string) */
67 char g_id[VFIELD_SIZE(-1)];
69 #define GP_TO_SUBCLASS(X,G) \
70 do {\
71 union __group_subclass {void *gs_vp; char *gs_cp;} __gs__;\
72 __gs__.gs_cp = (char*)UNCONST(G) + (G)->g_subclass_off;\
73 (X) = __gs__.gs_vp;\
74 } while (0)
76 struct grp_names_head {
77 struct grp_names *gnh_head;
80 struct grp_names {
81 struct grp_names *gn_next;
82 char gn_id[VFIELD_SIZE(0)];
85 #ifdef HAVE_REGEX
86 struct grp_regex {
87 struct grp_regex *gr_last;
88 struct grp_regex *gr_next;
89 struct group *gr_mygroup; /* xxx because lists use grp_regex*! ?? */
90 size_t gr_hits; /* Number of times this group matched */
91 regex_t gr_regex;
93 #endif
95 struct group_lookup {
96 struct group **gl_htable;
97 struct group **gl_slot;
98 struct group *gl_slot_last;
99 struct group *gl_group;
102 /* List of alternate names of user */
103 static char **_altnames;
105 /* `alias' */
106 static struct group *_alias_heads[HSHSIZE]; /* TODO dynamic hash */
108 /* `mlist', `mlsubscribe'. Anything is stored in the hashmap.. */
109 static struct group *_mlist_heads[HSHSIZE]; /* TODO dynamic hash */
111 /* ..but entries which have GT_REGEX set are false lookups and will really be
112 * accessed via sequential lists instead, which are type-specific for better
113 * performance, but also to make it possible to have ".*@xy.org" as a mlist
114 * and "(one|two)@xy.org" as a mlsubscription.
115 * These lists use a bit of QOS optimization in that a matching group will
116 * become relinked as the new list head if its hit count is
117 * (>= ((xy_hits / _xy_size) >> 2))
118 * Note that the hit counts only count currently linked in nodes.. */
119 #ifdef HAVE_REGEX
120 static struct grp_regex *_mlist_regex, *_mlsub_regex;
121 static size_t _mlist_size, _mlist_hits, _mlsub_size, _mlsub_hits;
122 #endif
124 /* `shortcut' */
125 static struct group *_shortcut_heads[HSHSIZE]; /* TODO dynamic hash */
127 /* `customhdr' */
128 static struct group *_customhdr_heads[HSHSIZE]; /* TODO dynamic hash */
130 /* Same name, while taking care for *allnet*? */
131 static bool_t _same_name(char const *n1, char const *n2);
133 /* Delete the given name from a namelist */
134 static struct name * delname(struct name *np, char const *name);
136 /* Put another node onto a list of names and return the list */
137 static struct name * put(struct name *list, struct name *node);
139 /* Grab a single name (liberal name) */
140 static char const * yankname(char const *ap, char *wbuf,
141 char const *separators, int keepcomms);
143 /* Extraction multiplexer that splits an input line to names */
144 static struct name * _extract1(char const *line, enum gfield ntype,
145 char const *separators, bool_t keepcomms);
147 /* Recursively expand a alias name. Limit expansion to some fixed level.
148 * Direct recursion is not expanded for convenience */
149 static struct name * _gexpand(size_t level, struct name *nlist,
150 struct group *gp, bool_t metoo, int ntype);
152 /* Lookup a group, return it or NULL, fill in glp anyway */
153 static struct group * _group_lookup(enum group_type gt,
154 struct group_lookup *glp, char const *id);
156 /* Easier-to-use wrapper around _group_lookup() */
157 static struct group * _group_find(enum group_type gt, char const *id);
159 /* Iteration: go to the first group, which also inits the iterator. A valid
160 * iterator can be stepped via _next(). A NULL return means no (more) groups
161 * to be iterated exist, in which case only glp->gl_group is set (NULL) */
162 static struct group * _group_go_first(enum group_type gt,
163 struct group_lookup *glp);
164 static struct group * _group_go_next(struct group_lookup *glp);
166 /* Fetch the group id, create it as necessary */
167 static struct group * _group_fetch(enum group_type gt, char const *id,
168 size_t addsz);
170 /* "Intelligent" delete which handles a "*" id, too;
171 * returns a true boolean if a group was deleted, and always succeeds for "*" */
172 static bool_t _group_del(enum group_type gt, char const *id);
174 static struct group * __group_del(struct group_lookup *glp);
175 static void __names_del(struct group *gp);
177 /* Print all groups of the given type, alphasorted */
178 static void _group_print_all(enum group_type gt);
180 static int __group_print_qsorter(void const *a, void const *b);
182 /* Really print a group, actually. Return number of written lines */
183 static size_t _group_print(struct group const *gp, FILE *fo);
185 /* Multiplexers for list and subscribe commands */
186 static int _mlmux(enum group_type gt, char **argv);
187 static int _unmlmux(enum group_type gt, char **argv);
189 /* Relinkers for the sequential match lists */
190 #ifdef HAVE_REGEX
191 static void _mlmux_linkin(struct group *gp);
192 static void _mlmux_linkout(struct group *gp);
193 # define _MLMUX_LINKIN(GP) \
194 do if ((GP)->g_type & GT_REGEX) _mlmux_linkin(GP); while (0)
195 # define _MLMUX_LINKOUT(GP) \
196 do if ((GP)->g_type & GT_REGEX) _mlmux_linkout(GP); while (0)
197 #else
198 # define _MLMUX_LINKIN(GP)
199 # define _MLMUX_LINKOUT(GP)
200 #endif
202 static bool_t
203 _same_name(char const *n1, char const *n2)
205 bool_t rv = FAL0;
206 char c1, c2;
207 NYD_ENTER;
209 if (ok_blook(allnet)) {
210 do {
211 c1 = *n1++;
212 c2 = *n2++;
213 c1 = lowerconv(c1);
214 c2 = lowerconv(c2);
215 if (c1 != c2)
216 goto jleave;
217 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
218 rv = 1;
219 } else
220 rv = !asccasecmp(n1, n2);
221 jleave:
222 NYD_LEAVE;
223 return rv;
226 static struct name *
227 delname(struct name *np, char const *name)
229 struct name *p;
230 NYD_ENTER;
232 for (p = np; p != NULL; p = p->n_flink)
233 if (_same_name(p->n_name, name)) {
234 if (p->n_blink == NULL) {
235 if (p->n_flink != NULL)
236 p->n_flink->n_blink = NULL;
237 np = p->n_flink;
238 continue;
240 if (p->n_flink == NULL) {
241 if (p->n_blink != NULL)
242 p->n_blink->n_flink = NULL;
243 continue;
245 p->n_blink->n_flink = p->n_flink;
246 p->n_flink->n_blink = p->n_blink;
248 NYD_LEAVE;
249 return np;
252 static struct name *
253 put(struct name *list, struct name *node)
255 NYD_ENTER;
256 node->n_flink = list;
257 node->n_blink = NULL;
258 if (list != NULL)
259 list->n_blink = node;
260 NYD_LEAVE;
261 return node;
264 static char const *
265 yankname(char const *ap, char *wbuf, char const *separators, int keepcomms)
267 char const *cp;
268 char *wp, c, inquote, lc, lastsp;
269 NYD_ENTER;
271 *(wp = wbuf) = '\0';
273 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
274 for (c = *ap; blankchar(c) || c == ','; c = *++ap)
276 if (c == '\0') {
277 cp = NULL;
278 goto jleave;
281 /* Parse a full name: TODO RFC 5322
282 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
283 * - Skip entire (nested) comments
284 * - In non-quote, non-comment, join adjacent space to a single SP
285 * - Understand separators only in non-quote, non-comment context,
286 * and only if not part of a *quoted-pair* (XXX too liberal) */
287 cp = ap;
288 for (inquote = lc = lastsp = 0;; lc = c, ++cp) {
289 c = *cp;
290 if (c == '\0')
291 break;
292 if (c == '\\') {
293 lastsp = 0;
294 continue;
296 if (c == '"') {
297 if (lc != '\\')
298 inquote = !inquote;
299 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
300 else
301 --wp;
302 #endif
303 goto jwpwc;
305 if (inquote || lc == '\\') {
306 jwpwc:
307 *wp++ = c;
308 lastsp = 0;
309 continue;
311 if (c == '(') {
312 ap = cp;
313 cp = skip_comment(cp + 1);
314 if (keepcomms)
315 while (ap < cp)
316 *wp++ = *ap++;
317 --cp;
318 lastsp = 0;
319 continue;
321 if (strchr(separators, c) != NULL)
322 break;
324 lc = lastsp;
325 lastsp = blankchar(c);
326 if (!lastsp || !lc)
327 *wp++ = c;
329 if (blankchar(lc))
330 --wp;
332 *wp = '\0';
333 jleave:
334 NYD_LEAVE;
335 return cp;
338 static struct name *
339 _extract1(char const *line, enum gfield ntype, char const *separators,
340 bool_t keepcomms)
342 struct name *topp, *np, *t;
343 char const *cp;
344 char *nbuf;
345 NYD_ENTER;
347 topp = NULL;
348 if (line == NULL || *line == '\0')
349 goto jleave;
351 np = NULL;
352 cp = line;
353 nbuf = smalloc(strlen(line) +1);
354 while ((cp = yankname(cp, nbuf, separators, keepcomms)) != NULL) {
355 t = nalloc(nbuf, ntype);
356 if (topp == NULL)
357 topp = t;
358 else
359 np->n_flink = t;
360 t->n_blink = np;
361 np = t;
363 free(nbuf);
364 jleave:
365 NYD_LEAVE;
366 return topp;
369 static struct name *
370 _gexpand(size_t level, struct name *nlist, struct group *gp, bool_t metoo,
371 int ntype)
373 struct grp_names_head *gnhp;
374 struct grp_names *gnp;
375 NYD_ENTER;
377 if (UICMP(z, level++, >, MAXEXP)) {
378 n_err(_("Expanding alias to depth larger than %d\n"), MAXEXP);
379 goto jleave;
382 GP_TO_SUBCLASS(gnhp, gp);
383 for (gnp = gnhp->gnh_head; gnp != NULL; gnp = gnp->gn_next) {
384 char *cp;
385 struct group *ngp;
387 /* FIXME we do not really support leading backslash quoting do we??? */
388 if (*(cp = gnp->gn_id) == '\\' || !strcmp(cp, gp->g_id))
389 goto jquote;
391 if ((ngp = _group_find(GT_ALIAS, cp)) != NULL) {
392 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
393 * to a full address specification; aliases cannot be empty */
394 struct grp_names_head *ngnhp;
395 GP_TO_SUBCLASS(ngnhp, ngp);
397 assert(ngnhp->gnh_head != NULL);
398 if (metoo || ngnhp->gnh_head->gn_next != NULL ||
399 !_same_name(cp, myname))
400 nlist = _gexpand(level, nlist, ngp, metoo, ntype);
401 continue;
404 /* Here we should allow to expand to itself if only person in alias */
405 jquote:
406 if (metoo || gnhp->gnh_head->gn_next == NULL || !_same_name(cp, myname))
407 nlist = put(nlist, nalloc(cp, ntype | GFULL));
409 jleave:
410 NYD_LEAVE;
411 return nlist;
414 static struct group *
415 _group_lookup(enum group_type gt, struct group_lookup *glp, char const *id)
417 struct group *lgp, *gp;
418 NYD_ENTER;
420 gt &= GT_MASK;
421 lgp = NULL;
422 gp = *(glp->gl_htable = glp->gl_slot =
423 ((gt & GT_ALIAS ? _alias_heads :
424 (gt & GT_MLIST ? _mlist_heads :
425 (gt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads))) +
426 torek_hash(id) % HSHSIZE));
428 for (; gp != NULL; lgp = gp, gp = gp->g_next)
429 if ((gp->g_type & gt) && *gp->g_id == *id && !strcmp(gp->g_id, id))
430 break;
432 glp->gl_slot_last = lgp;
433 glp->gl_group = gp;
434 NYD_LEAVE;
435 return gp;
438 static struct group *
439 _group_find(enum group_type gt, char const *id)
441 struct group_lookup gl;
442 struct group *gp;
443 NYD_ENTER;
445 gp = _group_lookup(gt, &gl, id);
446 NYD_LEAVE;
447 return gp;
450 static struct group *
451 _group_go_first(enum group_type gt, struct group_lookup *glp)
453 struct group **gpa, *gp;
454 size_t i;
455 NYD_ENTER;
457 for (glp->gl_htable = gpa = (gt & GT_ALIAS ? _alias_heads :
458 (gt & GT_MLIST ? _mlist_heads :
459 (gt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads))), i = 0;
460 i < HSHSIZE; ++gpa, ++i)
461 if ((gp = *gpa) != NULL) {
462 glp->gl_slot = gpa;
463 glp->gl_group = gp;
464 goto jleave;
467 glp->gl_group = gp = NULL;
468 jleave:
469 glp->gl_slot_last = NULL;
470 NYD_LEAVE;
471 return gp;
474 static struct group *
475 _group_go_next(struct group_lookup *glp)
477 struct group *gp, **gpa;
478 NYD_ENTER;
480 if ((gp = glp->gl_group->g_next) != NULL)
481 glp->gl_slot_last = glp->gl_group;
482 else {
483 glp->gl_slot_last = NULL;
484 for (gpa = glp->gl_htable + HSHSIZE; ++glp->gl_slot < gpa;)
485 if ((gp = *glp->gl_slot) != NULL)
486 break;
488 glp->gl_group = gp;
489 NYD_LEAVE;
490 return gp;
493 static struct group *
494 _group_fetch(enum group_type gt, char const *id, size_t addsz)
496 struct group_lookup gl;
497 struct group *gp;
498 size_t l, i;
499 NYD_ENTER;
501 if ((gp = _group_lookup(gt, &gl, id)) != NULL)
502 goto jleave;
504 l = strlen(id) +1;
505 i = n_ALIGN(sizeof(*gp) - VFIELD_SIZEOF(struct group, g_id) + l);
506 switch (gt & GT_MASK) {
507 case GT_ALIAS:
508 addsz = sizeof(struct grp_names_head);
509 break;
510 case GT_MLIST:
511 #ifdef HAVE_REGEX
512 if (is_maybe_regex(id)) {
513 addsz = sizeof(struct grp_regex);
514 gt |= GT_REGEX;
515 } else
516 #endif
517 case GT_SHORTCUT:
518 default:
519 break;
522 gp = smalloc(i + addsz);
523 gp->g_subclass_off = i;
524 gp->g_type = gt;
525 memcpy(gp->g_id, id, l);
527 if (gt & GT_ALIAS) {
528 struct grp_names_head *gnhp;
529 GP_TO_SUBCLASS(gnhp, gp);
530 gnhp->gnh_head = NULL;
532 #ifdef HAVE_REGEX
533 else if (/*(gt & GT_MLIST) &&*/ gt & GT_REGEX) {
534 struct grp_regex *grp;
535 GP_TO_SUBCLASS(grp, gp);
537 if (regcomp(&grp->gr_regex, id, REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
538 n_err(_("Invalid regular expression: \"%s\"\n"), id);
539 free(gp);
540 gp = NULL;
541 goto jleave;
543 grp->gr_mygroup = gp;
544 _mlmux_linkin(gp);
546 #endif
548 gp->g_next = *gl.gl_slot;
549 *gl.gl_slot = gp;
550 jleave:
551 NYD_LEAVE;
552 return gp;
555 static bool_t
556 _group_del(enum group_type gt, char const *id)
558 enum group_type xgt = gt & GT_MASK;
559 struct group_lookup gl;
560 struct group *gp;
561 NYD_ENTER;
563 /* Delete 'em all? */
564 if (id[0] == '*' && id[1] == '\0') {
565 for (gp = _group_go_first(gt, &gl); gp != NULL;)
566 gp = (gp->g_type & xgt) ? __group_del(&gl) : _group_go_next(&gl);
567 gp = (struct group*)TRU1;
568 } else if ((gp = _group_lookup(gt, &gl, id)) != NULL) {
569 if (gp->g_type & xgt)
570 __group_del(&gl);
571 else
572 gp = NULL;
574 NYD_LEAVE;
575 return (gp != NULL);
578 static struct group *
579 __group_del(struct group_lookup *glp)
581 struct group *x, *gp;
582 NYD_ENTER;
584 /* Overly complicated: link off this node, step ahead to next.. */
585 x = glp->gl_group;
586 if ((gp = glp->gl_slot_last) != NULL) {
587 gp = (gp->g_next = x->g_next);
588 } else {
589 glp->gl_slot_last = NULL;
590 gp = (*glp->gl_slot = x->g_next);
592 if (gp == NULL) {
593 struct group **gpa = glp->gl_htable + HSHSIZE;
595 while (++glp->gl_slot < gpa)
596 if ((gp = *glp->gl_slot) != NULL)
597 break;
600 glp->gl_group = gp;
602 if (x->g_type & GT_ALIAS)
603 __names_del(x);
604 #ifdef HAVE_REGEX
605 else if (/*(x->g_type & GT_MLIST) &&*/ x->g_type & GT_REGEX) {
606 struct grp_regex *grp;
607 GP_TO_SUBCLASS(grp, x);
609 regfree(&grp->gr_regex);
610 _mlmux_linkout(x);
612 #endif
614 free(x);
615 NYD_LEAVE;
616 return gp;
619 static void
620 __names_del(struct group *gp)
622 struct grp_names_head *gnhp;
623 struct grp_names *gnp;
624 NYD_ENTER;
626 GP_TO_SUBCLASS(gnhp, gp);
627 for (gnp = gnhp->gnh_head; gnp != NULL;) {
628 struct grp_names *x = gnp;
629 gnp = gnp->gn_next;
630 free(x);
632 NYD_LEAVE;
635 static void
636 _group_print_all(enum group_type gt)
638 enum group_type xgt;
639 struct group **gpa;
640 struct group const *gp;
641 ui32_t h, i;
642 char const **ida;
643 FILE *fp;
644 size_t lines;
645 NYD_ENTER;
647 xgt = gt & GT_PRINT_MASK;
648 gpa = (xgt & GT_ALIAS ? _alias_heads
649 : (xgt & GT_MLIST ? _mlist_heads
650 : (xgt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads)));
652 for (h = 0, i = 1; h < HSHSIZE; ++h)
653 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
654 if ((gp->g_type & xgt) == xgt)
655 ++i;
656 ida = salloc(i * sizeof *ida);
658 for (i = h = 0; h < HSHSIZE; ++h)
659 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
660 if ((gp->g_type & xgt) == xgt)
661 ida[i++] = gp->g_id;
662 ida[i] = NULL;
664 if (i > 1)
665 qsort(ida, i, sizeof *ida, &__group_print_qsorter);
667 if ((fp = Ftmp(NULL, "prgroup", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
668 fp = stdout;
669 lines = 0;
671 for (i = 0; ida[i] != NULL; ++i)
672 lines += _group_print(_group_find(gt, ida[i]), fp);
673 #ifdef HAVE_REGEX
674 if (gt & GT_MLIST) {
675 if (gt & GT_SUBSCRIBE)
676 i = (ui32_t)_mlsub_size, h = (ui32_t)_mlsub_hits;
677 else
678 i = (ui32_t)_mlist_size, h = (ui32_t)_mlist_hits;
679 if (i > 0)
680 fprintf(fp, _("# %s list regex(7) total: %u entries, %u hits\n"),
681 (gt & GT_SUBSCRIBE ? _("Subscribed") : _("Non-subscribed")),
682 i, h);
684 #endif
686 if (fp != stdout) {
687 page_or_print(fp, lines);
688 Fclose(fp);
690 NYD_LEAVE;
693 static int
694 __group_print_qsorter(void const *a, void const *b)
696 int rv;
697 NYD_ENTER;
699 rv = strcmp(*(char**)UNCONST(a), *(char**)UNCONST(b));
700 NYD_LEAVE;
701 return rv;
704 static size_t
705 _group_print(struct group const *gp, FILE *fo)
707 char const *cp;
708 size_t rv;
709 NYD_ENTER;
711 rv = 1;
713 if (gp->g_type & GT_ALIAS) {
714 struct grp_names_head *gnhp;
715 struct grp_names *gnp;
717 fprintf(fo, "alias %s", gp->g_id);
719 GP_TO_SUBCLASS(gnhp, gp);
720 if ((gnp = gnhp->gnh_head) != NULL) { /* xxx always 1+ entries */
721 do {
722 struct grp_names *x = gnp;
723 gnp = gnp->gn_next;
724 fprintf(fo, " \"%s\"", string_quote(x->gn_id));
725 } while (gnp != NULL);
727 } else if (gp->g_type & GT_MLIST) {
728 #ifdef HAVE_REGEX
729 if (gp->g_type & GT_REGEX) {
730 size_t i;
731 struct grp_regex *grp,
732 *lp = (gp->g_type & GT_SUBSCRIBE ? _mlsub_regex : _mlist_regex);
734 GP_TO_SUBCLASS(grp, gp);
735 for (i = 1; lp != grp; lp = lp->gr_next)
736 ++i;
737 fprintf(fo, "# regex(7): hits %" PRIuZ ", sort %" PRIuZ ".\n ",
738 grp->gr_hits, i);
739 ++rv;
741 #endif
743 fprintf(fo, "%s %s",
744 (gp->g_type & GT_SUBSCRIBE ? "mlsubscribe" : "mlist"), gp->g_id);
745 } else if (gp->g_type & GT_SHORTCUT) {
746 GP_TO_SUBCLASS(cp, gp);
747 fprintf(fo, "shortcut %s \"%s\"", gp->g_id, string_quote(cp));
748 } else if (gp->g_type & GT_CUSTOMHDR) {
749 GP_TO_SUBCLASS(cp, gp);
750 fprintf(fo, "customhdr %s \"%s\"", gp->g_id, string_quote(cp));
753 putc('\n', fo);
754 NYD_LEAVE;
755 return rv;
758 static int
759 _mlmux(enum group_type gt, char **argv)
761 struct group *gp;
762 int rv = 0;
763 NYD_ENTER;
765 if (*argv == NULL)
766 _group_print_all(gt);
767 else do {
768 if ((gp = _group_find(gt, *argv)) != NULL) {
769 if (gt & GT_SUBSCRIBE) {
770 if (!(gp->g_type & GT_SUBSCRIBE)) {
771 _MLMUX_LINKOUT(gp);
772 gp->g_type |= GT_SUBSCRIBE;
773 _MLMUX_LINKIN(gp);
774 } else {
775 n_err(_("Mailing-list already `mlsubscribe'd: %s\n"),
776 *argv);
777 rv = 1;
779 } else {
780 n_err(_("Mailing-list already `mlist'ed: %s\n"), *argv);
781 rv = 1;
783 } else
784 _group_fetch(gt, *argv, 0);
785 } while (*++argv != NULL);
787 NYD_LEAVE;
788 return rv;
791 static int
792 _unmlmux(enum group_type gt, char **argv)
794 struct group *gp;
795 int rv = 0;
796 NYD_ENTER;
798 for (; *argv != NULL; ++argv) {
799 if (gt & GT_SUBSCRIBE) {
800 struct group_lookup gl;
801 bool_t isaster;
803 if (!(isaster = (**argv == '*')))
804 gp = _group_find(gt, *argv);
805 else if ((gp = _group_go_first(gt, &gl)) == NULL)
806 continue;
807 else if (gp != NULL && !(gp->g_type & GT_SUBSCRIBE))
808 goto jaster_entry;
810 if (gp != NULL) {
811 jaster_redo:
812 if (gp->g_type & GT_SUBSCRIBE) {
813 _MLMUX_LINKOUT(gp);
814 gp->g_type &= ~GT_SUBSCRIBE;
815 _MLMUX_LINKIN(gp);
816 if (isaster) {
817 jaster_entry:
818 while ((gp = _group_go_next(&gl)) != NULL &&
819 !(gp->g_type & GT_SUBSCRIBE))
821 if (gp != NULL)
822 goto jaster_redo;
824 } else {
825 n_err(_("Mailing-list not `mlsubscribe'd: \"%s\"\n"), *argv);
826 rv = 1;
828 continue;
830 } else if (_group_del(gt, *argv))
831 continue;
832 n_err(_("No such mailing-list: \"%s\"\n"), *argv);
833 rv = 1;
835 NYD_LEAVE;
836 return rv;
839 #ifdef HAVE_REGEX
840 static void
841 _mlmux_linkin(struct group *gp)
843 struct grp_regex **lpp, *grp, *lhp;
844 NYD_ENTER;
846 if (gp->g_type & GT_SUBSCRIBE) {
847 lpp = &_mlsub_regex;
848 ++_mlsub_size;
849 } else {
850 lpp = &_mlist_regex;
851 ++_mlist_size;
854 GP_TO_SUBCLASS(grp, gp);
855 if ((lhp = *lpp) != NULL) {
856 (grp->gr_last = lhp->gr_last)->gr_next = grp;
857 (grp->gr_next = lhp)->gr_last = grp;
858 } else
859 *lpp = grp->gr_last = grp->gr_next = grp;
860 grp->gr_hits = 0;
861 NYD_LEAVE;
864 static void
865 _mlmux_linkout(struct group *gp)
867 struct grp_regex *grp, **lpp;
868 NYD_ENTER;
870 GP_TO_SUBCLASS(grp, gp);
872 if (gp->g_type & GT_SUBSCRIBE) {
873 lpp = &_mlsub_regex;
874 --_mlsub_size;
875 _mlsub_hits -= grp->gr_hits;
876 } else {
877 lpp = &_mlist_regex;
878 --_mlist_size;
879 _mlist_hits -= grp->gr_hits;
882 if (grp->gr_next == grp)
883 *lpp = NULL;
884 else {
885 (grp->gr_last->gr_next = grp->gr_next)->gr_last = grp->gr_last;
886 if (*lpp == grp)
887 *lpp = grp->gr_next;
889 NYD_LEAVE;
891 #endif /* HAVE_REGEX */
893 FL struct name *
894 nalloc(char *str, enum gfield ntype)
896 struct addrguts ag;
897 struct str in, out;
898 struct name *np;
899 NYD_ENTER;
900 assert(!(ntype & GFULLEXTRA) || (ntype & GFULL) != 0);
902 addrspec_with_guts(((ntype & (GFULL | GSKIN | GREF)) != 0), str, &ag);
903 if (!(ag.ag_n_flags & NAME_NAME_SALLOC)) {
904 ag.ag_n_flags |= NAME_NAME_SALLOC;
905 np = salloc(sizeof(*np) + ag.ag_slen +1);
906 memcpy(np + 1, ag.ag_skinned, ag.ag_slen +1);
907 ag.ag_skinned = (char*)(np + 1);
908 } else
909 np = salloc(sizeof *np);
911 np->n_flink = NULL;
912 np->n_blink = NULL;
913 np->n_type = ntype;
914 np->n_flags = 0;
916 np->n_fullname = np->n_name = ag.ag_skinned;
917 np->n_fullextra = NULL;
918 np->n_flags = ag.ag_n_flags;
920 if (ntype & GFULL) {
921 if (ag.ag_ilen == ag.ag_slen
922 #ifdef HAVE_IDNA
923 && !(ag.ag_n_flags & NAME_IDNA)
924 #endif
926 goto jleave;
927 if (ag.ag_n_flags & NAME_ADDRSPEC_ISFILEORPIPE)
928 goto jleave;
930 /* n_fullextra is only the complete name part without address.
931 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
932 if ((ntype & GFULLEXTRA) && ag.ag_ilen > ag.ag_slen + 2) {
933 size_t s = ag.ag_iaddr_start, e = ag.ag_iaddr_aend, i;
934 char const *cp;
936 if (s == 0 || str[--s] != '<' || str[e++] != '>')
937 goto jskipfullextra;
938 i = ag.ag_ilen - e;
939 in.s = ac_alloc(s + i +1);
940 memcpy(in.s, str, s);
941 if (i > 0)
942 memcpy(in.s + s, str + e, i);
943 s += i;
944 in.s[in.l = s] = '\0';
945 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
947 for (cp = out.s, i = out.l; i > 0 && spacechar(*cp); --i, ++cp)
949 while (i > 0 && spacechar(cp[i - 1]))
950 --i;
951 np->n_fullextra = savestrbuf(cp, i);
953 free(out.s);
954 ac_free(in.s);
956 jskipfullextra:
958 /* n_fullname depends on IDNA conversion */
959 #ifdef HAVE_IDNA
960 if (!(ag.ag_n_flags & NAME_IDNA)) {
961 #endif
962 in.s = str;
963 in.l = ag.ag_ilen;
964 #ifdef HAVE_IDNA
965 } else {
966 /* The domain name was IDNA and has been converted. We also have to
967 * ensure that the domain name in .n_fullname is replaced with the
968 * converted version, since MIME doesn't perform encoding of addrs */
969 size_t l = ag.ag_iaddr_start,
970 lsuff = ag.ag_ilen - ag.ag_iaddr_aend;
971 in.s = ac_alloc(l + ag.ag_slen + lsuff +1);
972 memcpy(in.s, str, l);
973 memcpy(in.s + l, ag.ag_skinned, ag.ag_slen);
974 l += ag.ag_slen;
975 memcpy(in.s + l, str + ag.ag_iaddr_aend, lsuff);
976 l += lsuff;
977 in.s[l] = '\0';
978 in.l = l;
980 #endif
981 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
982 np->n_fullname = savestr(out.s);
983 free(out.s);
984 #ifdef HAVE_IDNA
985 if (ag.ag_n_flags & NAME_IDNA)
986 ac_free(in.s);
987 #endif
988 np->n_flags |= NAME_FULLNAME_SALLOC;
990 jleave:
991 NYD_LEAVE;
992 return np;
995 FL struct name *
996 ndup(struct name *np, enum gfield ntype)
998 struct name *nnp;
999 NYD_ENTER;
1001 if ((ntype & (GFULL | GSKIN)) && !(np->n_flags & NAME_SKINNED)) {
1002 nnp = nalloc(np->n_name, ntype);
1003 goto jleave;
1006 nnp = salloc(sizeof *np);
1007 nnp->n_flink = nnp->n_blink = NULL;
1008 nnp->n_type = ntype;
1009 nnp->n_flags = (np->n_flags & ~(NAME_NAME_SALLOC | NAME_FULLNAME_SALLOC)) |
1010 NAME_NAME_SALLOC;
1011 nnp->n_name = savestr(np->n_name);
1012 if (np->n_name == np->n_fullname || !(ntype & (GFULL | GSKIN))) {
1013 nnp->n_fullname = nnp->n_name;
1014 nnp->n_fullextra = NULL;
1015 } else {
1016 nnp->n_flags |= NAME_FULLNAME_SALLOC;
1017 nnp->n_fullname = savestr(np->n_fullname);
1018 nnp->n_fullextra = (np->n_fullextra == NULL) ? NULL
1019 : savestr(np->n_fullextra);
1021 jleave:
1022 NYD_LEAVE;
1023 return nnp;
1026 FL struct name *
1027 cat(struct name *n1, struct name *n2)
1029 struct name *tail;
1030 NYD_ENTER;
1032 tail = n2;
1033 if (n1 == NULL)
1034 goto jleave;
1035 tail = n1;
1036 if (n2 == NULL)
1037 goto jleave;
1039 while (tail->n_flink != NULL)
1040 tail = tail->n_flink;
1041 tail->n_flink = n2;
1042 n2->n_blink = tail;
1043 tail = n1;
1044 jleave:
1045 NYD_LEAVE;
1046 return tail;
1049 FL struct name *
1050 namelist_dup(struct name const *np, enum gfield ntype)
1052 struct name *nnp;
1053 NYD_ENTER;
1055 for (nnp = NULL; np != NULL; np = np->n_flink) {
1056 struct name *x = ndup(UNCONST(np), (np->n_type & ~GMASK) | ntype);
1057 x->n_flink = nnp;
1058 nnp = x;
1060 NYD_LEAVE;
1061 return nnp;
1064 FL ui32_t
1065 count(struct name const *np)
1067 ui32_t c;
1068 NYD_ENTER;
1070 for (c = 0; np != NULL; np = np->n_flink)
1071 if (!(np->n_type & GDEL))
1072 ++c;
1073 NYD_LEAVE;
1074 return c;
1077 FL ui32_t
1078 count_nonlocal(struct name const *np)
1080 ui32_t c;
1081 NYD_ENTER;
1083 for (c = 0; np != NULL; np = np->n_flink)
1084 if (!(np->n_type & GDEL) && !(np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE))
1085 ++c;
1086 NYD_LEAVE;
1087 return c;
1090 FL struct name *
1091 extract(char const *line, enum gfield ntype)
1093 struct name *rv;
1094 NYD_ENTER;
1096 rv = _extract1(line, ntype, " \t,", 0);
1097 NYD_LEAVE;
1098 return rv;
1101 FL struct name *
1102 lextract(char const *line, enum gfield ntype)
1104 struct name *rv;
1105 NYD_ENTER;
1107 rv = ((line != NULL && strpbrk(line, ",\"\\(<|"))
1108 ? _extract1(line, ntype, ",", 1) : extract(line, ntype));
1109 NYD_LEAVE;
1110 return rv;
1113 FL char *
1114 detract(struct name *np, enum gfield ntype)
1116 char *topp, *cp;
1117 struct name *p;
1118 int flags, s;
1119 NYD_ENTER;
1121 topp = NULL;
1122 if (np == NULL)
1123 goto jleave;
1125 flags = ntype & (GCOMMA | GNAMEONLY);
1126 ntype &= ~(GCOMMA | GNAMEONLY);
1127 s = 0;
1129 for (p = np; p != NULL; p = p->n_flink) {
1130 if (ntype && (p->n_type & GMASK) != ntype)
1131 continue;
1132 s += strlen(flags & GNAMEONLY ? p->n_name : p->n_fullname) +1;
1133 if (flags & GCOMMA)
1134 ++s;
1136 if (s == 0)
1137 goto jleave;
1139 s += 2;
1140 topp = salloc(s);
1141 cp = topp;
1142 for (p = np; p != NULL; p = p->n_flink) {
1143 if (ntype && (p->n_type & GMASK) != ntype)
1144 continue;
1145 cp = sstpcpy(cp, (flags & GNAMEONLY ? p->n_name : p->n_fullname));
1146 if ((flags & GCOMMA) && p->n_flink != NULL)
1147 *cp++ = ',';
1148 *cp++ = ' ';
1150 *--cp = 0;
1151 if ((flags & GCOMMA) && *--cp == ',')
1152 *cp = 0;
1153 jleave:
1154 NYD_LEAVE;
1155 return topp;
1158 FL struct name *
1159 grab_names(char const *field, struct name *np, int comma, enum gfield gflags)
1161 struct name *nq;
1162 NYD_ENTER;
1164 jloop:
1165 np = lextract(n_input_cp_addhist(field, detract(np, comma), TRU1), gflags);
1166 for (nq = np; nq != NULL; nq = nq->n_flink)
1167 if (is_addr_invalid(nq, EACM_NONE))
1168 goto jloop;
1169 NYD_LEAVE;
1170 return np;
1173 FL bool_t
1174 name_is_same_domain(struct name const *n1, struct name const *n2)
1176 char const *d1, *d2;
1177 bool_t rv;
1178 NYD_ENTER;
1180 d1 = strrchr(n1->n_name, '@');
1181 d2 = strrchr(n2->n_name, '@');
1183 rv = (d1 != NULL && d2 != NULL) ? !asccasecmp(++d1, ++d2) : FAL0;
1185 NYD_LEAVE;
1186 return rv;
1189 FL struct name *
1190 checkaddrs(struct name *np, enum expand_addr_check_mode eacm,
1191 si8_t *set_on_error)
1193 struct name *n;
1194 NYD_ENTER;
1196 for (n = np; n != NULL;) {
1197 si8_t rv;
1199 if ((rv = is_addr_invalid(n, eacm)) != 0) {
1200 if (set_on_error != NULL)
1201 *set_on_error |= rv; /* don't loose -1! */
1202 if (n->n_blink)
1203 n->n_blink->n_flink = n->n_flink;
1204 if (n->n_flink)
1205 n->n_flink->n_blink = n->n_blink;
1206 if (n == np)
1207 np = n->n_flink;
1209 n = n->n_flink;
1211 NYD_LEAVE;
1212 return np;
1215 FL struct name *
1216 namelist_vaporise_head(struct header *hp, enum expand_addr_check_mode eacm,
1217 bool_t metoo, si8_t *set_on_error)
1219 struct name *tolist, *np, **npp;
1220 NYD_ENTER;
1222 tolist = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), metoo);
1223 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
1225 tolist = elide(checkaddrs(tolist, eacm, set_on_error));
1227 for (np = tolist; np != NULL; np = np->n_flink) {
1228 switch (np->n_type & (GDEL | GMASK)) {
1229 case GTO: npp = &hp->h_to; break;
1230 case GCC: npp = &hp->h_cc; break;
1231 case GBCC: npp = &hp->h_bcc; break;
1232 default: continue;
1234 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
1236 NYD_LEAVE;
1237 return tolist;
1240 FL struct name *
1241 usermap(struct name *names, bool_t force_metoo)
1243 struct name *new, *np, *cp;
1244 struct group *gp;
1245 int metoo;
1246 NYD_ENTER;
1248 new = NULL;
1249 np = names;
1250 metoo = (force_metoo || ok_blook(metoo));
1251 while (np != NULL) {
1252 assert(!(np->n_type & GDEL)); /* TODO legacy */
1253 if (is_fileorpipe_addr(np) || np->n_name[0] == '\\') {
1254 cp = np->n_flink;
1255 new = put(new, np);
1256 np = cp;
1257 continue;
1259 gp = _group_find(GT_ALIAS, np->n_name);
1260 cp = np->n_flink;
1261 if (gp != NULL)
1262 new = _gexpand(0, new, gp, metoo, np->n_type);
1263 else
1264 new = put(new, np);
1265 np = cp;
1267 NYD_LEAVE;
1268 return new;
1271 FL struct name *
1272 elide(struct name *names)
1274 struct name *np, *t, *newn, *x;
1275 NYD_ENTER;
1277 newn = NULL;
1278 if (names == NULL)
1279 goto jleave;
1281 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
1282 for (np = NULL; names != NULL; names = names->n_flink)
1283 if (!(names->n_type & GDEL)) {
1284 names->n_blink = np;
1285 if (np)
1286 np->n_flink = names;
1287 else
1288 newn = names;
1289 np = names;
1291 if (newn == NULL)
1292 goto jleave;
1294 np = newn->n_flink;
1295 if (np != NULL)
1296 np->n_blink = NULL;
1297 newn->n_flink = NULL;
1299 while (np != NULL) {
1300 int cmpres;
1302 t = newn;
1303 while ((cmpres = asccasecmp(t->n_name, np->n_name)) < 0) {
1304 if (t->n_flink == NULL)
1305 break;
1306 t = t->n_flink;
1309 /* If we ran out of t's, put new entry after the current value of t */
1310 if (cmpres < 0) {
1311 t->n_flink = np;
1312 np->n_blink = t;
1313 t = np;
1314 np = np->n_flink;
1315 t->n_flink = NULL;
1316 continue;
1319 /* Otherwise, put the new entry in front of the current t. If at the
1320 * front of the list, the new guy becomes the new head of the list */
1321 if (t == newn) {
1322 t = np;
1323 np = np->n_flink;
1324 t->n_flink = newn;
1325 newn->n_blink = t;
1326 t->n_blink = NULL;
1327 newn = t;
1328 continue;
1331 /* The normal case -- we are inserting into the middle of the list */
1332 x = np;
1333 np = np->n_flink;
1334 x->n_flink = t;
1335 x->n_blink = t->n_blink;
1336 t->n_blink->n_flink = x;
1337 t->n_blink = x;
1340 /* Now the list headed up by new is sorted. Remove duplicates */
1341 np = newn;
1342 while (np != NULL) {
1343 t = np;
1344 while (t->n_flink != NULL && !asccasecmp(np->n_name, t->n_flink->n_name))
1345 t = t->n_flink;
1346 if (t == np) {
1347 np = np->n_flink;
1348 continue;
1351 /* Now t points to the last entry with the same name as np.
1352 * Make np point beyond t */
1353 np->n_flink = t->n_flink;
1354 if (t->n_flink != NULL)
1355 t->n_flink->n_blink = np;
1356 np = np->n_flink;
1358 jleave:
1359 NYD_LEAVE;
1360 return newn;
1363 FL int
1364 c_alternates(void *v)
1366 size_t l;
1367 char **namelist = v, **ap, **ap2, *cp;
1368 NYD_ENTER;
1370 l = argcount(namelist) +1;
1372 if (l == 1) {
1373 if (_altnames != NULL) {
1374 printf("alternates ");
1375 for (ap = _altnames; *ap != NULL; ++ap)
1376 printf("%s ", *ap);
1377 printf("\n");
1379 goto jleave;
1382 if (_altnames != NULL) {
1383 for (ap = _altnames; *ap != NULL; ++ap)
1384 free(*ap);
1385 free(_altnames);
1388 _altnames = smalloc(l * sizeof(*_altnames));
1389 for (ap = namelist, ap2 = _altnames; *ap != NULL; ++ap, ++ap2) {
1390 l = strlen(*ap) +1;
1391 cp = smalloc(l);
1392 memcpy(cp, *ap, l);
1393 *ap2 = cp;
1395 *ap2 = NULL;
1396 jleave:
1397 NYD_LEAVE;
1398 return 0;
1401 FL struct name *
1402 delete_alternates(struct name *np)
1404 struct name *xp;
1405 char **ap;
1406 NYD_ENTER;
1408 np = delname(np, myname);
1409 if (_altnames != NULL)
1410 for (ap = _altnames; *ap != '\0'; ++ap)
1411 np = delname(np, *ap);
1413 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1414 while (xp != NULL) {
1415 np = delname(np, xp->n_name);
1416 xp = xp->n_flink;
1419 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1420 while (xp != NULL) {
1421 np = delname(np, xp->n_name);
1422 xp = xp->n_flink;
1425 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1426 while (xp != NULL) {
1427 np = delname(np, xp->n_name);
1428 xp = xp->n_flink;
1430 NYD_LEAVE;
1431 return np;
1434 FL int
1435 is_myname(char const *name)
1437 int rv = 1;
1438 struct name *xp;
1439 char **ap;
1440 NYD_ENTER;
1442 if (_same_name(myname, name))
1443 goto jleave;
1444 if (_altnames != NULL)
1445 for (ap = _altnames; *ap != NULL; ++ap)
1446 if (_same_name(*ap, name))
1447 goto jleave;
1449 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1450 while (xp != NULL) {
1451 if (_same_name(xp->n_name, name))
1452 goto jleave;
1453 xp = xp->n_flink;
1456 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1457 while (xp != NULL) {
1458 if (_same_name(xp->n_name, name))
1459 goto jleave;
1460 xp = xp->n_flink;
1463 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1464 while (xp != NULL) {
1465 if (_same_name(xp->n_name, name))
1466 goto jleave;
1467 xp = xp->n_flink;
1469 rv = 0;
1470 jleave:
1471 NYD_LEAVE;
1472 return rv;
1475 FL int
1476 c_alias(void *v)
1478 char **argv = v;
1479 struct group *gp;
1480 int rv = 0;
1481 NYD_ENTER;
1483 if (*argv == NULL)
1484 _group_print_all(GT_ALIAS);
1485 else if (argv[1] == NULL) {
1486 if ((gp = _group_find(GT_ALIAS, *argv)) != NULL)
1487 _group_print(gp, stdout);
1488 else {
1489 n_err(_("No such alias: \"%s\"\n"), *argv);
1490 rv = 1;
1492 } else {
1493 struct grp_names_head *gnhp;
1495 gp = _group_fetch(GT_ALIAS, *argv, 0);
1496 GP_TO_SUBCLASS(gnhp, gp);
1498 for (++argv; *argv != NULL; ++argv) {
1499 size_t l = strlen(*argv) +1;
1500 struct grp_names *gnp = smalloc(sizeof(*gnp) -
1501 VFIELD_SIZEOF(struct grp_names, gn_id) + l);
1502 gnp->gn_next = gnhp->gnh_head;
1503 gnhp->gnh_head = gnp;
1504 memcpy(gnp->gn_id, *argv, l);
1506 assert(gnhp->gnh_head != NULL);
1508 NYD_LEAVE;
1509 return rv;
1512 FL int
1513 c_unalias(void *v)
1515 char **argv = v;
1516 int rv = 0;
1517 NYD_ENTER;
1519 do if (!_group_del(GT_ALIAS, *argv)) {
1520 n_err(_("No such alias: \"%s\"\n"), *argv);
1521 rv = 1;
1522 } while (*++argv != NULL);
1523 NYD_LEAVE;
1524 return rv;
1527 FL int
1528 c_mlist(void *v)
1530 int rv;
1531 NYD_ENTER;
1533 rv = _mlmux(GT_MLIST, v);
1534 NYD_LEAVE;
1535 return rv;
1538 FL int
1539 c_unmlist(void *v)
1541 int rv;
1542 NYD_ENTER;
1544 rv = _unmlmux(GT_MLIST, v);
1545 NYD_LEAVE;
1546 return rv;
1549 FL int
1550 c_mlsubscribe(void *v)
1552 int rv;
1553 NYD_ENTER;
1555 rv = _mlmux(GT_MLIST | GT_SUBSCRIBE, v);
1556 NYD_LEAVE;
1557 return rv;
1560 FL int
1561 c_unmlsubscribe(void *v)
1563 int rv;
1564 NYD_ENTER;
1566 rv = _unmlmux(GT_MLIST | GT_SUBSCRIBE, v);
1567 NYD_LEAVE;
1568 return rv;
1571 FL enum mlist_state
1572 is_mlist(char const *name, bool_t subscribed_only)
1574 struct group *gp;
1575 #ifdef HAVE_REGEX
1576 struct grp_regex **lpp, *grp;
1577 bool_t re2;
1578 #endif
1579 enum mlist_state rv;
1580 NYD_ENTER;
1582 gp = _group_find(GT_MLIST, name);
1583 rv = (gp != NULL) ? MLIST_KNOWN : MLIST_OTHER;
1584 if (rv == MLIST_KNOWN) {
1585 if (gp->g_type & GT_SUBSCRIBE)
1586 rv = MLIST_SUBSCRIBED;
1587 else if (subscribed_only)
1588 rv = MLIST_OTHER;
1589 /* Of course, if that is a regular expression it doesn't mean a thing */
1590 #ifdef HAVE_REGEX
1591 if (gp->g_type & GT_REGEX)
1592 rv = MLIST_OTHER;
1593 else
1594 #endif
1595 goto jleave;
1598 /* Not in the hashmap (as something matchable), walk the lists */
1599 #ifdef HAVE_REGEX
1600 re2 = FAL0;
1601 lpp = &_mlsub_regex;
1602 jregex_redo:
1603 if ((grp = *lpp) != NULL) {
1604 do if (regexec(&grp->gr_regex, name, 0,NULL, 0) != REG_NOMATCH) {
1605 /* Relink as the head of this list if the hit count of this group is
1606 * >= 25% of the average hit count */
1607 size_t i;
1608 if (!re2)
1609 i = ++_mlsub_hits / _mlsub_size;
1610 else
1611 i = ++_mlist_hits / _mlist_size;
1612 i >>= 2;
1614 if (++grp->gr_hits >= i && *lpp != grp && grp->gr_next != grp) {
1615 grp->gr_last->gr_next = grp->gr_next;
1616 grp->gr_next->gr_last = grp->gr_last;
1617 (grp->gr_last = (*lpp)->gr_last)->gr_next = grp;
1618 (grp->gr_next = *lpp)->gr_last = grp;
1619 *lpp = grp;
1621 rv = !re2 ? MLIST_SUBSCRIBED : MLIST_KNOWN;
1622 goto jleave;
1623 } while ((grp = grp->gr_next) != *lpp);
1625 if (!re2 && !subscribed_only) {
1626 re2 = TRU1;
1627 lpp = &_mlist_regex;
1628 goto jregex_redo;
1630 assert(rv == MLIST_OTHER);
1631 #endif
1633 jleave:
1634 NYD_LEAVE;
1635 return rv;
1638 FL int
1639 c_shortcut(void *v)
1641 char **argv = v;
1642 int rv = 0;
1643 NYD_ENTER;
1645 if (*argv == NULL)
1646 _group_print_all(GT_SHORTCUT);
1647 else for (; *argv != NULL; argv += 2) {
1648 /* Because one hardly ever redefines, anything is stored in one chunk */
1649 size_t l;
1650 struct group *gp;
1651 char *cp;
1653 if (argv[1] == NULL) {
1654 n_err(_("Shortcut expansion is missing: \"%s\"\n"), *argv);
1655 rv = 1;
1656 break;
1658 if (_group_find(GT_SHORTCUT, *argv) != NULL)
1659 _group_del(GT_SHORTCUT, *argv);
1661 l = strlen(argv[1]) +1;
1662 gp = _group_fetch(GT_SHORTCUT, *argv, l);
1663 GP_TO_SUBCLASS(cp, gp);
1664 memcpy(cp, argv[1], l);
1666 NYD_LEAVE;
1667 return rv;
1670 FL int
1671 c_unshortcut(void *v)
1673 char **argv = v;
1674 int rv = 0;
1675 NYD_ENTER;
1677 do if (!_group_del(GT_SHORTCUT, *argv)) {
1678 n_err(_("No such shortcut: \"%s\"\n"), *argv);
1679 rv = 1;
1680 } while (*++argv != NULL);
1681 NYD_LEAVE;
1682 return rv;
1685 FL char const *
1686 shortcut_expand(char const *str)
1688 struct group *gp;
1689 NYD_ENTER;
1691 if ((gp = _group_find(GT_SHORTCUT, str)) != NULL)
1692 GP_TO_SUBCLASS(str, gp);
1693 else
1694 str = NULL;
1695 NYD_LEAVE;
1696 return str;
1699 FL int
1700 c_customhdr(void *v){
1701 struct group *gp;
1702 char const **argv, *hcp;
1703 int rv;
1704 NYD_ENTER;
1706 rv = 0;
1708 if((hcp = *(argv = v)) == NULL){
1709 _group_print_all(GT_CUSTOMHDR);
1710 goto jleave;
1713 /* Verify the header field name */
1714 for(rv = 1;; ++hcp){
1715 if(fieldnamechar(*hcp))
1716 continue;
1717 if(*hcp == '\0'){
1718 if(hcp == *argv){
1719 n_err(_("Cannot use nameless custom header\n"));
1720 goto jleave;
1722 hcp = *argv;
1723 break;
1725 n_err(_("Invalid custom header name: \"%s\"\n"), *argv);
1726 goto jleave;
1728 rv = 0;
1730 if(*++argv == NULL){
1731 if((gp = _group_find(GT_CUSTOMHDR, hcp)) != NULL)
1732 _group_print(gp, stdout);
1733 else{
1734 n_err(_("No such custom header: \"%s\"\n"), hcp);
1735 rv = 1;
1737 }else{
1738 /* Because one hardly ever redefines, anything is stored in one chunk */
1739 size_t i, l;
1740 char *cp;
1742 if(_group_find(GT_CUSTOMHDR, hcp) != NULL)
1743 _group_del(GT_CUSTOMHDR, hcp);
1745 for(l = i = 0; argv[i] != NULL; ++i)
1746 l += strlen(argv[i]) + 1;
1747 gp = _group_fetch(GT_CUSTOMHDR, hcp, l +1);
1748 GP_TO_SUBCLASS(cp, gp);
1749 for(i = 0; argv[i] != NULL; ++i){
1750 if(i > 0)
1751 *cp++ = ' ';
1752 l = strlen(argv[i]);
1753 memcpy(cp, argv[i], l);
1754 cp += l;
1756 *cp = '\0';
1758 jleave:
1759 NYD_LEAVE;
1760 return rv;
1763 FL int
1764 c_uncustomhdr(void *v){
1765 int rv;
1766 char **argv;
1767 NYD_ENTER;
1769 rv = 0;
1771 argv = v;
1773 if(!_group_del(GT_CUSTOMHDR, *argv)){
1774 n_err(_("No such custom header: \"%s\"\n"), *argv);
1775 rv = 1;
1777 }while(*++argv != NULL);
1778 NYD_LEAVE;
1779 return rv;
1782 FL struct n_header_field *
1783 n_customhdr_query(void){ /* XXX Uses salloc()! */
1784 struct group const *gp;
1785 ui32_t h;
1786 struct n_header_field *rv, **tail, *hfp;
1787 NYD_ENTER;
1789 rv = NULL;
1790 tail = &rv;
1792 for(h = 0; h < HSHSIZE; ++h)
1793 for(gp = _customhdr_heads[h]; gp != NULL; gp = gp->g_next){
1794 char const *cp;
1795 ui32_t nl, bl;
1797 GP_TO_SUBCLASS(cp, gp);
1798 nl = (ui32_t)strlen(gp->g_id) +1;
1799 bl = (ui32_t)strlen(cp) +1;
1801 *tail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
1802 nl + bl);
1803 tail = &hfp->hf_next;
1804 hfp->hf_next = NULL;
1805 hfp->hf_nl = nl - 1;
1806 hfp->hf_bl = bl - 1;
1807 memcpy(hfp->hf_dat, gp->g_id, nl);
1808 memcpy(hfp->hf_dat + nl, cp, bl);
1811 /* TODO We have no copy-on-write environments yet, and since custom headers
1812 * TODO are a perfect subject for localization, add the OBSOLETE variable
1813 * TODO *customhdr*, too */
1815 char const *vp = ok_vlook(customhdr);
1817 if(vp != NULL){
1818 char *buf = savestr(vp);
1819 jch_outer:
1820 while((vp = n_strescsep(&buf, ',', TRU1)) != NULL){
1821 ui32_t nl, bl;
1822 char const *nstart, *cp;
1824 for(nstart = cp = vp;; ++cp){
1825 if(fieldnamechar(*cp))
1826 continue;
1827 if(*cp == '\0'){
1828 if(cp == nstart){
1829 n_err(_("Invalid nameless *customhdr* entry\n"));
1830 goto jch_outer;
1832 }else if(*cp != ':' && !blankchar(*cp)){
1833 jch_badent:
1834 n_err(_("Invalid *customhdr* entry: \"%s\"\n"), vp);
1835 goto jch_outer;
1837 break;
1839 nl = (ui32_t)PTR2SIZE(cp - nstart);
1841 while(blankchar(*cp))
1842 ++cp;
1843 if(*cp++ != ':')
1844 goto jch_badent;
1845 while(blankchar(*cp))
1846 ++cp;
1847 bl = (ui32_t)strlen(cp) +1;
1849 *tail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
1850 nl +1 + bl);
1851 tail = &hfp->hf_next;
1852 hfp->hf_next = NULL;
1853 hfp->hf_nl = nl;
1854 hfp->hf_bl = bl - 1;
1855 memcpy(hfp->hf_dat, nstart, nl);
1856 hfp->hf_dat[nl++] = '\0';
1857 memcpy(hfp->hf_dat + nl, cp, bl);
1861 NYD_LEAVE;
1862 return rv;
1865 /* s-it-mode */