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