11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*15532Seric SCCSID(@(#)collect.c	4.2		11/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*15532Seric 	(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 
75*15532Seric 	do
761392Seric 	{
77*15532Seric 		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 		{
83*15532Seric 			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;
115*15532Seric 	} 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 
130*15532Seric 	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);
154*15532Seric 	} 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)
16111145Seric 		syserr("collect: unexpected close");
16211145Seric 
1632900Seric 	/*
1642900Seric 	**  Find out some information from the headers.
1653386Seric 	**	Examples are who is the from person & the date.
1662900Seric 	*/
1672900Seric 
1689371Seric 	eatheader(CurEnv);
1697673Seric 
1707782Seric 	/*
1717782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1727782Seric 	*/
1734622Seric 
1747367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1757367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
1767367Seric 	{
1777367Seric 		register ADDRESS *q;
1787367Seric 
1797367Seric 		/* create an Apparently-To: field */
1807367Seric 		/*    that or reject the message.... */
1817367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
1827367Seric 		{
1837389Seric 			if (q->q_alias != NULL)
1847389Seric 				continue;
1857367Seric # ifdef DEBUG
1867673Seric 			if (tTd(30, 3))
1877367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
1887367Seric # endif DEBUG
1897367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
1907367Seric 		}
1917367Seric 	}
1927367Seric 
1939539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
1946986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
1951392Seric }
1961392Seric /*
19711544Seric **  TFERROR -- signal error on writing the temporary file.
19811544Seric **
19911544Seric **	Parameters:
20011544Seric **		tf -- the file pointer for the temporary file.
20111544Seric **
20211544Seric **	Returns:
20311544Seric **		none.
20411544Seric **
20511544Seric **	Side Effects:
20611544Seric **		Gives an error message.
20711544Seric **		Arranges for following output to go elsewhere.
20811544Seric */
20911544Seric 
21011544Seric tferror(tf)
21111544Seric 	FILE *tf;
21211544Seric {
21311544Seric 	if (errno == ENOSPC)
21411544Seric 	{
21511544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
21611544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
21711544Seric 		usrerr("452 Out of disk space for temp file");
21811544Seric 	}
21911544Seric 	else
22011544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
22111544Seric 	(void) freopen("/dev/null", "w", tf);
22211544Seric }
22311544Seric /*
2242900Seric **  EATFROM -- chew up a UNIX style from line and process
2252900Seric **
2262900Seric **	This does indeed make some assumptions about the format
2272900Seric **	of UNIX messages.
2282900Seric **
2292900Seric **	Parameters:
2302900Seric **		fm -- the from line.
2312900Seric **
2322900Seric **	Returns:
2332900Seric **		none.
2342900Seric **
2352900Seric **	Side Effects:
2362900Seric **		extracts what information it can from the header,
2373386Seric **		such as the date.
2382900Seric */
2392900Seric 
2404321Seric # ifndef NOTUNIX
2414321Seric 
2424203Seric char	*DowList[] =
2434203Seric {
2444203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2454203Seric };
2464203Seric 
2472900Seric char	*MonthList[] =
2482900Seric {
2492900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2502900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2512900Seric 	NULL
2522900Seric };
2532900Seric 
2542900Seric eatfrom(fm)
2552900Seric 	char *fm;
2562900Seric {
2572900Seric 	register char *p;
2582900Seric 	register char **dt;
2592900Seric 
2604203Seric # ifdef DEBUG
2617673Seric 	if (tTd(30, 2))
2624203Seric 		printf("eatfrom(%s)\n", fm);
2634203Seric # endif DEBUG
2644203Seric 
2652900Seric 	/* find the date part */
2662900Seric 	p = fm;
2672900Seric 	while (*p != '\0')
2682900Seric 	{
2692900Seric 		/* skip a word */
2702900Seric 		while (*p != '\0' && *p != ' ')
2712900Seric 			*p++;
2722900Seric 		while (*p == ' ')
2732900Seric 			*p++;
2742900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2752900Seric 			continue;
2762900Seric 
2772900Seric 		/* we have a possible date */
2784203Seric 		for (dt = DowList; *dt != NULL; dt++)
2792900Seric 			if (strncmp(*dt, p, 3) == 0)
2802900Seric 				break;
2814203Seric 		if (*dt == NULL)
2824203Seric 			continue;
2832900Seric 
2844203Seric 		for (dt = MonthList; *dt != NULL; dt++)
2854203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
2864203Seric 				break;
2872900Seric 		if (*dt != NULL)
2882900Seric 			break;
2892900Seric 	}
2902900Seric 
2912900Seric 	if (*p != NULL)
2922900Seric 	{
2933386Seric 		char *q;
2945366Seric 		extern char *arpadate();
2953386Seric 
2962900Seric 		/* we have found a date */
2973386Seric 		q = xalloc(25);
2983386Seric 		strncpy(q, p, 25);
2993386Seric 		q[24] = '\0';
3009371Seric 		define('d', q, CurEnv);
3015366Seric 		q = arpadate(q);
3029371Seric 		define('a', newstr(q), CurEnv);
3032900Seric 	}
3042900Seric }
3054321Seric 
3064321Seric # endif NOTUNIX
307