Improve config/make system, silence smake(1)..
[s-mailx.git] / names.c
blobc3a0bb96e1b3b3a34e06bd659e0570adef7af87a
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 addtype)
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 = np->n_type | addtype;
106 nnp->n_flags = np->n_flags;
107 nnp->n_name = savestr(np->n_name);
108 nnp->n_fullname = (np->n_name == np->n_fullname) ? nnp->n_name
109 : savestr(np->n_fullname);
110 return (nnp);
114 * Find the tail of a list and return it.
116 static struct name *
117 tailof(struct name *name)
119 struct name *np;
121 np = name;
122 if (np == NULL)
123 return(NULL);
124 while (np->n_flink != NULL)
125 np = np->n_flink;
126 return(np);
130 * Extract a list of names from a line,
131 * and make a list of names from it.
132 * Return the list or NULL if none found.
134 struct name *
135 extract(char *line, enum gfield ntype)
137 return extract1(line, ntype, " \t,(", 0);
140 struct name *
141 sextract(char *line, enum gfield ntype)
143 if (line && strpbrk(line, ",\"\\(<"))
144 return extract1(line, ntype, ",", 1);
145 else
146 return extract(line, ntype);
149 static struct name *
150 extract1(char *line, enum gfield ntype, char *separators, int copypfx)
152 char *cp, *nbuf;
153 struct name *top, *np, *t;
155 if (line == NULL || *line == '\0')
156 return NULL;
157 top = NULL;
158 np = NULL;
159 cp = line;
160 nbuf = ac_alloc(strlen(line) + 1);
161 while ((cp = yankword(cp, nbuf, separators, copypfx)) != NULL) {
162 t = nalloc(nbuf, ntype);
163 if (top == NULL)
164 top = t;
165 else
166 np->n_flink = t;
167 t->n_blink = np;
168 np = t;
170 ac_free(nbuf);
171 return top;
175 * Turn a list of names into a string of the same names.
177 char *
178 detract(struct name *np, enum gfield ntype)
180 int s;
181 char *cp, *top;
182 struct name *p;
183 int comma;
185 comma = ntype & GCOMMA;
186 if (np == NULL)
187 return(NULL);
188 ntype &= ~GCOMMA;
189 s = 0;
190 if ((debug || value("debug")) && comma)
191 fprintf(stderr, catgets(catd, CATSET, 145,
192 "detract asked to insert commas\n"));
193 for (p = np; p != NULL; p = p->n_flink) {
194 if (ntype && (p->n_type & GMASK) != ntype)
195 continue;
196 s += strlen(p->n_fullname) + 1;
197 if (comma)
198 s++;
200 if (s == 0)
201 return(NULL);
202 s += 2;
203 top = salloc(s);
204 cp = top;
205 for (p = np; p != NULL; p = p->n_flink) {
206 if (ntype && (p->n_type & GMASK) != ntype)
207 continue;
208 cp = sstpcpy(cp, p->n_fullname);
209 if (comma && p->n_flink != NULL)
210 *cp++ = ',';
211 *cp++ = ' ';
213 *--cp = 0;
214 if (comma && *--cp == ',')
215 *cp = 0;
216 return(top);
220 * Grab a single word (liberal word)
221 * Throw away things between ()'s, and take anything between <>.
222 * Strip trailing whitespace as *ap* may come directly from user.
224 static char *
225 yankword(char *ap, char *wbuf, char *separators, int copypfx)
227 char *cp, *pp, *wp;
229 cp = ap;
230 wp = wbuf;
231 while (blankchar(*cp & 0377) || *cp == ',')
232 cp++;
233 pp = cp;
234 if ((cp = nexttoken(cp)) == NULL)
235 return NULL;
236 if (copypfx)
237 while (pp < cp)
238 *wp++ = *pp++;
239 if (*cp == '<')
240 while (*cp && (*wp++ = *cp++) != '>');
241 else {
242 int incomm = 0;
244 while (*cp && (incomm || !strchr(separators, *cp))) {
245 if (*cp == '\"') {
246 if (cp == ap || *(cp - 1) != '\\') {
247 if (incomm)
248 incomm--;
249 else
250 incomm++;
251 *wp++ = '\"';
252 } else if (cp != ap) {
253 *(wp - 1) = '\"';
255 cp++;
256 continue;
258 *wp++ = *cp++;
261 while (wp > wbuf && blankspacechar(wp[-1]))
262 --wp;
263 *wp = '\0';
264 return cp;
268 * For each recipient in the passed name list with a /
269 * in the name, append the message to the end of the named file
270 * and remove him from the recipient list.
272 * Recipients whose name begins with | are piped through the given
273 * program and removed.
275 /*ARGSUSED 3*/
276 struct name *
277 outof(struct name *names, FILE *fo, struct header *hp)
279 int c, lastc;
280 struct name *np, *top;
281 time_t now;
282 char *date, *fname;
283 FILE *fout, *fin;
284 int ispipe;
285 (void)hp;
287 top = names;
288 np = names;
289 time(&now);
290 date = ctime(&now);
291 while (np != NULL) {
292 if (!is_fileaddr(np->n_name) && np->n_name[0] != '|') {
293 np = np->n_flink;
294 continue;
296 ispipe = np->n_name[0] == '|';
297 if (ispipe)
298 fname = np->n_name+1;
299 else
300 fname = expand(np->n_name);
303 * See if we have copied the complete message out yet.
304 * If not, do so.
307 if (image < 0) {
308 char *tempEdit;
310 if ((fout = Ftemp(&tempEdit, "Re", "w", 0600, 1))
311 == NULL) {
312 perror(catgets(catd, CATSET, 146,
313 "temporary edit file"));
314 senderr++;
315 goto cant;
317 image = open(tempEdit, O_RDWR);
318 unlink(tempEdit);
319 Ftfree(&tempEdit);
320 if (image < 0) {
321 perror(catgets(catd, CATSET, 147,
322 "temporary edit file"));
323 senderr++;
324 Fclose(fout);
325 goto cant;
327 fcntl(image, F_SETFD, FD_CLOEXEC);
328 fprintf(fout, "From %s %s", myname, date);
329 c = EOF;
330 while (lastc = c, (c = getc(fo)) != EOF)
331 putc(c, fout);
332 rewind(fo);
333 if (lastc != '\n')
334 putc('\n', fout);
335 putc('\n', fout);
336 fflush(fout);
337 if (ferror(fout))
338 perror(catgets(catd, CATSET, 148,
339 "temporary edit file"));
340 Fclose(fout);
344 * Now either copy "image" to the desired file
345 * or give it as the standard input to the desired
346 * program as appropriate.
349 if (ispipe) {
350 int pid;
351 char *shell;
352 sigset_t nset;
355 * XXX
356 * We can't really reuse the same image file,
357 * because multiple piped recipients will
358 * share the same lseek location and trample
359 * on one another.
361 if ((shell = value("SHELL")) == NULL)
362 shell = SHELL;
363 sigemptyset(&nset);
364 sigaddset(&nset, SIGHUP);
365 sigaddset(&nset, SIGINT);
366 sigaddset(&nset, SIGQUIT);
367 pid = start_command(shell, &nset,
368 image, -1, "-c", fname, NULL);
369 if (pid < 0) {
370 senderr++;
371 goto cant;
373 free_child(pid);
374 } else {
375 int f;
376 if ((fout = Zopen(fname, "a", NULL)) == NULL) {
377 perror(fname);
378 senderr++;
379 goto cant;
381 if ((f = dup(image)) < 0) {
382 perror("dup");
383 fin = NULL;
384 } else
385 fin = Fdopen(f, "r");
386 if (fin == NULL) {
387 fprintf(stderr, catgets(catd, CATSET, 149,
388 "Can't reopen image\n"));
389 Fclose(fout);
390 senderr++;
391 goto cant;
393 rewind(fin);
394 while ((c = getc(fin)) != EOF)
395 putc(c, fout);
396 if (ferror(fout))
397 senderr++, perror(fname);
398 Fclose(fout);
399 Fclose(fin);
401 cant:
403 * In days of old we removed the entry from the
404 * the list; now for sake of header expansion
405 * we leave it in and mark it as deleted.
407 np->n_type |= GDEL;
408 np = np->n_flink;
410 if (image >= 0) {
411 close(image);
412 image = -1;
414 return(top);
418 * Determine if the passed address is a local "send to file" address.
419 * If any of the network metacharacters precedes any slashes, it can't
420 * be a filename. We cheat with .'s to allow path names like ./...
422 int
423 is_fileaddr(char *name)
425 char *cp;
427 if (strchr(name, '@') != NULL)
428 return 0;
429 if (*name == '+')
430 return 1;
431 for (cp = name; *cp; cp++) {
432 if (*cp == '!' || *cp == '%')
433 return 0;
434 if (*cp == '/')
435 return 1;
437 return 0;
440 static int
441 same_name(char *n1, char *n2)
443 int c1, c2;
445 if (value("allnet") != NULL) {
446 do {
447 c1 = (*n1++ & 0377);
448 c2 = (*n2++ & 0377);
449 c1 = lowerconv(c1);
450 c2 = lowerconv(c2);
451 if (c1 != c2)
452 return 0;
453 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
454 return 1;
455 } else
456 return asccasecmp(n1, n2) == 0;
460 * Map all of the aliased users in the invoker's mailrc
461 * file and insert them into the list.
462 * Changed after all these months of service to recursively
463 * expand names (2/14/80).
466 struct name *
467 usermap(struct name *names)
469 struct name *new, *np, *cp;
470 struct grouphead *gh;
471 int metoo;
473 new = NULL;
474 np = names;
475 metoo = (value("metoo") != NULL);
476 while (np != NULL) {
477 if (np->n_name[0] == '\\') {
478 cp = np->n_flink;
479 new = put(new, np);
480 np = cp;
481 continue;
483 gh = findgroup(np->n_name);
484 cp = np->n_flink;
485 if (gh != NULL)
486 new = gexpand(new, gh, metoo, np->n_type);
487 else
488 new = put(new, np);
489 np = cp;
491 return(new);
495 * Recursively expand a group name. We limit the expansion to some
496 * fixed level to keep things from going haywire.
497 * Direct recursion is not expanded for convenience.
500 static struct name *
501 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
503 struct group *gp;
504 struct grouphead *ngh;
505 struct name *np;
506 static int depth;
507 char *cp;
509 if (depth > MAXEXP) {
510 printf(catgets(catd, CATSET, 150,
511 "Expanding alias to depth larger than %d\n"), MAXEXP);
512 return(nlist);
514 depth++;
515 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
516 cp = gp->ge_name;
517 if (*cp == '\\')
518 goto quote;
519 if (strcmp(cp, gh->g_name) == 0)
520 goto quote;
521 if ((ngh = findgroup(cp)) != NULL) {
522 nlist = gexpand(nlist, ngh, metoo, ntype);
523 continue;
525 quote:
526 np = nalloc(cp, ntype|GFULL);
528 * At this point should allow to expand
529 * to self if only person in group
531 if (gp == gh->g_list && gp->ge_link == NULL)
532 goto skip;
533 if (!metoo && same_name(cp, myname))
534 np->n_type |= GDEL;
535 skip:
536 nlist = put(nlist, np);
538 depth--;
539 return(nlist);
543 * Concatenate the two passed name lists, return the result.
545 struct name *
546 cat(struct name *n1, struct name *n2)
548 struct name *tail;
550 if (n1 == NULL)
551 return(n2);
552 if (n2 == NULL)
553 return(n1);
554 tail = tailof(n1);
555 tail->n_flink = n2;
556 n2->n_blink = tail;
557 return(n1);
561 * Unpack the name list onto a vector of strings.
562 * Return an error if the name list won't fit.
564 char **
565 unpack(struct name *np)
567 char **ap, **top;
568 struct name *n;
569 int t, extra, metoo, verbose;
571 n = np;
572 if ((t = count(n)) == 0)
573 panic(catgets(catd, CATSET, 151, "No names to unpack"));
575 * Compute the number of extra arguments we will need.
576 * We need at least two extra -- one for "mail" and one for
577 * the terminating 0 pointer. Additional spots may be needed
578 * to pass along -f to the host mailer.
580 extra = 2;
581 extra++;
582 metoo = value("metoo") != NULL;
583 if (metoo)
584 extra++;
585 verbose = value("verbose") != NULL;
586 if (verbose)
587 extra++;
588 /*LINTED*/
589 top = (char **)salloc((t + extra) * sizeof *top);
590 ap = top;
591 *ap++ = "send-mail";
592 *ap++ = "-i";
593 if (metoo)
594 *ap++ = "-m";
595 if (verbose)
596 *ap++ = "-v";
597 for (; n != NULL; n = n->n_flink)
598 if ((n->n_type & GDEL) == 0)
599 *ap++ = n->n_name;
600 *ap = NULL;
601 return(top);
605 * Remove all of the duplicates from the passed name list by
606 * insertion sorting them, then checking for dups.
607 * Return the head of the new list.
609 struct name *
610 elide(struct name *names)
612 struct name *np, *t, *new;
613 struct name *x;
615 if (names == NULL)
616 return(NULL);
617 new = names;
618 np = names;
619 np = np->n_flink;
620 if (np != NULL)
621 np->n_blink = NULL;
622 new->n_flink = NULL;
623 while (np != NULL) {
624 t = new;
625 while (asccasecmp(t->n_name, np->n_name) < 0) {
626 if (t->n_flink == NULL)
627 break;
628 t = t->n_flink;
632 * If we ran out of t's, put the new entry after
633 * the current value of t.
636 if (asccasecmp(t->n_name, np->n_name) < 0) {
637 t->n_flink = np;
638 np->n_blink = t;
639 t = np;
640 np = np->n_flink;
641 t->n_flink = NULL;
642 continue;
646 * Otherwise, put the new entry in front of the
647 * current t. If at the front of the list,
648 * the new guy becomes the new head of the list.
651 if (t == new) {
652 t = np;
653 np = np->n_flink;
654 t->n_flink = new;
655 new->n_blink = t;
656 t->n_blink = NULL;
657 new = t;
658 continue;
662 * The normal case -- we are inserting into the
663 * middle of the list.
666 x = np;
667 np = np->n_flink;
668 x->n_flink = t;
669 x->n_blink = t->n_blink;
670 t->n_blink->n_flink = x;
671 t->n_blink = x;
675 * Now the list headed up by new is sorted.
676 * Go through it and remove duplicates.
679 np = new;
680 while (np != NULL) {
681 t = np;
682 while (t->n_flink != NULL &&
683 asccasecmp(np->n_name, t->n_flink->n_name) == 0)
684 t = t->n_flink;
685 if (t == np || t == NULL) {
686 np = np->n_flink;
687 continue;
691 * Now t points to the last entry with the same name
692 * as np. Make np point beyond t.
695 np->n_flink = t->n_flink;
696 if (t->n_flink != NULL)
697 t->n_flink->n_blink = np;
698 np = np->n_flink;
700 return(new);
704 * Put another node onto a list of names and return
705 * the list.
707 static struct name *
708 put(struct name *list, struct name *node)
710 node->n_flink = list;
711 node->n_blink = NULL;
712 if (list != NULL)
713 list->n_blink = node;
714 return(node);
718 * Determine the number of undeleted elements in
719 * a name list and return it.
721 int
722 count(struct name *np)
724 int c;
726 for (c = 0; np != NULL; np = np->n_flink)
727 if ((np->n_type & GDEL) == 0)
728 c++;
729 return c;
733 * Delete the given name from a namelist.
735 static struct name *
736 delname(struct name *np, char *name)
738 struct name *p;
740 for (p = np; p != NULL; p = p->n_flink)
741 if (same_name(p->n_name, name)) {
742 if (p->n_blink == NULL) {
743 if (p->n_flink != NULL)
744 p->n_flink->n_blink = NULL;
745 np = p->n_flink;
746 continue;
748 if (p->n_flink == NULL) {
749 if (p->n_blink != NULL)
750 p->n_blink->n_flink = NULL;
751 continue;
753 p->n_blink->n_flink = p->n_flink;
754 p->n_flink->n_blink = p->n_blink;
756 return np;
760 * Pretty print a name list
761 * Uncomment it if you need it.
765 void
766 prettyprint(struct name *name)
768 struct name *np;
770 np = name;
771 while (np != NULL) {
772 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
773 np = np->n_flink;
775 fprintf(stderr, "\n");
779 struct name *
780 delete_alternates(struct name *np)
782 struct name *xp;
783 char **ap;
785 np = delname(np, myname);
786 if (altnames)
787 for (ap = altnames; *ap; ap++)
788 np = delname(np, *ap);
789 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
790 while (xp) {
791 np = delname(np, xp->n_name);
792 xp = xp->n_flink;
794 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
795 while (xp) {
796 np = delname(np, xp->n_name);
797 xp = xp->n_flink;
799 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
800 while (xp) {
801 np = delname(np, xp->n_name);
802 xp = xp->n_flink;
804 return np;
808 is_myname(char *name)
810 struct name *xp;
811 char **ap;
813 if (same_name(myname, name))
814 return 1;
815 if (altnames)
816 for (ap = altnames; *ap; ap++)
817 if (same_name(*ap, name))
818 return 1;
819 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
820 while (xp) {
821 if (same_name(xp->n_name, name))
822 return 1;
823 xp = xp->n_flink;
825 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
826 while (xp) {
827 if (same_name(xp->n_name, name))
828 return 1;
829 xp = xp->n_flink;
831 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
832 while (xp) {
833 if (same_name(xp->n_name, name))
834 return 1;
835 xp = xp->n_flink;
837 return 0;