11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*6901Seric SCCSID(@(#)collect.c	3.37		05/22/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 
334710Seric collect(sayok)
344710Seric 	bool sayok;
351392Seric {
361392Seric 	register FILE *tf;
371392Seric 	char buf[MAXFIELD+1];
381392Seric 	register char *p;
392900Seric 	char *xfrom;
402900Seric 	extern char *hvalue();
414083Seric 	extern char *mktemp();
424622Seric 	static char tempfname[40];
435192Seric 	extern char *macvalue();
441392Seric 
451392Seric 	/*
461392Seric 	**  Create the temp file name and create the file.
471392Seric 	*/
481392Seric 
494622Seric 	strcpy(tempfname, QueueDir);
504622Seric 	strcat(tempfname, "/dfaXXXXXX");
514622Seric 	(void) mktemp(tempfname);
526888Seric 	if ((tf = dfopen(tempfname, "w")) == NULL)
531392Seric 	{
544622Seric 		syserr("Cannot create %s", tempfname);
555366Seric 		NoReturn = TRUE;
565366Seric 		finis();
571392Seric 	}
586888Seric 	chmod(tempfname, 0600);
594622Seric 	InFileName = tempfname;
601392Seric 
614316Seric 	/*
625185Seric 	**  Create the Mail-From line if we want to.
635185Seric 	*/
645185Seric 
655366Seric 	if (Smtp && macvalue('s') != NULL)
665185Seric 	{
675185Seric 		char xbuf[50];
685185Seric 
695192Seric 		(void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b",
705185Seric 			macvalue('r') == NULL ? "" : "$r host ");
715185Seric 		(void) expand(xbuf, buf, &buf[sizeof buf - 1]);
725192Seric 		(void) chompheader(buf, FALSE);
735185Seric 	}
745185Seric 
755185Seric 	/*
764322Seric 	**  Tell ARPANET to go ahead.
774322Seric 	*/
784322Seric 
794710Seric 	if (sayok)
804710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
814322Seric 
824322Seric 	/*
834316Seric 	**  Try to read a UNIX-style From line
844316Seric 	*/
854316Seric 
865975Seric 	if (fgets(buf, sizeof buf, InChannel) == NULL)
874162Seric 		return;
884557Seric 	fixcrlf(buf, FALSE);
894321Seric # ifndef NOTUNIX
904322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
912900Seric 	{
922900Seric 		eatfrom(buf);
935975Seric 		(void) fgets(buf, sizeof buf, InChannel);
944557Seric 		fixcrlf(buf, FALSE);
952900Seric 	}
964321Seric # endif NOTUNIX
972900Seric 
981392Seric 	/*
995975Seric 	**  Copy InChannel to temp file & do message editing.
1001392Seric 	**	To keep certain mailers from getting confused,
1011392Seric 	**	and to keep the output clean, lines that look
1021392Seric 	**	like UNIX "From" lines are deleted in the header,
1031392Seric 	**	and prepended with ">" in the body.
1041392Seric 	*/
1051392Seric 
1065975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1071392Seric 	{
1084316Seric 		register char c;
1094316Seric 		extern bool isheader();
1104316Seric 
1114557Seric 		fixcrlf(buf, FALSE);
1124557Seric 
1132900Seric 		/* see if the header is over */
1142900Seric 		if (!isheader(buf))
1152900Seric 			break;
1162900Seric 
1172900Seric 		/* get the rest of this field */
1185975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1191392Seric 		{
1202900Seric 			p = &buf[strlen(buf)];
1212900Seric 			*p++ = c;
1225975Seric 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
1232900Seric 				break;
1244557Seric 			fixcrlf(p, FALSE);
1251392Seric 		}
1265975Seric 		if (!feof(InChannel))
1275975Seric 			(void) ungetc(c, InChannel);
1281392Seric 
129*6901Seric 		CurEnv->e_msgsize += strlen(buf);
1301392Seric 
1312900Seric 		/*
1322900Seric 		**  Snarf header away.
1332900Seric 		*/
1342900Seric 
1353391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1363058Seric 			break;
1372900Seric 	}
1381392Seric 
1392900Seric # ifdef DEBUG
1402900Seric 	if (Debug)
1412900Seric 		printf("EOH\n");
1422900Seric # endif DEBUG
1432900Seric 
1442900Seric 	/* throw away a blank line */
1452900Seric 	if (buf[0] == '\n')
1464557Seric 	{
1475975Seric 		(void) fgets(buf, sizeof buf, InChannel);
1484557Seric 		fixcrlf(buf, FALSE);
1494557Seric 	}
1502900Seric 
1512900Seric 	/*
1522900Seric 	**  Collect the body of the message.
1532900Seric 	*/
1542900Seric 
1555975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1562900Seric 	{
1574156Seric 		register int i;
1584551Seric 		register char *bp = buf;
1594156Seric 
1604557Seric 		fixcrlf(buf, FALSE);
1614557Seric 
1622900Seric 		/* check for end-of-message */
1632900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1642900Seric 			break;
1652900Seric 
1664551Seric 		/* check for transparent dot */
1674551Seric 		if (Smtp && *bp == '.')
1684551Seric 			bp++;
1694551Seric 
1704321Seric # ifndef NOTUNIX
1712900Seric 		/* Hide UNIX-like From lines */
1724551Seric 		if (strncmp(bp, "From ", 5) == 0)
1731392Seric 		{
1742900Seric 			fputs(">", tf);
175*6901Seric 			CurEnv->e_msgsize++;
1761392Seric 		}
1774321Seric # endif NOTUNIX
1784156Seric 
1794156Seric 		/*
1804156Seric 		**  Figure message length, output the line to the temp
1814156Seric 		**  file, and insert a newline if missing.
1824156Seric 		*/
1834156Seric 
1844551Seric 		i = strlen(bp);
185*6901Seric 		CurEnv->e_msgsize += i;
1864551Seric 		fputs(bp, tf);
1874551Seric 		if (bp[i - 1] != '\n')
1884156Seric 			fputs("\n", tf);
1891392Seric 		if (ferror(tf))
1901392Seric 		{
1911439Seric 			if (errno == ENOSPC)
1921439Seric 			{
1934083Seric 				(void) freopen(InFileName, "w", tf);
1941439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1954557Seric 				usrerr("452 Out of disk space for temp file");
1961439Seric 			}
1971439Seric 			else
1984453Seric 				syserr("collect: Cannot write %s", InFileName);
1994083Seric 			(void) freopen("/dev/null", "w", tf);
2001392Seric 		}
2011392Seric 	}
2024083Seric 	(void) fclose(tf);
2032900Seric 
2042900Seric 	/*
2052900Seric 	**  Find out some information from the headers.
2063386Seric 	**	Examples are who is the from person & the date.
2072900Seric 	*/
2082900Seric 
2095982Seric 	/* message priority */
2105033Seric 	if (!QueueRun)
2115033Seric 	{
2125033Seric 		/* adjust total priority by message priority */
213*6901Seric 		CurEnv->e_msgpriority = CurEnv->e_msgsize;
2145033Seric 		p = hvalue("priority");
2155033Seric 		if (p != NULL)
216*6901Seric 			CurEnv->e_msgpriority -= priencode(p) * WKPRIFACT;
2175033Seric 	}
2184622Seric 
2195982Seric 	/* special handling */
2205982Seric 	p = hvalue("special-handling");
2215982Seric 	if (p != NULL)
2225982Seric 		spechandling(p);
2235982Seric 
2242900Seric 	/* from person */
2252900Seric 	xfrom = hvalue("sender");
2262900Seric 	if (xfrom == NULL)
227*6901Seric 		xfrom = CurEnv->e_origfrom;
2284710Seric 	if (ArpaMode)
2294316Seric 		setfrom(xfrom, (char *) NULL);
2302900Seric 
2313390Seric 	/* full name of from person */
2323390Seric 	p = hvalue("full-name");
2333390Seric 	if (p != NULL)
2343390Seric 		define('x', p);
2354210Seric 	else
2364210Seric 	{
2375917Seric 		extern char *getxpart();
2383390Seric 
2394210Seric 		/*
2404210Seric 		**  Try to extract the full name from a general From:
2414210Seric 		**  field.  We take anything which is a comment as a
2424210Seric 		**  first choice.  Failing in that, we see if there is
2434210Seric 		**  a "machine readable" name (in <angle brackets>); if
2444210Seric 		**  so we take anything preceeding that clause.
2454210Seric 		**
2464210Seric 		**  If we blow it here it's not all that serious.
2474210Seric 		*/
2484210Seric 
2494210Seric 		p = hvalue("original-from");
2504371Seric 		if (p == NULL)
251*6901Seric 			p = CurEnv->e_origfrom;
2525917Seric 		p = getxpart(p);
2535917Seric 		if (p != NULL)
2545917Seric 			define('x', newstr(p));
2554210Seric 	}
2564210Seric 
2572900Seric 	/* date message originated */
2584149Seric 	p = hvalue("posted-date");
2594149Seric 	if (p == NULL)
2604149Seric 		p = hvalue("date");
2612900Seric 	if (p != NULL)
2622900Seric 	{
2633386Seric 		define('a', p);
2643386Seric 		/* we don't have a good way to do canonical conversion ....
2653386Seric 		define('d', newstr(arpatounix(p)));
2663386Seric 		.... so we will ignore the problem for the time being */
2672900Seric 	}
2682900Seric 
2694182Seric 	if ((TempFile = fopen(InFileName, "r")) == NULL)
2701392Seric 		syserr("Cannot reopen %s", InFileName);
2712900Seric 
2722900Seric # ifdef DEBUG
2732900Seric 	if (Debug)
2742900Seric 	{
2754316Seric 		HDR *h;
2764316Seric 		extern char *capitalize();
2774316Seric 
2782900Seric 		printf("----- collected header -----\n");
279*6901Seric 		for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2802900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2812900Seric 		printf("----------------------------\n");
2822900Seric 	}
2832900Seric # endif DEBUG
2844162Seric 	return;
2851392Seric }
2861392Seric /*
2872900Seric **  EATFROM -- chew up a UNIX style from line and process
2882900Seric **
2892900Seric **	This does indeed make some assumptions about the format
2902900Seric **	of UNIX messages.
2912900Seric **
2922900Seric **	Parameters:
2932900Seric **		fm -- the from line.
2942900Seric **
2952900Seric **	Returns:
2962900Seric **		none.
2972900Seric **
2982900Seric **	Side Effects:
2992900Seric **		extracts what information it can from the header,
3003386Seric **		such as the date.
3012900Seric */
3022900Seric 
3034321Seric # ifndef NOTUNIX
3044321Seric 
3054203Seric char	*DowList[] =
3064203Seric {
3074203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3084203Seric };
3094203Seric 
3102900Seric char	*MonthList[] =
3112900Seric {
3122900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3132900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3142900Seric 	NULL
3152900Seric };
3162900Seric 
3172900Seric eatfrom(fm)
3182900Seric 	char *fm;
3192900Seric {
3202900Seric 	register char *p;
3212900Seric 	register char **dt;
3222900Seric 
3234203Seric # ifdef DEBUG
3244203Seric 	if (Debug > 1)
3254203Seric 		printf("eatfrom(%s)\n", fm);
3264203Seric # endif DEBUG
3274203Seric 
3282900Seric 	/* find the date part */
3292900Seric 	p = fm;
3302900Seric 	while (*p != '\0')
3312900Seric 	{
3322900Seric 		/* skip a word */
3332900Seric 		while (*p != '\0' && *p != ' ')
3342900Seric 			*p++;
3352900Seric 		while (*p == ' ')
3362900Seric 			*p++;
3372900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3382900Seric 			continue;
3392900Seric 
3402900Seric 		/* we have a possible date */
3414203Seric 		for (dt = DowList; *dt != NULL; dt++)
3422900Seric 			if (strncmp(*dt, p, 3) == 0)
3432900Seric 				break;
3444203Seric 		if (*dt == NULL)
3454203Seric 			continue;
3462900Seric 
3474203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3484203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3494203Seric 				break;
3502900Seric 		if (*dt != NULL)
3512900Seric 			break;
3522900Seric 	}
3532900Seric 
3542900Seric 	if (*p != NULL)
3552900Seric 	{
3563386Seric 		char *q;
3575366Seric 		extern char *arpadate();
3583386Seric 
3592900Seric 		/* we have found a date */
3603386Seric 		q = xalloc(25);
3613386Seric 		strncpy(q, p, 25);
3623386Seric 		q[24] = '\0';
3633386Seric 		define('d', q);
3645366Seric 		q = arpadate(q);
3655366Seric 		define('a', newstr(q));
3662900Seric 	}
3672900Seric }
3684321Seric 
3694321Seric # endif NOTUNIX
3704622Seric /*
3714622Seric **  PRIENCODE -- encode external priority names into internal values.
3724622Seric **
3734622Seric **	Parameters:
3744622Seric **		p -- priority in ascii.
3754622Seric **
3764622Seric **	Returns:
3774622Seric **		priority as a numeric level.
3784622Seric **
3794622Seric **	Side Effects:
3804622Seric **		none.
3814622Seric */
3824622Seric 
3834622Seric struct prio
3844622Seric {
3854622Seric 	char	*pri_name;	/* external name of priority */
3864622Seric 	int	pri_val;	/* internal value for same */
3874622Seric };
3884622Seric 
3894622Seric static struct prio	Prio[] =
3904622Seric {
3914633Seric 	"alert",		PRI_ALERT,
3924633Seric 	"quick",		PRI_QUICK,
3934633Seric 	"first-class",		PRI_FIRSTCL,
3944622Seric 	"normal",		PRI_NORMAL,
3954622Seric 	"second-class",		PRI_SECONDCL,
3964622Seric 	"third-class",		PRI_THIRDCL,
3974622Seric 	NULL,			PRI_NORMAL,
3984622Seric };
3994622Seric 
4004622Seric priencode(p)
4014622Seric 	char *p;
4024622Seric {
4034622Seric 	register struct prio *pl;
4044633Seric 	extern bool sameword();
4054622Seric 
4064622Seric 	for (pl = Prio; pl->pri_name != NULL; pl++)
4074622Seric 	{
4084633Seric 		if (sameword(p, pl->pri_name))
4094622Seric 			break;
4104622Seric 	}
4114622Seric 	return (pl->pri_val);
4124622Seric }
4135982Seric /*
4145982Seric **  SPECHANDLE -- do special handling
4155982Seric **
4165982Seric **	Parameters:
4175982Seric **		p -- pointer to list of special handling words.
4185982Seric **
4195982Seric **	Returns:
4205982Seric **		none.
4215982Seric **
4225982Seric **	Side Effects:
4235982Seric **		Sets flags as indicated by p.
4245982Seric */
4255982Seric 
4265982Seric struct handling
4275982Seric {
4285982Seric 	char	*han_name;		/* word to get this magic */
4295982Seric 	int	han_what;		/* what to do, see below */
4305982Seric };
4315982Seric 
4325982Seric /* modes for han_what */
4335982Seric # define	HAN_NONE	0	/* nothing special */
4345982Seric # define	HAN_RRECEIPT	1	/* give return receipt */
4355982Seric 
4365982Seric struct handling	Handling[] =
4375982Seric {
4385982Seric 	"return-receipt-requested",	HAN_RRECEIPT,
4395982Seric 	NULL,				HAN_NONE
4405982Seric };
4415982Seric 
4425982Seric spechandling(p)
4435982Seric 	register char *p;
4445982Seric {
4455982Seric 	register char *w;
4465982Seric 	register struct handling *h;
4475982Seric 	extern bool sameword();
4485982Seric 
4495982Seric 	while (*p != '\0')
4505982Seric 	{
4515982Seric 		/* collect a word to compare to */
4525982Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
4535982Seric 			p++;
4545982Seric 		if (*p == '\0')
4555982Seric 			break;
4565982Seric 		w = p;
4575982Seric 		while (*p != '\0' && *p != ',' && !isspace(*p))
4585982Seric 			p++;
4595982Seric 		if (*p != '\0')
4605982Seric 			*p++ = '\0';
4615982Seric 
4625982Seric 		/* scan the special handling table */
4635982Seric 		for (h = Handling; h->han_name != NULL; h++)
4645982Seric 			if (sameword(h->han_name, w))
4655982Seric 				break;
4665982Seric 
4675982Seric 		/* see if we can do anything interesting */
4685982Seric 		switch (h->han_what)
4695982Seric 		{
4705982Seric 		  case HAN_NONE:	/* nothing to be done */
4715982Seric 			break;
4725982Seric 
4735982Seric 		  case HAN_RRECEIPT:	/* give return receipt */
474*6901Seric 			CurEnv->e_retreceipt = TRUE;
4755982Seric # ifdef DEBUG
4765982Seric 			if (Debug > 2)
4775982Seric 				printf(">>> Return receipt requested\n");
4785982Seric # endif DEBUG
4795982Seric 			break;
4805982Seric 
4815982Seric 		  default:
4825982Seric 			syserr("spechandling: handling %d (%s)", h->han_what, w);
4835982Seric 		}
4845982Seric 	}
4855982Seric }
486