1 # include <errno.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)collect.c	4.3		03/11/84);
5 
6 /*
7 **  COLLECT -- read & parse message header & make temp file.
8 **
9 **	Creates a temporary file name and copies the standard
10 **	input to that file.  Leading UNIX-style "From" lines are
11 **	stripped off (after important information is extracted).
12 **
13 **	Parameters:
14 **		sayok -- if set, give an ARPANET style message
15 **			to say we are ready to collect input.
16 **
17 **	Returns:
18 **		none.
19 **
20 **	Side Effects:
21 **		Temp file is created and filled.
22 **		The from person may be set.
23 */
24 
25 collect(sayok)
26 	bool sayok;
27 {
28 	register FILE *tf;
29 	char buf[MAXFIELD+2];
30 	register char *p;
31 	extern char *hvalue();
32 
33 	/*
34 	**  Create the temp file name and create the file.
35 	*/
36 
37 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
38 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
39 	{
40 		syserr("Cannot create %s", CurEnv->e_df);
41 		NoReturn = TRUE;
42 		finis();
43 	}
44 	(void) chmod(CurEnv->e_df, FileMode);
45 
46 	/*
47 	**  Tell ARPANET to go ahead.
48 	*/
49 
50 	if (sayok)
51 		message("354", "Enter mail, end with \".\" on a line by itself");
52 
53 	/*
54 	**  Try to read a UNIX-style From line
55 	*/
56 
57 	(void) sfgets(buf, sizeof buf, InChannel);
58 	fixcrlf(buf, FALSE);
59 # ifndef NOTUNIX
60 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
61 	{
62 		eatfrom(buf);
63 		(void) sfgets(buf, sizeof buf, InChannel);
64 		fixcrlf(buf, FALSE);
65 	}
66 # endif NOTUNIX
67 
68 	/*
69 	**  Copy InChannel to temp file & do message editing.
70 	**	To keep certain mailers from getting confused,
71 	**	and to keep the output clean, lines that look
72 	**	like UNIX "From" lines are deleted in the header.
73 	*/
74 
75 	do
76 	{
77 		int c;
78 		extern bool isheader();
79 
80 		/* if the line is too long, throw the rest away */
81 		if (index(buf, '\n') == NULL)
82 		{
83 			while ((c = getc(InChannel)) != '\n' && c != EOF)
84 				continue;
85 			/* give an error? */
86 		}
87 
88 		fixcrlf(buf, TRUE);
89 
90 		/* see if the header is over */
91 		if (!isheader(buf))
92 			break;
93 
94 		/* get the rest of this field */
95 		while ((c = getc(InChannel)) == ' ' || c == '\t')
96 		{
97 			p = &buf[strlen(buf)];
98 			*p++ = '\n';
99 			*p++ = c;
100 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
101 				break;
102 			fixcrlf(p, TRUE);
103 		}
104 		if (!feof(InChannel))
105 			(void) ungetc(c, InChannel);
106 
107 		CurEnv->e_msgsize += strlen(buf);
108 
109 		/*
110 		**  Snarf header away.
111 		*/
112 
113 		if (bitset(H_EOH, chompheader(buf, FALSE)))
114 			break;
115 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
116 
117 # ifdef DEBUG
118 	if (tTd(30, 1))
119 		printf("EOH\n");
120 # endif DEBUG
121 
122 	/* throw away a blank line */
123 	if (buf[0] == '\0')
124 		(void) sfgets(buf, MAXFIELD, InChannel);
125 
126 	/*
127 	**  Collect the body of the message.
128 	*/
129 
130 	do
131 	{
132 		register char *bp = buf;
133 
134 		fixcrlf(buf, TRUE);
135 
136 		/* check for end-of-message */
137 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
138 			break;
139 
140 		/* check for transparent dot */
141 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
142 			bp++;
143 
144 		/*
145 		**  Figure message length, output the line to the temp
146 		**  file, and insert a newline if missing.
147 		*/
148 
149 		CurEnv->e_msgsize += strlen(bp) + 1;
150 		fputs(bp, tf);
151 		fputs("\n", tf);
152 		if (ferror(tf))
153 			tferror(tf);
154 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
155 	if (fflush(tf) != 0)
156 		tferror(tf);
157 	(void) fclose(tf);
158 
159 	/* An EOF when running SMTP is an error */
160 	if (feof(InChannel) && OpMode == MD_SMTP)
161 	{
162 		syserr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
163 
164 		/* don't return an error indication */
165 		CurEnv->e_to = NULL;
166 		CurEnv->e_flags &= ~EF_FATALERRS;
167 
168 		/* and don't try to deliver the partial message either */
169 		finis();
170 	}
171 
172 	/*
173 	**  Find out some information from the headers.
174 	**	Examples are who is the from person & the date.
175 	*/
176 
177 	eatheader(CurEnv);
178 
179 	/*
180 	**  Add an Apparently-To: line if we have no recipient lines.
181 	*/
182 
183 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
184 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
185 	{
186 		register ADDRESS *q;
187 
188 		/* create an Apparently-To: field */
189 		/*    that or reject the message.... */
190 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
191 		{
192 			if (q->q_alias != NULL)
193 				continue;
194 # ifdef DEBUG
195 			if (tTd(30, 3))
196 				printf("Adding Apparently-To: %s\n", q->q_paddr);
197 # endif DEBUG
198 			addheader("apparently-to", q->q_paddr, CurEnv);
199 		}
200 	}
201 
202 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
203 		syserr("Cannot reopen %s", CurEnv->e_df);
204 }
205 /*
206 **  TFERROR -- signal error on writing the temporary file.
207 **
208 **	Parameters:
209 **		tf -- the file pointer for the temporary file.
210 **
211 **	Returns:
212 **		none.
213 **
214 **	Side Effects:
215 **		Gives an error message.
216 **		Arranges for following output to go elsewhere.
217 */
218 
219 tferror(tf)
220 	FILE *tf;
221 {
222 	if (errno == ENOSPC)
223 	{
224 		(void) freopen(CurEnv->e_df, "w", tf);
225 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
226 		usrerr("452 Out of disk space for temp file");
227 	}
228 	else
229 		syserr("collect: Cannot write %s", CurEnv->e_df);
230 	(void) freopen("/dev/null", "w", tf);
231 }
232 /*
233 **  EATFROM -- chew up a UNIX style from line and process
234 **
235 **	This does indeed make some assumptions about the format
236 **	of UNIX messages.
237 **
238 **	Parameters:
239 **		fm -- the from line.
240 **
241 **	Returns:
242 **		none.
243 **
244 **	Side Effects:
245 **		extracts what information it can from the header,
246 **		such as the date.
247 */
248 
249 # ifndef NOTUNIX
250 
251 char	*DowList[] =
252 {
253 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
254 };
255 
256 char	*MonthList[] =
257 {
258 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
259 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
260 	NULL
261 };
262 
263 eatfrom(fm)
264 	char *fm;
265 {
266 	register char *p;
267 	register char **dt;
268 
269 # ifdef DEBUG
270 	if (tTd(30, 2))
271 		printf("eatfrom(%s)\n", fm);
272 # endif DEBUG
273 
274 	/* find the date part */
275 	p = fm;
276 	while (*p != '\0')
277 	{
278 		/* skip a word */
279 		while (*p != '\0' && *p != ' ')
280 			*p++;
281 		while (*p == ' ')
282 			*p++;
283 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
284 			continue;
285 
286 		/* we have a possible date */
287 		for (dt = DowList; *dt != NULL; dt++)
288 			if (strncmp(*dt, p, 3) == 0)
289 				break;
290 		if (*dt == NULL)
291 			continue;
292 
293 		for (dt = MonthList; *dt != NULL; dt++)
294 			if (strncmp(*dt, &p[4], 3) == 0)
295 				break;
296 		if (*dt != NULL)
297 			break;
298 	}
299 
300 	if (*p != NULL)
301 	{
302 		char *q;
303 		extern char *arpadate();
304 
305 		/* we have found a date */
306 		q = xalloc(25);
307 		strncpy(q, p, 25);
308 		q[24] = '\0';
309 		define('d', q, CurEnv);
310 		q = arpadate(q);
311 		define('a', newstr(q), CurEnv);
312 	}
313 }
314 
315 # endif NOTUNIX
316