Move c_flag() etc. to cmd2.c: they belong
[s-mailx.git] / nam_a_grp.c
blob30cdb21728ec05e0414b205d4141c18a758e72f8
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 /* TODO v15: drop *customhdr* (OR change syntax and use shell tokens) */
203 static char *a_nag_custom_sep(char **iolist);
205 static bool_t
206 _same_name(char const *n1, char const *n2)
208 bool_t rv = FAL0;
209 char c1, c2;
210 NYD_ENTER;
212 if (ok_blook(allnet)) {
213 do {
214 c1 = *n1++;
215 c2 = *n2++;
216 c1 = lowerconv(c1);
217 c2 = lowerconv(c2);
218 if (c1 != c2)
219 goto jleave;
220 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
221 rv = 1;
222 } else
223 rv = !asccasecmp(n1, n2);
224 jleave:
225 NYD_LEAVE;
226 return rv;
229 static struct name *
230 delname(struct name *np, char const *name)
232 struct name *p;
233 NYD_ENTER;
235 for (p = np; p != NULL; p = p->n_flink)
236 if (_same_name(p->n_name, name)) {
237 if (p->n_blink == NULL) {
238 if (p->n_flink != NULL)
239 p->n_flink->n_blink = NULL;
240 np = p->n_flink;
241 continue;
243 if (p->n_flink == NULL) {
244 if (p->n_blink != NULL)
245 p->n_blink->n_flink = NULL;
246 continue;
248 p->n_blink->n_flink = p->n_flink;
249 p->n_flink->n_blink = p->n_blink;
251 NYD_LEAVE;
252 return np;
255 static struct name *
256 put(struct name *list, struct name *node)
258 NYD_ENTER;
259 node->n_flink = list;
260 node->n_blink = NULL;
261 if (list != NULL)
262 list->n_blink = node;
263 NYD_LEAVE;
264 return node;
267 static char const *
268 yankname(char const *ap, char *wbuf, char const *separators, int keepcomms)
270 char const *cp;
271 char *wp, c, inquote, lc, lastsp;
272 NYD_ENTER;
274 *(wp = wbuf) = '\0';
276 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
277 for (c = *ap; blankchar(c) || c == ','; c = *++ap)
279 if (c == '\0') {
280 cp = NULL;
281 goto jleave;
284 /* Parse a full name: TODO RFC 5322
285 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
286 * - Skip entire (nested) comments
287 * - In non-quote, non-comment, join adjacent space to a single SP
288 * - Understand separators only in non-quote, non-comment context,
289 * and only if not part of a *quoted-pair* (XXX too liberal) */
290 cp = ap;
291 for (inquote = lc = lastsp = 0;; lc = c, ++cp) {
292 c = *cp;
293 if (c == '\0')
294 break;
295 if (c == '\\') {
296 lastsp = 0;
297 continue;
299 if (c == '"') {
300 if (lc != '\\')
301 inquote = !inquote;
302 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
303 else
304 --wp;
305 #endif
306 goto jwpwc;
308 if (inquote || lc == '\\') {
309 jwpwc:
310 *wp++ = c;
311 lastsp = 0;
312 continue;
314 if (c == '(') {
315 ap = cp;
316 cp = skip_comment(cp + 1);
317 if (keepcomms)
318 while (ap < cp)
319 *wp++ = *ap++;
320 --cp;
321 lastsp = 0;
322 continue;
324 if (strchr(separators, c) != NULL)
325 break;
327 lc = lastsp;
328 lastsp = blankchar(c);
329 if (!lastsp || !lc)
330 *wp++ = c;
332 if (blankchar(lc))
333 --wp;
335 *wp = '\0';
336 jleave:
337 NYD_LEAVE;
338 return cp;
341 static struct name *
342 _extract1(char const *line, enum gfield ntype, char const *separators,
343 bool_t keepcomms)
345 struct name *topp, *np, *t;
346 char const *cp;
347 char *nbuf;
348 NYD_ENTER;
350 topp = NULL;
351 if (line == NULL || *line == '\0')
352 goto jleave;
354 np = NULL;
355 cp = line;
356 nbuf = smalloc(strlen(line) +1);
357 while ((cp = yankname(cp, nbuf, separators, keepcomms)) != NULL) {
358 t = nalloc(nbuf, ntype);
359 if (topp == NULL)
360 topp = t;
361 else
362 np->n_flink = t;
363 t->n_blink = np;
364 np = t;
366 free(nbuf);
367 jleave:
368 NYD_LEAVE;
369 return topp;
372 static struct name *
373 _gexpand(size_t level, struct name *nlist, struct group *gp, bool_t metoo,
374 int ntype)
376 struct grp_names_head *gnhp;
377 struct grp_names *gnp;
378 NYD_ENTER;
380 if (UICMP(z, level++, >, MAXEXP)) {
381 n_err(_("Expanding alias to depth larger than %d\n"), MAXEXP);
382 goto jleave;
385 GP_TO_SUBCLASS(gnhp, gp);
386 for (gnp = gnhp->gnh_head; gnp != NULL; gnp = gnp->gn_next) {
387 char *cp;
388 struct group *ngp;
390 /* FIXME we do not really support leading backslash quoting do we??? */
391 if (*(cp = gnp->gn_id) == '\\' || !strcmp(cp, gp->g_id))
392 goto jquote;
394 if ((ngp = _group_find(GT_ALIAS, cp)) != NULL) {
395 /* For S-nail(1), the "alias" may *be* the sender in that a name maps
396 * to a full address specification; aliases cannot be empty */
397 struct grp_names_head *ngnhp;
398 GP_TO_SUBCLASS(ngnhp, ngp);
400 assert(ngnhp->gnh_head != NULL);
401 if (metoo || ngnhp->gnh_head->gn_next != NULL ||
402 !_same_name(cp, myname))
403 nlist = _gexpand(level, nlist, ngp, metoo, ntype);
404 continue;
407 /* Here we should allow to expand to itself if only person in alias */
408 jquote:
409 if (metoo || gnhp->gnh_head->gn_next == NULL || !_same_name(cp, myname))
410 nlist = put(nlist, nalloc(cp, ntype | GFULL));
412 jleave:
413 NYD_LEAVE;
414 return nlist;
417 static struct group *
418 _group_lookup(enum group_type gt, struct group_lookup *glp, char const *id)
420 struct group *lgp, *gp;
421 NYD_ENTER;
423 gt &= GT_MASK;
424 lgp = NULL;
425 gp = *(glp->gl_htable = glp->gl_slot =
426 ((gt & GT_ALIAS ? _alias_heads :
427 (gt & GT_MLIST ? _mlist_heads :
428 (gt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads))) +
429 torek_hash(id) % HSHSIZE));
431 for (; gp != NULL; lgp = gp, gp = gp->g_next)
432 if ((gp->g_type & gt) && *gp->g_id == *id && !strcmp(gp->g_id, id))
433 break;
435 glp->gl_slot_last = lgp;
436 glp->gl_group = gp;
437 NYD_LEAVE;
438 return gp;
441 static struct group *
442 _group_find(enum group_type gt, char const *id)
444 struct group_lookup gl;
445 struct group *gp;
446 NYD_ENTER;
448 gp = _group_lookup(gt, &gl, id);
449 NYD_LEAVE;
450 return gp;
453 static struct group *
454 _group_go_first(enum group_type gt, struct group_lookup *glp)
456 struct group **gpa, *gp;
457 size_t i;
458 NYD_ENTER;
460 for (glp->gl_htable = gpa = (gt & GT_ALIAS ? _alias_heads :
461 (gt & GT_MLIST ? _mlist_heads :
462 (gt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads))), i = 0;
463 i < HSHSIZE; ++gpa, ++i)
464 if ((gp = *gpa) != NULL) {
465 glp->gl_slot = gpa;
466 glp->gl_group = gp;
467 goto jleave;
470 glp->gl_group = gp = NULL;
471 jleave:
472 glp->gl_slot_last = NULL;
473 NYD_LEAVE;
474 return gp;
477 static struct group *
478 _group_go_next(struct group_lookup *glp)
480 struct group *gp, **gpa;
481 NYD_ENTER;
483 if ((gp = glp->gl_group->g_next) != NULL)
484 glp->gl_slot_last = glp->gl_group;
485 else {
486 glp->gl_slot_last = NULL;
487 for (gpa = glp->gl_htable + HSHSIZE; ++glp->gl_slot < gpa;)
488 if ((gp = *glp->gl_slot) != NULL)
489 break;
491 glp->gl_group = gp;
492 NYD_LEAVE;
493 return gp;
496 static struct group *
497 _group_fetch(enum group_type gt, char const *id, size_t addsz)
499 struct group_lookup gl;
500 struct group *gp;
501 size_t l, i;
502 NYD_ENTER;
504 if ((gp = _group_lookup(gt, &gl, id)) != NULL)
505 goto jleave;
507 l = strlen(id) +1;
508 i = n_ALIGN(sizeof(*gp) - VFIELD_SIZEOF(struct group, g_id) + l);
509 switch (gt & GT_MASK) {
510 case GT_ALIAS:
511 addsz = sizeof(struct grp_names_head);
512 break;
513 case GT_MLIST:
514 #ifdef HAVE_REGEX
515 if (is_maybe_regex(id)) {
516 addsz = sizeof(struct grp_regex);
517 gt |= GT_REGEX;
518 } else
519 #endif
520 case GT_SHORTCUT:
521 default:
522 break;
525 gp = smalloc(i + addsz);
526 gp->g_subclass_off = i;
527 gp->g_type = gt;
528 memcpy(gp->g_id, id, l);
530 if (gt & GT_ALIAS) {
531 struct grp_names_head *gnhp;
532 GP_TO_SUBCLASS(gnhp, gp);
533 gnhp->gnh_head = NULL;
535 #ifdef HAVE_REGEX
536 else if (/*(gt & GT_MLIST) &&*/ gt & GT_REGEX) {
537 struct grp_regex *grp;
538 GP_TO_SUBCLASS(grp, gp);
540 if (regcomp(&grp->gr_regex, id, REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
541 n_err(_("Invalid regular expression: \"%s\"\n"), id);
542 free(gp);
543 gp = NULL;
544 goto jleave;
546 grp->gr_mygroup = gp;
547 _mlmux_linkin(gp);
549 #endif
551 gp->g_next = *gl.gl_slot;
552 *gl.gl_slot = gp;
553 jleave:
554 NYD_LEAVE;
555 return gp;
558 static bool_t
559 _group_del(enum group_type gt, char const *id)
561 enum group_type xgt = gt & GT_MASK;
562 struct group_lookup gl;
563 struct group *gp;
564 NYD_ENTER;
566 /* Delete 'em all? */
567 if (id[0] == '*' && id[1] == '\0') {
568 for (gp = _group_go_first(gt, &gl); gp != NULL;)
569 gp = (gp->g_type & xgt) ? __group_del(&gl) : _group_go_next(&gl);
570 gp = (struct group*)TRU1;
571 } else if ((gp = _group_lookup(gt, &gl, id)) != NULL) {
572 if (gp->g_type & xgt)
573 __group_del(&gl);
574 else
575 gp = NULL;
577 NYD_LEAVE;
578 return (gp != NULL);
581 static struct group *
582 __group_del(struct group_lookup *glp)
584 struct group *x, *gp;
585 NYD_ENTER;
587 /* Overly complicated: link off this node, step ahead to next.. */
588 x = glp->gl_group;
589 if ((gp = glp->gl_slot_last) != NULL) {
590 gp = (gp->g_next = x->g_next);
591 } else {
592 glp->gl_slot_last = NULL;
593 gp = (*glp->gl_slot = x->g_next);
595 if (gp == NULL) {
596 struct group **gpa = glp->gl_htable + HSHSIZE;
598 while (++glp->gl_slot < gpa)
599 if ((gp = *glp->gl_slot) != NULL)
600 break;
603 glp->gl_group = gp;
605 if (x->g_type & GT_ALIAS)
606 __names_del(x);
607 #ifdef HAVE_REGEX
608 else if (/*(x->g_type & GT_MLIST) &&*/ x->g_type & GT_REGEX) {
609 struct grp_regex *grp;
610 GP_TO_SUBCLASS(grp, x);
612 regfree(&grp->gr_regex);
613 _mlmux_linkout(x);
615 #endif
617 free(x);
618 NYD_LEAVE;
619 return gp;
622 static void
623 __names_del(struct group *gp)
625 struct grp_names_head *gnhp;
626 struct grp_names *gnp;
627 NYD_ENTER;
629 GP_TO_SUBCLASS(gnhp, gp);
630 for (gnp = gnhp->gnh_head; gnp != NULL;) {
631 struct grp_names *x = gnp;
632 gnp = gnp->gn_next;
633 free(x);
635 NYD_LEAVE;
638 static void
639 _group_print_all(enum group_type gt)
641 enum group_type xgt;
642 struct group **gpa;
643 struct group const *gp;
644 ui32_t h, i;
645 char const **ida;
646 FILE *fp;
647 size_t lines;
648 NYD_ENTER;
650 xgt = gt & GT_PRINT_MASK;
651 gpa = (xgt & GT_ALIAS ? _alias_heads
652 : (xgt & GT_MLIST ? _mlist_heads
653 : (xgt & GT_SHORTCUT ? _shortcut_heads : _customhdr_heads)));
655 for (h = 0, i = 1; h < HSHSIZE; ++h)
656 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
657 if ((gp->g_type & xgt) == xgt)
658 ++i;
659 ida = salloc(i * sizeof *ida);
661 for (i = h = 0; h < HSHSIZE; ++h)
662 for (gp = gpa[h]; gp != NULL; gp = gp->g_next)
663 if ((gp->g_type & xgt) == xgt)
664 ida[i++] = gp->g_id;
665 ida[i] = NULL;
667 if (i > 1)
668 qsort(ida, i, sizeof *ida, &__group_print_qsorter);
670 if ((fp = Ftmp(NULL, "prgroup", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
671 fp = stdout;
672 lines = 0;
674 for (i = 0; ida[i] != NULL; ++i)
675 lines += _group_print(_group_find(gt, ida[i]), fp);
676 #ifdef HAVE_REGEX
677 if (gt & GT_MLIST) {
678 if (gt & GT_SUBSCRIBE)
679 i = (ui32_t)_mlsub_size, h = (ui32_t)_mlsub_hits;
680 else
681 i = (ui32_t)_mlist_size, h = (ui32_t)_mlist_hits;
682 if (i > 0 && (options & OPT_D_V)){
683 fprintf(fp, _("# %s list regex(7) total: %u entries, %u hits\n"),
684 (gt & GT_SUBSCRIBE ? _("Subscribed") : _("Non-subscribed")),
685 i, h);
686 ++lines;
689 #endif
691 if (fp != stdout) {
692 page_or_print(fp, lines);
693 Fclose(fp);
695 NYD_LEAVE;
698 static int
699 __group_print_qsorter(void const *a, void const *b)
701 int rv;
702 NYD_ENTER;
704 rv = strcmp(*(char**)UNCONST(a), *(char**)UNCONST(b));
705 NYD_LEAVE;
706 return rv;
709 static size_t
710 _group_print(struct group const *gp, FILE *fo)
712 char const *cp;
713 size_t rv;
714 NYD_ENTER;
716 rv = 1;
718 if (gp->g_type & GT_ALIAS) {
719 struct grp_names_head *gnhp;
720 struct grp_names *gnp;
722 fprintf(fo, "alias %s", gp->g_id);
724 GP_TO_SUBCLASS(gnhp, gp);
725 if ((gnp = gnhp->gnh_head) != NULL) { /* xxx always 1+ entries */
726 do {
727 struct grp_names *x = gnp;
728 gnp = gnp->gn_next;
729 fprintf(fo, " \"%s\"", string_quote(x->gn_id));
730 } while (gnp != NULL);
732 } else if (gp->g_type & GT_MLIST) {
733 #ifdef HAVE_REGEX
734 if ((gp->g_type & GT_REGEX) && (options & OPT_D_V)){
735 size_t i;
736 struct grp_regex *grp,
737 *lp = (gp->g_type & GT_SUBSCRIBE ? _mlsub_regex : _mlist_regex);
739 GP_TO_SUBCLASS(grp, gp);
740 for (i = 1; lp != grp; lp = lp->gr_next)
741 ++i;
742 fprintf(fo, "# regex(7): hits %" PRIuZ ", sort %" PRIuZ ".\n ",
743 grp->gr_hits, i);
744 ++rv;
746 #endif
748 fprintf(fo, "wysh %s %s",
749 (gp->g_type & GT_SUBSCRIBE ? "mlsubscribe" : "mlist"),
750 n_shell_quote_cp(gp->g_id, TRU1));
751 } else if (gp->g_type & GT_SHORTCUT) {
752 GP_TO_SUBCLASS(cp, gp);
753 fprintf(fo, "wysh shortcut %s %s", gp->g_id, n_shell_quote_cp(cp, TRU1));
754 } else if (gp->g_type & GT_CUSTOMHDR) {
755 GP_TO_SUBCLASS(cp, gp);
756 fprintf(fo, "customhdr %s %s", gp->g_id, n_shell_quote_cp(cp, TRU1));
759 putc('\n', fo);
760 NYD_LEAVE;
761 return rv;
764 static int
765 _mlmux(enum group_type gt, char **argv)
767 struct group *gp;
768 int rv = 0;
769 NYD_ENTER;
771 if (*argv == NULL)
772 _group_print_all(gt);
773 else do {
774 if ((gp = _group_find(gt, *argv)) != NULL) {
775 if (gt & GT_SUBSCRIBE) {
776 if (!(gp->g_type & GT_SUBSCRIBE)) {
777 _MLMUX_LINKOUT(gp);
778 gp->g_type |= GT_SUBSCRIBE;
779 _MLMUX_LINKIN(gp);
780 } else {
781 n_err(_("Mailing-list already `mlsubscribe'd: %s\n"),
782 *argv);
783 rv = 1;
785 } else {
786 n_err(_("Mailing-list already `mlist'ed: %s\n"), *argv);
787 rv = 1;
789 } else
790 _group_fetch(gt, *argv, 0);
791 } while (*++argv != NULL);
793 NYD_LEAVE;
794 return rv;
797 static int
798 _unmlmux(enum group_type gt, char **argv)
800 struct group *gp;
801 int rv = 0;
802 NYD_ENTER;
804 for (; *argv != NULL; ++argv) {
805 if (gt & GT_SUBSCRIBE) {
806 struct group_lookup gl;
807 bool_t isaster;
809 if (!(isaster = (**argv == '*')))
810 gp = _group_find(gt, *argv);
811 else if ((gp = _group_go_first(gt, &gl)) == NULL)
812 continue;
813 else if (gp != NULL && !(gp->g_type & GT_SUBSCRIBE))
814 goto jaster_entry;
816 if (gp != NULL) {
817 jaster_redo:
818 if (gp->g_type & GT_SUBSCRIBE) {
819 _MLMUX_LINKOUT(gp);
820 gp->g_type &= ~GT_SUBSCRIBE;
821 _MLMUX_LINKIN(gp);
822 if (isaster) {
823 jaster_entry:
824 while ((gp = _group_go_next(&gl)) != NULL &&
825 !(gp->g_type & GT_SUBSCRIBE))
827 if (gp != NULL)
828 goto jaster_redo;
830 } else {
831 n_err(_("Mailing-list not `mlsubscribe'd: \"%s\"\n"), *argv);
832 rv = 1;
834 continue;
836 } else if (_group_del(gt, *argv))
837 continue;
838 n_err(_("No such mailing-list: \"%s\"\n"), *argv);
839 rv = 1;
841 NYD_LEAVE;
842 return rv;
845 #ifdef HAVE_REGEX
846 static void
847 _mlmux_linkin(struct group *gp)
849 struct grp_regex **lpp, *grp, *lhp;
850 NYD_ENTER;
852 if (gp->g_type & GT_SUBSCRIBE) {
853 lpp = &_mlsub_regex;
854 ++_mlsub_size;
855 } else {
856 lpp = &_mlist_regex;
857 ++_mlist_size;
860 GP_TO_SUBCLASS(grp, gp);
861 if ((lhp = *lpp) != NULL) {
862 (grp->gr_last = lhp->gr_last)->gr_next = grp;
863 (grp->gr_next = lhp)->gr_last = grp;
864 } else
865 *lpp = grp->gr_last = grp->gr_next = grp;
866 grp->gr_hits = 0;
867 NYD_LEAVE;
870 static void
871 _mlmux_linkout(struct group *gp)
873 struct grp_regex *grp, **lpp;
874 NYD_ENTER;
876 GP_TO_SUBCLASS(grp, gp);
878 if (gp->g_type & GT_SUBSCRIBE) {
879 lpp = &_mlsub_regex;
880 --_mlsub_size;
881 _mlsub_hits -= grp->gr_hits;
882 } else {
883 lpp = &_mlist_regex;
884 --_mlist_size;
885 _mlist_hits -= grp->gr_hits;
888 if (grp->gr_next == grp)
889 *lpp = NULL;
890 else {
891 (grp->gr_last->gr_next = grp->gr_next)->gr_last = grp->gr_last;
892 if (*lpp == grp)
893 *lpp = grp->gr_next;
895 NYD_LEAVE;
897 #endif /* HAVE_REGEX */
899 static char *
900 a_nag_custom_sep(char **iolist){
901 char *cp, c, *base;
902 bool_t isesc, anyesc;
903 NYD2_ENTER;
905 for(base = *iolist; base != NULL; base = *iolist){
906 while((c = *base) != '\0' && blankspacechar(c))
907 ++base;
909 for(isesc = anyesc = FAL0, cp = base;; ++cp){
910 if(UNLIKELY((c = *cp) == '\0')){
911 *iolist = NULL;
912 break;
913 }else if(!isesc){
914 if(c == ','){
915 *iolist = cp + 1;
916 break;
918 isesc = (c == '\\');
919 }else{
920 isesc = FAL0;
921 anyesc |= (c == ',');
925 while(cp > base && blankspacechar(cp[-1]))
926 --cp;
927 *cp = '\0';
929 if(*base != '\0'){
930 if(anyesc){
931 char *ins;
933 for(ins = cp = base;; ++ins)
934 if((c = *cp) == '\\' && cp[1] == ','){
935 *ins = ',';
936 cp += 2;
937 }else if((*ins = (++cp, c)) == '\0')
938 break;
940 break;
943 NYD2_LEAVE;
944 return base;
947 FL struct name *
948 nalloc(char *str, enum gfield ntype)
950 struct addrguts ag;
951 struct str in, out;
952 struct name *np;
953 NYD_ENTER;
954 assert(!(ntype & GFULLEXTRA) || (ntype & GFULL) != 0);
956 addrspec_with_guts(((ntype & (GFULL | GSKIN | GREF)) != 0), str, &ag);
957 if (!(ag.ag_n_flags & NAME_NAME_SALLOC)) {
958 ag.ag_n_flags |= NAME_NAME_SALLOC;
959 np = salloc(sizeof(*np) + ag.ag_slen +1);
960 memcpy(np + 1, ag.ag_skinned, ag.ag_slen +1);
961 ag.ag_skinned = (char*)(np + 1);
962 } else
963 np = salloc(sizeof *np);
965 np->n_flink = NULL;
966 np->n_blink = NULL;
967 np->n_type = ntype;
968 np->n_flags = 0;
970 np->n_fullname = np->n_name = ag.ag_skinned;
971 np->n_fullextra = NULL;
972 np->n_flags = ag.ag_n_flags;
974 if (ntype & GFULL) {
975 if (ag.ag_ilen == ag.ag_slen
976 #ifdef HAVE_IDNA
977 && !(ag.ag_n_flags & NAME_IDNA)
978 #endif
980 goto jleave;
981 if (ag.ag_n_flags & NAME_ADDRSPEC_ISFILEORPIPE)
982 goto jleave;
984 /* n_fullextra is only the complete name part without address.
985 * Beware of "-r '<abc@def>'", don't treat that as FULLEXTRA */
986 if ((ntype & GFULLEXTRA) && ag.ag_ilen > ag.ag_slen + 2) {
987 size_t s = ag.ag_iaddr_start, e = ag.ag_iaddr_aend, i;
988 char const *cp;
990 if (s == 0 || str[--s] != '<' || str[e++] != '>')
991 goto jskipfullextra;
992 i = ag.ag_ilen - e;
993 in.s = ac_alloc(s + i +1);
994 memcpy(in.s, str, s);
995 if (i > 0)
996 memcpy(in.s + s, str + e, i);
997 s += i;
998 in.s[in.l = s] = '\0';
999 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1001 for (cp = out.s, i = out.l; i > 0 && spacechar(*cp); --i, ++cp)
1003 while (i > 0 && spacechar(cp[i - 1]))
1004 --i;
1005 np->n_fullextra = savestrbuf(cp, i);
1007 free(out.s);
1008 ac_free(in.s);
1010 jskipfullextra:
1012 /* n_fullname depends on IDNA conversion */
1013 #ifdef HAVE_IDNA
1014 if (!(ag.ag_n_flags & NAME_IDNA)) {
1015 #endif
1016 in.s = str;
1017 in.l = ag.ag_ilen;
1018 #ifdef HAVE_IDNA
1019 } else {
1020 /* The domain name was IDNA and has been converted. We also have to
1021 * ensure that the domain name in .n_fullname is replaced with the
1022 * converted version, since MIME doesn't perform encoding of addrs */
1023 size_t l = ag.ag_iaddr_start,
1024 lsuff = ag.ag_ilen - ag.ag_iaddr_aend;
1025 in.s = ac_alloc(l + ag.ag_slen + lsuff +1);
1026 memcpy(in.s, str, l);
1027 memcpy(in.s + l, ag.ag_skinned, ag.ag_slen);
1028 l += ag.ag_slen;
1029 memcpy(in.s + l, str + ag.ag_iaddr_aend, lsuff);
1030 l += lsuff;
1031 in.s[l] = '\0';
1032 in.l = l;
1034 #endif
1035 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1036 np->n_fullname = savestr(out.s);
1037 free(out.s);
1038 #ifdef HAVE_IDNA
1039 if (ag.ag_n_flags & NAME_IDNA)
1040 ac_free(in.s);
1041 #endif
1042 np->n_flags |= NAME_FULLNAME_SALLOC;
1044 jleave:
1045 NYD_LEAVE;
1046 return np;
1049 FL struct name *
1050 ndup(struct name *np, enum gfield ntype)
1052 struct name *nnp;
1053 NYD_ENTER;
1055 if ((ntype & (GFULL | GSKIN)) && !(np->n_flags & NAME_SKINNED)) {
1056 nnp = nalloc(np->n_name, ntype);
1057 goto jleave;
1060 nnp = salloc(sizeof *np);
1061 nnp->n_flink = nnp->n_blink = NULL;
1062 nnp->n_type = ntype;
1063 nnp->n_flags = (np->n_flags & ~(NAME_NAME_SALLOC | NAME_FULLNAME_SALLOC)) |
1064 NAME_NAME_SALLOC;
1065 nnp->n_name = savestr(np->n_name);
1066 if (np->n_name == np->n_fullname || !(ntype & (GFULL | GSKIN))) {
1067 nnp->n_fullname = nnp->n_name;
1068 nnp->n_fullextra = NULL;
1069 } else {
1070 nnp->n_flags |= NAME_FULLNAME_SALLOC;
1071 nnp->n_fullname = savestr(np->n_fullname);
1072 nnp->n_fullextra = (np->n_fullextra == NULL) ? NULL
1073 : savestr(np->n_fullextra);
1075 jleave:
1076 NYD_LEAVE;
1077 return nnp;
1080 FL struct name *
1081 cat(struct name *n1, struct name *n2)
1083 struct name *tail;
1084 NYD_ENTER;
1086 tail = n2;
1087 if (n1 == NULL)
1088 goto jleave;
1089 tail = n1;
1090 if (n2 == NULL)
1091 goto jleave;
1093 while (tail->n_flink != NULL)
1094 tail = tail->n_flink;
1095 tail->n_flink = n2;
1096 n2->n_blink = tail;
1097 tail = n1;
1098 jleave:
1099 NYD_LEAVE;
1100 return tail;
1103 FL struct name *
1104 namelist_dup(struct name const *np, enum gfield ntype)
1106 struct name *nnp;
1107 NYD_ENTER;
1109 for (nnp = NULL; np != NULL; np = np->n_flink) {
1110 struct name *x = ndup(UNCONST(np), (np->n_type & ~GMASK) | ntype);
1111 x->n_flink = nnp;
1112 nnp = x;
1114 NYD_LEAVE;
1115 return nnp;
1118 FL ui32_t
1119 count(struct name const *np)
1121 ui32_t c;
1122 NYD_ENTER;
1124 for (c = 0; np != NULL; np = np->n_flink)
1125 if (!(np->n_type & GDEL))
1126 ++c;
1127 NYD_LEAVE;
1128 return c;
1131 FL ui32_t
1132 count_nonlocal(struct name const *np)
1134 ui32_t c;
1135 NYD_ENTER;
1137 for (c = 0; np != NULL; np = np->n_flink)
1138 if (!(np->n_type & GDEL) && !(np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE))
1139 ++c;
1140 NYD_LEAVE;
1141 return c;
1144 FL struct name *
1145 extract(char const *line, enum gfield ntype)
1147 struct name *rv;
1148 NYD_ENTER;
1150 rv = _extract1(line, ntype, " \t,", 0);
1151 NYD_LEAVE;
1152 return rv;
1155 FL struct name *
1156 lextract(char const *line, enum gfield ntype)
1158 struct name *rv;
1159 NYD_ENTER;
1161 rv = ((line != NULL && strpbrk(line, ",\"\\(<|"))
1162 ? _extract1(line, ntype, ",", 1) : extract(line, ntype));
1163 NYD_LEAVE;
1164 return rv;
1167 FL char *
1168 detract(struct name *np, enum gfield ntype)
1170 char *topp, *cp;
1171 struct name *p;
1172 int flags, s;
1173 NYD_ENTER;
1175 topp = NULL;
1176 if (np == NULL)
1177 goto jleave;
1179 flags = ntype & (GCOMMA | GNAMEONLY);
1180 ntype &= ~(GCOMMA | GNAMEONLY);
1181 s = 0;
1183 for (p = np; p != NULL; p = p->n_flink) {
1184 if (ntype && (p->n_type & GMASK) != ntype)
1185 continue;
1186 s += strlen(flags & GNAMEONLY ? p->n_name : p->n_fullname) +1;
1187 if (flags & GCOMMA)
1188 ++s;
1190 if (s == 0)
1191 goto jleave;
1193 s += 2;
1194 topp = salloc(s);
1195 cp = topp;
1196 for (p = np; p != NULL; p = p->n_flink) {
1197 if (ntype && (p->n_type & GMASK) != ntype)
1198 continue;
1199 cp = sstpcpy(cp, (flags & GNAMEONLY ? p->n_name : p->n_fullname));
1200 if ((flags & GCOMMA) && p->n_flink != NULL)
1201 *cp++ = ',';
1202 *cp++ = ' ';
1204 *--cp = 0;
1205 if ((flags & GCOMMA) && *--cp == ',')
1206 *cp = 0;
1207 jleave:
1208 NYD_LEAVE;
1209 return topp;
1212 FL struct name *
1213 grab_names(char const *field, struct name *np, int comma, enum gfield gflags)
1215 struct name *nq;
1216 NYD_ENTER;
1218 jloop:
1219 np = lextract(n_lex_input_cp_addhist(field, detract(np, comma), TRU1),
1220 gflags);
1221 for (nq = np; nq != NULL; nq = nq->n_flink)
1222 if (is_addr_invalid(nq, EACM_NONE))
1223 goto jloop;
1224 NYD_LEAVE;
1225 return np;
1228 FL bool_t
1229 name_is_same_domain(struct name const *n1, struct name const *n2)
1231 char const *d1, *d2;
1232 bool_t rv;
1233 NYD_ENTER;
1235 d1 = strrchr(n1->n_name, '@');
1236 d2 = strrchr(n2->n_name, '@');
1238 rv = (d1 != NULL && d2 != NULL) ? !asccasecmp(++d1, ++d2) : FAL0;
1240 NYD_LEAVE;
1241 return rv;
1244 FL struct name *
1245 checkaddrs(struct name *np, enum expand_addr_check_mode eacm,
1246 si8_t *set_on_error)
1248 struct name *n;
1249 NYD_ENTER;
1251 for (n = np; n != NULL;) {
1252 si8_t rv;
1254 if ((rv = is_addr_invalid(n, eacm)) != 0) {
1255 if (set_on_error != NULL)
1256 *set_on_error |= rv; /* don't loose -1! */
1257 if (n->n_blink)
1258 n->n_blink->n_flink = n->n_flink;
1259 if (n->n_flink)
1260 n->n_flink->n_blink = n->n_blink;
1261 if (n == np)
1262 np = n->n_flink;
1264 n = n->n_flink;
1266 NYD_LEAVE;
1267 return np;
1270 FL struct name *
1271 namelist_vaporise_head(struct header *hp, enum expand_addr_check_mode eacm,
1272 bool_t metoo, si8_t *set_on_error)
1274 struct name *tolist, *np, **npp;
1275 NYD_ENTER;
1277 tolist = usermap(cat(hp->h_to, cat(hp->h_cc, hp->h_bcc)), metoo);
1278 hp->h_to = hp->h_cc = hp->h_bcc = NULL;
1280 tolist = elide(checkaddrs(tolist, eacm, set_on_error));
1282 for (np = tolist; np != NULL; np = np->n_flink) {
1283 switch (np->n_type & (GDEL | GMASK)) {
1284 case GTO: npp = &hp->h_to; break;
1285 case GCC: npp = &hp->h_cc; break;
1286 case GBCC: npp = &hp->h_bcc; break;
1287 default: continue;
1289 *npp = cat(*npp, ndup(np, np->n_type | GFULL));
1291 NYD_LEAVE;
1292 return tolist;
1295 FL struct name *
1296 usermap(struct name *names, bool_t force_metoo)
1298 struct name *new, *np, *cp;
1299 struct group *gp;
1300 int metoo;
1301 NYD_ENTER;
1303 new = NULL;
1304 np = names;
1305 metoo = (force_metoo || ok_blook(metoo));
1306 while (np != NULL) {
1307 assert(!(np->n_type & GDEL)); /* TODO legacy */
1308 if (is_fileorpipe_addr(np) || np->n_name[0] == '\\') {
1309 cp = np->n_flink;
1310 new = put(new, np);
1311 np = cp;
1312 continue;
1314 gp = _group_find(GT_ALIAS, np->n_name);
1315 cp = np->n_flink;
1316 if (gp != NULL)
1317 new = _gexpand(0, new, gp, metoo, np->n_type);
1318 else
1319 new = put(new, np);
1320 np = cp;
1322 NYD_LEAVE;
1323 return new;
1326 FL struct name *
1327 elide(struct name *names)
1329 struct name *np, *t, *newn, *x;
1330 NYD_ENTER;
1332 newn = NULL;
1333 if (names == NULL)
1334 goto jleave;
1336 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
1337 for (np = NULL; names != NULL; names = names->n_flink)
1338 if (!(names->n_type & GDEL)) {
1339 names->n_blink = np;
1340 if (np)
1341 np->n_flink = names;
1342 else
1343 newn = names;
1344 np = names;
1346 if (newn == NULL)
1347 goto jleave;
1349 np = newn->n_flink;
1350 if (np != NULL)
1351 np->n_blink = NULL;
1352 newn->n_flink = NULL;
1354 while (np != NULL) {
1355 int cmpres;
1357 t = newn;
1358 while ((cmpres = asccasecmp(t->n_name, np->n_name)) < 0) {
1359 if (t->n_flink == NULL)
1360 break;
1361 t = t->n_flink;
1364 /* If we ran out of t's, put new entry after the current value of t */
1365 if (cmpres < 0) {
1366 t->n_flink = np;
1367 np->n_blink = t;
1368 t = np;
1369 np = np->n_flink;
1370 t->n_flink = NULL;
1371 continue;
1374 /* Otherwise, put the new entry in front of the current t. If at the
1375 * front of the list, the new guy becomes the new head of the list */
1376 if (t == newn) {
1377 t = np;
1378 np = np->n_flink;
1379 t->n_flink = newn;
1380 newn->n_blink = t;
1381 t->n_blink = NULL;
1382 newn = t;
1383 continue;
1386 /* The normal case -- we are inserting into the middle of the list */
1387 x = np;
1388 np = np->n_flink;
1389 x->n_flink = t;
1390 x->n_blink = t->n_blink;
1391 t->n_blink->n_flink = x;
1392 t->n_blink = x;
1395 /* Now the list headed up by new is sorted. Remove duplicates */
1396 np = newn;
1397 while (np != NULL) {
1398 t = np;
1399 while (t->n_flink != NULL && !asccasecmp(np->n_name, t->n_flink->n_name))
1400 t = t->n_flink;
1401 if (t == np) {
1402 np = np->n_flink;
1403 continue;
1406 /* Now t points to the last entry with the same name as np.
1407 * Make np point beyond t */
1408 np->n_flink = t->n_flink;
1409 if (t->n_flink != NULL)
1410 t->n_flink->n_blink = np;
1411 np = np->n_flink;
1413 jleave:
1414 NYD_LEAVE;
1415 return newn;
1418 FL int
1419 c_alternates(void *v)
1421 size_t l;
1422 char **namelist = v, **ap, **ap2, *cp;
1423 NYD_ENTER;
1425 for (namelist = v, l = 0; namelist[l] != NULL; ++l)
1428 if (l == 0) {
1429 if (_altnames != NULL) {
1430 printf("alternates ");
1431 for (ap = _altnames; *ap != NULL; ++ap)
1432 printf("%s ", *ap);
1433 printf("\n");
1435 goto jleave;
1438 if (_altnames != NULL) {
1439 for (ap = _altnames; *ap != NULL; ++ap)
1440 free(*ap);
1441 free(_altnames);
1444 ++l;
1445 _altnames = smalloc(l * sizeof(*_altnames));
1446 for (ap = namelist, ap2 = _altnames; *ap != NULL; ++ap, ++ap2) {
1447 l = strlen(*ap) +1;
1448 cp = smalloc(l);
1449 memcpy(cp, *ap, l);
1450 *ap2 = cp;
1452 *ap2 = NULL;
1453 jleave:
1454 NYD_LEAVE;
1455 return 0;
1458 FL struct name *
1459 delete_alternates(struct name *np)
1461 struct name *xp;
1462 char **ap;
1463 NYD_ENTER;
1465 np = delname(np, myname);
1466 if (_altnames != NULL)
1467 for (ap = _altnames; *ap != '\0'; ++ap)
1468 np = delname(np, *ap);
1470 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1471 while (xp != NULL) {
1472 np = delname(np, xp->n_name);
1473 xp = xp->n_flink;
1476 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1477 while (xp != NULL) {
1478 np = delname(np, xp->n_name);
1479 xp = xp->n_flink;
1482 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1483 while (xp != NULL) {
1484 np = delname(np, xp->n_name);
1485 xp = xp->n_flink;
1487 NYD_LEAVE;
1488 return np;
1491 FL int
1492 is_myname(char const *name)
1494 int rv = 1;
1495 struct name *xp;
1496 char **ap;
1497 NYD_ENTER;
1499 if (_same_name(myname, name))
1500 goto jleave;
1501 if (_altnames != NULL)
1502 for (ap = _altnames; *ap != NULL; ++ap)
1503 if (_same_name(*ap, name))
1504 goto jleave;
1506 if ((xp = lextract(ok_vlook(from), GEXTRA | GSKIN)) != NULL)
1507 while (xp != NULL) {
1508 if (_same_name(xp->n_name, name))
1509 goto jleave;
1510 xp = xp->n_flink;
1513 if ((xp = lextract(ok_vlook(replyto), GEXTRA | GSKIN)) != NULL)
1514 while (xp != NULL) {
1515 if (_same_name(xp->n_name, name))
1516 goto jleave;
1517 xp = xp->n_flink;
1520 if ((xp = extract(ok_vlook(sender), GEXTRA | GSKIN)) != NULL)
1521 while (xp != NULL) {
1522 if (_same_name(xp->n_name, name))
1523 goto jleave;
1524 xp = xp->n_flink;
1526 rv = 0;
1527 jleave:
1528 NYD_LEAVE;
1529 return rv;
1532 FL int
1533 c_alias(void *v)
1535 char **argv = v;
1536 struct group *gp;
1537 int rv = 0;
1538 NYD_ENTER;
1540 if (*argv == NULL)
1541 _group_print_all(GT_ALIAS);
1542 else if (argv[1] == NULL) {
1543 if ((gp = _group_find(GT_ALIAS, *argv)) != NULL)
1544 _group_print(gp, stdout);
1545 else {
1546 n_err(_("No such alias: \"%s\"\n"), *argv);
1547 rv = 1;
1549 } else {
1550 struct grp_names_head *gnhp;
1552 gp = _group_fetch(GT_ALIAS, *argv, 0);
1553 GP_TO_SUBCLASS(gnhp, gp);
1555 for (++argv; *argv != NULL; ++argv) {
1556 size_t l = strlen(*argv) +1;
1557 struct grp_names *gnp = smalloc(sizeof(*gnp) -
1558 VFIELD_SIZEOF(struct grp_names, gn_id) + l);
1559 gnp->gn_next = gnhp->gnh_head;
1560 gnhp->gnh_head = gnp;
1561 memcpy(gnp->gn_id, *argv, l);
1563 assert(gnhp->gnh_head != NULL);
1565 NYD_LEAVE;
1566 return rv;
1569 FL int
1570 c_unalias(void *v)
1572 char **argv = v;
1573 int rv = 0;
1574 NYD_ENTER;
1576 do if (!_group_del(GT_ALIAS, *argv)) {
1577 n_err(_("No such alias: \"%s\"\n"), *argv);
1578 rv = 1;
1579 } while (*++argv != NULL);
1580 NYD_LEAVE;
1581 return rv;
1584 FL int
1585 c_mlist(void *v)
1587 int rv;
1588 NYD_ENTER;
1590 rv = _mlmux(GT_MLIST, v);
1591 NYD_LEAVE;
1592 return rv;
1595 FL int
1596 c_unmlist(void *v)
1598 int rv;
1599 NYD_ENTER;
1601 rv = _unmlmux(GT_MLIST, v);
1602 NYD_LEAVE;
1603 return rv;
1606 FL int
1607 c_mlsubscribe(void *v)
1609 int rv;
1610 NYD_ENTER;
1612 rv = _mlmux(GT_MLIST | GT_SUBSCRIBE, v);
1613 NYD_LEAVE;
1614 return rv;
1617 FL int
1618 c_unmlsubscribe(void *v)
1620 int rv;
1621 NYD_ENTER;
1623 rv = _unmlmux(GT_MLIST | GT_SUBSCRIBE, v);
1624 NYD_LEAVE;
1625 return rv;
1628 FL enum mlist_state
1629 is_mlist(char const *name, bool_t subscribed_only)
1631 struct group *gp;
1632 #ifdef HAVE_REGEX
1633 struct grp_regex **lpp, *grp;
1634 bool_t re2;
1635 #endif
1636 enum mlist_state rv;
1637 NYD_ENTER;
1639 gp = _group_find(GT_MLIST, name);
1640 rv = (gp != NULL) ? MLIST_KNOWN : MLIST_OTHER;
1641 if (rv == MLIST_KNOWN) {
1642 if (gp->g_type & GT_SUBSCRIBE)
1643 rv = MLIST_SUBSCRIBED;
1644 else if (subscribed_only)
1645 rv = MLIST_OTHER;
1646 /* Of course, if that is a regular expression it doesn't mean a thing */
1647 #ifdef HAVE_REGEX
1648 if (gp->g_type & GT_REGEX)
1649 rv = MLIST_OTHER;
1650 else
1651 #endif
1652 goto jleave;
1655 /* Not in the hashmap (as something matchable), walk the lists */
1656 #ifdef HAVE_REGEX
1657 re2 = FAL0;
1658 lpp = &_mlsub_regex;
1659 jregex_redo:
1660 if ((grp = *lpp) != NULL) {
1661 do if (regexec(&grp->gr_regex, name, 0,NULL, 0) != REG_NOMATCH) {
1662 /* Relink as the head of this list if the hit count of this group is
1663 * >= 25% of the average hit count */
1664 size_t i;
1665 if (!re2)
1666 i = ++_mlsub_hits / _mlsub_size;
1667 else
1668 i = ++_mlist_hits / _mlist_size;
1669 i >>= 2;
1671 if (++grp->gr_hits >= i && *lpp != grp && grp->gr_next != grp) {
1672 grp->gr_last->gr_next = grp->gr_next;
1673 grp->gr_next->gr_last = grp->gr_last;
1674 (grp->gr_last = (*lpp)->gr_last)->gr_next = grp;
1675 (grp->gr_next = *lpp)->gr_last = grp;
1676 *lpp = grp;
1678 rv = !re2 ? MLIST_SUBSCRIBED : MLIST_KNOWN;
1679 goto jleave;
1680 } while ((grp = grp->gr_next) != *lpp);
1682 if (!re2 && !subscribed_only) {
1683 re2 = TRU1;
1684 lpp = &_mlist_regex;
1685 goto jregex_redo;
1687 assert(rv == MLIST_OTHER);
1688 #endif
1690 jleave:
1691 NYD_LEAVE;
1692 return rv;
1695 FL int
1696 c_shortcut(void *v)
1698 char **argv = v;
1699 int rv = 0;
1700 NYD_ENTER;
1702 if (*argv == NULL)
1703 _group_print_all(GT_SHORTCUT);
1704 else for (; *argv != NULL; argv += 2) {
1705 /* Because one hardly ever redefines, anything is stored in one chunk */
1706 size_t l;
1707 struct group *gp;
1708 char *cp;
1710 if (argv[1] == NULL) {
1711 n_err(_("Shortcut expansion is missing: \"%s\"\n"), *argv);
1712 rv = 1;
1713 break;
1715 if (_group_find(GT_SHORTCUT, *argv) != NULL)
1716 _group_del(GT_SHORTCUT, *argv);
1718 l = strlen(argv[1]) +1;
1719 gp = _group_fetch(GT_SHORTCUT, *argv, l);
1720 GP_TO_SUBCLASS(cp, gp);
1721 memcpy(cp, argv[1], l);
1723 NYD_LEAVE;
1724 return rv;
1727 FL int
1728 c_unshortcut(void *v)
1730 char **argv = v;
1731 int rv = 0;
1732 NYD_ENTER;
1734 do if (!_group_del(GT_SHORTCUT, *argv)) {
1735 n_err(_("No such shortcut: \"%s\"\n"), *argv);
1736 rv = 1;
1737 } while (*++argv != NULL);
1738 NYD_LEAVE;
1739 return rv;
1742 FL char const *
1743 shortcut_expand(char const *str)
1745 struct group *gp;
1746 NYD_ENTER;
1748 if ((gp = _group_find(GT_SHORTCUT, str)) != NULL)
1749 GP_TO_SUBCLASS(str, gp);
1750 else
1751 str = NULL;
1752 NYD_LEAVE;
1753 return str;
1756 FL int
1757 c_customhdr(void *v){
1758 struct group *gp;
1759 char const **argv, *hcp;
1760 int rv;
1761 NYD_ENTER;
1763 rv = 0;
1765 if((hcp = *(argv = v)) == NULL){
1766 _group_print_all(GT_CUSTOMHDR);
1767 goto jleave;
1770 /* Verify the header field name */
1771 for(rv = 1;; ++hcp){
1772 if(fieldnamechar(*hcp))
1773 continue;
1774 if(*hcp == '\0'){
1775 if(hcp == *argv){
1776 n_err(_("`customhdr': no name specified\n"));
1777 goto jleave;
1779 hcp = *argv;
1780 break;
1782 n_err(_("`customhdr': invalid name: \"%s\"\n"), *argv);
1783 goto jleave;
1785 rv = 0;
1787 if(*++argv == NULL){
1788 if((gp = _group_find(GT_CUSTOMHDR, hcp)) != NULL)
1789 _group_print(gp, stdout);
1790 else{
1791 n_err(_("No such `customhdr': \"%s\"\n"), hcp);
1792 rv = 1;
1794 }else{
1795 /* Because one hardly ever redefines, anything is stored in one chunk */
1796 size_t i, l;
1797 char *cp;
1799 if(pstate & PS_WYSHLIST_SAW_CONTROL){
1800 n_err(_("`customhdr': \"%s\": control characters not allowed: %s%s\n"),
1801 hcp, n_shell_quote_cp(*argv, FAL0), (argv[1] != NULL ? "..." : ""));
1802 rv = 1;
1803 goto jleave;
1806 if(_group_find(GT_CUSTOMHDR, hcp) != NULL)
1807 _group_del(GT_CUSTOMHDR, hcp);
1809 for(l = i = 0; argv[i] != NULL; ++i)
1810 l += strlen(argv[i]) + 1;
1811 gp = _group_fetch(GT_CUSTOMHDR, hcp, l +1);
1812 GP_TO_SUBCLASS(cp, gp);
1813 for(i = 0; argv[i] != NULL; ++i){
1814 if(i > 0)
1815 *cp++ = ' ';
1816 l = strlen(argv[i]);
1817 memcpy(cp, argv[i], l);
1818 cp += l;
1820 *cp = '\0';
1822 jleave:
1823 NYD_LEAVE;
1824 return rv;
1827 FL int
1828 c_uncustomhdr(void *v){
1829 int rv;
1830 char **argv;
1831 NYD_ENTER;
1833 rv = 0;
1835 argv = v;
1837 if(!_group_del(GT_CUSTOMHDR, *argv)){
1838 n_err(_("No such custom header: \"%s\"\n"), *argv);
1839 rv = 1;
1841 }while(*++argv != NULL);
1842 NYD_LEAVE;
1843 return rv;
1846 FL struct n_header_field *
1847 n_customhdr_query(void){ /* XXX Uses salloc()! */
1848 struct group const *gp;
1849 ui32_t h;
1850 struct n_header_field *rv, **tail, *hfp;
1851 NYD_ENTER;
1853 rv = NULL;
1854 tail = &rv;
1856 for(h = 0; h < HSHSIZE; ++h)
1857 for(gp = _customhdr_heads[h]; gp != NULL; gp = gp->g_next){
1858 char const *cp;
1859 ui32_t nl, bl;
1861 GP_TO_SUBCLASS(cp, gp);
1862 nl = (ui32_t)strlen(gp->g_id) +1;
1863 bl = (ui32_t)strlen(cp) +1;
1865 *tail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
1866 nl + bl);
1867 tail = &hfp->hf_next;
1868 hfp->hf_next = NULL;
1869 hfp->hf_nl = nl - 1;
1870 hfp->hf_bl = bl - 1;
1871 memcpy(hfp->hf_dat, gp->g_id, nl);
1872 memcpy(hfp->hf_dat + nl, cp, bl);
1875 /* TODO We have no copy-on-write environments yet, and since custom headers
1876 * TODO are a perfect subject for localization, add the OBSOLETE variable
1877 * TODO *customhdr*, too */
1879 char const *vp = ok_vlook(customhdr);
1881 if(vp != NULL){
1882 char *buf = savestr(vp);
1883 jch_outer:
1884 while((vp = a_nag_custom_sep(&buf)) != NULL){
1885 ui32_t nl, bl;
1886 char const *nstart, *cp;
1888 for(nstart = cp = vp;; ++cp){
1889 if(fieldnamechar(*cp))
1890 continue;
1891 if(*cp == '\0'){
1892 if(cp == nstart){
1893 n_err(_("Invalid nameless *customhdr* entry\n"));
1894 goto jch_outer;
1896 }else if(*cp != ':' && !blankchar(*cp)){
1897 jch_badent:
1898 n_err(_("Invalid *customhdr* entry: \"%s\"\n"), vp);
1899 goto jch_outer;
1901 break;
1903 nl = (ui32_t)PTR2SIZE(cp - nstart);
1905 while(blankchar(*cp))
1906 ++cp;
1907 if(*cp++ != ':')
1908 goto jch_badent;
1909 while(blankchar(*cp))
1910 ++cp;
1911 bl = (ui32_t)strlen(cp) +1;
1913 *tail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
1914 nl +1 + bl);
1915 tail = &hfp->hf_next;
1916 hfp->hf_next = NULL;
1917 hfp->hf_nl = nl;
1918 hfp->hf_bl = bl - 1;
1919 memcpy(hfp->hf_dat, nstart, nl);
1920 hfp->hf_dat[nl++] = '\0';
1921 memcpy(hfp->hf_dat + nl, cp, bl);
1925 NYD_LEAVE;
1926 return rv;
1929 /* s-it-mode */