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.
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
41 * Mail -- a mail program
53 static struct name
* tailof(struct name
*name
);
54 static struct name
* extract1(char *line
, enum gfield ntype
,
55 char *separators
, int copypfx
);
56 static char * yankword(char *ap
, char *wbuf
, char *separators
,
58 static int same_name(char *n1
, char *n2
);
59 static struct name
* gexpand(struct name
*nlist
, struct grouphead
*gh
,
60 int metoo
, int ntype
);
61 static struct name
* put(struct name
*list
, struct name
*node
);
62 static struct name
* delname(struct name
*np
, char *name
);
65 * Allocate a single element of a name list,
66 * initialize its name field to the passed
70 nalloc(char *str
, enum gfield ntype
)
76 np
= (struct name
*)salloc(sizeof *np
);
82 (void)addrspec_with_guts((ntype
& (GFULL
|GSKIN
|GREF
)) != 0, str
, &ag
);
83 if ((ag
.ag_n_flags
& NAME_NAME_SALLOC
) == 0) {
84 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
85 ag
.ag_skinned
= savestr(ag
.ag_skinned
);
87 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
88 np
->n_flags
= ag
.ag_n_flags
;
91 if (ag
.ag_ilen
!= ag
.ag_slen
) {
94 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
95 np
->n_fullname
= savestr(out
.s
);
97 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
99 } else if (ntype
& GREF
) { /* TODO LEGACY */
100 /* TODO Unfortunately we had to skin GREFerences i.e. the
101 * TODO surrounding angle brackets have been stripped away.
102 * TODO Necessarily since otherwise the plain address check
103 * TODO fails due to them; insert them back so that valid
104 * TODO headers will be created */
105 np
->n_fullname
= np
->n_name
= str
= salloc(ag
.ag_slen
+ 2 + 1);
107 memcpy(str
, ag
.ag_skinned
, ag
.ag_slen
);
116 ndup(struct name
*np
, enum gfield ntype
)
120 if ((ntype
& (GFULL
|GSKIN
)) && (np
->n_flags
& NAME_SKINNED
) == 0) {
121 nnp
= nalloc(np
->n_name
, ntype
);
125 nnp
= (struct name
*)salloc(sizeof *np
);
126 nnp
->n_flink
= nnp
->n_blink
= NULL
;
128 nnp
->n_flags
= (np
->n_flags
&
129 ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
131 nnp
->n_name
= savestr(np
->n_name
);
132 if (np
->n_name
== np
->n_fullname
|| (ntype
& (GFULL
|GSKIN
)) == 0)
133 nnp
->n_fullname
= nnp
->n_name
;
135 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
136 nnp
->n_fullname
= savestr(np
->n_fullname
);
143 * Find the tail of a list and return it.
146 tailof(struct name
*name
)
153 while (np
->n_flink
!= NULL
)
159 * Extract a list of names from a line,
160 * and make a list of names from it.
161 * Return the list or NULL if none found.
164 extract(char *line
, enum gfield ntype
)
166 return extract1(line
, ntype
, " \t,(", 0);
170 sextract(char *line
, enum gfield ntype
)
172 if (line
&& strpbrk(line
, ",\"\\(<"))
173 return extract1(line
, ntype
, ",", 1);
175 return extract(line
, ntype
);
179 lextract(char *line
, enum gfield ntype
)
181 return (extract1(line
, ntype
, ",", 1));
185 extract1(char *line
, enum gfield ntype
, char *separators
, int copypfx
)
188 struct name
*top
, *np
, *t
;
190 if (line
== NULL
|| *line
== '\0')
195 nbuf
= ac_alloc(strlen(line
) + 1);
196 while ((cp
= yankword(cp
, nbuf
, separators
, copypfx
)) != NULL
) {
197 t
= nalloc(nbuf
, ntype
);
210 * Turn a list of names into a string of the same names.
213 detract(struct name
*np
, enum gfield ntype
)
220 comma
= ntype
& GCOMMA
;
225 if ((debug
|| value("debug")) && comma
)
226 fprintf(stderr
, catgets(catd
, CATSET
, 145,
227 "detract asked to insert commas\n"));
228 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
229 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
231 s
+= strlen(p
->n_fullname
) + 1;
240 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
241 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
243 cp
= sstpcpy(cp
, p
->n_fullname
);
244 if (comma
&& p
->n_flink
!= NULL
)
249 if (comma
&& *--cp
== ',')
255 * Grab a single word (liberal word)
256 * Throw away things between ()'s, and take anything between <>.
257 * Strip trailing whitespace as *ap* may come directly from user.
260 yankword(char *ap
, char *wbuf
, char *separators
, int copypfx
)
266 while (blankspacechar(*cp
) || *cp
== ',')
269 if ((cp
= nexttoken(cp
)) == NULL
)
275 while (*cp
&& (*wp
++ = *cp
++) != '>');
279 while (*cp
&& (incomm
|| !strchr(separators
, *cp
))) {
281 if (cp
== ap
|| *(cp
- 1) != '\\') {
287 } else if (cp
!= ap
) {
296 while (wp
> wbuf
&& blankspacechar(wp
[-1]))
303 * Check all addresses in np and delete invalid ones.
306 checkaddrs(struct name
*np
)
310 for (n
= np
; n
!= NULL
;) {
311 if (is_addr_invalid(n
, 1)) {
313 n
->n_blink
->n_flink
= n
->n_flink
;
315 n
->n_flink
->n_blink
= n
->n_blink
;
325 * For each recipient in the passed name list with a /
326 * in the name, append the message to the end of the named file
327 * and remove him from the recipient list.
329 * Recipients whose name begins with | are piped through the given
330 * program and removed.
333 outof(struct name
*names
, FILE *fo
, struct header
*hp
)
335 int pipecnt
, xcnt
, *fda
, i
;
339 FILE *fin
= NULL
, *fout
;
343 * Look through all recipients and do a quick return if no file or pipe
344 * addressee is found.
346 fda
= NULL
; /* Silence cc */
347 for (pipecnt
= xcnt
= 0, np
= names
; np
!= NULL
; np
= np
->n_flink
)
348 switch (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
) {
349 case NAME_ADDRSPEC_ISFILE
:
352 case NAME_ADDRSPEC_ISPIPE
:
356 if (pipecnt
== 0 && xcnt
== 0)
360 * Otherwise create an array of file descriptors for each found pipe
361 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
362 * each pipe subprocess needs its very own file descriptor, and we need
364 * To make our life a bit easier let's just use the auto-reclaimed
371 fda
= (int*)salloc(sizeof(int) * pipecnt
);
372 for (i
= 0; i
< pipecnt
; ++i
)
374 if ((shell
= value("SHELL")) == NULL
)
381 for (np
= names
; np
!= NULL
;) {
382 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
389 * See if we have copied the complete message out yet.
396 if ((fout
= Ftemp(&tempEdit
, "Re", "w", 0600, 1))
398 perror(tr(146, "Creation of temporary image"));
402 image
= open(tempEdit
, O_RDWR
);
404 for (i
= 0; i
< pipecnt
; ++i
) {
405 int fd
= open(tempEdit
, O_RDONLY
);
413 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
418 perror(tr(147, "Creating descriptor duplicate "
419 "of temporary image"));
424 fcntl(image
, F_SETFD
, FD_CLOEXEC
);
426 fprintf(fout
, "From %s %s", myname
, date
);
428 while (i
= c
, (c
= getc(fo
)) != EOF
)
436 perror(tr(148, "Finalizing write of temporary "
440 /* If we have to serve file addressees, open reader */
441 if (xcnt
!= 0 && (fin
= Fdopen(image
, "r")) == NULL
) {
442 perror(tr(149, "Failed to open a duplicate of "
443 "the temporary image"));
450 /* From now on use xcnt as a counter for pipecnt */
455 * Now either copy "image" to the desired file
456 * or give it as the standard input to the desired
457 * program as appropriate.
460 if (np
->n_flags
& NAME_ADDRSPEC_ISPIPE
) {
465 sigaddset(&nset
, SIGHUP
);
466 sigaddset(&nset
, SIGINT
);
467 sigaddset(&nset
, SIGQUIT
);
468 pid
= start_command(shell
, &nset
,
469 fda
[xcnt
++], -1, "-c", np
->n_name
+ 1, NULL
);
476 char *fname
= file_expand(np
->n_name
);
477 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
483 while ((i
= getc(fin
)) != EOF
)
493 * In days of old we removed the entry from the
494 * the list; now for sake of header expansion
495 * we leave it in and mark it as deleted.
505 for (i
= 0; i
< pipecnt
; ++i
)
515 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
524 same_name(char *n1
, char *n2
)
528 if (value("allnet") != NULL
) {
536 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
539 return asccasecmp(n1
, n2
) == 0;
543 * Map all of the aliased users in the invoker's mailrc
544 * file and insert them into the list.
545 * Changed after all these months of service to recursively
546 * expand names (2/14/80).
550 usermap(struct name
*names
)
552 struct name
*new, *np
, *cp
;
553 struct grouphead
*gh
;
558 metoo
= (value("metoo") != NULL
);
560 if (np
->n_name
[0] == '\\') {
566 gh
= findgroup(np
->n_name
);
569 new = gexpand(new, gh
, metoo
, np
->n_type
);
578 * Recursively expand a group name. We limit the expansion to some
579 * fixed level to keep things from going haywire.
580 * Direct recursion is not expanded for convenience.
584 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
587 struct grouphead
*ngh
;
592 if (depth
> MAXEXP
) {
593 printf(catgets(catd
, CATSET
, 150,
594 "Expanding alias to depth larger than %d\n"), MAXEXP
);
598 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
602 if (strcmp(cp
, gh
->g_name
) == 0)
604 if ((ngh
= findgroup(cp
)) != NULL
) {
605 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
609 np
= nalloc(cp
, ntype
|GFULL
);
611 * At this point should allow to expand
612 * to self if only person in group
614 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
616 if (!metoo
&& same_name(cp
, myname
))
619 nlist
= put(nlist
, np
);
626 * Concatenate the two passed name lists, return the result.
629 cat(struct name
*n1
, struct name
*n2
)
644 * Unpack the name list onto a vector of strings.
645 * Return an error if the name list won't fit.
648 unpack(struct name
*np
)
652 int t
, extra
, metoo
, verbose
;
655 if ((t
= count(n
)) == 0)
656 panic(catgets(catd
, CATSET
, 151, "No names to unpack"));
658 * Compute the number of extra arguments we will need.
659 * We need at least two extra -- one for "mail" and one for
660 * the terminating 0 pointer. Additional spots may be needed
661 * to pass along -f to the host mailer.
665 metoo
= value("metoo") != NULL
;
668 verbose
= value("verbose") != NULL
;
672 top
= (char **)salloc((t
+ extra
) * sizeof *top
);
680 for (; n
!= NULL
; n
= n
->n_flink
)
681 if ((n
->n_type
& GDEL
) == 0)
688 * Remove all of the duplicates from the passed name list by
689 * insertion sorting them, then checking for dups.
690 * Return the head of the new list.
693 elide(struct name
*names
)
695 struct name
*np
, *t
, *newn
, *x
;
699 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
700 for (newn
= np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
701 if ((names
->n_type
& GDEL
) == 0) {
715 newn
->n_flink
= NULL
;
719 while (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
720 if (t
->n_flink
== NULL
)
726 * If we ran out of t's, put the new entry after
727 * the current value of t.
730 if (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
740 * Otherwise, put the new entry in front of the
741 * current t. If at the front of the list,
742 * the new guy becomes the new head of the list.
756 * The normal case -- we are inserting into the
757 * middle of the list.
763 x
->n_blink
= t
->n_blink
;
764 t
->n_blink
->n_flink
= x
;
769 * Now the list headed up by new is sorted.
770 * Go through it and remove duplicates.
776 while (t
->n_flink
!= NULL
&&
777 asccasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
779 if (t
== np
|| t
== NULL
) {
785 * Now t points to the last entry with the same name
786 * as np. Make np point beyond t.
789 np
->n_flink
= t
->n_flink
;
790 if (t
->n_flink
!= NULL
)
791 t
->n_flink
->n_blink
= np
;
798 * Put another node onto a list of names and return
802 put(struct name
*list
, struct name
*node
)
804 node
->n_flink
= list
;
805 node
->n_blink
= NULL
;
807 list
->n_blink
= node
;
812 * Determine the number of undeleted elements in
813 * a name list and return it.
816 count(struct name
*np
)
820 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
821 if ((np
->n_type
& GDEL
) == 0)
827 * Delete the given name from a namelist.
830 delname(struct name
*np
, char *name
)
834 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
835 if (same_name(p
->n_name
, name
)) {
836 if (p
->n_blink
== NULL
) {
837 if (p
->n_flink
!= NULL
)
838 p
->n_flink
->n_blink
= NULL
;
842 if (p
->n_flink
== NULL
) {
843 if (p
->n_blink
!= NULL
)
844 p
->n_blink
->n_flink
= NULL
;
847 p
->n_blink
->n_flink
= p
->n_flink
;
848 p
->n_flink
->n_blink
= p
->n_blink
;
854 * Pretty print a name list
855 * Uncomment it if you need it.
860 prettyprint(struct name *name)
866 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
869 fprintf(stderr, "\n");
874 delete_alternates(struct name
*np
)
879 np
= delname(np
, myname
);
881 for (ap
= altnames
; *ap
; ap
++)
882 np
= delname(np
, *ap
);
883 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
885 np
= delname(np
, xp
->n_name
);
888 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
890 np
= delname(np
, xp
->n_name
);
893 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
895 np
= delname(np
, xp
->n_name
);
902 is_myname(char *name
)
907 if (same_name(myname
, name
))
910 for (ap
= altnames
; *ap
; ap
++)
911 if (same_name(*ap
, name
))
913 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
915 if (same_name(xp
->n_name
, name
))
919 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
921 if (same_name(xp
->n_name
, name
))
925 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
927 if (same_name(xp
->n_name
, name
))