xref: /illumos-gate/usr/src/cmd/mail/goback.c (revision 2a8bcb4e)
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  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include "mail.h"
31 /*
32 	This routine returns undeliverable mail as well as handles
33 	replying to letters
34 */
35 void
goback(letnum)36 goback(letnum)
37 int	letnum;
38 {
39 	static char pn[] = "goback";
40 	int	i, w;
41 	char	buf[1024], *cp, work[1024], wuser[1024];
42 
43 	/*
44 	 * If dflag already == 1, then been here already and
45 	 * having a problem delivering failure notification.
46 	 * Reset dflag to 9 to avoid endless loop.....
47 	 */
48 	if (dflag == 1) {
49 		dflag = 9;
50 		Dout(pn, 0, "dflag = %d\n", dflag);
51 		if (!error) {
52 			error = E_REMOTE;
53 			Dout(pn, 0, "error = %d\n", error);
54 		}
55 	}
56 
57 	if (dflag < 2) {
58 		work[0] = '\0';
59 		wuser[0] = '\0';
60 		fclose(tmpf);
61 		if (!replying) {
62 			dflag = 1;
63 			Dout(pn, 0, "dflag = 1\n");
64 			if ((debug > 0) && (keepdbgfile == 0)) {
65 				keepdbgfile++;
66 			}
67 			if (ckdlivopts(H_TCOPY, (int *)0) & IGNORE) {
68 				goto skipsend;
69 			}
70 		}
71 		tmpf = doopen(lettmp, "r+", E_TMP);
72 		if (replying) {
73 			fseek(tmpf, let[letnum].adr, 0);
74 		}
75 		for (fgets(line, LSIZE, tmpf);
76 		strncmp(line, header[H_FROM].tag, strlen(header[H_FROM].tag))
77 		    == SAME ||
78 		    strncmp(line, header[H_FROM1].tag,
79 			strlen(header[H_FROM1].tag)) == SAME; ) {
80 			if ((i = substr(line, "remote from")) != -1) {
81 				for (i = 0, cp = strrchr(line, ' ') + 1;
82 					*cp != '\n';
83 					cp++) {
84 					buf[i++] = *cp;
85 				}
86 				buf[i++] = '!';
87 				buf[i] = '\0';
88 				strcat(work, buf);
89 				if (line[0] == '>') {
90 					i = 6;
91 				} else {
92 					i = 5;
93 				}
94 				for (w = i; line[w] != ' '; w++) {
95 					wuser[w-i] = line[w];
96 				}
97 				wuser[w-i] = '\0';
98 			} else if ((i = substr(line, "forwarded by")) == -1) {
99 				if (line[0] == '>') {
100 					break;
101 				} else {
102 					i = 5;
103 				}
104 				for (w = i; line[w] != ' '; w++) {
105 					wuser[w-i] = line[w];
106 				}
107 				wuser[w-i] = '\0';
108 			} else if ((i = substr(line, "forwarded by")) > -1) {
109 				break;
110 			}
111 			fgets(line, LSIZE, tmpf);
112 		}
113 		strcat(work, wuser);
114 		fclose(tmpf);
115 		tmpf = doopen(lettmp, "r+", E_TMP);
116 		if (work[0] != '\0') {
117 			reciplist list;
118 			if (replying) {
119 				(void) snprintf(buf, sizeof (buf),
120 				    "mail %s %s", m_sendto, work);
121 				printf("%s\n", buf);
122 				systm(buf);
123 				return;
124 			}
125 			if (interactive) {
126 				(void) strlcpy(work, my_name, sizeof (work));
127 			}
128 			fprintf(stderr, "%s: Return to %s\n", program, work);
129 			/* Put header info from message aside so it won't */
130 			/* get confused with the Delivery Notification info */
131 			Daffbytecnt = affbytecnt; affbytecnt = 0;
132 			Daffcnt = affcnt; affcnt = 0;
133 			Drcvbytecnt = rcvbytecnt; rcvbytecnt = 0;
134 
135 			hdrlines[H_DAFWDFROM].head = hdrlines[H_AFWDFROM].head;
136 			hdrlines[H_DAFWDFROM].tail = hdrlines[H_AFWDFROM].tail;
137 			hdrlines[H_AFWDFROM].head = (struct hdrs *)NULL;
138 			hdrlines[H_AFWDFROM].tail = (struct hdrs *)NULL;
139 			hdrlines[H_DRECEIVED].head = hdrlines[H_RECEIVED].head;
140 			hdrlines[H_DRECEIVED].tail = hdrlines[H_RECEIVED].tail;
141 			hdrlines[H_RECEIVED].head = (struct hdrs *)NULL;
142 			hdrlines[H_RECEIVED].tail = (struct hdrs *)NULL;
143 			hdrlines[H_DTCOPY].head = hdrlines[H_TCOPY].head;
144 			hdrlines[H_DTCOPY].tail = hdrlines[H_TCOPY].tail;
145 			hdrlines[H_TCOPY].head = (struct hdrs *)NULL;
146 			hdrlines[H_TCOPY].tail = (struct hdrs *)NULL;
147 
148 			pushlist(H_TCOPY, HEAD, work, FALSE);
149 
150 			new_reciplist(&list);
151 			add_recip(&list, work, FALSE);
152 			sendlist(&list, 0, 0);
153 			del_reciplist(&list);
154 		}
155 	}
156 
157 	skipsend:
158 	if (dflag == 9) {
159 		fprintf(stderr,
160 			"%s: Cannot return mail.\n",
161 			program);
162 		mkdead();
163 	}
164 
165 	else if (dflag < 2) {
166 		if (!maxerr && (dflag != 1)) {
167 			maxerr = error;
168 			Dout(pn, 0, "maxerr = %d\n", maxerr);
169 		}
170 		dflag = 0;
171 		error = 0;
172 		Dout(pn, 0, "before return,  dflag = %d, error = %d\n",
173 			dflag, error);
174 	}
175 }
176