Use skinned_name()
[s-mailx.git] / names.c
blob3518994f01bdc39ab98c244284e750bddde3b38b
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"
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <time.h>
51 #include <unistd.h>
53 static struct name *tailof(struct name *name);
54 static struct name *extract1(char *line, enum gfield ntype, char *separators,
55 int copypfx);
56 static char *yankword(char *ap, char *wbuf, char *separators, int copypfx);
57 static int same_name(char *n1, char *n2);
58 static struct name *gexpand(struct name *nlist, struct grouphead *gh,
59 int metoo, int ntype);
60 static struct name *put(struct name *list, struct name *node);
61 static struct name *delname(struct name *np, char *name);
64 * Allocate a single element of a name list,
65 * initialize its name field to the passed
66 * name and return it.
68 struct name *
69 nalloc(char *str, enum gfield ntype)
71 struct name *np;
72 struct str in, out;
74 /*LINTED*/
75 np = (struct name *)salloc(sizeof *np);
76 np->n_flink = NULL;
77 np->n_blink = NULL;
78 np->n_type = ntype;
79 np->n_flags = 0;
80 if (ntype & GFULL) {
81 np->n_name = savestr(skin(str));
82 if (strcmp(np->n_name, str)) {
83 in.s = str;
84 in.l = strlen(str);
85 mime_fromhdr(&in, &out, TD_ISPR|TD_ICONV);
86 np->n_fullname = savestr(out.s);
87 free(out.s);
88 } else
89 np->n_fullname = np->n_name;
90 } else if (ntype & GSKIN)
91 np->n_fullname = np->n_name = savestr(skin(str));
92 else
93 np->n_fullname = np->n_name = savestr(str);
94 return(np);
97 struct name *
98 ndup(struct name *np, enum gfield ntype)
100 struct name *nnp;
102 nnp = (struct name*)salloc(sizeof *np);
103 nnp->n_flink = NULL;
104 nnp->n_blink = NULL;
105 nnp->n_type = ntype;
106 nnp->n_flags = np->n_flags;
107 nnp->n_name = savestr(np->n_name);
108 nnp->n_fullname = (((ntype & (GFULL|GSKIN)) == 0) ||
109 np->n_name == np->n_fullname)
110 ? nnp->n_name : savestr(np->n_fullname);
111 return (nnp);
115 * Find the tail of a list and return it.
117 static struct name *
118 tailof(struct name *name)
120 struct name *np;
122 np = name;
123 if (np == NULL)
124 return(NULL);
125 while (np->n_flink != NULL)
126 np = np->n_flink;
127 return(np);
131 * Extract a list of names from a line,
132 * and make a list of names from it.
133 * Return the list or NULL if none found.
135 struct name *
136 extract(char *line, enum gfield ntype)
138 return extract1(line, ntype, " \t,(", 0);
141 struct name *
142 sextract(char *line, enum gfield ntype)
144 if (line && strpbrk(line, ",\"\\(<"))
145 return extract1(line, ntype, ",", 1);
146 else
147 return extract(line, ntype);
150 static struct name *
151 extract1(char *line, enum gfield ntype, char *separators, int copypfx)
153 char *cp, *nbuf;
154 struct name *top, *np, *t;
156 if (line == NULL || *line == '\0')
157 return NULL;
158 top = NULL;
159 np = NULL;
160 cp = line;
161 nbuf = ac_alloc(strlen(line) + 1);
162 while ((cp = yankword(cp, nbuf, separators, copypfx)) != NULL) {
163 t = nalloc(nbuf, ntype);
164 if (top == NULL)
165 top = t;
166 else
167 np->n_flink = t;
168 t->n_blink = np;
169 np = t;
171 ac_free(nbuf);
172 return top;
176 * Turn a list of names into a string of the same names.
178 char *
179 detract(struct name *np, enum gfield ntype)
181 int s;
182 char *cp, *top;
183 struct name *p;
184 int comma;
186 comma = ntype & GCOMMA;
187 if (np == NULL)
188 return(NULL);
189 ntype &= ~GCOMMA;
190 s = 0;
191 if ((debug || value("debug")) && comma)
192 fprintf(stderr, catgets(catd, CATSET, 145,
193 "detract asked to insert commas\n"));
194 for (p = np; p != NULL; p = p->n_flink) {
195 if (ntype && (p->n_type & GMASK) != ntype)
196 continue;
197 s += strlen(p->n_fullname) + 1;
198 if (comma)
199 s++;
201 if (s == 0)
202 return(NULL);
203 s += 2;
204 top = salloc(s);
205 cp = top;
206 for (p = np; p != NULL; p = p->n_flink) {
207 if (ntype && (p->n_type & GMASK) != ntype)
208 continue;
209 cp = sstpcpy(cp, p->n_fullname);
210 if (comma && p->n_flink != NULL)
211 *cp++ = ',';
212 *cp++ = ' ';
214 *--cp = 0;
215 if (comma && *--cp == ',')
216 *cp = 0;
217 return(top);
221 * Grab a single word (liberal word)
222 * Throw away things between ()'s, and take anything between <>.
223 * Strip trailing whitespace as *ap* may come directly from user.
225 static char *
226 yankword(char *ap, char *wbuf, char *separators, int copypfx)
228 char *cp, *pp, *wp;
230 cp = ap;
231 wp = wbuf;
232 while (blankchar(*cp & 0377) || *cp == ',')
233 cp++;
234 pp = cp;
235 if ((cp = nexttoken(cp)) == NULL)
236 return NULL;
237 if (copypfx)
238 while (pp < cp)
239 *wp++ = *pp++;
240 if (*cp == '<')
241 while (*cp && (*wp++ = *cp++) != '>');
242 else {
243 int incomm = 0;
245 while (*cp && (incomm || !strchr(separators, *cp))) {
246 if (*cp == '\"') {
247 if (cp == ap || *(cp - 1) != '\\') {
248 if (incomm)
249 incomm--;
250 else
251 incomm++;
252 *wp++ = '\"';
253 } else if (cp != ap) {
254 *(wp - 1) = '\"';
256 cp++;
257 continue;
259 *wp++ = *cp++;
262 while (wp > wbuf && blankspacechar(wp[-1]))
263 --wp;
264 *wp = '\0';
265 return cp;
269 * For each recipient in the passed name list with a /
270 * in the name, append the message to the end of the named file
271 * and remove him from the recipient list.
273 * Recipients whose name begins with | are piped through the given
274 * program and removed.
276 /*ARGSUSED 3*/
277 struct name *
278 outof(struct name *names, FILE *fo, struct header *hp)
280 int c, lastc;
281 struct name *np, *top;
282 time_t now;
283 char *date, *fname;
284 FILE *fout, *fin;
285 int ispipe;
286 (void)hp;
288 top = names;
289 np = names;
290 time(&now);
291 date = ctime(&now);
292 while (np != NULL) {
293 if (!is_fileaddr(np->n_name) && np->n_name[0] != '|') {
294 np = np->n_flink;
295 continue;
297 ispipe = np->n_name[0] == '|';
298 if (ispipe)
299 fname = np->n_name+1;
300 else
301 fname = expand(np->n_name);
304 * See if we have copied the complete message out yet.
305 * If not, do so.
308 if (image < 0) {
309 char *tempEdit;
311 if ((fout = Ftemp(&tempEdit, "Re", "w", 0600, 1))
312 == NULL) {
313 perror(catgets(catd, CATSET, 146,
314 "temporary edit file"));
315 senderr++;
316 goto cant;
318 image = open(tempEdit, O_RDWR);
319 unlink(tempEdit);
320 Ftfree(&tempEdit);
321 if (image < 0) {
322 perror(catgets(catd, CATSET, 147,
323 "temporary edit file"));
324 senderr++;
325 Fclose(fout);
326 goto cant;
328 fcntl(image, F_SETFD, FD_CLOEXEC);
329 fprintf(fout, "From %s %s", myname, date);
330 c = EOF;
331 while (lastc = c, (c = getc(fo)) != EOF)
332 putc(c, fout);
333 rewind(fo);
334 if (lastc != '\n')
335 putc('\n', fout);
336 putc('\n', fout);
337 fflush(fout);
338 if (ferror(fout))
339 perror(catgets(catd, CATSET, 148,
340 "temporary edit file"));
341 Fclose(fout);
345 * Now either copy "image" to the desired file
346 * or give it as the standard input to the desired
347 * program as appropriate.
350 if (ispipe) {
351 int pid;
352 char *shell;
353 sigset_t nset;
356 * XXX
357 * We can't really reuse the same image file,
358 * because multiple piped recipients will
359 * share the same lseek location and trample
360 * on one another.
362 if ((shell = value("SHELL")) == NULL)
363 shell = SHELL;
364 sigemptyset(&nset);
365 sigaddset(&nset, SIGHUP);
366 sigaddset(&nset, SIGINT);
367 sigaddset(&nset, SIGQUIT);
368 pid = start_command(shell, &nset,
369 image, -1, "-c", fname, NULL);
370 if (pid < 0) {
371 senderr++;
372 goto cant;
374 free_child(pid);
375 } else {
376 int f;
377 if ((fout = Zopen(fname, "a", NULL)) == NULL) {
378 perror(fname);
379 senderr++;
380 goto cant;
382 if ((f = dup(image)) < 0) {
383 perror("dup");
384 fin = NULL;
385 } else
386 fin = Fdopen(f, "r");
387 if (fin == NULL) {
388 fprintf(stderr, catgets(catd, CATSET, 149,
389 "Can't reopen image\n"));
390 Fclose(fout);
391 senderr++;
392 goto cant;
394 rewind(fin);
395 while ((c = getc(fin)) != EOF)
396 putc(c, fout);
397 if (ferror(fout))
398 senderr++, perror(fname);
399 Fclose(fout);
400 Fclose(fin);
402 cant:
404 * In days of old we removed the entry from the
405 * the list; now for sake of header expansion
406 * we leave it in and mark it as deleted.
408 np->n_type |= GDEL;
409 np = np->n_flink;
411 if (image >= 0) {
412 close(image);
413 image = -1;
415 return(top);
419 * Determine if the passed address is a local "send to file" address.
420 * If any of the network metacharacters precedes any slashes, it can't
421 * be a filename. We cheat with .'s to allow path names like ./...
423 int
424 is_fileaddr(char *name)
426 char *cp;
428 if (strchr(name, '@') != NULL)
429 return 0;
430 if (*name == '+')
431 return 1;
432 for (cp = name; *cp; cp++) {
433 if (*cp == '!' || *cp == '%')
434 return 0;
435 if (*cp == '/')
436 return 1;
438 return 0;
441 static int
442 same_name(char *n1, char *n2)
444 int c1, c2;
446 if (value("allnet") != NULL) {
447 do {
448 c1 = (*n1++ & 0377);
449 c2 = (*n2++ & 0377);
450 c1 = lowerconv(c1);
451 c2 = lowerconv(c2);
452 if (c1 != c2)
453 return 0;
454 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
455 return 1;
456 } else
457 return asccasecmp(n1, n2) == 0;
461 * Map all of the aliased users in the invoker's mailrc
462 * file and insert them into the list.
463 * Changed after all these months of service to recursively
464 * expand names (2/14/80).
467 struct name *
468 usermap(struct name *names)
470 struct name *new, *np, *cp;
471 struct grouphead *gh;
472 int metoo;
474 new = NULL;
475 np = names;
476 metoo = (value("metoo") != NULL);
477 while (np != NULL) {
478 if (np->n_name[0] == '\\') {
479 cp = np->n_flink;
480 new = put(new, np);
481 np = cp;
482 continue;
484 gh = findgroup(np->n_name);
485 cp = np->n_flink;
486 if (gh != NULL)
487 new = gexpand(new, gh, metoo, np->n_type);
488 else
489 new = put(new, np);
490 np = cp;
492 return(new);
496 * Recursively expand a group name. We limit the expansion to some
497 * fixed level to keep things from going haywire.
498 * Direct recursion is not expanded for convenience.
501 static struct name *
502 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
504 struct group *gp;
505 struct grouphead *ngh;
506 struct name *np;
507 static int depth;
508 char *cp;
510 if (depth > MAXEXP) {
511 printf(catgets(catd, CATSET, 150,
512 "Expanding alias to depth larger than %d\n"), MAXEXP);
513 return(nlist);
515 depth++;
516 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
517 cp = gp->ge_name;
518 if (*cp == '\\')
519 goto quote;
520 if (strcmp(cp, gh->g_name) == 0)
521 goto quote;
522 if ((ngh = findgroup(cp)) != NULL) {
523 nlist = gexpand(nlist, ngh, metoo, ntype);
524 continue;
526 quote:
527 np = nalloc(cp, ntype|GFULL);
529 * At this point should allow to expand
530 * to self if only person in group
532 if (gp == gh->g_list && gp->ge_link == NULL)
533 goto skip;
534 if (!metoo && same_name(cp, myname))
535 np->n_type |= GDEL;
536 skip:
537 nlist = put(nlist, np);
539 depth--;
540 return(nlist);
544 * Concatenate the two passed name lists, return the result.
546 struct name *
547 cat(struct name *n1, struct name *n2)
549 struct name *tail;
551 if (n1 == NULL)
552 return(n2);
553 if (n2 == NULL)
554 return(n1);
555 tail = tailof(n1);
556 tail->n_flink = n2;
557 n2->n_blink = tail;
558 return(n1);
562 * Unpack the name list onto a vector of strings.
563 * Return an error if the name list won't fit.
565 char **
566 unpack(struct name *np)
568 char **ap, **top;
569 struct name *n;
570 int t, extra, metoo, verbose;
572 n = np;
573 if ((t = count(n)) == 0)
574 panic(catgets(catd, CATSET, 151, "No names to unpack"));
576 * Compute the number of extra arguments we will need.
577 * We need at least two extra -- one for "mail" and one for
578 * the terminating 0 pointer. Additional spots may be needed
579 * to pass along -f to the host mailer.
581 extra = 2;
582 extra++;
583 metoo = value("metoo") != NULL;
584 if (metoo)
585 extra++;
586 verbose = value("verbose") != NULL;
587 if (verbose)
588 extra++;
589 /*LINTED*/
590 top = (char **)salloc((t + extra) * sizeof *top);
591 ap = top;
592 *ap++ = "send-mail";
593 *ap++ = "-i";
594 if (metoo)
595 *ap++ = "-m";
596 if (verbose)
597 *ap++ = "-v";
598 for (; n != NULL; n = n->n_flink)
599 if ((n->n_type & GDEL) == 0)
600 *ap++ = n->n_name;
601 *ap = NULL;
602 return(top);
606 * Remove all of the duplicates from the passed name list by
607 * insertion sorting them, then checking for dups.
608 * Return the head of the new list.
610 struct name *
611 elide(struct name *names)
613 struct name *np, *t, *new;
614 struct name *x;
616 if (names == NULL)
617 return(NULL);
618 new = names;
619 np = names;
620 np = np->n_flink;
621 if (np != NULL)
622 np->n_blink = NULL;
623 new->n_flink = NULL;
624 while (np != NULL) {
625 t = new;
626 while (asccasecmp(t->n_name, np->n_name) < 0) {
627 if (t->n_flink == NULL)
628 break;
629 t = t->n_flink;
633 * If we ran out of t's, put the new entry after
634 * the current value of t.
637 if (asccasecmp(t->n_name, np->n_name) < 0) {
638 t->n_flink = np;
639 np->n_blink = t;
640 t = np;
641 np = np->n_flink;
642 t->n_flink = NULL;
643 continue;
647 * Otherwise, put the new entry in front of the
648 * current t. If at the front of the list,
649 * the new guy becomes the new head of the list.
652 if (t == new) {
653 t = np;
654 np = np->n_flink;
655 t->n_flink = new;
656 new->n_blink = t;
657 t->n_blink = NULL;
658 new = t;
659 continue;
663 * The normal case -- we are inserting into the
664 * middle of the list.
667 x = np;
668 np = np->n_flink;
669 x->n_flink = t;
670 x->n_blink = t->n_blink;
671 t->n_blink->n_flink = x;
672 t->n_blink = x;
676 * Now the list headed up by new is sorted.
677 * Go through it and remove duplicates.
680 np = new;
681 while (np != NULL) {
682 t = np;
683 while (t->n_flink != NULL &&
684 asccasecmp(np->n_name, t->n_flink->n_name) == 0)
685 t = t->n_flink;
686 if (t == np || t == NULL) {
687 np = np->n_flink;
688 continue;
692 * Now t points to the last entry with the same name
693 * as np. Make np point beyond t.
696 np->n_flink = t->n_flink;
697 if (t->n_flink != NULL)
698 t->n_flink->n_blink = np;
699 np = np->n_flink;
701 return(new);
705 * Put another node onto a list of names and return
706 * the list.
708 static struct name *
709 put(struct name *list, struct name *node)
711 node->n_flink = list;
712 node->n_blink = NULL;
713 if (list != NULL)
714 list->n_blink = node;
715 return(node);
719 * Determine the number of undeleted elements in
720 * a name list and return it.
722 int
723 count(struct name *np)
725 int c;
727 for (c = 0; np != NULL; np = np->n_flink)
728 if ((np->n_type & GDEL) == 0)
729 c++;
730 return c;
734 * Delete the given name from a namelist.
736 static struct name *
737 delname(struct name *np, char *name)
739 struct name *p;
741 for (p = np; p != NULL; p = p->n_flink)
742 if (same_name(p->n_name, name)) {
743 if (p->n_blink == NULL) {
744 if (p->n_flink != NULL)
745 p->n_flink->n_blink = NULL;
746 np = p->n_flink;
747 continue;
749 if (p->n_flink == NULL) {
750 if (p->n_blink != NULL)
751 p->n_blink->n_flink = NULL;
752 continue;
754 p->n_blink->n_flink = p->n_flink;
755 p->n_flink->n_blink = p->n_blink;
757 return np;
761 * Pretty print a name list
762 * Uncomment it if you need it.
766 void
767 prettyprint(struct name *name)
769 struct name *np;
771 np = name;
772 while (np != NULL) {
773 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
774 np = np->n_flink;
776 fprintf(stderr, "\n");
780 struct name *
781 delete_alternates(struct name *np)
783 struct name *xp;
784 char **ap;
786 np = delname(np, myname);
787 if (altnames)
788 for (ap = altnames; *ap; ap++)
789 np = delname(np, *ap);
790 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
791 while (xp) {
792 np = delname(np, xp->n_name);
793 xp = xp->n_flink;
795 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
796 while (xp) {
797 np = delname(np, xp->n_name);
798 xp = xp->n_flink;
800 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
801 while (xp) {
802 np = delname(np, xp->n_name);
803 xp = xp->n_flink;
805 return np;
809 is_myname(char *name)
811 struct name *xp;
812 char **ap;
814 if (same_name(myname, name))
815 return 1;
816 if (altnames)
817 for (ap = altnames; *ap; ap++)
818 if (same_name(*ap, name))
819 return 1;
820 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
821 while (xp) {
822 if (same_name(xp->n_name, name))
823 return 1;
824 xp = xp->n_flink;
826 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
827 while (xp) {
828 if (same_name(xp->n_name, name))
829 return 1;
830 xp = xp->n_flink;
832 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
833 while (xp) {
834 if (same_name(xp->n_name, name))
835 return 1;
836 xp = xp->n_flink;
838 return 0;