11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*11145Seric SCCSID(@(#)collect.c	3.57		02/18/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))
1691392Seric 		{
1701439Seric 			if (errno == ENOSPC)
1711439Seric 			{
1726986Seric 				(void) freopen(CurEnv->e_df, "w", tf);
1731439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1744557Seric 				usrerr("452 Out of disk space for temp file");
1751439Seric 			}
1761439Seric 			else
1776986Seric 				syserr("collect: Cannot write %s", CurEnv->e_df);
1784083Seric 			(void) freopen("/dev/null", "w", tf);
1791392Seric 		}
1801392Seric 	}
1814083Seric 	(void) fclose(tf);
1822900Seric 
183*11145Seric 	/* An EOF when running SMTP is an error */
184*11145Seric 	if (feof(InChannel) && OpMode == MD_SMTP)
185*11145Seric 		syserr("collect: unexpected close");
186*11145Seric 
1872900Seric 	/*
1882900Seric 	**  Find out some information from the headers.
1893386Seric 	**	Examples are who is the from person & the date.
1902900Seric 	*/
1912900Seric 
1929371Seric 	eatheader(CurEnv);
1937673Seric 
1947782Seric 	/*
1957782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
1967782Seric 	*/
1974622Seric 
1987367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
1997367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2007367Seric 	{
2017367Seric 		register ADDRESS *q;
2027367Seric 
2037367Seric 		/* create an Apparently-To: field */
2047367Seric 		/*    that or reject the message.... */
2057367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2067367Seric 		{
2077389Seric 			if (q->q_alias != NULL)
2087389Seric 				continue;
2097367Seric # ifdef DEBUG
2107673Seric 			if (tTd(30, 3))
2117367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2127367Seric # endif DEBUG
2137367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2147367Seric 		}
2157367Seric 	}
2167367Seric 
2179539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2186986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2192900Seric 
2207673Seric 	/*
2217673Seric 	**  Log collection information.
2227673Seric 	*/
2237673Seric 
2247673Seric # ifdef LOG
2257673Seric 	if (LogLevel > 1)
2267809Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
2277809Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
2287758Seric 		       CurEnv->e_class);
2297673Seric # endif LOG
2304162Seric 	return;
2311392Seric }
2321392Seric /*
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 != ' ')
2802900Seric 			*p++;
2812900Seric 		while (*p == ' ')
2822900Seric 			*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