11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*11544Seric SCCSID(@(#)collect.c	3.59		03/12/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 
575975Seric 	if (fgets(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);
645975Seric 		(void) fgets(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
731392Seric 	**	like UNIX "From" lines are deleted in the header,
741392Seric 	**	and prepended with ">" in the body.
751392Seric 	*/
761392Seric 
779371Seric 	for (; !feof(InChannel); !feof(InChannel) &&
789371Seric 				 fgets(buf, MAXFIELD, InChannel) != NULL)
791392Seric 	{
804316Seric 		register char c;
814316Seric 		extern bool isheader();
824316Seric 
837681Seric 		/* if the line is too long, throw the rest away */
847681Seric 		if (index(buf, '\n') == NULL)
857681Seric 		{
867681Seric 			while ((c = getc(InChannel)) != '\n')
877681Seric 				continue;
887681Seric 			/* give an error? */
897681Seric 		}
907681Seric 
917852Seric 		fixcrlf(buf, TRUE);
924557Seric 
932900Seric 		/* see if the header is over */
942900Seric 		if (!isheader(buf))
952900Seric 			break;
962900Seric 
972900Seric 		/* get the rest of this field */
985975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
991392Seric 		{
1002900Seric 			p = &buf[strlen(buf)];
1017852Seric 			*p++ = '\n';
1022900Seric 			*p++ = c;
1037852Seric 			if (fgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1042900Seric 				break;
1057852Seric 			fixcrlf(p, TRUE);
1061392Seric 		}
1075975Seric 		if (!feof(InChannel))
1085975Seric 			(void) ungetc(c, InChannel);
1091392Seric 
1106901Seric 		CurEnv->e_msgsize += strlen(buf);
1111392Seric 
1122900Seric 		/*
1132900Seric 		**  Snarf header away.
1142900Seric 		*/
1152900Seric 
1163391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1173058Seric 			break;
1182900Seric 	}
1191392Seric 
1202900Seric # ifdef DEBUG
1217673Seric 	if (tTd(30, 1))
1222900Seric 		printf("EOH\n");
1232900Seric # endif DEBUG
1242900Seric 
1252900Seric 	/* throw away a blank line */
1267852Seric 	if (buf[0] == '\0')
1274557Seric 	{
1287852Seric 		(void) fgets(buf, MAXFIELD, InChannel);
1297852Seric 		fixcrlf(buf, TRUE);
1304557Seric 	}
1312900Seric 
1322900Seric 	/*
1332900Seric 	**  Collect the body of the message.
1342900Seric 	*/
1352900Seric 
1369371Seric 	for (; !feof(InChannel); !feof(InChannel) &&
1379371Seric 				 fgets(buf, sizeof buf, InChannel) != NULL)
1382900Seric 	{
1394551Seric 		register char *bp = buf;
1404156Seric 
1417852Seric 		fixcrlf(buf, TRUE);
1424557Seric 
1432900Seric 		/* check for end-of-message */
1442900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1452900Seric 			break;
1462900Seric 
1474551Seric 		/* check for transparent dot */
1489371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1494551Seric 			bp++;
1504551Seric 
1514321Seric # ifndef NOTUNIX
1522900Seric 		/* Hide UNIX-like From lines */
1534551Seric 		if (strncmp(bp, "From ", 5) == 0)
1541392Seric 		{
1552900Seric 			fputs(">", tf);
1566901Seric 			CurEnv->e_msgsize++;
1571392Seric 		}
1584321Seric # endif NOTUNIX
1594156Seric 
1604156Seric 		/*
1614156Seric 		**  Figure message length, output the line to the temp
1624156Seric 		**  file, and insert a newline if missing.
1634156Seric 		*/
1644156Seric 
1659371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1664551Seric 		fputs(bp, tf);
1677852Seric 		fputs("\n", tf);
1681392Seric 		if (ferror(tf))
169*11544Seric 			tferror(tf);
1701392Seric 	}
171*11544Seric 	if (fflush(tf) != 0)
172*11544Seric 		tferror(tf);
1734083Seric 	(void) fclose(tf);
1742900Seric 
17511145Seric 	/* An EOF when running SMTP is an error */
17611145Seric 	if (feof(InChannel) && OpMode == MD_SMTP)
17711145Seric 		syserr("collect: unexpected close");
17811145Seric 
1792900Seric 	/*
1802900Seric 	**  Find out some information from the headers.
1813386Seric 	**	Examples are who is the from person & the date.
1822900Seric 	*/
1832900Seric 
1849371Seric 	eatheader(CurEnv);
1857673Seric 
1867782Seric 	/*
1877782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1887782Seric 	*/
1894622Seric 
1907367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1917367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
1927367Seric 	{
1937367Seric 		register ADDRESS *q;
1947367Seric 
1957367Seric 		/* create an Apparently-To: field */
1967367Seric 		/*    that or reject the message.... */
1977367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
1987367Seric 		{
1997389Seric 			if (q->q_alias != NULL)
2007389Seric 				continue;
2017367Seric # ifdef DEBUG
2027673Seric 			if (tTd(30, 3))
2037367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2047367Seric # endif DEBUG
2057367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2067367Seric 		}
2077367Seric 	}
2087367Seric 
2099539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2106986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2111392Seric }
2121392Seric /*
213*11544Seric **  TFERROR -- signal error on writing the temporary file.
214*11544Seric **
215*11544Seric **	Parameters:
216*11544Seric **		tf -- the file pointer for the temporary file.
217*11544Seric **
218*11544Seric **	Returns:
219*11544Seric **		none.
220*11544Seric **
221*11544Seric **	Side Effects:
222*11544Seric **		Gives an error message.
223*11544Seric **		Arranges for following output to go elsewhere.
224*11544Seric */
225*11544Seric 
226*11544Seric tferror(tf)
227*11544Seric 	FILE *tf;
228*11544Seric {
229*11544Seric 	if (errno == ENOSPC)
230*11544Seric 	{
231*11544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
232*11544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
233*11544Seric 		usrerr("452 Out of disk space for temp file");
234*11544Seric 	}
235*11544Seric 	else
236*11544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
237*11544Seric 	(void) freopen("/dev/null", "w", tf);
238*11544Seric }
239*11544Seric /*
2402900Seric **  EATFROM -- chew up a UNIX style from line and process
2412900Seric **
2422900Seric **	This does indeed make some assumptions about the format
2432900Seric **	of UNIX messages.
2442900Seric **
2452900Seric **	Parameters:
2462900Seric **		fm -- the from line.
2472900Seric **
2482900Seric **	Returns:
2492900Seric **		none.
2502900Seric **
2512900Seric **	Side Effects:
2522900Seric **		extracts what information it can from the header,
2533386Seric **		such as the date.
2542900Seric */
2552900Seric 
2564321Seric # ifndef NOTUNIX
2574321Seric 
2584203Seric char	*DowList[] =
2594203Seric {
2604203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2614203Seric };
2624203Seric 
2632900Seric char	*MonthList[] =
2642900Seric {
2652900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2662900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2672900Seric 	NULL
2682900Seric };
2692900Seric 
2702900Seric eatfrom(fm)
2712900Seric 	char *fm;
2722900Seric {
2732900Seric 	register char *p;
2742900Seric 	register char **dt;
2752900Seric 
2764203Seric # ifdef DEBUG
2777673Seric 	if (tTd(30, 2))
2784203Seric 		printf("eatfrom(%s)\n", fm);
2794203Seric # endif DEBUG
2804203Seric 
2812900Seric 	/* find the date part */
2822900Seric 	p = fm;
2832900Seric 	while (*p != '\0')
2842900Seric 	{
2852900Seric 		/* skip a word */
2862900Seric 		while (*p != '\0' && *p != ' ')
2872900Seric 			*p++;
2882900Seric 		while (*p == ' ')
2892900Seric 			*p++;
2902900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2912900Seric 			continue;
2922900Seric 
2932900Seric 		/* we have a possible date */
2944203Seric 		for (dt = DowList; *dt != NULL; dt++)
2952900Seric 			if (strncmp(*dt, p, 3) == 0)
2962900Seric 				break;
2974203Seric 		if (*dt == NULL)
2984203Seric 			continue;
2992900Seric 
3004203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3014203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3024203Seric 				break;
3032900Seric 		if (*dt != NULL)
3042900Seric 			break;
3052900Seric 	}
3062900Seric 
3072900Seric 	if (*p != NULL)
3082900Seric 	{
3093386Seric 		char *q;
3105366Seric 		extern char *arpadate();
3113386Seric 
3122900Seric 		/* we have found a date */
3133386Seric 		q = xalloc(25);
3143386Seric 		strncpy(q, p, 25);
3153386Seric 		q[24] = '\0';
3169371Seric 		define('d', q, CurEnv);
3175366Seric 		q = arpadate(q);
3189371Seric 		define('a', newstr(q), CurEnv);
3192900Seric 	}
3202900Seric }
3214321Seric 
3224321Seric # endif NOTUNIX
323