1 # include <errno.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)collect.c	3.56		12/05/82);
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 	if (fgets(buf, sizeof buf, InChannel) == NULL)
58 		return;
59 	fixcrlf(buf, FALSE);
60 # ifndef NOTUNIX
61 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
62 	{
63 		eatfrom(buf);
64 		(void) fgets(buf, sizeof buf, InChannel);
65 		fixcrlf(buf, FALSE);
66 	}
67 # endif NOTUNIX
68 
69 	/*
70 	**  Copy InChannel to temp file & do message editing.
71 	**	To keep certain mailers from getting confused,
72 	**	and to keep the output clean, lines that look
73 	**	like UNIX "From" lines are deleted in the header,
74 	**	and prepended with ">" in the body.
75 	*/
76 
77 	for (; !feof(InChannel); !feof(InChannel) &&
78 				 fgets(buf, MAXFIELD, InChannel) != NULL)
79 	{
80 		register char c;
81 		extern bool isheader();
82 
83 		/* if the line is too long, throw the rest away */
84 		if (index(buf, '\n') == NULL)
85 		{
86 			while ((c = getc(InChannel)) != '\n')
87 				continue;
88 			/* give an error? */
89 		}
90 
91 		fixcrlf(buf, TRUE);
92 
93 		/* see if the header is over */
94 		if (!isheader(buf))
95 			break;
96 
97 		/* get the rest of this field */
98 		while ((c = getc(InChannel)) == ' ' || c == '\t')
99 		{
100 			p = &buf[strlen(buf)];
101 			*p++ = '\n';
102 			*p++ = c;
103 			if (fgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
104 				break;
105 			fixcrlf(p, TRUE);
106 		}
107 		if (!feof(InChannel))
108 			(void) ungetc(c, InChannel);
109 
110 		CurEnv->e_msgsize += strlen(buf);
111 
112 		/*
113 		**  Snarf header away.
114 		*/
115 
116 		if (bitset(H_EOH, chompheader(buf, FALSE)))
117 			break;
118 	}
119 
120 # ifdef DEBUG
121 	if (tTd(30, 1))
122 		printf("EOH\n");
123 # endif DEBUG
124 
125 	/* throw away a blank line */
126 	if (buf[0] == '\0')
127 	{
128 		(void) fgets(buf, MAXFIELD, InChannel);
129 		fixcrlf(buf, TRUE);
130 	}
131 
132 	/*
133 	**  Collect the body of the message.
134 	*/
135 
136 	for (; !feof(InChannel); !feof(InChannel) &&
137 				 fgets(buf, sizeof buf, InChannel) != NULL)
138 	{
139 		register char *bp = buf;
140 
141 		fixcrlf(buf, TRUE);
142 
143 		/* check for end-of-message */
144 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
145 			break;
146 
147 		/* check for transparent dot */
148 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
149 			bp++;
150 
151 # ifndef NOTUNIX
152 		/* Hide UNIX-like From lines */
153 		if (strncmp(bp, "From ", 5) == 0)
154 		{
155 			fputs(">", tf);
156 			CurEnv->e_msgsize++;
157 		}
158 # endif NOTUNIX
159 
160 		/*
161 		**  Figure message length, output the line to the temp
162 		**  file, and insert a newline if missing.
163 		*/
164 
165 		CurEnv->e_msgsize += strlen(bp) + 1;
166 		fputs(bp, tf);
167 		fputs("\n", tf);
168 		if (ferror(tf))
169 		{
170 			if (errno == ENOSPC)
171 			{
172 				(void) freopen(CurEnv->e_df, "w", tf);
173 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
174 				usrerr("452 Out of disk space for temp file");
175 			}
176 			else
177 				syserr("collect: Cannot write %s", CurEnv->e_df);
178 			(void) freopen("/dev/null", "w", tf);
179 		}
180 	}
181 	(void) fclose(tf);
182 
183 	/*
184 	**  Find out some information from the headers.
185 	**	Examples are who is the from person & the date.
186 	*/
187 
188 	eatheader(CurEnv);
189 
190 	/*
191 	**  Add an Apparently-To: line if we have no recipient lines.
192 	*/
193 
194 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
195 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
196 	{
197 		register ADDRESS *q;
198 
199 		/* create an Apparently-To: field */
200 		/*    that or reject the message.... */
201 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
202 		{
203 			if (q->q_alias != NULL)
204 				continue;
205 # ifdef DEBUG
206 			if (tTd(30, 3))
207 				printf("Adding Apparently-To: %s\n", q->q_paddr);
208 # endif DEBUG
209 			addheader("apparently-to", q->q_paddr, CurEnv);
210 		}
211 	}
212 
213 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
214 		syserr("Cannot reopen %s", CurEnv->e_df);
215 
216 	/*
217 	**  Log collection information.
218 	*/
219 
220 # ifdef LOG
221 	if (LogLevel > 1)
222 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
223 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
224 		       CurEnv->e_class);
225 # endif LOG
226 	return;
227 }
228 /*
229 **  EATFROM -- chew up a UNIX style from line and process
230 **
231 **	This does indeed make some assumptions about the format
232 **	of UNIX messages.
233 **
234 **	Parameters:
235 **		fm -- the from line.
236 **
237 **	Returns:
238 **		none.
239 **
240 **	Side Effects:
241 **		extracts what information it can from the header,
242 **		such as the date.
243 */
244 
245 # ifndef NOTUNIX
246 
247 char	*DowList[] =
248 {
249 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
250 };
251 
252 char	*MonthList[] =
253 {
254 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
255 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
256 	NULL
257 };
258 
259 eatfrom(fm)
260 	char *fm;
261 {
262 	register char *p;
263 	register char **dt;
264 
265 # ifdef DEBUG
266 	if (tTd(30, 2))
267 		printf("eatfrom(%s)\n", fm);
268 # endif DEBUG
269 
270 	/* find the date part */
271 	p = fm;
272 	while (*p != '\0')
273 	{
274 		/* skip a word */
275 		while (*p != '\0' && *p != ' ')
276 			*p++;
277 		while (*p == ' ')
278 			*p++;
279 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
280 			continue;
281 
282 		/* we have a possible date */
283 		for (dt = DowList; *dt != NULL; dt++)
284 			if (strncmp(*dt, p, 3) == 0)
285 				break;
286 		if (*dt == NULL)
287 			continue;
288 
289 		for (dt = MonthList; *dt != NULL; dt++)
290 			if (strncmp(*dt, &p[4], 3) == 0)
291 				break;
292 		if (*dt != NULL)
293 			break;
294 	}
295 
296 	if (*p != NULL)
297 	{
298 		char *q;
299 		extern char *arpadate();
300 
301 		/* we have found a date */
302 		q = xalloc(25);
303 		strncpy(q, p, 25);
304 		q[24] = '\0';
305 		define('d', q, CurEnv);
306 		q = arpadate(q);
307 		define('a', newstr(q), CurEnv);
308 	}
309 }
310 
311 # endif NOTUNIX
312