Add IDNA support..
[s-mailx.git] / names.c
blob90d016842078faf6c1d06b155fdfc6712fd3253f
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
41 * Mail -- a mail program
43 * Handle name lists.
46 #include "rcv.h"
47 #include "extern.h"
49 #include <errno.h>
51 #include <fcntl.h>
52 #include <time.h>
53 #include <unistd.h>
54 #include <sys/stat.h>
56 static struct name * tailof(struct name *name);
57 static struct name * extract1(char *line, enum gfield ntype,
58 char *separators, int copypfx);
59 static char * yankword(char *ap, char *wbuf, char *separators,
60 int copypfx);
61 static int same_name(char *n1, char *n2);
62 static struct name * gexpand(struct name *nlist, struct grouphead *gh,
63 int metoo, int ntype);
64 static struct name * put(struct name *list, struct name *node);
65 static struct name * delname(struct name *np, char *name);
68 * Allocate a single element of a name list, initialize its name field to the
69 * passed name and return it.
71 struct name *
72 nalloc(char *str, enum gfield ntype)
74 struct addrguts ag;
75 struct str in, out;
76 struct name *np;
78 np = (struct name*)salloc(sizeof *np);
79 np->n_flink = NULL;
80 np->n_blink = NULL;
81 np->n_type = ntype;
82 np->n_flags = 0;
84 (void)addrspec_with_guts((ntype & (GFULL|GSKIN|GREF)) != 0, str, &ag);
85 if ((ag.ag_n_flags & NAME_NAME_SALLOC) == 0) {
86 ag.ag_n_flags |= NAME_NAME_SALLOC;
87 ag.ag_skinned = savestrbuf(ag.ag_skinned, ag.ag_slen);
89 np->n_fullname = np->n_name = ag.ag_skinned;
90 np->n_flags = ag.ag_n_flags;
92 if (ntype & GFULL) {
93 if (ag.ag_ilen == ag.ag_slen
94 #ifdef USE_IDNA
95 && (ag.ag_n_flags & NAME_IDNA) == 0
96 #endif
98 goto jleave;
99 if (ag.ag_n_flags & NAME_ADDRSPEC_ISFILEORPIPE)
100 goto jleave;
101 #ifdef USE_IDNA
102 if ((ag.ag_n_flags & NAME_IDNA) == 0) {
103 #endif
104 in.s = str;
105 in.l = ag.ag_ilen;
106 #ifdef USE_IDNA
107 } else {
109 * The domain name was IDNA and has been converted.
110 * We also have to ensure that the domain name in
111 * .n_fullname is replaced with the converted version,
112 * since MIME doesn't perform encoding of addresses.
114 size_t l = ag.ag_iaddr_start,
115 lsuff = ag.ag_ilen - ag.ag_iaddr_end;
116 in.s = ac_alloc(l + ag.ag_slen + lsuff + 1);
117 memcpy(in.s, str, l);
118 memcpy(in.s + l, ag.ag_skinned, ag.ag_slen);
119 l += ag.ag_slen;
120 memcpy(in.s + l, str + ag.ag_iaddr_end, lsuff);
121 l += lsuff;
122 in.s[l] = '\0';
123 in.l = l;
125 #endif
126 mime_fromhdr(&in, &out, TD_ISPR|TD_ICONV);
127 np->n_fullname = savestr(out.s);
128 free(out.s);
129 #ifdef USE_IDNA
130 if (ag.ag_n_flags & NAME_IDNA)
131 ac_free(in.s);
132 #endif
133 np->n_flags |= NAME_FULLNAME_SALLOC;
134 } else if (ntype & GREF) { /* TODO LEGACY */
135 /* TODO Unfortunately we had to skin GREFerences i.e. the
136 * TODO surrounding angle brackets have been stripped away.
137 * TODO Necessarily since otherwise the plain address check
138 * TODO fails due to them; insert them back so that valid
139 * TODO headers will be created */
140 np->n_fullname = np->n_name = str = salloc(ag.ag_slen + 2 + 1);
141 *(str++) = '<';
142 memcpy(str, ag.ag_skinned, ag.ag_slen);
143 str += ag.ag_slen;
144 *(str++) = '>';
145 *str = '\0';
147 jleave:
148 return (np);
151 struct name *
152 ndup(struct name *np, enum gfield ntype)
154 struct name *nnp;
156 if ((ntype & (GFULL|GSKIN)) && (np->n_flags & NAME_SKINNED) == 0) {
157 nnp = nalloc(np->n_name, ntype);
158 goto jleave;
161 nnp = (struct name*)salloc(sizeof *np);
162 nnp->n_flink = nnp->n_blink = NULL;
163 nnp->n_type = ntype;
164 nnp->n_flags = (np->n_flags &
165 ~(NAME_NAME_SALLOC | NAME_FULLNAME_SALLOC)) |
166 NAME_NAME_SALLOC;
167 nnp->n_name = savestr(np->n_name);
168 if (np->n_name == np->n_fullname || (ntype & (GFULL|GSKIN)) == 0)
169 nnp->n_fullname = nnp->n_name;
170 else {
171 nnp->n_flags |= NAME_FULLNAME_SALLOC;
172 nnp->n_fullname = savestr(np->n_fullname);
174 jleave:
175 return (nnp);
179 * Find the tail of a list and return it.
181 static struct name *
182 tailof(struct name *name)
184 struct name *np;
186 np = name;
187 if (np == NULL)
188 return(NULL);
189 while (np->n_flink != NULL)
190 np = np->n_flink;
191 return(np);
195 * Extract a list of names from a line,
196 * and make a list of names from it.
197 * Return the list or NULL if none found.
199 struct name *
200 extract(char *line, enum gfield ntype)
202 return extract1(line, ntype, " \t,(", 0);
205 struct name *
206 sextract(char *line, enum gfield ntype)
208 if (line && strpbrk(line, ",\"\\(<"))
209 return extract1(line, ntype, ",", 1);
210 else
211 return extract(line, ntype);
214 struct name *
215 lextract(char *line, enum gfield ntype)
217 return (extract1(line, ntype, ",", 1));
220 static struct name *
221 extract1(char *line, enum gfield ntype, char *separators, int copypfx)
223 char *cp, *nbuf;
224 struct name *top, *np, *t;
226 if (line == NULL || *line == '\0')
227 return NULL;
228 top = NULL;
229 np = NULL;
230 cp = line;
231 nbuf = ac_alloc(strlen(line) + 1);
232 while ((cp = yankword(cp, nbuf, separators, copypfx)) != NULL) {
233 t = nalloc(nbuf, ntype);
234 if (top == NULL)
235 top = t;
236 else
237 np->n_flink = t;
238 t->n_blink = np;
239 np = t;
241 ac_free(nbuf);
242 return top;
246 * Turn a list of names into a string of the same names.
248 char *
249 detract(struct name *np, enum gfield ntype)
251 int s;
252 char *cp, *top;
253 struct name *p;
254 int comma;
256 comma = ntype & GCOMMA;
257 if (np == NULL)
258 return(NULL);
259 ntype &= ~GCOMMA;
260 s = 0;
261 if ((debug || value("debug")) && comma)
262 fprintf(stderr, catgets(catd, CATSET, 145,
263 "detract asked to insert commas\n"));
264 for (p = np; p != NULL; p = p->n_flink) {
265 if (ntype && (p->n_type & GMASK) != ntype)
266 continue;
267 s += strlen(p->n_fullname) + 1;
268 if (comma)
269 s++;
271 if (s == 0)
272 return(NULL);
273 s += 2;
274 top = salloc(s);
275 cp = top;
276 for (p = np; p != NULL; p = p->n_flink) {
277 if (ntype && (p->n_type & GMASK) != ntype)
278 continue;
279 cp = sstpcpy(cp, p->n_fullname);
280 if (comma && p->n_flink != NULL)
281 *cp++ = ',';
282 *cp++ = ' ';
284 *--cp = 0;
285 if (comma && *--cp == ',')
286 *cp = 0;
287 return(top);
291 * Grab a single word (liberal word)
292 * Throw away things between ()'s, and take anything between <>.
293 * Strip trailing whitespace as *ap* may come directly from user.
295 static char *
296 yankword(char *ap, char *wbuf, char *separators, int copypfx)
298 char *cp, *pp, *wp;
300 cp = ap;
301 wp = wbuf;
302 while (blankspacechar(*cp) || *cp == ',')
303 ++cp;
304 pp = cp;
305 if ((cp = (char*)nexttoken((char*)cp)) == NULL)
306 return NULL;
307 if (copypfx)
308 while (pp < cp)
309 *wp++ = *pp++;
310 if (*cp == '<')
311 while (*cp && (*wp++ = *cp++) != '>');
312 else {
313 int incomm = 0;
315 while (*cp && (incomm || !strchr(separators, *cp))) {
316 if (*cp == '\"') {
317 if (cp == ap || *(cp - 1) != '\\') {
318 if (incomm)
319 incomm--;
320 else
321 incomm++;
322 *wp++ = '\"';
323 } else if (cp != ap) {
324 *(wp - 1) = '\"';
326 cp++;
327 continue;
329 *wp++ = *cp++;
332 while (wp > wbuf && blankspacechar(wp[-1]))
333 --wp;
334 *wp = '\0';
335 return cp;
339 * Check all addresses in np and delete invalid ones.
341 struct name *
342 checkaddrs(struct name *np)
344 struct name *n;
346 for (n = np; n != NULL;) {
347 if (is_addr_invalid(n, 1)) {
348 if (n->n_blink)
349 n->n_blink->n_flink = n->n_flink;
350 if (n->n_flink)
351 n->n_flink->n_blink = n->n_blink;
352 if (n == np)
353 np = n->n_flink;
355 n = n->n_flink;
357 return (np);
361 * For each recipient in the passed name list with a /
362 * in the name, append the message to the end of the named file
363 * and remove him from the recipient list.
365 * Recipients whose name begins with | are piped through the given
366 * program and removed.
368 struct name *
369 outof(struct name *names, FILE *fo, struct header *hp)
371 int pipecnt, xcnt, *fda, i;
372 char *shell, *date;
373 struct name *np;
374 time_t now;
375 FILE *fin = NULL, *fout;
376 (void)hp;
379 * Look through all recipients and do a quick return if no file or pipe
380 * addressee is found.
382 fda = NULL; /* Silence cc */
383 for (pipecnt = xcnt = 0, np = names; np != NULL; np = np->n_flink)
384 switch (np->n_flags & NAME_ADDRSPEC_ISFILEORPIPE) {
385 case NAME_ADDRSPEC_ISFILE:
386 ++xcnt;
387 break;
388 case NAME_ADDRSPEC_ISPIPE:
389 ++pipecnt;
390 break;
392 if (pipecnt == 0 && xcnt == 0)
393 goto jleave;
396 * Otherwise create an array of file descriptors for each found pipe
397 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
398 * each pipe subprocess needs its very own file descriptor, and we need
399 * to deal with that.
400 * To make our life a bit easier let's just use the auto-reclaimed
401 * string storage.
403 if (pipecnt == 0) {
404 fda = NULL;
405 shell = NULL;
406 } else {
407 fda = (int*)salloc(sizeof(int) * pipecnt);
408 for (i = 0; i < pipecnt; ++i)
409 fda[i] = -1;
410 if ((shell = value("SHELL")) == NULL)
411 shell = SHELL;
414 time(&now);
415 date = ctime(&now);
417 for (np = names; np != NULL;) {
418 if ((np->n_flags & (NAME_ADDRSPEC_ISFILE|NAME_ADDRSPEC_ISPIPE))
419 == 0) {
420 np = np->n_flink;
421 continue;
425 * See if we have copied the complete message out yet.
426 * If not, do so.
428 if (image < 0) {
429 int c;
430 char *tempEdit;
432 if ((fout = Ftemp(&tempEdit, "Re", "w", 0600, 1))
433 == NULL) {
434 perror(tr(146, "Creation of temporary image"));
435 ++senderr;
436 goto jcant;
438 image = open(tempEdit, O_RDWR);
439 if (image >= 0)
440 for (i = 0; i < pipecnt; ++i) {
441 int fd = open(tempEdit, O_RDONLY);
442 if (fd < 0) {
443 (void)close(image);
444 image = -1;
445 pipecnt = i;
446 break;
448 fda[i] = fd;
449 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
451 unlink(tempEdit);
452 Ftfree(&tempEdit);
453 if (image < 0) {
454 perror(tr(147, "Creating descriptor duplicate "
455 "of temporary image"));
456 ++senderr;
457 Fclose(fout);
458 goto jcant;
460 fcntl(image, F_SETFD, FD_CLOEXEC);
462 fprintf(fout, "From %s %s", myname, date);
463 c = EOF;
464 while (i = c, (c = getc(fo)) != EOF)
465 putc(c, fout);
466 rewind(fo);
467 if (i != '\n')
468 putc('\n', fout);
469 putc('\n', fout);
470 fflush(fout);
471 if (ferror(fout)) {
472 perror(tr(148, "Finalizing write of temporary "
473 "image"));
474 Fclose(fout);
475 goto jcantfout;
477 Fclose(fout);
479 /* If we have to serve file addressees, open reader */
480 if (xcnt != 0 && (fin = Fdopen(image, "r")) == NULL) {
481 perror(tr(149, "Failed to open a duplicate of "
482 "the temporary image"));
483 jcantfout: ++senderr;
484 (void)close(image);
485 image = -1;
486 goto jcant;
489 /* From now on use xcnt as a counter for pipecnt */
490 xcnt = 0;
494 * Now either copy "image" to the desired file
495 * or give it as the standard input to the desired
496 * program as appropriate.
499 if (np->n_flags & NAME_ADDRSPEC_ISPIPE) {
500 int pid;
501 sigset_t nset;
503 sigemptyset(&nset);
504 sigaddset(&nset, SIGHUP);
505 sigaddset(&nset, SIGINT);
506 sigaddset(&nset, SIGQUIT);
507 pid = start_command(shell, &nset,
508 fda[xcnt++], -1, "-c", np->n_name + 1, NULL);
509 if (pid < 0) {
510 fprintf(stderr, tr(281,
511 "Message piping to <%s> failed\n"),
512 np->n_name);
513 ++senderr;
514 goto jcant;
516 free_child(pid);
517 } else {
518 char *fname = file_expand(np->n_name);
519 if (fname == NULL) {
520 fprintf(stderr, tr(81,
521 "\"%s\": Expansion failed.\n"),
522 np->n_name);
523 ++senderr;
524 goto jcant;
526 if ((fout = Zopen(fname, "a", NULL)) == NULL) {
527 fprintf(stderr, tr(282,
528 "Message writing to <%s> failed: %s\n"),
529 fname, strerror(errno));
530 ++senderr;
531 goto jcant;
533 rewind(fin);
534 while ((i = getc(fin)) != EOF)
535 putc(i, fout);
536 if (ferror(fout)) {
537 fprintf(stderr, tr(282,
538 "Message writing to <%s> failed: %s\n"),
539 fname, tr(283, "write error"));
540 ++senderr;
542 Fclose(fout);
544 jcant:
546 * In days of old we removed the entry from the
547 * the list; now for sake of header expansion
548 * we leave it in and mark it as deleted.
550 np->n_type |= GDEL;
551 np = np->n_flink;
552 if (image < 0)
553 goto jdelall;
555 jleave:
556 if (fin != NULL)
557 Fclose(fin);
558 for (i = 0; i < pipecnt; ++i)
559 (void)close(fda[i]);
560 if (image >= 0) {
561 close(image);
562 image = -1;
564 return (names);
566 jdelall:
567 while (np != NULL) {
568 if ((np->n_flags & (NAME_ADDRSPEC_ISFILE|NAME_ADDRSPEC_ISPIPE))
569 != 0)
570 np->n_type |= GDEL;
571 np = np->n_flink;
573 goto jleave;
576 static int
577 same_name(char *n1, char *n2)
579 int c1, c2;
581 if (value("allnet") != NULL) {
582 do {
583 c1 = (*n1++ & 0377);
584 c2 = (*n2++ & 0377);
585 c1 = lowerconv(c1);
586 c2 = lowerconv(c2);
587 if (c1 != c2)
588 return 0;
589 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
590 return 1;
591 } else
592 return asccasecmp(n1, n2) == 0;
596 * Map all of the aliased users in the invoker's mailrc
597 * file and insert them into the list.
598 * Changed after all these months of service to recursively
599 * expand names (2/14/80).
602 struct name *
603 usermap(struct name *names)
605 struct name *new, *np, *cp;
606 struct grouphead *gh;
607 int metoo;
609 new = NULL;
610 np = names;
611 metoo = (value("metoo") != NULL);
612 while (np != NULL) {
613 if (np->n_name[0] == '\\') {
614 cp = np->n_flink;
615 new = put(new, np);
616 np = cp;
617 continue;
619 gh = findgroup(np->n_name);
620 cp = np->n_flink;
621 if (gh != NULL)
622 new = gexpand(new, gh, metoo, np->n_type);
623 else
624 new = put(new, np);
625 np = cp;
627 return(new);
631 * Recursively expand a group name. We limit the expansion to some
632 * fixed level to keep things from going haywire.
633 * Direct recursion is not expanded for convenience.
636 static struct name *
637 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
639 struct group *gp;
640 struct grouphead *ngh;
641 struct name *np;
642 static int depth;
643 char *cp;
645 if (depth > MAXEXP) {
646 printf(catgets(catd, CATSET, 150,
647 "Expanding alias to depth larger than %d\n"), MAXEXP);
648 return(nlist);
650 depth++;
651 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
652 cp = gp->ge_name;
653 if (*cp == '\\')
654 goto quote;
655 if (strcmp(cp, gh->g_name) == 0)
656 goto quote;
657 if ((ngh = findgroup(cp)) != NULL) {
658 nlist = gexpand(nlist, ngh, metoo, ntype);
659 continue;
661 quote:
662 np = nalloc(cp, ntype|GFULL);
664 * At this point should allow to expand
665 * to self if only person in group
667 if (gp == gh->g_list && gp->ge_link == NULL)
668 goto skip;
669 if (!metoo && same_name(cp, myname))
670 np->n_type |= GDEL;
671 skip:
672 nlist = put(nlist, np);
674 depth--;
675 return(nlist);
679 * Concatenate the two passed name lists, return the result.
681 struct name *
682 cat(struct name *n1, struct name *n2)
684 struct name *tail;
686 if (n1 == NULL)
687 return(n2);
688 if (n2 == NULL)
689 return(n1);
690 tail = tailof(n1);
691 tail->n_flink = n2;
692 n2->n_blink = tail;
693 return(n1);
697 * Unpack the name list onto a vector of strings.
698 * Return an error if the name list won't fit.
700 char **
701 unpack(struct name *np)
703 char **ap, **top;
704 struct name *n;
705 int t, extra, metoo, verbose;
707 n = np;
708 if ((t = count(n)) == 0)
709 panic(catgets(catd, CATSET, 151, "No names to unpack"));
711 * Compute the number of extra arguments we will need.
712 * We need at least two extra -- one for "mail" and one for
713 * the terminating 0 pointer. Additional spots may be needed
714 * to pass along -f to the host mailer.
716 extra = 2;
717 extra++;
718 metoo = value("metoo") != NULL;
719 if (metoo)
720 extra++;
721 verbose = value("verbose") != NULL;
722 if (verbose)
723 extra++;
724 /*LINTED*/
725 top = (char **)salloc((t + extra) * sizeof *top);
726 ap = top;
727 *ap++ = "send-mail";
728 *ap++ = "-i";
729 if (metoo)
730 *ap++ = "-m";
731 if (verbose)
732 *ap++ = "-v";
733 for (; n != NULL; n = n->n_flink)
734 if ((n->n_type & GDEL) == 0)
735 *ap++ = n->n_name;
736 *ap = NULL;
737 return(top);
741 * Remove all of the duplicates from the passed name list by
742 * insertion sorting them, then checking for dups.
743 * Return the head of the new list.
745 struct name *
746 elide(struct name *names)
748 struct name *np, *t, *newn, *x;
750 if (names == NULL)
751 return (NULL);
752 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
753 for (newn = np = NULL; names != NULL; names = names->n_flink)
754 if ((names->n_type & GDEL) == 0) {
755 names->n_blink = np;
756 if (np)
757 np->n_flink = names;
758 else
759 newn = names;
760 np = names;
762 if (newn == NULL)
763 return (NULL);
765 np = newn->n_flink;
766 if (np != NULL)
767 np->n_blink = NULL;
768 newn->n_flink = NULL;
770 while (np != NULL) {
771 t = newn;
772 while (asccasecmp(t->n_name, np->n_name) < 0) {
773 if (t->n_flink == NULL)
774 break;
775 t = t->n_flink;
779 * If we ran out of t's, put the new entry after
780 * the current value of t.
783 if (asccasecmp(t->n_name, np->n_name) < 0) {
784 t->n_flink = np;
785 np->n_blink = t;
786 t = np;
787 np = np->n_flink;
788 t->n_flink = NULL;
789 continue;
793 * Otherwise, put the new entry in front of the
794 * current t. If at the front of the list,
795 * the new guy becomes the new head of the list.
798 if (t == newn) {
799 t = np;
800 np = np->n_flink;
801 t->n_flink = newn;
802 newn->n_blink = t;
803 t->n_blink = NULL;
804 newn = t;
805 continue;
809 * The normal case -- we are inserting into the
810 * middle of the list.
813 x = np;
814 np = np->n_flink;
815 x->n_flink = t;
816 x->n_blink = t->n_blink;
817 t->n_blink->n_flink = x;
818 t->n_blink = x;
822 * Now the list headed up by new is sorted.
823 * Go through it and remove duplicates.
826 np = newn;
827 while (np != NULL) {
828 t = np;
829 while (t->n_flink != NULL &&
830 asccasecmp(np->n_name, t->n_flink->n_name) == 0)
831 t = t->n_flink;
832 if (t == np || t == NULL) {
833 np = np->n_flink;
834 continue;
838 * Now t points to the last entry with the same name
839 * as np. Make np point beyond t.
842 np->n_flink = t->n_flink;
843 if (t->n_flink != NULL)
844 t->n_flink->n_blink = np;
845 np = np->n_flink;
847 return (newn);
851 * Put another node onto a list of names and return
852 * the list.
854 static struct name *
855 put(struct name *list, struct name *node)
857 node->n_flink = list;
858 node->n_blink = NULL;
859 if (list != NULL)
860 list->n_blink = node;
861 return(node);
865 * Determine the number of undeleted elements in
866 * a name list and return it.
868 int
869 count(struct name *np)
871 int c;
873 for (c = 0; np != NULL; np = np->n_flink)
874 if ((np->n_type & GDEL) == 0)
875 c++;
876 return c;
880 * Delete the given name from a namelist.
882 static struct name *
883 delname(struct name *np, char *name)
885 struct name *p;
887 for (p = np; p != NULL; p = p->n_flink)
888 if (same_name(p->n_name, name)) {
889 if (p->n_blink == NULL) {
890 if (p->n_flink != NULL)
891 p->n_flink->n_blink = NULL;
892 np = p->n_flink;
893 continue;
895 if (p->n_flink == NULL) {
896 if (p->n_blink != NULL)
897 p->n_blink->n_flink = NULL;
898 continue;
900 p->n_blink->n_flink = p->n_flink;
901 p->n_flink->n_blink = p->n_blink;
903 return np;
907 * Pretty print a name list
908 * Uncomment it if you need it.
912 void
913 prettyprint(struct name *name)
915 struct name *np;
917 np = name;
918 while (np != NULL) {
919 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
920 np = np->n_flink;
922 fprintf(stderr, "\n");
926 struct name *
927 delete_alternates(struct name *np)
929 struct name *xp;
930 char **ap;
932 np = delname(np, myname);
933 if (altnames)
934 for (ap = altnames; *ap; ap++)
935 np = delname(np, *ap);
936 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
937 while (xp) {
938 np = delname(np, xp->n_name);
939 xp = xp->n_flink;
941 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
942 while (xp) {
943 np = delname(np, xp->n_name);
944 xp = xp->n_flink;
946 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
947 while (xp) {
948 np = delname(np, xp->n_name);
949 xp = xp->n_flink;
951 return np;
955 is_myname(char *name)
957 struct name *xp;
958 char **ap;
960 if (same_name(myname, name))
961 return 1;
962 if (altnames)
963 for (ap = altnames; *ap; ap++)
964 if (same_name(*ap, name))
965 return 1;
966 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
967 while (xp) {
968 if (same_name(xp->n_name, name))
969 return 1;
970 xp = xp->n_flink;
972 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
973 while (xp) {
974 if (same_name(xp->n_name, name))
975 return 1;
976 xp = xp->n_flink;
978 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
979 while (xp) {
980 if (same_name(xp->n_name, name))
981 return 1;
982 xp = xp->n_flink;
984 return 0;