11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*5975Seric SCCSID(@(#)collect.c	3.34		02/26/82);
51392Seric 
61392Seric /*
72969Seric **  COLLECT -- read & parse message header & make temp file.
81392Seric **
91392Seric **	Creates a temporary file name and copies the standard
101392Seric **	input to that file.  While it is doing it, it looks for
111392Seric **	"From:" and "Sender:" fields to use as the from-person
121392Seric **	(but only if the -a flag is specified).  It prefers to
131392Seric **	to use the "Sender:" field.
141392Seric **
151392Seric **	MIT seems to like to produce "Sent-By:" fields instead
161392Seric **	of "Sender:" fields.  We used to catch this, but it turns
171392Seric **	out that the "Sent-By:" field doesn't always correspond
181392Seric **	to someone real ("___057", for instance), as required by
191392Seric **	the protocol.  So we limp by.....
201392Seric **
211392Seric **	Parameters:
224710Seric **		sayok -- if set, give an ARPANET style message
234710Seric **			to say we are ready to collect input.
241392Seric **
251392Seric **	Returns:
264162Seric **		none.
271392Seric **
281392Seric **	Side Effects:
291392Seric **		Temp file is created and filled.
304162Seric **		The from person may be set.
311392Seric */
321392Seric 
331624Seric long	MsgSize;		/* size of message in bytes */
341397Seric 
354710Seric collect(sayok)
364710Seric 	bool sayok;
371392Seric {
381392Seric 	register FILE *tf;
391392Seric 	char buf[MAXFIELD+1];
401392Seric 	register char *p;
412900Seric 	char *xfrom;
422900Seric 	extern char *hvalue();
434083Seric 	extern char *mktemp();
444622Seric 	static char tempfname[40];
455192Seric 	extern char *macvalue();
461392Seric 
471392Seric 	/*
481392Seric 	**  Create the temp file name and create the file.
491392Seric 	*/
501392Seric 
514622Seric 	strcpy(tempfname, QueueDir);
524622Seric 	strcat(tempfname, "/dfaXXXXXX");
534622Seric 	(void) mktemp(tempfname);
544622Seric 	(void) close(creat(tempfname, 0600));
554622Seric 	if ((tf = fopen(tempfname, "w")) == NULL)
561392Seric 	{
574622Seric 		syserr("Cannot create %s", tempfname);
585366Seric 		NoReturn = TRUE;
595366Seric 		finis();
601392Seric 	}
614622Seric 	InFileName = tempfname;
621392Seric 
634316Seric 	/*
645185Seric 	**  Create the Mail-From line if we want to.
655185Seric 	*/
665185Seric 
675366Seric 	if (Smtp && macvalue('s') != NULL)
685185Seric 	{
695185Seric 		char xbuf[50];
705185Seric 
715192Seric 		(void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b",
725185Seric 			macvalue('r') == NULL ? "" : "$r host ");
735185Seric 		(void) expand(xbuf, buf, &buf[sizeof buf - 1]);
745192Seric 		(void) chompheader(buf, FALSE);
755185Seric 	}
765185Seric 
775185Seric 	/*
784322Seric 	**  Tell ARPANET to go ahead.
794322Seric 	*/
804322Seric 
814710Seric 	if (sayok)
824710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
834322Seric 
844322Seric 	/*
854316Seric 	**  Try to read a UNIX-style From line
864316Seric 	*/
874316Seric 
88*5975Seric 	if (fgets(buf, sizeof buf, InChannel) == NULL)
894162Seric 		return;
904557Seric 	fixcrlf(buf, FALSE);
914321Seric # ifndef NOTUNIX
924322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
932900Seric 	{
942900Seric 		eatfrom(buf);
95*5975Seric 		(void) fgets(buf, sizeof buf, InChannel);
964557Seric 		fixcrlf(buf, FALSE);
972900Seric 	}
984321Seric # endif NOTUNIX
992900Seric 
1001392Seric 	/*
101*5975Seric 	**  Copy InChannel to temp file & do message editing.
1021392Seric 	**	To keep certain mailers from getting confused,
1031392Seric 	**	and to keep the output clean, lines that look
1041392Seric 	**	like UNIX "From" lines are deleted in the header,
1051392Seric 	**	and prepended with ">" in the body.
1061392Seric 	*/
1071392Seric 
108*5975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1091392Seric 	{
1104316Seric 		register char c;
1114316Seric 		extern bool isheader();
1124316Seric 
1134557Seric 		fixcrlf(buf, FALSE);
1144557Seric 
1152900Seric 		/* see if the header is over */
1162900Seric 		if (!isheader(buf))
1172900Seric 			break;
1182900Seric 
1192900Seric 		/* get the rest of this field */
120*5975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1211392Seric 		{
1222900Seric 			p = &buf[strlen(buf)];
1232900Seric 			*p++ = c;
124*5975Seric 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
1252900Seric 				break;
1264557Seric 			fixcrlf(p, FALSE);
1271392Seric 		}
128*5975Seric 		if (!feof(InChannel))
129*5975Seric 			(void) ungetc(c, InChannel);
1301392Seric 
1312900Seric 		MsgSize += strlen(buf);
1321392Seric 
1332900Seric 		/*
1342900Seric 		**  Snarf header away.
1352900Seric 		*/
1362900Seric 
1373391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1383058Seric 			break;
1392900Seric 	}
1401392Seric 
1412900Seric # ifdef DEBUG
1422900Seric 	if (Debug)
1432900Seric 		printf("EOH\n");
1442900Seric # endif DEBUG
1452900Seric 
1462900Seric 	/* throw away a blank line */
1472900Seric 	if (buf[0] == '\n')
1484557Seric 	{
149*5975Seric 		(void) fgets(buf, sizeof buf, InChannel);
1504557Seric 		fixcrlf(buf, FALSE);
1514557Seric 	}
1522900Seric 
1532900Seric 	/*
1542900Seric 	**  Collect the body of the message.
1552900Seric 	*/
1562900Seric 
157*5975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1582900Seric 	{
1594156Seric 		register int i;
1604551Seric 		register char *bp = buf;
1614156Seric 
1624557Seric 		fixcrlf(buf, FALSE);
1634557Seric 
1642900Seric 		/* check for end-of-message */
1652900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1662900Seric 			break;
1672900Seric 
1684551Seric 		/* check for transparent dot */
1694551Seric 		if (Smtp && *bp == '.')
1704551Seric 			bp++;
1714551Seric 
1724321Seric # ifndef NOTUNIX
1732900Seric 		/* Hide UNIX-like From lines */
1744551Seric 		if (strncmp(bp, "From ", 5) == 0)
1751392Seric 		{
1762900Seric 			fputs(">", tf);
1772900Seric 			MsgSize++;
1781392Seric 		}
1794321Seric # endif NOTUNIX
1804156Seric 
1814156Seric 		/*
1824156Seric 		**  Figure message length, output the line to the temp
1834156Seric 		**  file, and insert a newline if missing.
1844156Seric 		*/
1854156Seric 
1864551Seric 		i = strlen(bp);
1874156Seric 		MsgSize += i;
1884551Seric 		fputs(bp, tf);
1894551Seric 		if (bp[i - 1] != '\n')
1904156Seric 			fputs("\n", tf);
1911392Seric 		if (ferror(tf))
1921392Seric 		{
1931439Seric 			if (errno == ENOSPC)
1941439Seric 			{
1954083Seric 				(void) freopen(InFileName, "w", tf);
1961439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1974557Seric 				usrerr("452 Out of disk space for temp file");
1981439Seric 			}
1991439Seric 			else
2004453Seric 				syserr("collect: Cannot write %s", InFileName);
2014083Seric 			(void) freopen("/dev/null", "w", tf);
2021392Seric 		}
2031392Seric 	}
2044083Seric 	(void) fclose(tf);
2052900Seric 
2062900Seric 	/*
2072900Seric 	**  Find out some information from the headers.
2083386Seric 	**	Examples are who is the from person & the date.
2092900Seric 	*/
2102900Seric 
2115033Seric 	if (!QueueRun)
2125033Seric 	{
2135033Seric 		/* adjust total priority by message priority */
2145033Seric 		MsgPriority = MsgSize;
2155033Seric 		p = hvalue("priority");
2165033Seric 		if (p != NULL)
2175033Seric 			MsgPriority -= priencode(p) * WKPRIFACT;
2185033Seric 	}
2194622Seric 
2202900Seric 	/* from person */
2212900Seric 	xfrom = hvalue("sender");
2222900Seric 	if (xfrom == NULL)
2234371Seric 		xfrom = OrigFrom;
2244710Seric 	if (ArpaMode)
2254316Seric 		setfrom(xfrom, (char *) NULL);
2262900Seric 
2273390Seric 	/* full name of from person */
2283390Seric 	p = hvalue("full-name");
2293390Seric 	if (p != NULL)
2303390Seric 		define('x', p);
2314210Seric 	else
2324210Seric 	{
2335917Seric 		extern char *getxpart();
2343390Seric 
2354210Seric 		/*
2364210Seric 		**  Try to extract the full name from a general From:
2374210Seric 		**  field.  We take anything which is a comment as a
2384210Seric 		**  first choice.  Failing in that, we see if there is
2394210Seric 		**  a "machine readable" name (in <angle brackets>); if
2404210Seric 		**  so we take anything preceeding that clause.
2414210Seric 		**
2424210Seric 		**  If we blow it here it's not all that serious.
2434210Seric 		*/
2444210Seric 
2454210Seric 		p = hvalue("original-from");
2464371Seric 		if (p == NULL)
2474371Seric 			p = OrigFrom;
2485917Seric 		p = getxpart(p);
2495917Seric 		if (p != NULL)
2505917Seric 			define('x', newstr(p));
2514210Seric 	}
2524210Seric 
2532900Seric 	/* date message originated */
2544149Seric 	p = hvalue("posted-date");
2554149Seric 	if (p == NULL)
2564149Seric 		p = hvalue("date");
2572900Seric 	if (p != NULL)
2582900Seric 	{
2593386Seric 		define('a', p);
2603386Seric 		/* we don't have a good way to do canonical conversion ....
2613386Seric 		define('d', newstr(arpatounix(p)));
2623386Seric 		.... so we will ignore the problem for the time being */
2632900Seric 	}
2642900Seric 
2654182Seric 	if ((TempFile = fopen(InFileName, "r")) == NULL)
2661392Seric 		syserr("Cannot reopen %s", InFileName);
2672900Seric 
2682900Seric # ifdef DEBUG
2692900Seric 	if (Debug)
2702900Seric 	{
2714316Seric 		HDR *h;
2724316Seric 		extern char *capitalize();
2734316Seric 
2742900Seric 		printf("----- collected header -----\n");
2752900Seric 		for (h = Header; h != NULL; h = h->h_link)
2762900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2772900Seric 		printf("----------------------------\n");
2782900Seric 	}
2792900Seric # endif DEBUG
2804162Seric 	return;
2811392Seric }
2821392Seric /*
2832900Seric **  EATFROM -- chew up a UNIX style from line and process
2842900Seric **
2852900Seric **	This does indeed make some assumptions about the format
2862900Seric **	of UNIX messages.
2872900Seric **
2882900Seric **	Parameters:
2892900Seric **		fm -- the from line.
2902900Seric **
2912900Seric **	Returns:
2922900Seric **		none.
2932900Seric **
2942900Seric **	Side Effects:
2952900Seric **		extracts what information it can from the header,
2963386Seric **		such as the date.
2972900Seric */
2982900Seric 
2994321Seric # ifndef NOTUNIX
3004321Seric 
3014203Seric char	*DowList[] =
3024203Seric {
3034203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3044203Seric };
3054203Seric 
3062900Seric char	*MonthList[] =
3072900Seric {
3082900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3092900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3102900Seric 	NULL
3112900Seric };
3122900Seric 
3132900Seric eatfrom(fm)
3142900Seric 	char *fm;
3152900Seric {
3162900Seric 	register char *p;
3172900Seric 	register char **dt;
3182900Seric 
3194203Seric # ifdef DEBUG
3204203Seric 	if (Debug > 1)
3214203Seric 		printf("eatfrom(%s)\n", fm);
3224203Seric # endif DEBUG
3234203Seric 
3242900Seric 	/* find the date part */
3252900Seric 	p = fm;
3262900Seric 	while (*p != '\0')
3272900Seric 	{
3282900Seric 		/* skip a word */
3292900Seric 		while (*p != '\0' && *p != ' ')
3302900Seric 			*p++;
3312900Seric 		while (*p == ' ')
3322900Seric 			*p++;
3332900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3342900Seric 			continue;
3352900Seric 
3362900Seric 		/* we have a possible date */
3374203Seric 		for (dt = DowList; *dt != NULL; dt++)
3382900Seric 			if (strncmp(*dt, p, 3) == 0)
3392900Seric 				break;
3404203Seric 		if (*dt == NULL)
3414203Seric 			continue;
3422900Seric 
3434203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3444203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3454203Seric 				break;
3462900Seric 		if (*dt != NULL)
3472900Seric 			break;
3482900Seric 	}
3492900Seric 
3502900Seric 	if (*p != NULL)
3512900Seric 	{
3523386Seric 		char *q;
3535366Seric 		extern char *arpadate();
3543386Seric 
3552900Seric 		/* we have found a date */
3563386Seric 		q = xalloc(25);
3573386Seric 		strncpy(q, p, 25);
3583386Seric 		q[24] = '\0';
3593386Seric 		define('d', q);
3605366Seric 		q = arpadate(q);
3615366Seric 		define('a', newstr(q));
3622900Seric 	}
3632900Seric }
3644321Seric 
3654321Seric # endif NOTUNIX
3664622Seric /*
3674622Seric **  PRIENCODE -- encode external priority names into internal values.
3684622Seric **
3694622Seric **	Parameters:
3704622Seric **		p -- priority in ascii.
3714622Seric **
3724622Seric **	Returns:
3734622Seric **		priority as a numeric level.
3744622Seric **
3754622Seric **	Side Effects:
3764622Seric **		none.
3774622Seric */
3784622Seric 
3794622Seric struct prio
3804622Seric {
3814622Seric 	char	*pri_name;	/* external name of priority */
3824622Seric 	int	pri_val;	/* internal value for same */
3834622Seric };
3844622Seric 
3854622Seric static struct prio	Prio[] =
3864622Seric {
3874633Seric 	"alert",		PRI_ALERT,
3884633Seric 	"quick",		PRI_QUICK,
3894633Seric 	"first-class",		PRI_FIRSTCL,
3904622Seric 	"normal",		PRI_NORMAL,
3914622Seric 	"second-class",		PRI_SECONDCL,
3924622Seric 	"third-class",		PRI_THIRDCL,
3934622Seric 	NULL,			PRI_NORMAL,
3944622Seric };
3954622Seric 
3964622Seric priencode(p)
3974622Seric 	char *p;
3984622Seric {
3994622Seric 	register struct prio *pl;
4004633Seric 	extern bool sameword();
4014622Seric 
4024622Seric 	for (pl = Prio; pl->pri_name != NULL; pl++)
4034622Seric 	{
4044633Seric 		if (sameword(p, pl->pri_name))
4054622Seric 			break;
4064622Seric 	}
4074622Seric 	return (pl->pri_val);
4084622Seric }
409