2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)names.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/mail/names.c,v 1.4.6.3 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/names.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
35 * Mail -- a mail program
45 * Allocate a single element of a name list,
46 * initialize its name field to the passed
50 nalloc(char *str, int ntype)
54 np = (struct name *)salloc(sizeof(*np));
58 np->n_name = savestr(str);
63 * Find the tail of a list and return it.
66 tailof(struct name *name)
73 while (np->n_flink != NULL)
79 * Extract a list of names from a line,
80 * and make a list of names from it.
81 * Return the list or NULL if none found.
84 extract(char *line, int ntype)
87 struct name *top, *np, *t;
89 if (line == NULL || *line == '\0')
91 if ((nbuf = malloc(strlen(line) + 1)) == NULL)
92 err(1, "Out of memory");
96 while ((cp = yankword(cp, nbuf)) != NULL) {
97 t = nalloc(nbuf, ntype);
110 * Turn a list of names into a string of the same names.
113 detract(struct name *np, int ntype)
119 comma = ntype & GCOMMA;
125 fprintf(stderr, "detract asked to insert commas\n");
126 for (p = np; p != NULL; p = p->n_flink) {
127 if (ntype && (p->n_type & GMASK) != ntype)
129 s += strlen(p->n_name) + 1;
138 for (p = np; p != NULL; p = p->n_flink) {
139 if (ntype && (p->n_type & GMASK) != ntype)
141 cp += strlcpy(cp, p->n_name, strlen(p->n_name) + 1);
142 if (comma && p->n_flink != NULL)
147 if (comma && *--cp == ',')
153 * Grab a single word (liberal word)
154 * Throw away things between ()'s, and take anything between <>.
157 yankword(char *ap, char *wbuf)
168 while (*cp != '\0') {
180 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
186 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
189 for (cp2 = wbuf; *cp != '\0' && strchr(" \t,(", *cp) == NULL;
197 * Grab a single login name (liberal word).
198 * Throw away things between ()'s, take anything between <>,
199 * and look for words before metacharacters %, @, !.
202 yanklogin(char *ap, char *wbuf)
204 char *cp, *cp2, *cp_temp;
214 while (*cp != '\0') {
226 } else if (*cp == ' ' || *cp == '\t' || *cp == ',') {
234 * Now, let's go forward till we meet the needed character,
235 * and step one word back.
238 /* First, remember current point. */
243 * Note that we look ahead in a cycle. This is safe, since
244 * non-end of string is checked first.
246 while (*cp != '\0' && strchr("@%!", *(cp + 1)) == NULL)
250 * Now, start stepping back to the first non-word character,
251 * while counting the number of symbols in a word.
253 while (cp != cp_temp && strchr(" \t,<>", *(cp - 1)) == NULL) {
258 /* Finally, grab the word forward. */
270 * For each recipient in the passed name list with a /
271 * in the name, append the message to the end of the named file
272 * and remove him from the recipient list.
274 * Recipients whose name begins with | are piped through the given
275 * program and removed.
278 outof(struct name *names, FILE *fo, struct header *hp)
281 struct name *np, *top;
291 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
295 ispipe = np->n_name[0] == '|';
297 fname = np->n_name+1;
299 fname = expand(np->n_name);
302 * See if we have copied the complete message out yet.
308 char tempname[PATHSIZE];
310 snprintf(tempname, sizeof(tempname),
311 "%s/mail.ReXXXXXXXXXX", tmpdir);
312 if ((fd = mkstemp(tempname)) == -1 ||
313 (fout = Fdopen(fd, "a")) == NULL) {
314 warn("%s", tempname);
318 image = open(tempname, O_RDWR);
321 warn("%s", tempname);
326 fcntl(image, F_SETFD, 1);
327 fprintf(fout, "From %s %s", myname, date);
329 GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
330 while ((c = getc(fo)) != EOF)
336 warn("%s", tempname);
345 * Now either copy "image" to the desired file
346 * or give it as the standard input to the desired
347 * program as appropriate.
357 * We can't really reuse the same image file,
358 * because multiple piped recipients will
359 * share the same lseek location and trample
362 if ((sh = value("SHELL")) == NULL)
365 sigaddset(&nset, SIGHUP);
366 sigaddset(&nset, SIGINT);
367 sigaddset(&nset, SIGQUIT);
368 pid = start_command(sh, &nset, image, -1, "-c", fname,
377 if ((fout = Fopen(fname, "a")) == NULL) {
382 if ((f = dup(image)) < 0) {
386 fin = Fdopen(f, "r");
388 fprintf(stderr, "Can't reopen image\n");
394 while ((c = getc(fin)) != EOF)
408 * In days of old we removed the entry from the
409 * the list; now for sake of header expansion
410 * we leave it in and mark it as deleted.
423 * Determine if the passed address is a local "send to file" address.
424 * If any of the network metacharacters precedes any slashes, it can't
425 * be a filename. We cheat with .'s to allow path names like ./...
428 isfileaddr(char *name)
434 for (cp = name; *cp != '\0'; cp++) {
435 if (*cp == '!' || *cp == '%' || *cp == '@')
444 * Map all of the aliased users in the invoker's mailrc
445 * file and insert them into the list.
446 * Changed after all these months of service to recursively
447 * expand names (2/14/80).
451 usermap(struct name *names)
453 struct name *new, *np, *cp;
454 struct grouphead *gh;
459 metoo = (value("metoo") != NULL);
461 if (np->n_name[0] == '\\') {
467 gh = findgroup(np->n_name);
470 new = gexpand(new, gh, metoo, np->n_type);
479 * Recursively expand a group name. We limit the expansion to some
480 * fixed level to keep things from going haywire.
481 * Direct recursion is not expanded for convenience.
485 gexpand(struct name *nlist, struct grouphead *gh, int metoo, int ntype)
488 struct grouphead *ngh;
493 if (depth > MAXEXP) {
494 printf("Expanding alias to depth larger than %d\n", MAXEXP);
498 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) {
502 if (strcmp(cp, gh->g_name) == 0)
504 if ((ngh = findgroup(cp)) != NULL) {
505 nlist = gexpand(nlist, ngh, metoo, ntype);
509 np = nalloc(cp, ntype);
511 * At this point should allow to expand
512 * to self if only person in group
514 if (gp == gh->g_list && gp->ge_link == NULL)
516 if (!metoo && strcmp(cp, myname) == 0)
519 nlist = put(nlist, np);
526 * Concatenate the two passed name lists, return the result.
529 cat(struct name *n1, struct name *n2)
544 * Unpack the name list onto a vector of strings.
545 * Return an error if the name list won't fit.
548 unpack(struct name *np)
552 int t, extra, metoo, verbose;
555 if ((t = count(n)) == 0)
556 errx(1, "No names to unpack");
558 * Compute the number of extra arguments we will need.
559 * We need at least two extra -- one for "mail" and one for
560 * the terminating 0 pointer. Additional spots may be needed
561 * to pass along -f to the host mailer.
565 metoo = value("metoo") != NULL;
568 verbose = value("verbose") != NULL;
571 top = (char **)salloc((t + extra) * sizeof(*top));
579 for (; n != NULL; n = n->n_flink)
580 if ((n->n_type & GDEL) == 0)
587 * Remove all of the duplicates from the passed name list by
588 * insertion sorting them, then checking for dups.
589 * Return the head of the new list.
592 elide(struct name *names)
594 struct name *np, *t, *new;
607 while (strcasecmp(t->n_name, np->n_name) < 0) {
608 if (t->n_flink == NULL)
614 * If we ran out of t's, put the new entry after
615 * the current value of t.
618 if (strcasecmp(t->n_name, np->n_name) < 0) {
628 * Otherwise, put the new entry in front of the
629 * current t. If at the front of the list,
630 * the new guy becomes the new head of the list.
644 * The normal case -- we are inserting into the
645 * middle of the list.
651 x->n_blink = t->n_blink;
652 t->n_blink->n_flink = x;
657 * Now the list headed up by new is sorted.
658 * Go through it and remove duplicates.
664 while (t->n_flink != NULL &&
665 strcasecmp(np->n_name, t->n_flink->n_name) == 0)
667 if (t == np || t == NULL) {
673 * Now t points to the last entry with the same name
674 * as np. Make np point beyond t.
677 np->n_flink = t->n_flink;
678 if (t->n_flink != NULL)
679 t->n_flink->n_blink = np;
686 * Put another node onto a list of names and return
690 put(struct name *list, struct name *node)
692 node->n_flink = list;
693 node->n_blink = NULL;
695 list->n_blink = node;
700 * Determine the number of undeleted elements in
701 * a name list and return it.
704 count(struct name *np)
708 for (c = 0; np != NULL; np = np->n_flink)
709 if ((np->n_type & GDEL) == 0)
715 * Delete the given name from a namelist.
718 delname(struct name *np, char *name)
722 for (p = np; p != NULL; p = p->n_flink)
723 if (strcasecmp(p->n_name, name) == 0) {
724 if (p->n_blink == NULL) {
725 if (p->n_flink != NULL)
726 p->n_flink->n_blink = NULL;
730 if (p->n_flink == NULL) {
731 if (p->n_blink != NULL)
732 p->n_blink->n_flink = NULL;
735 p->n_blink->n_flink = p->n_flink;
736 p->n_flink->n_blink = p->n_blink;
742 * Pretty print a name list
743 * Uncomment it if you need it.
748 prettyprint(struct name *name)
754 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
757 fprintf(stderr, "\n");