1 # include <errno.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)collect.c	3.39		05/31/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.  While it is doing it, it looks for
11 **	"From:" and "Sender:" fields to use as the from-person
12 **	(but only if the -a flag is specified).  It prefers to
13 **	to use the "Sender:" field.
14 **
15 **	MIT seems to like to produce "Sent-By:" fields instead
16 **	of "Sender:" fields.  We used to catch this, but it turns
17 **	out that the "Sent-By:" field doesn't always correspond
18 **	to someone real ("___057", for instance), as required by
19 **	the protocol.  So we limp by.....
20 **
21 **	Parameters:
22 **		sayok -- if set, give an ARPANET style message
23 **			to say we are ready to collect input.
24 **
25 **	Returns:
26 **		none.
27 **
28 **	Side Effects:
29 **		Temp file is created and filled.
30 **		The from person may be set.
31 */
32 
33 collect(sayok)
34 	bool sayok;
35 {
36 	register FILE *tf;
37 	char buf[MAXFIELD+1];
38 	register char *p;
39 	char *xfrom;
40 	extern char *hvalue();
41 	extern char *mktemp();
42 	static char tempfname[40];
43 	extern char *macvalue();
44 
45 	/*
46 	**  Create the temp file name and create the file.
47 	*/
48 
49 	strcpy(tempfname, QueueDir);
50 	strcat(tempfname, "/dfXXXXXX");
51 	(void) mktemp(tempfname);
52 	if ((tf = dfopen(tempfname, "w")) == NULL)
53 	{
54 		syserr("Cannot create %s", tempfname);
55 		NoReturn = TRUE;
56 		finis();
57 	}
58 	chmod(tempfname, 0600);
59 	CurEnv->e_df = tempfname;
60 
61 	/*
62 	**  Create the Mail-From line if we want to.
63 	*/
64 
65 	if (Smtp && macvalue('s') != NULL)
66 	{
67 		char xbuf[50];
68 
69 		(void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b",
70 			macvalue('r') == NULL ? "" : "$r host ");
71 		expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv);
72 		(void) chompheader(buf, FALSE);
73 	}
74 
75 	/*
76 	**  Tell ARPANET to go ahead.
77 	*/
78 
79 	if (sayok)
80 		message("354", "Enter mail, end with \".\" on a line by itself");
81 
82 	/*
83 	**  Try to read a UNIX-style From line
84 	*/
85 
86 	if (fgets(buf, sizeof buf, InChannel) == NULL)
87 		return;
88 	fixcrlf(buf, FALSE);
89 # ifndef NOTUNIX
90 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
91 	{
92 		eatfrom(buf);
93 		(void) fgets(buf, sizeof buf, InChannel);
94 		fixcrlf(buf, FALSE);
95 	}
96 # endif NOTUNIX
97 
98 	/*
99 	**  Copy InChannel to temp file & do message editing.
100 	**	To keep certain mailers from getting confused,
101 	**	and to keep the output clean, lines that look
102 	**	like UNIX "From" lines are deleted in the header,
103 	**	and prepended with ">" in the body.
104 	*/
105 
106 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
107 	{
108 		register char c;
109 		extern bool isheader();
110 
111 		fixcrlf(buf, FALSE);
112 
113 		/* see if the header is over */
114 		if (!isheader(buf))
115 			break;
116 
117 		/* get the rest of this field */
118 		while ((c = getc(InChannel)) == ' ' || c == '\t')
119 		{
120 			p = &buf[strlen(buf)];
121 			*p++ = c;
122 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
123 				break;
124 			fixcrlf(p, FALSE);
125 		}
126 		if (!feof(InChannel))
127 			(void) ungetc(c, InChannel);
128 
129 		CurEnv->e_msgsize += strlen(buf);
130 
131 		/*
132 		**  Snarf header away.
133 		*/
134 
135 		if (bitset(H_EOH, chompheader(buf, FALSE)))
136 			break;
137 	}
138 
139 # ifdef DEBUG
140 	if (Debug)
141 		printf("EOH\n");
142 # endif DEBUG
143 
144 	/* throw away a blank line */
145 	if (buf[0] == '\n')
146 	{
147 		(void) fgets(buf, sizeof buf, InChannel);
148 		fixcrlf(buf, FALSE);
149 	}
150 
151 	/*
152 	**  Collect the body of the message.
153 	*/
154 
155 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
156 	{
157 		register int i;
158 		register char *bp = buf;
159 
160 		fixcrlf(buf, FALSE);
161 
162 		/* check for end-of-message */
163 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
164 			break;
165 
166 		/* check for transparent dot */
167 		if (Smtp && *bp == '.')
168 			bp++;
169 
170 # ifndef NOTUNIX
171 		/* Hide UNIX-like From lines */
172 		if (strncmp(bp, "From ", 5) == 0)
173 		{
174 			fputs(">", tf);
175 			CurEnv->e_msgsize++;
176 		}
177 # endif NOTUNIX
178 
179 		/*
180 		**  Figure message length, output the line to the temp
181 		**  file, and insert a newline if missing.
182 		*/
183 
184 		i = strlen(bp);
185 		CurEnv->e_msgsize += i;
186 		fputs(bp, tf);
187 		if (bp[i - 1] != '\n')
188 			fputs("\n", tf);
189 		if (ferror(tf))
190 		{
191 			if (errno == ENOSPC)
192 			{
193 				(void) freopen(CurEnv->e_df, "w", tf);
194 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
195 				usrerr("452 Out of disk space for temp file");
196 			}
197 			else
198 				syserr("collect: Cannot write %s", CurEnv->e_df);
199 			(void) freopen("/dev/null", "w", tf);
200 		}
201 	}
202 	(void) fclose(tf);
203 
204 	/*
205 	**  Find out some information from the headers.
206 	**	Examples are who is the from person & the date.
207 	*/
208 
209 	/* message priority */
210 	if (!QueueRun)
211 	{
212 		/* adjust total priority by message priority */
213 		CurEnv->e_msgpriority = CurEnv->e_msgsize;
214 		p = hvalue("priority");
215 		if (p != NULL)
216 			CurEnv->e_class = priencode(p);
217 		else
218 			CurEnv->e_class = PRI_NORMAL;
219 		CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT;
220 	}
221 
222 	/* special handling */
223 	p = hvalue("special-handling");
224 	if (p != NULL)
225 		spechandling(p);
226 
227 	/* from person */
228 	xfrom = hvalue("sender");
229 	if (xfrom == NULL)
230 		xfrom = CurEnv->e_origfrom;
231 	if (ArpaMode)
232 		setfrom(xfrom, (char *) NULL);
233 
234 	/* full name of from person */
235 	p = hvalue("full-name");
236 	if (p != NULL)
237 		define('x', p);
238 	else
239 	{
240 		extern char *getxpart();
241 
242 		/*
243 		**  Try to extract the full name from a general From:
244 		**  field.  We take anything which is a comment as a
245 		**  first choice.  Failing in that, we see if there is
246 		**  a "machine readable" name (in <angle brackets>); if
247 		**  so we take anything preceeding that clause.
248 		**
249 		**  If we blow it here it's not all that serious.
250 		*/
251 
252 		p = hvalue("original-from");
253 		if (p == NULL)
254 			p = CurEnv->e_origfrom;
255 		p = getxpart(p);
256 		if (p != NULL)
257 			define('x', newstr(p));
258 	}
259 
260 	/* date message originated */
261 	p = hvalue("posted-date");
262 	if (p == NULL)
263 		p = hvalue("date");
264 	if (p != NULL)
265 	{
266 		define('a', p);
267 		/* we don't have a good way to do canonical conversion ....
268 		define('d', newstr(arpatounix(p)));
269 		.... so we will ignore the problem for the time being */
270 	}
271 
272 	if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL)
273 		syserr("Cannot reopen %s", CurEnv->e_df);
274 
275 # ifdef DEBUG
276 	if (Debug)
277 	{
278 		HDR *h;
279 		extern char *capitalize();
280 
281 		printf("----- collected header -----\n");
282 		for (h = CurEnv->e_header; h != NULL; h = h->h_link)
283 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
284 		printf("----------------------------\n");
285 	}
286 # endif DEBUG
287 	return;
288 }
289 /*
290 **  EATFROM -- chew up a UNIX style from line and process
291 **
292 **	This does indeed make some assumptions about the format
293 **	of UNIX messages.
294 **
295 **	Parameters:
296 **		fm -- the from line.
297 **
298 **	Returns:
299 **		none.
300 **
301 **	Side Effects:
302 **		extracts what information it can from the header,
303 **		such as the date.
304 */
305 
306 # ifndef NOTUNIX
307 
308 char	*DowList[] =
309 {
310 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
311 };
312 
313 char	*MonthList[] =
314 {
315 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
316 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
317 	NULL
318 };
319 
320 eatfrom(fm)
321 	char *fm;
322 {
323 	register char *p;
324 	register char **dt;
325 
326 # ifdef DEBUG
327 	if (Debug > 1)
328 		printf("eatfrom(%s)\n", fm);
329 # endif DEBUG
330 
331 	/* find the date part */
332 	p = fm;
333 	while (*p != '\0')
334 	{
335 		/* skip a word */
336 		while (*p != '\0' && *p != ' ')
337 			*p++;
338 		while (*p == ' ')
339 			*p++;
340 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
341 			continue;
342 
343 		/* we have a possible date */
344 		for (dt = DowList; *dt != NULL; dt++)
345 			if (strncmp(*dt, p, 3) == 0)
346 				break;
347 		if (*dt == NULL)
348 			continue;
349 
350 		for (dt = MonthList; *dt != NULL; dt++)
351 			if (strncmp(*dt, &p[4], 3) == 0)
352 				break;
353 		if (*dt != NULL)
354 			break;
355 	}
356 
357 	if (*p != NULL)
358 	{
359 		char *q;
360 		extern char *arpadate();
361 
362 		/* we have found a date */
363 		q = xalloc(25);
364 		strncpy(q, p, 25);
365 		q[24] = '\0';
366 		define('d', q);
367 		q = arpadate(q);
368 		define('a', newstr(q));
369 	}
370 }
371 
372 # endif NOTUNIX
373 /*
374 **  PRIENCODE -- encode external priority names into internal values.
375 **
376 **	Parameters:
377 **		p -- priority in ascii.
378 **
379 **	Returns:
380 **		priority as a numeric level.
381 **
382 **	Side Effects:
383 **		none.
384 */
385 
386 struct prio
387 {
388 	char	*pri_name;	/* external name of priority */
389 	int	pri_val;	/* internal value for same */
390 };
391 
392 static struct prio	Prio[] =
393 {
394 	"alert",		PRI_ALERT,
395 	"quick",		PRI_QUICK,
396 	"first-class",		PRI_FIRSTCL,
397 	"normal",		PRI_NORMAL,
398 	"second-class",		PRI_SECONDCL,
399 	"third-class",		PRI_THIRDCL,
400 	"junk",			PRI_JUNK,
401 	NULL,			PRI_NORMAL,
402 };
403 
404 priencode(p)
405 	char *p;
406 {
407 	register struct prio *pl;
408 	extern bool sameword();
409 
410 	for (pl = Prio; pl->pri_name != NULL; pl++)
411 	{
412 		if (sameword(p, pl->pri_name))
413 			break;
414 	}
415 	return (pl->pri_val);
416 }
417 /*
418 **  SPECHANDLE -- do special handling
419 **
420 **	Parameters:
421 **		p -- pointer to list of special handling words.
422 **
423 **	Returns:
424 **		none.
425 **
426 **	Side Effects:
427 **		Sets flags as indicated by p.
428 */
429 
430 struct handling
431 {
432 	char	*han_name;		/* word to get this magic */
433 	int	han_what;		/* what to do, see below */
434 };
435 
436 /* modes for han_what */
437 # define	HAN_NONE	0	/* nothing special */
438 # define	HAN_RRECEIPT	1	/* give return receipt */
439 
440 struct handling	Handling[] =
441 {
442 	"return-receipt-requested",	HAN_RRECEIPT,
443 	NULL,				HAN_NONE
444 };
445 
446 spechandling(p)
447 	register char *p;
448 {
449 	register char *w;
450 	register struct handling *h;
451 	extern bool sameword();
452 
453 	while (*p != '\0')
454 	{
455 		/* collect a word to compare to */
456 		while (*p != '\0' && (*p == ',' || isspace(*p)))
457 			p++;
458 		if (*p == '\0')
459 			break;
460 		w = p;
461 		while (*p != '\0' && *p != ',' && !isspace(*p))
462 			p++;
463 		if (*p != '\0')
464 			*p++ = '\0';
465 
466 		/* scan the special handling table */
467 		for (h = Handling; h->han_name != NULL; h++)
468 			if (sameword(h->han_name, w))
469 				break;
470 
471 		/* see if we can do anything interesting */
472 		switch (h->han_what)
473 		{
474 		  case HAN_NONE:	/* nothing to be done */
475 			break;
476 
477 		  case HAN_RRECEIPT:	/* give return receipt */
478 			CurEnv->e_retreceipt = TRUE;
479 # ifdef DEBUG
480 			if (Debug > 2)
481 				printf(">>> Return receipt requested\n");
482 # endif DEBUG
483 			break;
484 
485 		  default:
486 			syserr("spechandling: handling %d (%s)", h->han_what, w);
487 		}
488 	}
489 }
490