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