11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*6888Seric SCCSID(@(#)collect.c	3.36		05/20/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);
54*6888Seric 	if ((tf = dfopen(tempfname, "w")) == NULL)
551392Seric 	{
564622Seric 		syserr("Cannot create %s", tempfname);
575366Seric 		NoReturn = TRUE;
585366Seric 		finis();
591392Seric 	}
60*6888Seric 	chmod(tempfname, 0600);
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 
885975Seric 	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);
955975Seric 		(void) fgets(buf, sizeof buf, InChannel);
964557Seric 		fixcrlf(buf, FALSE);
972900Seric 	}
984321Seric # endif NOTUNIX
992900Seric 
1001392Seric 	/*
1015975Seric 	**  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 
1085975Seric 	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 */
1205975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1211392Seric 		{
1222900Seric 			p = &buf[strlen(buf)];
1232900Seric 			*p++ = c;
1245975Seric 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
1252900Seric 				break;
1264557Seric 			fixcrlf(p, FALSE);
1271392Seric 		}
1285975Seric 		if (!feof(InChannel))
1295975Seric 			(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 	{
1495975Seric 		(void) fgets(buf, sizeof buf, InChannel);
1504557Seric 		fixcrlf(buf, FALSE);
1514557Seric 	}
1522900Seric 
1532900Seric 	/*
1542900Seric 	**  Collect the body of the message.
1552900Seric 	*/
1562900Seric 
1575975Seric 	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 
2115982Seric 	/* message priority */
2125033Seric 	if (!QueueRun)
2135033Seric 	{
2145033Seric 		/* adjust total priority by message priority */
2155033Seric 		MsgPriority = MsgSize;
2165033Seric 		p = hvalue("priority");
2175033Seric 		if (p != NULL)
2185033Seric 			MsgPriority -= priencode(p) * WKPRIFACT;
2195033Seric 	}
2204622Seric 
2215982Seric 	/* special handling */
2225982Seric 	p = hvalue("special-handling");
2235982Seric 	if (p != NULL)
2245982Seric 		spechandling(p);
2255982Seric 
2262900Seric 	/* from person */
2272900Seric 	xfrom = hvalue("sender");
2282900Seric 	if (xfrom == NULL)
2294371Seric 		xfrom = OrigFrom;
2304710Seric 	if (ArpaMode)
2314316Seric 		setfrom(xfrom, (char *) NULL);
2322900Seric 
2333390Seric 	/* full name of from person */
2343390Seric 	p = hvalue("full-name");
2353390Seric 	if (p != NULL)
2363390Seric 		define('x', p);
2374210Seric 	else
2384210Seric 	{
2395917Seric 		extern char *getxpart();
2403390Seric 
2414210Seric 		/*
2424210Seric 		**  Try to extract the full name from a general From:
2434210Seric 		**  field.  We take anything which is a comment as a
2444210Seric 		**  first choice.  Failing in that, we see if there is
2454210Seric 		**  a "machine readable" name (in <angle brackets>); if
2464210Seric 		**  so we take anything preceeding that clause.
2474210Seric 		**
2484210Seric 		**  If we blow it here it's not all that serious.
2494210Seric 		*/
2504210Seric 
2514210Seric 		p = hvalue("original-from");
2524371Seric 		if (p == NULL)
2534371Seric 			p = OrigFrom;
2545917Seric 		p = getxpart(p);
2555917Seric 		if (p != NULL)
2565917Seric 			define('x', newstr(p));
2574210Seric 	}
2584210Seric 
2592900Seric 	/* date message originated */
2604149Seric 	p = hvalue("posted-date");
2614149Seric 	if (p == NULL)
2624149Seric 		p = hvalue("date");
2632900Seric 	if (p != NULL)
2642900Seric 	{
2653386Seric 		define('a', p);
2663386Seric 		/* we don't have a good way to do canonical conversion ....
2673386Seric 		define('d', newstr(arpatounix(p)));
2683386Seric 		.... so we will ignore the problem for the time being */
2692900Seric 	}
2702900Seric 
2714182Seric 	if ((TempFile = fopen(InFileName, "r")) == NULL)
2721392Seric 		syserr("Cannot reopen %s", InFileName);
2732900Seric 
2742900Seric # ifdef DEBUG
2752900Seric 	if (Debug)
2762900Seric 	{
2774316Seric 		HDR *h;
2784316Seric 		extern char *capitalize();
2794316Seric 
2802900Seric 		printf("----- collected header -----\n");
2812900Seric 		for (h = Header; h != NULL; h = h->h_link)
2822900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2832900Seric 		printf("----------------------------\n");
2842900Seric 	}
2852900Seric # endif DEBUG
2864162Seric 	return;
2871392Seric }
2881392Seric /*
2892900Seric **  EATFROM -- chew up a UNIX style from line and process
2902900Seric **
2912900Seric **	This does indeed make some assumptions about the format
2922900Seric **	of UNIX messages.
2932900Seric **
2942900Seric **	Parameters:
2952900Seric **		fm -- the from line.
2962900Seric **
2972900Seric **	Returns:
2982900Seric **		none.
2992900Seric **
3002900Seric **	Side Effects:
3012900Seric **		extracts what information it can from the header,
3023386Seric **		such as the date.
3032900Seric */
3042900Seric 
3054321Seric # ifndef NOTUNIX
3064321Seric 
3074203Seric char	*DowList[] =
3084203Seric {
3094203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3104203Seric };
3114203Seric 
3122900Seric char	*MonthList[] =
3132900Seric {
3142900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3152900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3162900Seric 	NULL
3172900Seric };
3182900Seric 
3192900Seric eatfrom(fm)
3202900Seric 	char *fm;
3212900Seric {
3222900Seric 	register char *p;
3232900Seric 	register char **dt;
3242900Seric 
3254203Seric # ifdef DEBUG
3264203Seric 	if (Debug > 1)
3274203Seric 		printf("eatfrom(%s)\n", fm);
3284203Seric # endif DEBUG
3294203Seric 
3302900Seric 	/* find the date part */
3312900Seric 	p = fm;
3322900Seric 	while (*p != '\0')
3332900Seric 	{
3342900Seric 		/* skip a word */
3352900Seric 		while (*p != '\0' && *p != ' ')
3362900Seric 			*p++;
3372900Seric 		while (*p == ' ')
3382900Seric 			*p++;
3392900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3402900Seric 			continue;
3412900Seric 
3422900Seric 		/* we have a possible date */
3434203Seric 		for (dt = DowList; *dt != NULL; dt++)
3442900Seric 			if (strncmp(*dt, p, 3) == 0)
3452900Seric 				break;
3464203Seric 		if (*dt == NULL)
3474203Seric 			continue;
3482900Seric 
3494203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3504203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3514203Seric 				break;
3522900Seric 		if (*dt != NULL)
3532900Seric 			break;
3542900Seric 	}
3552900Seric 
3562900Seric 	if (*p != NULL)
3572900Seric 	{
3583386Seric 		char *q;
3595366Seric 		extern char *arpadate();
3603386Seric 
3612900Seric 		/* we have found a date */
3623386Seric 		q = xalloc(25);
3633386Seric 		strncpy(q, p, 25);
3643386Seric 		q[24] = '\0';
3653386Seric 		define('d', q);
3665366Seric 		q = arpadate(q);
3675366Seric 		define('a', newstr(q));
3682900Seric 	}
3692900Seric }
3704321Seric 
3714321Seric # endif NOTUNIX
3724622Seric /*
3734622Seric **  PRIENCODE -- encode external priority names into internal values.
3744622Seric **
3754622Seric **	Parameters:
3764622Seric **		p -- priority in ascii.
3774622Seric **
3784622Seric **	Returns:
3794622Seric **		priority as a numeric level.
3804622Seric **
3814622Seric **	Side Effects:
3824622Seric **		none.
3834622Seric */
3844622Seric 
3854622Seric struct prio
3864622Seric {
3874622Seric 	char	*pri_name;	/* external name of priority */
3884622Seric 	int	pri_val;	/* internal value for same */
3894622Seric };
3904622Seric 
3914622Seric static struct prio	Prio[] =
3924622Seric {
3934633Seric 	"alert",		PRI_ALERT,
3944633Seric 	"quick",		PRI_QUICK,
3954633Seric 	"first-class",		PRI_FIRSTCL,
3964622Seric 	"normal",		PRI_NORMAL,
3974622Seric 	"second-class",		PRI_SECONDCL,
3984622Seric 	"third-class",		PRI_THIRDCL,
3994622Seric 	NULL,			PRI_NORMAL,
4004622Seric };
4014622Seric 
4024622Seric priencode(p)
4034622Seric 	char *p;
4044622Seric {
4054622Seric 	register struct prio *pl;
4064633Seric 	extern bool sameword();
4074622Seric 
4084622Seric 	for (pl = Prio; pl->pri_name != NULL; pl++)
4094622Seric 	{
4104633Seric 		if (sameword(p, pl->pri_name))
4114622Seric 			break;
4124622Seric 	}
4134622Seric 	return (pl->pri_val);
4144622Seric }
4155982Seric /*
4165982Seric **  SPECHANDLE -- do special handling
4175982Seric **
4185982Seric **	Parameters:
4195982Seric **		p -- pointer to list of special handling words.
4205982Seric **
4215982Seric **	Returns:
4225982Seric **		none.
4235982Seric **
4245982Seric **	Side Effects:
4255982Seric **		Sets flags as indicated by p.
4265982Seric */
4275982Seric 
4285982Seric struct handling
4295982Seric {
4305982Seric 	char	*han_name;		/* word to get this magic */
4315982Seric 	int	han_what;		/* what to do, see below */
4325982Seric };
4335982Seric 
4345982Seric /* modes for han_what */
4355982Seric # define	HAN_NONE	0	/* nothing special */
4365982Seric # define	HAN_RRECEIPT	1	/* give return receipt */
4375982Seric 
4385982Seric struct handling	Handling[] =
4395982Seric {
4405982Seric 	"return-receipt-requested",	HAN_RRECEIPT,
4415982Seric 	NULL,				HAN_NONE
4425982Seric };
4435982Seric 
4445982Seric spechandling(p)
4455982Seric 	register char *p;
4465982Seric {
4475982Seric 	register char *w;
4485982Seric 	register struct handling *h;
4495982Seric 	extern bool sameword();
4505982Seric 
4515982Seric 	while (*p != '\0')
4525982Seric 	{
4535982Seric 		/* collect a word to compare to */
4545982Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
4555982Seric 			p++;
4565982Seric 		if (*p == '\0')
4575982Seric 			break;
4585982Seric 		w = p;
4595982Seric 		while (*p != '\0' && *p != ',' && !isspace(*p))
4605982Seric 			p++;
4615982Seric 		if (*p != '\0')
4625982Seric 			*p++ = '\0';
4635982Seric 
4645982Seric 		/* scan the special handling table */
4655982Seric 		for (h = Handling; h->han_name != NULL; h++)
4665982Seric 			if (sameword(h->han_name, w))
4675982Seric 				break;
4685982Seric 
4695982Seric 		/* see if we can do anything interesting */
4705982Seric 		switch (h->han_what)
4715982Seric 		{
4725982Seric 		  case HAN_NONE:	/* nothing to be done */
4735982Seric 			break;
4745982Seric 
4755982Seric 		  case HAN_RRECEIPT:	/* give return receipt */
4765982Seric 			RetReceipt = TRUE;
4775982Seric # ifdef DEBUG
4785982Seric 			if (Debug > 2)
4795982Seric 				printf(">>> Return receipt requested\n");
4805982Seric # endif DEBUG
4815982Seric 			break;
4825982Seric 
4835982Seric 		  default:
4845982Seric 			syserr("spechandling: handling %d (%s)", h->han_what, w);
4855982Seric 		}
4865982Seric 	}
4875982Seric }
488