11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*19036Seric SCCSID(@(#)collect.c	4.5		04/28/85);
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 
80*19036Seric 		/* drop out on error */
81*19036Seric 		if (ferror(InChannel))
82*19036Seric 			break;
83*19036Seric 
847681Seric 		/* if the line is too long, throw the rest away */
857681Seric 		if (index(buf, '\n') == NULL)
867681Seric 		{
8715532Seric 			while ((c = getc(InChannel)) != '\n' && c != EOF)
887681Seric 				continue;
897681Seric 			/* give an error? */
907681Seric 		}
917681Seric 
927852Seric 		fixcrlf(buf, TRUE);
934557Seric 
942900Seric 		/* see if the header is over */
952900Seric 		if (!isheader(buf))
962900Seric 			break;
972900Seric 
982900Seric 		/* get the rest of this field */
995975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1001392Seric 		{
1012900Seric 			p = &buf[strlen(buf)];
1027852Seric 			*p++ = '\n';
1032900Seric 			*p++ = c;
10413932Seric 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1052900Seric 				break;
1067852Seric 			fixcrlf(p, TRUE);
1071392Seric 		}
108*19036Seric 		if (!feof(InChannel) && !ferror(InChannel))
1095975Seric 			(void) ungetc(c, InChannel);
1101392Seric 
1116901Seric 		CurEnv->e_msgsize += strlen(buf);
1121392Seric 
1132900Seric 		/*
1142900Seric 		**  Snarf header away.
1152900Seric 		*/
1162900Seric 
1173391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1183058Seric 			break;
11915532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
1201392Seric 
1212900Seric # ifdef DEBUG
1227673Seric 	if (tTd(30, 1))
1232900Seric 		printf("EOH\n");
1242900Seric # endif DEBUG
1252900Seric 
1262900Seric 	/* throw away a blank line */
1277852Seric 	if (buf[0] == '\0')
12813932Seric 		(void) sfgets(buf, MAXFIELD, InChannel);
1292900Seric 
1302900Seric 	/*
1312900Seric 	**  Collect the body of the message.
1322900Seric 	*/
1332900Seric 
13415532Seric 	do
1352900Seric 	{
1364551Seric 		register char *bp = buf;
1374156Seric 
1387852Seric 		fixcrlf(buf, TRUE);
1394557Seric 
1402900Seric 		/* check for end-of-message */
1412900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1422900Seric 			break;
1432900Seric 
1444551Seric 		/* check for transparent dot */
1459371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1464551Seric 			bp++;
1474551Seric 
1484156Seric 		/*
1494156Seric 		**  Figure message length, output the line to the temp
1504156Seric 		**  file, and insert a newline if missing.
1514156Seric 		*/
1524156Seric 
1539371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1544551Seric 		fputs(bp, tf);
1557852Seric 		fputs("\n", tf);
1561392Seric 		if (ferror(tf))
15711544Seric 			tferror(tf);
15815532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
15911544Seric 	if (fflush(tf) != 0)
16011544Seric 		tferror(tf);
1614083Seric 	(void) fclose(tf);
1622900Seric 
16311145Seric 	/* An EOF when running SMTP is an error */
164*19036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
16516136Seric 	{
16616136Seric 		syserr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
16711145Seric 
16816136Seric 		/* don't return an error indication */
16916136Seric 		CurEnv->e_to = NULL;
17016136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
17116136Seric 
17216136Seric 		/* and don't try to deliver the partial message either */
17316136Seric 		finis();
17416136Seric 	}
17516136Seric 
1762900Seric 	/*
1772900Seric 	**  Find out some information from the headers.
1783386Seric 	**	Examples are who is the from person & the date.
1792900Seric 	*/
1802900Seric 
1819371Seric 	eatheader(CurEnv);
1827673Seric 
1837782Seric 	/*
1847782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1857782Seric 	*/
1864622Seric 
1877367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1887367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
1897367Seric 	{
1907367Seric 		register ADDRESS *q;
1917367Seric 
1927367Seric 		/* create an Apparently-To: field */
1937367Seric 		/*    that or reject the message.... */
1947367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
1957367Seric 		{
1967389Seric 			if (q->q_alias != NULL)
1977389Seric 				continue;
1987367Seric # ifdef DEBUG
1997673Seric 			if (tTd(30, 3))
2007367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2017367Seric # endif DEBUG
2027367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2037367Seric 		}
2047367Seric 	}
2057367Seric 
2069539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2076986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2081392Seric }
2091392Seric /*
21011544Seric **  TFERROR -- signal error on writing the temporary file.
21111544Seric **
21211544Seric **	Parameters:
21311544Seric **		tf -- the file pointer for the temporary file.
21411544Seric **
21511544Seric **	Returns:
21611544Seric **		none.
21711544Seric **
21811544Seric **	Side Effects:
21911544Seric **		Gives an error message.
22011544Seric **		Arranges for following output to go elsewhere.
22111544Seric */
22211544Seric 
22311544Seric tferror(tf)
22411544Seric 	FILE *tf;
22511544Seric {
22611544Seric 	if (errno == ENOSPC)
22711544Seric 	{
22811544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
22911544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
23011544Seric 		usrerr("452 Out of disk space for temp file");
23111544Seric 	}
23211544Seric 	else
23311544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
23411544Seric 	(void) freopen("/dev/null", "w", tf);
23511544Seric }
23611544Seric /*
2372900Seric **  EATFROM -- chew up a UNIX style from line and process
2382900Seric **
2392900Seric **	This does indeed make some assumptions about the format
2402900Seric **	of UNIX messages.
2412900Seric **
2422900Seric **	Parameters:
2432900Seric **		fm -- the from line.
2442900Seric **
2452900Seric **	Returns:
2462900Seric **		none.
2472900Seric **
2482900Seric **	Side Effects:
2492900Seric **		extracts what information it can from the header,
2503386Seric **		such as the date.
2512900Seric */
2522900Seric 
2534321Seric # ifndef NOTUNIX
2544321Seric 
2554203Seric char	*DowList[] =
2564203Seric {
2574203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2584203Seric };
2594203Seric 
2602900Seric char	*MonthList[] =
2612900Seric {
2622900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2632900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2642900Seric 	NULL
2652900Seric };
2662900Seric 
2672900Seric eatfrom(fm)
2682900Seric 	char *fm;
2692900Seric {
2702900Seric 	register char *p;
2712900Seric 	register char **dt;
2722900Seric 
2734203Seric # ifdef DEBUG
2747673Seric 	if (tTd(30, 2))
2754203Seric 		printf("eatfrom(%s)\n", fm);
2764203Seric # endif DEBUG
2774203Seric 
2782900Seric 	/* find the date part */
2792900Seric 	p = fm;
2802900Seric 	while (*p != '\0')
2812900Seric 	{
2822900Seric 		/* skip a word */
2832900Seric 		while (*p != '\0' && *p != ' ')
28416896Seric 			p++;
2852900Seric 		while (*p == ' ')
28616896Seric 			p++;
2872900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2882900Seric 			continue;
2892900Seric 
2902900Seric 		/* we have a possible date */
2914203Seric 		for (dt = DowList; *dt != NULL; dt++)
2922900Seric 			if (strncmp(*dt, p, 3) == 0)
2932900Seric 				break;
2944203Seric 		if (*dt == NULL)
2954203Seric 			continue;
2962900Seric 
2974203Seric 		for (dt = MonthList; *dt != NULL; dt++)
2984203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
2994203Seric 				break;
3002900Seric 		if (*dt != NULL)
3012900Seric 			break;
3022900Seric 	}
3032900Seric 
3042900Seric 	if (*p != NULL)
3052900Seric 	{
3063386Seric 		char *q;
3075366Seric 		extern char *arpadate();
3083386Seric 
3092900Seric 		/* we have found a date */
3103386Seric 		q = xalloc(25);
3113386Seric 		strncpy(q, p, 25);
3123386Seric 		q[24] = '\0';
3139371Seric 		define('d', q, CurEnv);
3145366Seric 		q = arpadate(q);
3159371Seric 		define('a', newstr(q), CurEnv);
3162900Seric 	}
3172900Seric }
3184321Seric 
3194321Seric # endif NOTUNIX
320