fixhead(): use ndup()
[s-mailx.git] / names.c
blobf7bc9492f67284033ccc1ba9133aaef61cab2eb3
1 /*
2 * Heirloom mailx - 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.
40 #ifndef lint
41 #ifdef DOSCCS
42 static char sccsid[] = "@(#)names.c 2.22 (gritter) 3/4/06";
43 #endif
44 #endif /* not lint */
47 * Mail -- a mail program
49 * Handle name lists.
52 #include "rcv.h"
53 #include "extern.h"
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <time.h>
57 #include <unistd.h>
59 static struct name *tailof(struct name *name);
60 static struct name *extract1(char *line, enum gfield ntype, char *separators,
61 int copypfx);
62 static char *yankword(char *ap, char *wbuf, char *separators, int copypfx);
63 static int same_name(char *n1, char *n2);
64 static struct name *gexpand(struct name *nlist, struct grouphead *gh,
65 int metoo, int ntype);
66 static struct name *put(struct name *list, struct name *node);
67 static struct name *delname(struct name *np, char *name);
70 * Allocate a single element of a name list,
71 * initialize its name field to the passed
72 * name and return it.
74 struct name *
75 nalloc(char *str, enum gfield ntype)
77 struct name *np;
78 struct str in, out;
80 /*LINTED*/
81 np = (struct name *)salloc(sizeof *np);
82 np->n_flink = NULL;
83 np->n_blink = NULL;
84 np->n_type = ntype;
85 np->n_flags = 0;
86 if (ntype & GFULL) {
87 np->n_name = savestr(skin(str));
88 if (strcmp(np->n_name, str)) {
89 in.s = str;
90 in.l = strlen(str);
91 mime_fromhdr(&in, &out, TD_ISPR|TD_ICONV);
92 np->n_fullname = savestr(out.s);
93 free(out.s);
94 } else
95 np->n_fullname = np->n_name;
96 } else if (ntype & GSKIN)
97 np->n_fullname = np->n_name = savestr(skin(str));
98 else
99 np->n_fullname = np->n_name = savestr(str);
100 return(np);
103 struct name *
104 ndup(struct name *np, enum gfield addtype)
106 struct name *nnp;
108 nnp = (struct name*)salloc(sizeof *np);
109 nnp->n_flink = NULL;
110 nnp->n_blink = NULL;
111 nnp->n_type = np->n_type | addtype;
112 nnp->n_flags = np->n_flags;
113 nnp->n_name = savestr(np->n_name);
114 nnp->n_fullname = (np->n_name == np->n_fullname) ? nnp->n_name
115 : savestr(np->n_fullname);
116 return (nnp);
120 * Find the tail of a list and return it.
122 static struct name *
123 tailof(struct name *name)
125 struct name *np;
127 np = name;
128 if (np == NULL)
129 return(NULL);
130 while (np->n_flink != NULL)
131 np = np->n_flink;
132 return(np);
136 * Extract a list of names from a line,
137 * and make a list of names from it.
138 * Return the list or NULL if none found.
140 struct name *
141 extract(char *line, enum gfield ntype)
143 return extract1(line, ntype, " \t,(", 0);
146 struct name *
147 sextract(char *line, enum gfield ntype)
149 if (line && strpbrk(line, ",\"\\(<"))
150 return extract1(line, ntype, ",", 1);
151 else
152 return extract(line, ntype);
155 static struct name *
156 extract1(char *line, enum gfield ntype, char *separators, int copypfx)
158 char *cp, *nbuf;
159 struct name *top, *np, *t;
161 if (line == NULL || *line == '\0')
162 return NULL;
163 top = NULL;
164 np = NULL;
165 cp = line;
166 nbuf = ac_alloc(strlen(line) + 1);
167 while ((cp = yankword(cp, nbuf, separators, copypfx)) != NULL) {
168 t = nalloc(nbuf, ntype);
169 if (top == NULL)
170 top = t;
171 else
172 np->n_flink = t;
173 t->n_blink = np;
174 np = t;
176 ac_free(nbuf);
177 return top;
181 * Turn a list of names into a string of the same names.
183 char *
184 detract(struct name *np, enum gfield ntype)
186 int s;
187 char *cp, *top;
188 struct name *p;
189 int comma;
191 comma = ntype & GCOMMA;
192 if (np == NULL)
193 return(NULL);
194 ntype &= ~GCOMMA;
195 s = 0;
196 if ((debug || value("debug")) && comma)
197 fprintf(stderr, catgets(catd, CATSET, 145,
198 "detract asked to insert commas\n"));
199 for (p = np; p != NULL; p = p->n_flink) {
200 if (ntype && (p->n_type & GMASK) != ntype)
201 continue;
202 s += strlen(p->n_fullname) + 1;
203 if (comma)
204 s++;
206 if (s == 0)
207 return(NULL);
208 s += 2;
209 top = salloc(s);
210 cp = top;
211 for (p = np; p != NULL; p = p->n_flink) {
212 if (ntype && (p->n_type & GMASK) != ntype)
213 continue;
214 cp = sstpcpy(cp, p->n_fullname);
215 if (comma && p->n_flink != NULL)
216 *cp++ = ',';
217 *cp++ = ' ';
219 *--cp = 0;
220 if (comma && *--cp == ',')
221 *cp = 0;
222 return(top);
226 * Grab a single word (liberal word)
227 * Throw away things between ()'s, and take anything between <>.
229 static char *
230 yankword(char *ap, char *wbuf, char *separators, int copypfx)
232 char *cp, *pp, *wp;
234 cp = ap;
235 wp = wbuf;
236 while (blankchar(*cp & 0377) || *cp == ',')
237 cp++;
238 pp = cp;
239 if ((cp = nexttoken(cp)) == NULL)
240 return NULL;
241 if (copypfx)
242 while (pp < cp)
243 *wp++ = *pp++;
244 if (*cp == '<')
245 while (*cp && (*wp++ = *cp++) != '>');
246 else {
247 int incomm = 0;
249 while (*cp && (incomm || !strchr(separators, *cp))) {
250 if (*cp == '\"') {
251 if (cp == ap || *(cp - 1) != '\\') {
252 if (incomm)
253 incomm--;
254 else
255 incomm++;
256 *wp++ = '\"';
257 } else if (cp != ap) {
258 *(wp - 1) = '\"';
260 cp++;
261 continue;
263 *wp++ = *cp++;
266 *wp = '\0';
267 return cp;
271 * For each recipient in the passed name list with a /
272 * in the name, append the message to the end of the named file
273 * and remove him from the recipient list.
275 * Recipients whose name begins with | are piped through the given
276 * program and removed.
278 /*ARGSUSED 3*/
279 struct name *
280 outof(struct name *names, FILE *fo, struct header *hp)
282 int c, lastc;
283 struct name *np, *top;
284 time_t now;
285 char *date, *fname;
286 FILE *fout, *fin;
287 int ispipe;
288 (void)hp;
290 top = names;
291 np = names;
292 time(&now);
293 date = ctime(&now);
294 while (np != NULL) {
295 if (!is_fileaddr(np->n_name) && np->n_name[0] != '|') {
296 np = np->n_flink;
297 continue;
299 ispipe = np->n_name[0] == '|';
300 if (ispipe)
301 fname = np->n_name+1;
302 else
303 fname = expand(np->n_name);
306 * See if we have copied the complete message out yet.
307 * If not, do so.
310 if (image < 0) {
311 char *tempEdit;
313 if ((fout = Ftemp(&tempEdit, "Re", "w", 0600, 1))
314 == NULL) {
315 perror(catgets(catd, CATSET, 146,
316 "temporary edit file"));
317 senderr++;
318 goto cant;
320 image = open(tempEdit, O_RDWR);
321 unlink(tempEdit);
322 Ftfree(&tempEdit);
323 if (image < 0) {
324 perror(catgets(catd, CATSET, 147,
325 "temporary edit file"));
326 senderr++;
327 Fclose(fout);
328 goto cant;
330 fcntl(image, F_SETFD, FD_CLOEXEC);
331 fprintf(fout, "From %s %s", myname, date);
332 c = EOF;
333 while (lastc = c, (c = getc(fo)) != EOF)
334 putc(c, fout);
335 rewind(fo);
336 if (lastc != '\n')
337 putc('\n', fout);
338 putc('\n', fout);
339 fflush(fout);
340 if (ferror(fout))
341 perror(catgets(catd, CATSET, 148,
342 "temporary edit file"));
343 Fclose(fout);
347 * Now either copy "image" to the desired file
348 * or give it as the standard input to the desired
349 * program as appropriate.
352 if (ispipe) {
353 int pid;
354 char *shell;
355 sigset_t nset;
358 * XXX
359 * We can't really reuse the same image file,
360 * because multiple piped recipients will
361 * share the same lseek location and trample
362 * on one another.
364 if ((shell = value("SHELL")) == NULL)
365 shell = SHELL;
366 sigemptyset(&nset);
367 sigaddset(&nset, SIGHUP);
368 sigaddset(&nset, SIGINT);
369 sigaddset(&nset, SIGQUIT);
370 pid = start_command(shell, &nset,
371 image, -1, "-c", fname, NULL);
372 if (pid < 0) {
373 senderr++;
374 goto cant;
376 free_child(pid);
377 } else {
378 int f;
379 if ((fout = Zopen(fname, "a", NULL)) == NULL) {
380 perror(fname);
381 senderr++;
382 goto cant;
384 if ((f = dup(image)) < 0) {
385 perror("dup");
386 fin = NULL;
387 } else
388 fin = Fdopen(f, "r");
389 if (fin == NULL) {
390 fprintf(stderr, catgets(catd, CATSET, 149,
391 "Can't reopen image\n"));
392 Fclose(fout);
393 senderr++;
394 goto cant;
396 rewind(fin);
397 while ((c = getc(fin)) != EOF)
398 putc(c, fout);
399 if (ferror(fout))
400 senderr++, perror(fname);
401 Fclose(fout);
402 Fclose(fin);
404 cant:
406 * In days of old we removed the entry from the
407 * the list; now for sake of header expansion
408 * we leave it in and mark it as deleted.
410 np->n_type |= GDEL;
411 np = np->n_flink;
413 if (image >= 0) {
414 close(image);
415 image = -1;
417 return(top);
421 * Determine if the passed address is a local "send to file" address.
422 * If any of the network metacharacters precedes any slashes, it can't
423 * be a filename. We cheat with .'s to allow path names like ./...
425 int
426 is_fileaddr(char *name)
428 char *cp;
430 if (strchr(name, '@') != NULL)
431 return 0;
432 if (*name == '+')
433 return 1;
434 for (cp = name; *cp; cp++) {
435 if (*cp == '!' || *cp == '%')
436 return 0;
437 if (*cp == '/')
438 return 1;
440 return 0;
443 static int
444 same_name(char *n1, char *n2)
446 int c1, c2;
448 if (value("allnet") != NULL) {
449 do {
450 c1 = (*n1++ & 0377);
451 c2 = (*n2++ & 0377);
452 c1 = lowerconv(c1);
453 c2 = lowerconv(c2);
454 if (c1 != c2)
455 return 0;
456 } while (c1 != '\0' && c2 != '\0' && c1 != '@' && c2 != '@');
457 return 1;
458 } else
459 return asccasecmp(n1, n2) == 0;
463 * Map all of the aliased users in the invoker's mailrc
464 * file and insert them into the list.
465 * Changed after all these months of service to recursively
466 * expand names (2/14/80).
469 struct name *
470 usermap(struct name *names)
472 struct name *new, *np, *cp;
473 struct grouphead *gh;
474 int metoo;
476 new = NULL;
477 np = names;
478 metoo = (value("metoo") != NULL);
479 while (np != NULL) {
480 if (np->n_name[0] == '\\') {
481 cp = np->n_flink;
482 new = put(new, np);
483 np = cp;
484 continue;
486 gh = findgroup(np->n_name);
487 cp = np->n_flink;
488 if (gh != NULL)
489 new = gexpand(new, gh, metoo, np->n_type);
490 else
491 new = put(new, np);
492 np = cp;
494 return(new);
498 * Recursively expand a group name. We limit the expansion to some
499 * fixed level to keep things from going haywire.
500 * Direct recursion is not expanded for convenience.
503 static struct name *
504 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
506 struct group *gp;
507 struct grouphead *ngh;
508 struct name *np;
509 static int depth;
510 char *cp;
512 if (depth > MAXEXP) {
513 printf(catgets(catd, CATSET, 150,
514 "Expanding alias to depth larger than %d\n"), MAXEXP);
515 return(nlist);
517 depth++;
518 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
519 cp = gp->ge_name;
520 if (*cp == '\\')
521 goto quote;
522 if (strcmp(cp, gh->g_name) == 0)
523 goto quote;
524 if ((ngh = findgroup(cp)) != NULL) {
525 nlist = gexpand(nlist, ngh, metoo, ntype);
526 continue;
528 quote:
529 np = nalloc(cp, ntype|GFULL);
531 * At this point should allow to expand
532 * to self if only person in group
534 if (gp == gh->g_list && gp->ge_link == NULL)
535 goto skip;
536 if (!metoo && same_name(cp, myname))
537 np->n_type |= GDEL;
538 skip:
539 nlist = put(nlist, np);
541 depth--;
542 return(nlist);
546 * Concatenate the two passed name lists, return the result.
548 struct name *
549 cat(struct name *n1, struct name *n2)
551 struct name *tail;
553 if (n1 == NULL)
554 return(n2);
555 if (n2 == NULL)
556 return(n1);
557 tail = tailof(n1);
558 tail->n_flink = n2;
559 n2->n_blink = tail;
560 return(n1);
564 * Unpack the name list onto a vector of strings.
565 * Return an error if the name list won't fit.
567 char **
568 unpack(struct name *np)
570 char **ap, **top;
571 struct name *n;
572 int t, extra, metoo, verbose;
574 n = np;
575 if ((t = count(n)) == 0)
576 panic(catgets(catd, CATSET, 151, "No names to unpack"));
578 * Compute the number of extra arguments we will need.
579 * We need at least two extra -- one for "mail" and one for
580 * the terminating 0 pointer. Additional spots may be needed
581 * to pass along -f to the host mailer.
583 extra = 2;
584 extra++;
585 metoo = value("metoo") != NULL;
586 if (metoo)
587 extra++;
588 verbose = value("verbose") != NULL;
589 if (verbose)
590 extra++;
591 /*LINTED*/
592 top = (char **)salloc((t + extra) * sizeof *top);
593 ap = top;
594 *ap++ = "send-mail";
595 *ap++ = "-i";
596 if (metoo)
597 *ap++ = "-m";
598 if (verbose)
599 *ap++ = "-v";
600 for (; n != NULL; n = n->n_flink)
601 if ((n->n_type & GDEL) == 0)
602 *ap++ = n->n_name;
603 *ap = NULL;
604 return(top);
608 * Remove all of the duplicates from the passed name list by
609 * insertion sorting them, then checking for dups.
610 * Return the head of the new list.
612 struct name *
613 elide(struct name *names)
615 struct name *np, *t, *new;
616 struct name *x;
618 if (names == NULL)
619 return(NULL);
620 new = names;
621 np = names;
622 np = np->n_flink;
623 if (np != NULL)
624 np->n_blink = NULL;
625 new->n_flink = NULL;
626 while (np != NULL) {
627 t = new;
628 while (asccasecmp(t->n_name, np->n_name) < 0) {
629 if (t->n_flink == NULL)
630 break;
631 t = t->n_flink;
635 * If we ran out of t's, put the new entry after
636 * the current value of t.
639 if (asccasecmp(t->n_name, np->n_name) < 0) {
640 t->n_flink = np;
641 np->n_blink = t;
642 t = np;
643 np = np->n_flink;
644 t->n_flink = NULL;
645 continue;
649 * Otherwise, put the new entry in front of the
650 * current t. If at the front of the list,
651 * the new guy becomes the new head of the list.
654 if (t == new) {
655 t = np;
656 np = np->n_flink;
657 t->n_flink = new;
658 new->n_blink = t;
659 t->n_blink = NULL;
660 new = t;
661 continue;
665 * The normal case -- we are inserting into the
666 * middle of the list.
669 x = np;
670 np = np->n_flink;
671 x->n_flink = t;
672 x->n_blink = t->n_blink;
673 t->n_blink->n_flink = x;
674 t->n_blink = x;
678 * Now the list headed up by new is sorted.
679 * Go through it and remove duplicates.
682 np = new;
683 while (np != NULL) {
684 t = np;
685 while (t->n_flink != NULL &&
686 asccasecmp(np->n_name, t->n_flink->n_name) == 0)
687 t = t->n_flink;
688 if (t == np || t == NULL) {
689 np = np->n_flink;
690 continue;
694 * Now t points to the last entry with the same name
695 * as np. Make np point beyond t.
698 np->n_flink = t->n_flink;
699 if (t->n_flink != NULL)
700 t->n_flink->n_blink = np;
701 np = np->n_flink;
703 return(new);
707 * Put another node onto a list of names and return
708 * the list.
710 static struct name *
711 put(struct name *list, struct name *node)
713 node->n_flink = list;
714 node->n_blink = NULL;
715 if (list != NULL)
716 list->n_blink = node;
717 return(node);
721 * Determine the number of undeleted elements in
722 * a name list and return it.
724 int
725 count(struct name *np)
727 int c;
729 for (c = 0; np != NULL; np = np->n_flink)
730 if ((np->n_type & GDEL) == 0)
731 c++;
732 return c;
736 * Delete the given name from a namelist.
738 static struct name *
739 delname(struct name *np, char *name)
741 struct name *p;
743 for (p = np; p != NULL; p = p->n_flink)
744 if (same_name(p->n_name, name)) {
745 if (p->n_blink == NULL) {
746 if (p->n_flink != NULL)
747 p->n_flink->n_blink = NULL;
748 np = p->n_flink;
749 continue;
751 if (p->n_flink == NULL) {
752 if (p->n_blink != NULL)
753 p->n_blink->n_flink = NULL;
754 continue;
756 p->n_blink->n_flink = p->n_flink;
757 p->n_flink->n_blink = p->n_blink;
759 return np;
763 * Pretty print a name list
764 * Uncomment it if you need it.
768 void
769 prettyprint(struct name *name)
771 struct name *np;
773 np = name;
774 while (np != NULL) {
775 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
776 np = np->n_flink;
778 fprintf(stderr, "\n");
782 struct name *
783 delete_alternates(struct name *np)
785 struct name *xp;
786 char **ap;
788 np = delname(np, myname);
789 if (altnames)
790 for (ap = altnames; *ap; ap++)
791 np = delname(np, *ap);
792 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
793 while (xp) {
794 np = delname(np, xp->n_name);
795 xp = xp->n_flink;
797 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
798 while (xp) {
799 np = delname(np, xp->n_name);
800 xp = xp->n_flink;
802 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
803 while (xp) {
804 np = delname(np, xp->n_name);
805 xp = xp->n_flink;
807 return np;
811 is_myname(char *name)
813 struct name *xp;
814 char **ap;
816 if (same_name(myname, name))
817 return 1;
818 if (altnames)
819 for (ap = altnames; *ap; ap++)
820 if (same_name(*ap, name))
821 return 1;
822 if ((xp = sextract(value("from"), GEXTRA|GSKIN)) != NULL)
823 while (xp) {
824 if (same_name(xp->n_name, name))
825 return 1;
826 xp = xp->n_flink;
828 if ((xp = sextract(value("replyto"), GEXTRA|GSKIN)) != NULL)
829 while (xp) {
830 if (same_name(xp->n_name, name))
831 return 1;
832 xp = xp->n_flink;
834 if ((xp = sextract(value("sender"), GEXTRA|GSKIN)) != NULL)
835 while (xp) {
836 if (same_name(xp->n_name, name))
837 return 1;
838 xp = xp->n_flink;
840 return 0;