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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)names.c 8.1 (Berkeley) 6/6/93";
39 * Mail -- a mail program
49 * Allocate a single element of a name list,
50 * initialize its name field to the passed
58 register struct name *np;
60 np = (struct name *) salloc(sizeof *np);
64 np->n_name = savestr(str);
69 * Find the tail of a list and return it.
75 register struct name *np;
80 while (np->n_flink != NIL)
86 * Extract a list of names from a line,
87 * and make a list of names from it.
88 * Return the list or NIL if none found.
96 register struct name *top, *np, *t;
99 if (line == NOSTR || *line == '\0')
104 while ((cp = yankword(cp, nbuf)) != NOSTR) {
105 t = nalloc(nbuf, ntype);
117 * Turn a list of names into a string of the same names.
121 register struct name *np;
125 register char *cp, *top;
126 register struct name *p;
129 comma = ntype & GCOMMA;
135 fprintf(stderr, "detract asked to insert commas\n");
136 for (p = np; p != NIL; p = p->n_flink) {
137 if (ntype && (p->n_type & GMASK) != ntype)
139 s += strlen(p->n_name) + 1;
148 for (p = np; p != NIL; p = p->n_flink) {
149 if (ntype && (p->n_type & GMASK) != ntype)
151 cp = copy(p->n_name, cp);
152 if (comma && p->n_flink != NIL)
157 if (comma && *--cp == ',')
163 * Grab a single word (liberal word)
164 * Throw away things between ()'s, and take anything between <>.
170 register char *cp, *cp2;
177 register int nesting = 0;
179 while (*cp != '\0') {
191 } else if (*cp == ' ' || *cp == '\t' || *cp == ',')
197 for (cp2 = wbuf; *cp && (*cp2++ = *cp++) != '>';)
200 for (cp2 = wbuf; *cp && !index(" \t,(", *cp); *cp2++ = *cp++)
207 * For each recipient in the passed name list with a /
208 * in the name, append the message to the end of the named file
209 * and remove him from the recipient list.
211 * Recipients whose name begins with | are piped through the given
212 * program and removed.
221 register struct name *np, *top;
223 char *date, *fname, *ctime();
226 extern char *tempEdit;
233 if (!isfileaddr(np->n_name) && np->n_name[0] != '|') {
237 ispipe = np->n_name[0] == '|';
239 fname = np->n_name+1;
241 fname = expand(np->n_name);
244 * See if we have copied the complete message out yet.
249 if ((fout = Fopen(tempEdit, "a")) == NULL) {
254 image = open(tempEdit, 2);
255 (void) unlink(tempEdit);
262 (void) fcntl(image, F_SETFD, 1);
263 fprintf(fout, "From %s %s", myname, date);
265 GTO|GSUBJECT|GCC|GREPLYTO|GINREPLYTO|GNL);
266 while ((c = getc(fo)) != EOF)
267 (void) putc(c, fout);
269 (void) putc('\n', fout);
277 * Now either copy "image" to the desired file
278 * or give it as the standard input to the desired
279 * program as appropriate.
288 * We can't really reuse the same image file,
289 * because multiple piped recipients will
290 * share the same lseek location and trample
293 if ((shell = value("SHELL")) == NOSTR)
294 shell = _PATH_CSHELL;
295 pid = start_command(shell, sigmask(SIGHUP)|
296 sigmask(SIGINT)|sigmask(SIGQUIT),
297 image, -1, "-c", fname, NOSTR);
305 if ((fout = Fopen(fname, "a")) == NULL) {
310 if ((f = dup(image)) < 0) {
314 fin = Fdopen(f, "r");
316 fprintf(stderr, "Can't reopen image\n");
322 while ((c = getc(fin)) != EOF)
323 (void) putc(c, fout);
325 senderr++, perror(fname);
331 * In days of old we removed the entry from the
332 * the list; now for sake of header expansion
333 * we leave it in and mark it as deleted.
346 * Determine if the passed address is a local "send to file" address.
347 * If any of the network metacharacters precedes any slashes, it can't
348 * be a filename. We cheat with .'s to allow path names like ./...
358 for (cp = name; *cp; cp++) {
359 if (*cp == '!' || *cp == '%' || *cp == '@')
368 * Map all of the aliased users in the invoker's mailrc
369 * file and insert them into the list.
370 * Changed after all these months of service to recursively
371 * expand names (2/14/80).
378 register struct name *new, *np, *cp;
379 struct grouphead *gh;
384 metoo = (value("metoo") != NOSTR);
386 if (np->n_name[0] == '\\') {
392 gh = findgroup(np->n_name);
395 new = gexpand(new, gh, metoo, np->n_type);
404 * Recursively expand a group name. We limit the expansion to some
405 * fixed level to keep things from going haywire.
406 * Direct recursion is not expanded for convenience.
410 gexpand(nlist, gh, metoo, ntype)
412 struct grouphead *gh;
416 struct grouphead *ngh;
421 if (depth > MAXEXP) {
422 printf("Expanding alias to depth larger than %d\n", MAXEXP);
426 for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) {
430 if (strcmp(cp, gh->g_name) == 0)
432 if ((ngh = findgroup(cp)) != NOGRP) {
433 nlist = gexpand(nlist, ngh, metoo, ntype);
437 np = nalloc(cp, ntype);
439 * At this point should allow to expand
440 * to self if only person in group
442 if (gp == gh->g_list && gp->ge_link == NOGE)
444 if (!metoo && strcmp(cp, myname) == 0)
447 nlist = put(nlist, np);
454 * Concatenate the two passed name lists, return the result.
458 struct name *n1, *n2;
460 register struct name *tail;
473 * Unpack the name list onto a vector of strings.
474 * Return an error if the name list won't fit.
480 register char **ap, **top;
481 register struct name *n;
482 int t, extra, metoo, verbose;
485 if ((t = count(n)) == 0)
486 panic("No names to unpack");
488 * Compute the number of extra arguments we will need.
489 * We need at least two extra -- one for "mail" and one for
490 * the terminating 0 pointer. Additional spots may be needed
491 * to pass along -f to the host mailer.
495 metoo = value("metoo") != NOSTR;
498 verbose = value("verbose") != NOSTR;
501 top = (char **) salloc((t + extra) * sizeof *top);
509 for (; n != NIL; n = n->n_flink)
510 if ((n->n_type & GDEL) == 0)
517 * Remove all of the duplicates from the passed name list by
518 * insertion sorting them, then checking for dups.
519 * Return the head of the new list.
525 register struct name *np, *t, *new;
538 while (strcasecmp(t->n_name, np->n_name) < 0) {
539 if (t->n_flink == NIL)
545 * If we ran out of t's, put the new entry after
546 * the current value of t.
549 if (strcasecmp(t->n_name, np->n_name) < 0) {
559 * Otherwise, put the new entry in front of the
560 * current t. If at the front of the list,
561 * the new guy becomes the new head of the list.
575 * The normal case -- we are inserting into the
576 * middle of the list.
582 x->n_blink = t->n_blink;
583 t->n_blink->n_flink = x;
588 * Now the list headed up by new is sorted.
589 * Go through it and remove duplicates.
595 while (t->n_flink != NIL &&
596 strcasecmp(np->n_name, t->n_flink->n_name) == 0)
598 if (t == np || t == NIL) {
604 * Now t points to the last entry with the same name
605 * as np. Make np point beyond t.
608 np->n_flink = t->n_flink;
609 if (t->n_flink != NIL)
610 t->n_flink->n_blink = np;
617 * Put another node onto a list of names and return
622 struct name *list, *node;
624 node->n_flink = list;
627 list->n_blink = node;
632 * Determine the number of undeleted elements in
633 * a name list and return it.
637 register struct name *np;
641 for (c = 0; np != NIL; np = np->n_flink)
642 if ((np->n_type & GDEL) == 0)
648 * Delete the given name from a namelist.
652 register struct name *np;
655 register struct name *p;
657 for (p = np; p != NIL; p = p->n_flink)
658 if (strcasecmp(p->n_name, name) == 0) {
659 if (p->n_blink == NIL) {
660 if (p->n_flink != NIL)
661 p->n_flink->n_blink = NIL;
665 if (p->n_flink == NIL) {
666 if (p->n_blink != NIL)
667 p->n_blink->n_flink = NIL;
670 p->n_blink->n_flink = p->n_flink;
671 p->n_flink->n_blink = p->n_blink;
677 * Pretty print a name list
678 * Uncomment it if you need it.
686 register struct name *np;
690 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
693 fprintf(stderr, "\n");