11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*13932Seric SCCSID(@(#)collect.c	3.61		07/13/83);
51392Seric 
61392Seric /*
72969Seric **  COLLECT -- read & parse message header & make temp file.
81392Seric **
91392Seric **	Creates a temporary file name and copies the standard
109371Seric **	input to that file.  Leading UNIX-style "From" lines are
119371Seric **	stripped off (after important information is extracted).
121392Seric **
131392Seric **	Parameters:
144710Seric **		sayok -- if set, give an ARPANET style message
154710Seric **			to say we are ready to collect input.
161392Seric **
171392Seric **	Returns:
184162Seric **		none.
191392Seric **
201392Seric **	Side Effects:
211392Seric **		Temp file is created and filled.
224162Seric **		The from person may be set.
231392Seric */
241392Seric 
254710Seric collect(sayok)
264710Seric 	bool sayok;
271392Seric {
281392Seric 	register FILE *tf;
297852Seric 	char buf[MAXFIELD+2];
301392Seric 	register char *p;
312900Seric 	extern char *hvalue();
321392Seric 
331392Seric 	/*
341392Seric 	**  Create the temp file name and create the file.
351392Seric 	*/
361392Seric 
377809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
387809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
391392Seric 	{
407809Seric 		syserr("Cannot create %s", CurEnv->e_df);
415366Seric 		NoReturn = TRUE;
425366Seric 		finis();
431392Seric 	}
449047Seric 	(void) chmod(CurEnv->e_df, FileMode);
451392Seric 
464316Seric 	/*
474322Seric 	**  Tell ARPANET to go ahead.
484322Seric 	*/
494322Seric 
504710Seric 	if (sayok)
514710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
524322Seric 
534322Seric 	/*
544316Seric 	**  Try to read a UNIX-style From line
554316Seric 	*/
564316Seric 
57*13932Seric 	if (sfgets(buf, sizeof buf, InChannel) == NULL)
584162Seric 		return;
594557Seric 	fixcrlf(buf, FALSE);
604321Seric # ifndef NOTUNIX
614322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
622900Seric 	{
632900Seric 		eatfrom(buf);
64*13932Seric 		(void) sfgets(buf, sizeof buf, InChannel);
654557Seric 		fixcrlf(buf, FALSE);
662900Seric 	}
674321Seric # endif NOTUNIX
682900Seric 
691392Seric 	/*
705975Seric 	**  Copy InChannel to temp file & do message editing.
711392Seric 	**	To keep certain mailers from getting confused,
721392Seric 	**	and to keep the output clean, lines that look
73*13932Seric 	**	like UNIX "From" lines are deleted in the header.
741392Seric 	*/
751392Seric 
76*13932Seric 	for (; !feof(InChannel); !feof(InChannel) && !ferror(InChannel) &&
77*13932Seric 				 sfgets(buf, MAXFIELD, InChannel) != NULL)
781392Seric 	{
794316Seric 		register char c;
804316Seric 		extern bool isheader();
814316Seric 
827681Seric 		/* if the line is too long, throw the rest away */
837681Seric 		if (index(buf, '\n') == NULL)
847681Seric 		{
857681Seric 			while ((c = getc(InChannel)) != '\n')
867681Seric 				continue;
877681Seric 			/* give an error? */
887681Seric 		}
897681Seric 
907852Seric 		fixcrlf(buf, TRUE);
914557Seric 
922900Seric 		/* see if the header is over */
932900Seric 		if (!isheader(buf))
942900Seric 			break;
952900Seric 
962900Seric 		/* get the rest of this field */
975975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
981392Seric 		{
992900Seric 			p = &buf[strlen(buf)];
1007852Seric 			*p++ = '\n';
1012900Seric 			*p++ = c;
102*13932Seric 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1032900Seric 				break;
1047852Seric 			fixcrlf(p, TRUE);
1051392Seric 		}
1065975Seric 		if (!feof(InChannel))
1075975Seric 			(void) ungetc(c, InChannel);
1081392Seric 
1096901Seric 		CurEnv->e_msgsize += strlen(buf);
1101392Seric 
1112900Seric 		/*
1122900Seric 		**  Snarf header away.
1132900Seric 		*/
1142900Seric 
1153391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1163058Seric 			break;
1172900Seric 	}
1181392Seric 
1192900Seric # ifdef DEBUG
1207673Seric 	if (tTd(30, 1))
1212900Seric 		printf("EOH\n");
1222900Seric # endif DEBUG
1232900Seric 
1242900Seric 	/* throw away a blank line */
1257852Seric 	if (buf[0] == '\0')
1264557Seric 	{
127*13932Seric 		(void) sfgets(buf, MAXFIELD, InChannel);
1287852Seric 		fixcrlf(buf, TRUE);
1294557Seric 	}
1302900Seric 
1312900Seric 	/*
1322900Seric 	**  Collect the body of the message.
1332900Seric 	*/
1342900Seric 
135*13932Seric 	for (; !feof(InChannel); !feof(InChannel) && !ferror(InChannel) &&
136*13932Seric 				 sfgets(buf, sizeof buf, InChannel) != NULL)
1372900Seric 	{
1384551Seric 		register char *bp = buf;
1394156Seric 
1407852Seric 		fixcrlf(buf, TRUE);
1414557Seric 
1422900Seric 		/* check for end-of-message */
1432900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1442900Seric 			break;
1452900Seric 
1464551Seric 		/* check for transparent dot */
1479371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1484551Seric 			bp++;
1494551Seric 
1504156Seric 		/*
1514156Seric 		**  Figure message length, output the line to the temp
1524156Seric 		**  file, and insert a newline if missing.
1534156Seric 		*/
1544156Seric 
1559371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1564551Seric 		fputs(bp, tf);
1577852Seric 		fputs("\n", tf);
1581392Seric 		if (ferror(tf))
15911544Seric 			tferror(tf);
1601392Seric 	}
16111544Seric 	if (fflush(tf) != 0)
16211544Seric 		tferror(tf);
1634083Seric 	(void) fclose(tf);
1642900Seric 
16511145Seric 	/* An EOF when running SMTP is an error */
16611145Seric 	if (feof(InChannel) && OpMode == MD_SMTP)
16711145Seric 		syserr("collect: unexpected close");
16811145Seric 
1692900Seric 	/*
1702900Seric 	**  Find out some information from the headers.
1713386Seric 	**	Examples are who is the from person & the date.
1722900Seric 	*/
1732900Seric 
1749371Seric 	eatheader(CurEnv);
1757673Seric 
1767782Seric 	/*
1777782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1787782Seric 	*/
1794622Seric 
1807367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1817367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
1827367Seric 	{
1837367Seric 		register ADDRESS *q;
1847367Seric 
1857367Seric 		/* create an Apparently-To: field */
1867367Seric 		/*    that or reject the message.... */
1877367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
1887367Seric 		{
1897389Seric 			if (q->q_alias != NULL)
1907389Seric 				continue;
1917367Seric # ifdef DEBUG
1927673Seric 			if (tTd(30, 3))
1937367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
1947367Seric # endif DEBUG
1957367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
1967367Seric 		}
1977367Seric 	}
1987367Seric 
1999539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2006986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2011392Seric }
2021392Seric /*
20311544Seric **  TFERROR -- signal error on writing the temporary file.
20411544Seric **
20511544Seric **	Parameters:
20611544Seric **		tf -- the file pointer for the temporary file.
20711544Seric **
20811544Seric **	Returns:
20911544Seric **		none.
21011544Seric **
21111544Seric **	Side Effects:
21211544Seric **		Gives an error message.
21311544Seric **		Arranges for following output to go elsewhere.
21411544Seric */
21511544Seric 
21611544Seric tferror(tf)
21711544Seric 	FILE *tf;
21811544Seric {
21911544Seric 	if (errno == ENOSPC)
22011544Seric 	{
22111544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
22211544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
22311544Seric 		usrerr("452 Out of disk space for temp file");
22411544Seric 	}
22511544Seric 	else
22611544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
22711544Seric 	(void) freopen("/dev/null", "w", tf);
22811544Seric }
22911544Seric /*
2302900Seric **  EATFROM -- chew up a UNIX style from line and process
2312900Seric **
2322900Seric **	This does indeed make some assumptions about the format
2332900Seric **	of UNIX messages.
2342900Seric **
2352900Seric **	Parameters:
2362900Seric **		fm -- the from line.
2372900Seric **
2382900Seric **	Returns:
2392900Seric **		none.
2402900Seric **
2412900Seric **	Side Effects:
2422900Seric **		extracts what information it can from the header,
2433386Seric **		such as the date.
2442900Seric */
2452900Seric 
2464321Seric # ifndef NOTUNIX
2474321Seric 
2484203Seric char	*DowList[] =
2494203Seric {
2504203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2514203Seric };
2524203Seric 
2532900Seric char	*MonthList[] =
2542900Seric {
2552900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2562900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2572900Seric 	NULL
2582900Seric };
2592900Seric 
2602900Seric eatfrom(fm)
2612900Seric 	char *fm;
2622900Seric {
2632900Seric 	register char *p;
2642900Seric 	register char **dt;
2652900Seric 
2664203Seric # ifdef DEBUG
2677673Seric 	if (tTd(30, 2))
2684203Seric 		printf("eatfrom(%s)\n", fm);
2694203Seric # endif DEBUG
2704203Seric 
2712900Seric 	/* find the date part */
2722900Seric 	p = fm;
2732900Seric 	while (*p != '\0')
2742900Seric 	{
2752900Seric 		/* skip a word */
2762900Seric 		while (*p != '\0' && *p != ' ')
2772900Seric 			*p++;
2782900Seric 		while (*p == ' ')
2792900Seric 			*p++;
2802900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2812900Seric 			continue;
2822900Seric 
2832900Seric 		/* we have a possible date */
2844203Seric 		for (dt = DowList; *dt != NULL; dt++)
2852900Seric 			if (strncmp(*dt, p, 3) == 0)
2862900Seric 				break;
2874203Seric 		if (*dt == NULL)
2884203Seric 			continue;
2892900Seric 
2904203Seric 		for (dt = MonthList; *dt != NULL; dt++)
2914203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
2924203Seric 				break;
2932900Seric 		if (*dt != NULL)
2942900Seric 			break;
2952900Seric 	}
2962900Seric 
2972900Seric 	if (*p != NULL)
2982900Seric 	{
2993386Seric 		char *q;
3005366Seric 		extern char *arpadate();
3013386Seric 
3022900Seric 		/* we have found a date */
3033386Seric 		q = xalloc(25);
3043386Seric 		strncpy(q, p, 25);
3053386Seric 		q[24] = '\0';
3069371Seric 		define('d', q, CurEnv);
3075366Seric 		q = arpadate(q);
3089371Seric 		define('a', newstr(q), CurEnv);
3092900Seric 	}
3102900Seric }
3114321Seric 
3124321Seric # endif NOTUNIX
313