xref: /illumos-gate/usr/src/cmd/mail/goback.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
30*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "mail.h"
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate 	This routine returns undeliverable mail as well as handles
36*7c478bd9Sstevel@tonic-gate 	replying to letters
37*7c478bd9Sstevel@tonic-gate */
38*7c478bd9Sstevel@tonic-gate void
39*7c478bd9Sstevel@tonic-gate goback(letnum)
40*7c478bd9Sstevel@tonic-gate int	letnum;
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	static char pn[] = "goback";
43*7c478bd9Sstevel@tonic-gate 	int	i, w;
44*7c478bd9Sstevel@tonic-gate 	char	buf[1024], *cp, work[1024], wuser[1024];
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	/*
47*7c478bd9Sstevel@tonic-gate 	 * If dflag already == 1, then been here already and
48*7c478bd9Sstevel@tonic-gate 	 * having a problem delivering failure notification.
49*7c478bd9Sstevel@tonic-gate 	 * Reset dflag to 9 to avoid endless loop.....
50*7c478bd9Sstevel@tonic-gate 	 */
51*7c478bd9Sstevel@tonic-gate 	if (dflag == 1) {
52*7c478bd9Sstevel@tonic-gate 		dflag = 9;
53*7c478bd9Sstevel@tonic-gate 		Dout(pn, 0, "dflag = %d\n", dflag);
54*7c478bd9Sstevel@tonic-gate 		if (!error) {
55*7c478bd9Sstevel@tonic-gate 			error = E_REMOTE;
56*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "error = %d\n", error);
57*7c478bd9Sstevel@tonic-gate 		}
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (dflag < 2) {
61*7c478bd9Sstevel@tonic-gate 		work[0] = '\0';
62*7c478bd9Sstevel@tonic-gate 		wuser[0] = '\0';
63*7c478bd9Sstevel@tonic-gate 		fclose(tmpf);
64*7c478bd9Sstevel@tonic-gate 		if (!replying) {
65*7c478bd9Sstevel@tonic-gate 			dflag = 1;
66*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "dflag = 1\n");
67*7c478bd9Sstevel@tonic-gate 			if ((debug > 0) && (keepdbgfile == 0)) {
68*7c478bd9Sstevel@tonic-gate 				keepdbgfile++;
69*7c478bd9Sstevel@tonic-gate 			}
70*7c478bd9Sstevel@tonic-gate 			if (ckdlivopts(H_TCOPY, (int *)0) & IGNORE) {
71*7c478bd9Sstevel@tonic-gate 				goto skipsend;
72*7c478bd9Sstevel@tonic-gate 			}
73*7c478bd9Sstevel@tonic-gate 		}
74*7c478bd9Sstevel@tonic-gate 		tmpf = doopen(lettmp, "r+", E_TMP);
75*7c478bd9Sstevel@tonic-gate 		if (replying) {
76*7c478bd9Sstevel@tonic-gate 			fseek(tmpf, let[letnum].adr, 0);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 		for (fgets(line, LSIZE, tmpf);
79*7c478bd9Sstevel@tonic-gate 		strncmp(line, header[H_FROM].tag, strlen(header[H_FROM].tag))
80*7c478bd9Sstevel@tonic-gate 		    == SAME ||
81*7c478bd9Sstevel@tonic-gate 		    strncmp(line, header[H_FROM1].tag,
82*7c478bd9Sstevel@tonic-gate 			strlen(header[H_FROM1].tag)) == SAME; ) {
83*7c478bd9Sstevel@tonic-gate 			if ((i = substr(line, "remote from")) != -1) {
84*7c478bd9Sstevel@tonic-gate 				for (i = 0, cp = strrchr(line, ' ') + 1;
85*7c478bd9Sstevel@tonic-gate 					*cp != '\n';
86*7c478bd9Sstevel@tonic-gate 					cp++) {
87*7c478bd9Sstevel@tonic-gate 					buf[i++] = *cp;
88*7c478bd9Sstevel@tonic-gate 				}
89*7c478bd9Sstevel@tonic-gate 				buf[i++] = '!';
90*7c478bd9Sstevel@tonic-gate 				buf[i] = '\0';
91*7c478bd9Sstevel@tonic-gate 				strcat(work, buf);
92*7c478bd9Sstevel@tonic-gate 				if (line[0] == '>') {
93*7c478bd9Sstevel@tonic-gate 					i = 6;
94*7c478bd9Sstevel@tonic-gate 				} else {
95*7c478bd9Sstevel@tonic-gate 					i = 5;
96*7c478bd9Sstevel@tonic-gate 				}
97*7c478bd9Sstevel@tonic-gate 				for (w = i; line[w] != ' '; w++) {
98*7c478bd9Sstevel@tonic-gate 					wuser[w-i] = line[w];
99*7c478bd9Sstevel@tonic-gate 				}
100*7c478bd9Sstevel@tonic-gate 				wuser[w-i] = '\0';
101*7c478bd9Sstevel@tonic-gate 			} else if ((i = substr(line, "forwarded by")) == -1) {
102*7c478bd9Sstevel@tonic-gate 				if (line[0] == '>') {
103*7c478bd9Sstevel@tonic-gate 					break;
104*7c478bd9Sstevel@tonic-gate 				} else {
105*7c478bd9Sstevel@tonic-gate 					i = 5;
106*7c478bd9Sstevel@tonic-gate 				}
107*7c478bd9Sstevel@tonic-gate 				for (w = i; line[w] != ' '; w++) {
108*7c478bd9Sstevel@tonic-gate 					wuser[w-i] = line[w];
109*7c478bd9Sstevel@tonic-gate 				}
110*7c478bd9Sstevel@tonic-gate 				wuser[w-i] = '\0';
111*7c478bd9Sstevel@tonic-gate 			} else if ((i = substr(line, "forwarded by")) > -1) {
112*7c478bd9Sstevel@tonic-gate 				break;
113*7c478bd9Sstevel@tonic-gate 			}
114*7c478bd9Sstevel@tonic-gate 			fgets(line, LSIZE, tmpf);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 		strcat(work, wuser);
117*7c478bd9Sstevel@tonic-gate 		fclose(tmpf);
118*7c478bd9Sstevel@tonic-gate 		tmpf = doopen(lettmp, "r+", E_TMP);
119*7c478bd9Sstevel@tonic-gate 		if (work[0] != '\0') {
120*7c478bd9Sstevel@tonic-gate 			reciplist list;
121*7c478bd9Sstevel@tonic-gate 			if (replying) {
122*7c478bd9Sstevel@tonic-gate 				(void) snprintf(buf, sizeof (buf),
123*7c478bd9Sstevel@tonic-gate 				    "mail %s %s", m_sendto, work);
124*7c478bd9Sstevel@tonic-gate 				printf("%s\n", buf);
125*7c478bd9Sstevel@tonic-gate 				systm(buf);
126*7c478bd9Sstevel@tonic-gate 				return;
127*7c478bd9Sstevel@tonic-gate 			}
128*7c478bd9Sstevel@tonic-gate 			if (interactive) {
129*7c478bd9Sstevel@tonic-gate 				(void) strlcpy(work, my_name, sizeof (work));
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Return to %s\n", program, work);
132*7c478bd9Sstevel@tonic-gate 			/* Put header info from message aside so it won't */
133*7c478bd9Sstevel@tonic-gate 			/* get confused with the Delivery Notification info */
134*7c478bd9Sstevel@tonic-gate 			Daffbytecnt = affbytecnt; affbytecnt = 0;
135*7c478bd9Sstevel@tonic-gate 			Daffcnt = affcnt; affcnt = 0;
136*7c478bd9Sstevel@tonic-gate 			Drcvbytecnt = rcvbytecnt; rcvbytecnt = 0;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DAFWDFROM].head = hdrlines[H_AFWDFROM].head;
139*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DAFWDFROM].tail = hdrlines[H_AFWDFROM].tail;
140*7c478bd9Sstevel@tonic-gate 			hdrlines[H_AFWDFROM].head = (struct hdrs *)NULL;
141*7c478bd9Sstevel@tonic-gate 			hdrlines[H_AFWDFROM].tail = (struct hdrs *)NULL;
142*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DRECEIVED].head = hdrlines[H_RECEIVED].head;
143*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DRECEIVED].tail = hdrlines[H_RECEIVED].tail;
144*7c478bd9Sstevel@tonic-gate 			hdrlines[H_RECEIVED].head = (struct hdrs *)NULL;
145*7c478bd9Sstevel@tonic-gate 			hdrlines[H_RECEIVED].tail = (struct hdrs *)NULL;
146*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DTCOPY].head = hdrlines[H_TCOPY].head;
147*7c478bd9Sstevel@tonic-gate 			hdrlines[H_DTCOPY].tail = hdrlines[H_TCOPY].tail;
148*7c478bd9Sstevel@tonic-gate 			hdrlines[H_TCOPY].head = (struct hdrs *)NULL;
149*7c478bd9Sstevel@tonic-gate 			hdrlines[H_TCOPY].tail = (struct hdrs *)NULL;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 			pushlist(H_TCOPY, HEAD, work, FALSE);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 			new_reciplist(&list);
154*7c478bd9Sstevel@tonic-gate 			add_recip(&list, work, FALSE);
155*7c478bd9Sstevel@tonic-gate 			sendlist(&list, 0, 0);
156*7c478bd9Sstevel@tonic-gate 			del_reciplist(&list);
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	skipsend:
161*7c478bd9Sstevel@tonic-gate 	if (dflag == 9) {
162*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
163*7c478bd9Sstevel@tonic-gate 			"%s: Cannot return mail.\n",
164*7c478bd9Sstevel@tonic-gate 			program);
165*7c478bd9Sstevel@tonic-gate 		mkdead();
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	else if (dflag < 2) {
169*7c478bd9Sstevel@tonic-gate 		if (!maxerr && (dflag != 1)) {
170*7c478bd9Sstevel@tonic-gate 			maxerr = error;
171*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "maxerr = %d\n", maxerr);
172*7c478bd9Sstevel@tonic-gate 		}
173*7c478bd9Sstevel@tonic-gate 		dflag = 0;
174*7c478bd9Sstevel@tonic-gate 		error = 0;
175*7c478bd9Sstevel@tonic-gate 		Dout(pn, 0, "before return,  dflag = %d, error = %d\n",
176*7c478bd9Sstevel@tonic-gate 			dflag, error);
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate }
179