11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*16896Seric SCCSID(@(#)collect.c	4.4		08/11/84);
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 
5715532Seric 	(void) sfgets(buf, sizeof buf, InChannel);
584557Seric 	fixcrlf(buf, FALSE);
594321Seric # ifndef NOTUNIX
604322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
612900Seric 	{
622900Seric 		eatfrom(buf);
6313932Seric 		(void) sfgets(buf, sizeof buf, InChannel);
644557Seric 		fixcrlf(buf, FALSE);
652900Seric 	}
664321Seric # endif NOTUNIX
672900Seric 
681392Seric 	/*
695975Seric 	**  Copy InChannel to temp file & do message editing.
701392Seric 	**	To keep certain mailers from getting confused,
711392Seric 	**	and to keep the output clean, lines that look
7213932Seric 	**	like UNIX "From" lines are deleted in the header.
731392Seric 	*/
741392Seric 
7515532Seric 	do
761392Seric 	{
7715532Seric 		int c;
784316Seric 		extern bool isheader();
794316Seric 
807681Seric 		/* if the line is too long, throw the rest away */
817681Seric 		if (index(buf, '\n') == NULL)
827681Seric 		{
8315532Seric 			while ((c = getc(InChannel)) != '\n' && c != EOF)
847681Seric 				continue;
857681Seric 			/* give an error? */
867681Seric 		}
877681Seric 
887852Seric 		fixcrlf(buf, TRUE);
894557Seric 
902900Seric 		/* see if the header is over */
912900Seric 		if (!isheader(buf))
922900Seric 			break;
932900Seric 
942900Seric 		/* get the rest of this field */
955975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
961392Seric 		{
972900Seric 			p = &buf[strlen(buf)];
987852Seric 			*p++ = '\n';
992900Seric 			*p++ = c;
10013932Seric 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1012900Seric 				break;
1027852Seric 			fixcrlf(p, TRUE);
1031392Seric 		}
1045975Seric 		if (!feof(InChannel))
1055975Seric 			(void) ungetc(c, InChannel);
1061392Seric 
1076901Seric 		CurEnv->e_msgsize += strlen(buf);
1081392Seric 
1092900Seric 		/*
1102900Seric 		**  Snarf header away.
1112900Seric 		*/
1122900Seric 
1133391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1143058Seric 			break;
11515532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
1161392Seric 
1172900Seric # ifdef DEBUG
1187673Seric 	if (tTd(30, 1))
1192900Seric 		printf("EOH\n");
1202900Seric # endif DEBUG
1212900Seric 
1222900Seric 	/* throw away a blank line */
1237852Seric 	if (buf[0] == '\0')
12413932Seric 		(void) sfgets(buf, MAXFIELD, InChannel);
1252900Seric 
1262900Seric 	/*
1272900Seric 	**  Collect the body of the message.
1282900Seric 	*/
1292900Seric 
13015532Seric 	do
1312900Seric 	{
1324551Seric 		register char *bp = buf;
1334156Seric 
1347852Seric 		fixcrlf(buf, TRUE);
1354557Seric 
1362900Seric 		/* check for end-of-message */
1372900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1382900Seric 			break;
1392900Seric 
1404551Seric 		/* check for transparent dot */
1419371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1424551Seric 			bp++;
1434551Seric 
1444156Seric 		/*
1454156Seric 		**  Figure message length, output the line to the temp
1464156Seric 		**  file, and insert a newline if missing.
1474156Seric 		*/
1484156Seric 
1499371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1504551Seric 		fputs(bp, tf);
1517852Seric 		fputs("\n", tf);
1521392Seric 		if (ferror(tf))
15311544Seric 			tferror(tf);
15415532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
15511544Seric 	if (fflush(tf) != 0)
15611544Seric 		tferror(tf);
1574083Seric 	(void) fclose(tf);
1582900Seric 
15911145Seric 	/* An EOF when running SMTP is an error */
16011145Seric 	if (feof(InChannel) && OpMode == MD_SMTP)
16116136Seric 	{
16216136Seric 		syserr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
16311145Seric 
16416136Seric 		/* don't return an error indication */
16516136Seric 		CurEnv->e_to = NULL;
16616136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
16716136Seric 
16816136Seric 		/* and don't try to deliver the partial message either */
16916136Seric 		finis();
17016136Seric 	}
17116136Seric 
1722900Seric 	/*
1732900Seric 	**  Find out some information from the headers.
1743386Seric 	**	Examples are who is the from person & the date.
1752900Seric 	*/
1762900Seric 
1779371Seric 	eatheader(CurEnv);
1787673Seric 
1797782Seric 	/*
1807782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1817782Seric 	*/
1824622Seric 
1837367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1847367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
1857367Seric 	{
1867367Seric 		register ADDRESS *q;
1877367Seric 
1887367Seric 		/* create an Apparently-To: field */
1897367Seric 		/*    that or reject the message.... */
1907367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
1917367Seric 		{
1927389Seric 			if (q->q_alias != NULL)
1937389Seric 				continue;
1947367Seric # ifdef DEBUG
1957673Seric 			if (tTd(30, 3))
1967367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
1977367Seric # endif DEBUG
1987367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
1997367Seric 		}
2007367Seric 	}
2017367Seric 
2029539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2036986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2041392Seric }
2051392Seric /*
20611544Seric **  TFERROR -- signal error on writing the temporary file.
20711544Seric **
20811544Seric **	Parameters:
20911544Seric **		tf -- the file pointer for the temporary file.
21011544Seric **
21111544Seric **	Returns:
21211544Seric **		none.
21311544Seric **
21411544Seric **	Side Effects:
21511544Seric **		Gives an error message.
21611544Seric **		Arranges for following output to go elsewhere.
21711544Seric */
21811544Seric 
21911544Seric tferror(tf)
22011544Seric 	FILE *tf;
22111544Seric {
22211544Seric 	if (errno == ENOSPC)
22311544Seric 	{
22411544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
22511544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
22611544Seric 		usrerr("452 Out of disk space for temp file");
22711544Seric 	}
22811544Seric 	else
22911544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
23011544Seric 	(void) freopen("/dev/null", "w", tf);
23111544Seric }
23211544Seric /*
2332900Seric **  EATFROM -- chew up a UNIX style from line and process
2342900Seric **
2352900Seric **	This does indeed make some assumptions about the format
2362900Seric **	of UNIX messages.
2372900Seric **
2382900Seric **	Parameters:
2392900Seric **		fm -- the from line.
2402900Seric **
2412900Seric **	Returns:
2422900Seric **		none.
2432900Seric **
2442900Seric **	Side Effects:
2452900Seric **		extracts what information it can from the header,
2463386Seric **		such as the date.
2472900Seric */
2482900Seric 
2494321Seric # ifndef NOTUNIX
2504321Seric 
2514203Seric char	*DowList[] =
2524203Seric {
2534203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2544203Seric };
2554203Seric 
2562900Seric char	*MonthList[] =
2572900Seric {
2582900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2592900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2602900Seric 	NULL
2612900Seric };
2622900Seric 
2632900Seric eatfrom(fm)
2642900Seric 	char *fm;
2652900Seric {
2662900Seric 	register char *p;
2672900Seric 	register char **dt;
2682900Seric 
2694203Seric # ifdef DEBUG
2707673Seric 	if (tTd(30, 2))
2714203Seric 		printf("eatfrom(%s)\n", fm);
2724203Seric # endif DEBUG
2734203Seric 
2742900Seric 	/* find the date part */
2752900Seric 	p = fm;
2762900Seric 	while (*p != '\0')
2772900Seric 	{
2782900Seric 		/* skip a word */
2792900Seric 		while (*p != '\0' && *p != ' ')
280*16896Seric 			p++;
2812900Seric 		while (*p == ' ')
282*16896Seric 			p++;
2832900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2842900Seric 			continue;
2852900Seric 
2862900Seric 		/* we have a possible date */
2874203Seric 		for (dt = DowList; *dt != NULL; dt++)
2882900Seric 			if (strncmp(*dt, p, 3) == 0)
2892900Seric 				break;
2904203Seric 		if (*dt == NULL)
2914203Seric 			continue;
2922900Seric 
2934203Seric 		for (dt = MonthList; *dt != NULL; dt++)
2944203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
2954203Seric 				break;
2962900Seric 		if (*dt != NULL)
2972900Seric 			break;
2982900Seric 	}
2992900Seric 
3002900Seric 	if (*p != NULL)
3012900Seric 	{
3023386Seric 		char *q;
3035366Seric 		extern char *arpadate();
3043386Seric 
3052900Seric 		/* we have found a date */
3063386Seric 		q = xalloc(25);
3073386Seric 		strncpy(q, p, 25);
3083386Seric 		q[24] = '\0';
3099371Seric 		define('d', q, CurEnv);
3105366Seric 		q = arpadate(q);
3119371Seric 		define('a', newstr(q), CurEnv);
3122900Seric 	}
3132900Seric }
3144321Seric 
3154321Seric # endif NOTUNIX
316