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.
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
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
42 static char sccsid
[] = "@(#)names.c 2.22 (gritter) 3/4/06";
47 * Mail -- a mail program
59 static struct name
*tailof(struct name
*name
);
60 static struct name
*extract1(char *line
, enum gfield ntype
, char *separators
,
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
75 nalloc(char *str
, enum gfield ntype
)
81 np
= (struct name
*)salloc(sizeof *np
);
86 np
->n_name
= savestr(skin(str
));
87 if (strcmp(np
->n_name
, str
)) {
90 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
91 np
->n_fullname
= savestr(out
.s
);
94 np
->n_fullname
= np
->n_name
;
95 } else if (ntype
& GSKIN
)
96 np
->n_fullname
= np
->n_name
= savestr(skin(str
));
98 np
->n_fullname
= np
->n_name
= savestr(str
);
103 * Find the tail of a list and return it.
106 tailof(struct name
*name
)
113 while (np
->n_flink
!= NULL
)
119 * Extract a list of names from a line,
120 * and make a list of names from it.
121 * Return the list or NULL if none found.
124 extract(char *line
, enum gfield ntype
)
126 return extract1(line
, ntype
, " \t,(", 0);
130 sextract(char *line
, enum gfield ntype
)
132 if (line
&& strpbrk(line
, ",\"\\(<"))
133 return extract1(line
, ntype
, ",", 1);
135 return extract(line
, ntype
);
139 extract1(char *line
, enum gfield ntype
, char *separators
, int copypfx
)
142 struct name
*top
, *np
, *t
;
144 if (line
== NULL
|| *line
== '\0')
149 nbuf
= ac_alloc(strlen(line
) + 1);
150 while ((cp
= yankword(cp
, nbuf
, separators
, copypfx
)) != NULL
) {
151 t
= nalloc(nbuf
, ntype
);
164 * Turn a list of names into a string of the same names.
167 detract(struct name
*np
, enum gfield ntype
)
174 comma
= ntype
& GCOMMA
;
179 if ((debug
|| value("debug")) && comma
)
180 fprintf(stderr
, catgets(catd
, CATSET
, 145,
181 "detract asked to insert commas\n"));
182 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
183 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
185 s
+= strlen(p
->n_fullname
) + 1;
194 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
195 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
197 cp
= sstpcpy(cp
, p
->n_fullname
);
198 if (comma
&& p
->n_flink
!= NULL
)
203 if (comma
&& *--cp
== ',')
209 * Grab a single word (liberal word)
210 * Throw away things between ()'s, and take anything between <>.
213 yankword(char *ap
, char *wbuf
, char *separators
, int copypfx
)
219 while (blankchar(*cp
& 0377) || *cp
== ',')
222 if ((cp
= nexttoken(cp
)) == NULL
)
228 while (*cp
&& (*wp
++ = *cp
++) != '>');
232 while (*cp
&& (incomm
|| !strchr(separators
, *cp
))) {
234 if (cp
== ap
|| *(cp
- 1) != '\\') {
240 } else if (cp
!= ap
) {
254 * For each recipient in the passed name list with a /
255 * in the name, append the message to the end of the named file
256 * and remove him from the recipient list.
258 * Recipients whose name begins with | are piped through the given
259 * program and removed.
263 outof(struct name
*names
, FILE *fo
, struct header
*hp
)
266 struct name
*np
, *top
;
278 if (!is_fileaddr(np
->n_name
) && np
->n_name
[0] != '|') {
282 ispipe
= np
->n_name
[0] == '|';
284 fname
= np
->n_name
+1;
286 fname
= expand(np
->n_name
);
289 * See if we have copied the complete message out yet.
296 if ((fout
= Ftemp(&tempEdit
, "Re", "w", 0600, 1))
298 perror(catgets(catd
, CATSET
, 146,
299 "temporary edit file"));
303 image
= open(tempEdit
, O_RDWR
);
307 perror(catgets(catd
, CATSET
, 147,
308 "temporary edit file"));
313 fcntl(image
, F_SETFD
, FD_CLOEXEC
);
314 fprintf(fout
, "From %s %s", myname
, date
);
316 while (lastc
= c
, (c
= getc(fo
)) != EOF
)
324 perror(catgets(catd
, CATSET
, 148,
325 "temporary edit file"));
330 * Now either copy "image" to the desired file
331 * or give it as the standard input to the desired
332 * program as appropriate.
342 * We can't really reuse the same image file,
343 * because multiple piped recipients will
344 * share the same lseek location and trample
347 if ((shell
= value("SHELL")) == NULL
)
350 sigaddset(&nset
, SIGHUP
);
351 sigaddset(&nset
, SIGINT
);
352 sigaddset(&nset
, SIGQUIT
);
353 pid
= start_command(shell
, &nset
,
354 image
, -1, "-c", fname
, NULL
);
362 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
367 if ((f
= dup(image
)) < 0) {
371 fin
= Fdopen(f
, "r");
373 fprintf(stderr
, catgets(catd
, CATSET
, 149,
374 "Can't reopen image\n"));
380 while ((c
= getc(fin
)) != EOF
)
383 senderr
++, perror(fname
);
389 * In days of old we removed the entry from the
390 * the list; now for sake of header expansion
391 * we leave it in and mark it as deleted.
404 * Determine if the passed address is a local "send to file" address.
405 * If any of the network metacharacters precedes any slashes, it can't
406 * be a filename. We cheat with .'s to allow path names like ./...
409 is_fileaddr(char *name
)
413 if (strchr(name
, '@') != NULL
)
417 for (cp
= name
; *cp
; cp
++) {
418 if (*cp
== '!' || *cp
== '%')
427 same_name(char *n1
, char *n2
)
431 if (value("allnet") != NULL
) {
439 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
442 return asccasecmp(n1
, n2
) == 0;
446 * Map all of the aliased users in the invoker's mailrc
447 * file and insert them into the list.
448 * Changed after all these months of service to recursively
449 * expand names (2/14/80).
453 usermap(struct name
*names
)
455 struct name
*new, *np
, *cp
;
456 struct grouphead
*gh
;
461 metoo
= (value("metoo") != NULL
);
463 if (np
->n_name
[0] == '\\') {
469 gh
= findgroup(np
->n_name
);
472 new = gexpand(new, gh
, metoo
, np
->n_type
);
481 * Recursively expand a group name. We limit the expansion to some
482 * fixed level to keep things from going haywire.
483 * Direct recursion is not expanded for convenience.
487 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
490 struct grouphead
*ngh
;
495 if (depth
> MAXEXP
) {
496 printf(catgets(catd
, CATSET
, 150,
497 "Expanding alias to depth larger than %d\n"), MAXEXP
);
501 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
505 if (strcmp(cp
, gh
->g_name
) == 0)
507 if ((ngh
= findgroup(cp
)) != NULL
) {
508 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
512 np
= nalloc(cp
, ntype
|GFULL
);
514 * At this point should allow to expand
515 * to self if only person in group
517 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
519 if (!metoo
&& same_name(cp
, myname
))
522 nlist
= put(nlist
, np
);
529 * Concatenate the two passed name lists, return the result.
532 cat(struct name
*n1
, struct name
*n2
)
547 * Unpack the name list onto a vector of strings.
548 * Return an error if the name list won't fit.
551 unpack(struct name
*np
)
555 int t
, extra
, metoo
, verbose
;
558 if ((t
= count(n
)) == 0)
559 panic(catgets(catd
, CATSET
, 151, "No names to unpack"));
561 * Compute the number of extra arguments we will need.
562 * We need at least two extra -- one for "mail" and one for
563 * the terminating 0 pointer. Additional spots may be needed
564 * to pass along -f to the host mailer.
568 metoo
= value("metoo") != NULL
;
571 verbose
= value("verbose") != NULL
;
575 top
= (char **)salloc((t
+ extra
) * sizeof *top
);
583 for (; n
!= NULL
; n
= n
->n_flink
)
584 if ((n
->n_type
& GDEL
) == 0)
591 * Remove all of the duplicates from the passed name list by
592 * insertion sorting them, then checking for dups.
593 * Return the head of the new list.
596 elide(struct name
*names
)
598 struct name
*np
, *t
, *new;
611 while (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
612 if (t
->n_flink
== NULL
)
618 * If we ran out of t's, put the new entry after
619 * the current value of t.
622 if (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
632 * Otherwise, put the new entry in front of the
633 * current t. If at the front of the list,
634 * the new guy becomes the new head of the list.
648 * The normal case -- we are inserting into the
649 * middle of the list.
655 x
->n_blink
= t
->n_blink
;
656 t
->n_blink
->n_flink
= x
;
661 * Now the list headed up by new is sorted.
662 * Go through it and remove duplicates.
668 while (t
->n_flink
!= NULL
&&
669 asccasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
671 if (t
== np
|| t
== NULL
) {
677 * Now t points to the last entry with the same name
678 * as np. Make np point beyond t.
681 np
->n_flink
= t
->n_flink
;
682 if (t
->n_flink
!= NULL
)
683 t
->n_flink
->n_blink
= np
;
690 * Put another node onto a list of names and return
694 put(struct name
*list
, struct name
*node
)
696 node
->n_flink
= list
;
697 node
->n_blink
= NULL
;
699 list
->n_blink
= node
;
704 * Determine the number of undeleted elements in
705 * a name list and return it.
708 count(struct name
*np
)
712 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
713 if ((np
->n_type
& GDEL
) == 0)
719 * Delete the given name from a namelist.
722 delname(struct name
*np
, char *name
)
726 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
727 if (same_name(p
->n_name
, name
)) {
728 if (p
->n_blink
== NULL
) {
729 if (p
->n_flink
!= NULL
)
730 p
->n_flink
->n_blink
= NULL
;
734 if (p
->n_flink
== NULL
) {
735 if (p
->n_blink
!= NULL
)
736 p
->n_blink
->n_flink
= NULL
;
739 p
->n_blink
->n_flink
= p
->n_flink
;
740 p
->n_flink
->n_blink
= p
->n_blink
;
746 * Pretty print a name list
747 * Uncomment it if you need it.
752 prettyprint(struct name *name)
758 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
761 fprintf(stderr, "\n");
766 delete_alternates(struct name
*np
)
771 np
= delname(np
, myname
);
773 for (ap
= altnames
; *ap
; ap
++)
774 np
= delname(np
, *ap
);
775 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
777 np
= delname(np
, xp
->n_name
);
780 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
782 np
= delname(np
, xp
->n_name
);
785 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
787 np
= delname(np
, xp
->n_name
);
794 is_myname(char *name
)
799 if (same_name(myname
, name
))
802 for (ap
= altnames
; *ap
; ap
++)
803 if (same_name(*ap
, name
))
805 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
807 if (same_name(xp
->n_name
, name
))
811 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
813 if (same_name(xp
->n_name
, name
))
817 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
819 if (same_name(xp
->n_name
, name
))