11392Seric # include <stdio.h>
21392Seric # include <ctype.h>
31439Seric # include <errno.h>
4*3309Seric # include "sendmail.h"
51392Seric 
6*3309Seric static char	SccsId[] = "@(#)collect.c	3.7	03/20/81";
71392Seric 
81392Seric /*
92969Seric **  COLLECT -- read & parse message header & make temp file.
101392Seric **
111392Seric **	Creates a temporary file name and copies the standard
121392Seric **	input to that file.  While it is doing it, it looks for
131392Seric **	"From:" and "Sender:" fields to use as the from-person
141392Seric **	(but only if the -a flag is specified).  It prefers to
151392Seric **	to use the "Sender:" field.
161392Seric **
171392Seric **	MIT seems to like to produce "Sent-By:" fields instead
181392Seric **	of "Sender:" fields.  We used to catch this, but it turns
191392Seric **	out that the "Sent-By:" field doesn't always correspond
201392Seric **	to someone real ("___057", for instance), as required by
211392Seric **	the protocol.  So we limp by.....
221392Seric **
231392Seric **	Parameters:
241875Seric **		none
251392Seric **
261392Seric **	Returns:
271875Seric **		Name of temp file.
281392Seric **
291392Seric **	Side Effects:
301392Seric **		Temp file is created and filled.
311392Seric **
321392Seric **	Called By:
331392Seric **		main
341392Seric **
351392Seric **	Notes:
361392Seric **		This is broken off from main largely so that the
371392Seric **		temp buffer can be deallocated.
381392Seric */
391392Seric 
402900Seric char	*MsgId;			/* message-id, determined or created */
411624Seric long	MsgSize;		/* size of message in bytes */
422900Seric char	*Date;			/* UNIX-style origination date */
431397Seric 
441392Seric char *
452969Seric collect()
461392Seric {
471392Seric 	register FILE *tf;
481392Seric 	char buf[MAXFIELD+1];
491392Seric 	register char *p;
501392Seric 	char c;
511439Seric 	extern int errno;
522900Seric 	register HDR *h;
532900Seric 	HDR **hp;
542900Seric 	extern bool isheader();
552900Seric 	extern char *newstr();
562900Seric 	extern char *xalloc();
572900Seric 	char *fname;
582900Seric 	char *fvalue;
592900Seric 	extern char *index(), *rindex();
602900Seric 	char *xfrom;
612900Seric 	extern char *hvalue();
622900Seric 	struct hdrinfo *hi;
632988Seric 	extern char *strcpy(), *strcat(), *mktemp();
641392Seric 
651392Seric 	/*
661392Seric 	**  Create the temp file name and create the file.
671392Seric 	*/
681392Seric 
691392Seric 	mktemp(InFileName);
701392Seric 	close(creat(InFileName, 0600));
711392Seric 	if ((tf = fopen(InFileName, "w")) == NULL)
721392Seric 	{
731392Seric 		syserr("Cannot create %s", InFileName);
741392Seric 		return (NULL);
751392Seric 	}
761392Seric 
772900Seric 	/* try to read a UNIX-style From line */
782900Seric 	if (fgets(buf, sizeof buf, stdin) == NULL)
792900Seric 		return (NULL);
802900Seric 	if (strncmp(buf, "From ", 5) == 0)
812900Seric 	{
822900Seric 		eatfrom(buf);
832900Seric 		fgets(buf, sizeof buf, stdin);
842900Seric 	}
852900Seric 
861392Seric 	/*
871392Seric 	**  Copy stdin to temp file & do message editting.
881392Seric 	**	To keep certain mailers from getting confused,
891392Seric 	**	and to keep the output clean, lines that look
901392Seric 	**	like UNIX "From" lines are deleted in the header,
911392Seric 	**	and prepended with ">" in the body.
921392Seric 	*/
931392Seric 
942900Seric 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin))
951392Seric 	{
962900Seric 		/* see if the header is over */
972900Seric 		if (!isheader(buf))
982900Seric 			break;
992900Seric 
1002900Seric 		/* get the rest of this field */
1012900Seric 		while ((c = getc(stdin)) == ' ' || c == '\t')
1021392Seric 		{
1032900Seric 			p = &buf[strlen(buf)];
1042900Seric 			*p++ = c;
1052900Seric 			if (fgets(p, sizeof buf - (p - buf), stdin) == NULL)
1062900Seric 				break;
1071392Seric 		}
1082900Seric 		if (c != EOF)
1092900Seric 			ungetc(c, stdin);
1101392Seric 
1112900Seric 		MsgSize += strlen(buf);
1121392Seric 
1132900Seric 		/*
1142900Seric 		**  Snarf header away.
1152900Seric 		*/
1162900Seric 
1172900Seric 		/* strip off trailing newline */
1182900Seric 		p = rindex(buf, '\n');
1192900Seric 		if (p != NULL)
1202900Seric 			*p = '\0';
1212900Seric 
1222900Seric 		/* find canonical name */
1232900Seric 		fname = buf;
1242900Seric 		p = index(buf, ':');
1252900Seric 		fvalue = &p[1];
1262900Seric 		while (isspace(*--p))
1272900Seric 			continue;
1282900Seric 		*++p = '\0';
1292900Seric 		makelower(fname);
1302900Seric 
1312900Seric 		/* strip field value on front */
1322900Seric 		if (*fvalue == ' ')
1332900Seric 			fvalue++;
1342900Seric 
1352900Seric 		/* search header list for this header */
1362900Seric 		for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link)
1371392Seric 		{
1382969Seric 			if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags))
1392900Seric 				break;
1402900Seric 		}
1413058Seric 
1423058Seric 		/* see if it is a known type */
1433058Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1443058Seric 		{
1453058Seric 			if (strcmp(hi->hi_field, fname) == 0)
1463058Seric 				break;
1473058Seric 		}
1483058Seric 
1493058Seric 		/* if this means "end of header" quit now */
1503058Seric 		if (bitset(H_EOH, hi->hi_flags))
1513058Seric 			break;
1523058Seric 
1533058Seric 		/* create/fill in a new node */
1542900Seric 		if (h == NULL)
1552900Seric 		{
1562900Seric 			/* create a new node */
1572900Seric 			*hp = h = (HDR *) xalloc(sizeof *h);
1582900Seric 			h->h_field = newstr(fname);
1592900Seric 			h->h_value = NULL;
1602900Seric 			h->h_link = NULL;
1613058Seric 			h->h_flags = hi->hi_flags;
1622900Seric 		}
1633058Seric 		if (h->h_value != NULL)
1642900Seric 			free(h->h_value);
1653058Seric 		h->h_value = newstr(fvalue);
1661392Seric 
1673058Seric 		/* save the location of this field */
1683058Seric 		if (hi->hi_pptr != NULL)
1693058Seric 			*hi->hi_pptr = h->h_value;
1702900Seric 	}
1711392Seric 
1722900Seric # ifdef DEBUG
1732900Seric 	if (Debug)
1742900Seric 		printf("EOH\n");
1752900Seric # endif DEBUG
1762900Seric 
1772900Seric 	/* throw away a blank line */
1782900Seric 	if (buf[0] == '\n')
1792900Seric 		fgets(buf, sizeof buf, stdin);
1802900Seric 
1812900Seric 	/*
1822900Seric 	**  Collect the body of the message.
1832900Seric 	*/
1842900Seric 
1852900Seric 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL)
1862900Seric 	{
1872900Seric 		/* check for end-of-message */
1882900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1892900Seric 			break;
1902900Seric 
1912900Seric 		/* Hide UNIX-like From lines */
1922900Seric 		if (strncmp(buf, "From ", 5) == 0)
1931392Seric 		{
1942900Seric 			fputs(">", tf);
1952900Seric 			MsgSize++;
1961392Seric 		}
1971624Seric 		MsgSize += strlen(buf);
1981392Seric 		fputs(buf, tf);
1991392Seric 		if (ferror(tf))
2001392Seric 		{
2011439Seric 			if (errno == ENOSPC)
2021439Seric 			{
2031439Seric 				freopen(InFileName, "w", tf);
2041439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
2051439Seric 				syserr("Out of disk space for temp file");
2061439Seric 			}
2071439Seric 			else
2081439Seric 				syserr("Cannot write %s", InFileName);
2091439Seric 			freopen("/dev/null", "w", tf);
2101392Seric 		}
2111392Seric 	}
2121392Seric 	fclose(tf);
2132900Seric 
2142900Seric 	/*
2152900Seric 	**  Find out some information from the headers.
2163058Seric 	**	Examples are who is the from person & the date.  Some
2173058Seric 	**	fields, e.g., Message-Id, may have been handled by
2183058Seric 	**	the hi_pptr mechanism.
2192900Seric 	*/
2202900Seric 
2212900Seric 	/* from person */
2222900Seric 	xfrom = hvalue("sender");
2232900Seric 	if (xfrom == NULL)
2242900Seric 		xfrom = hvalue("from");
2252900Seric 
2262900Seric 	/* date message originated */
2272900Seric 	/* we don't seem to have a good way to do canonical conversion ....
2282900Seric 	p = hvalue("date");
2292900Seric 	if (p != NULL)
2302900Seric 		Date = newstr(arpatounix(p));
2312900Seric 	.... so we will ignore the problem for the time being */
2322900Seric 	if (Date == NULL)
2332900Seric 	{
2342900Seric 		extern char *ctime();
2352900Seric 
2363187Seric 		Date = newstr(ctime(&CurTime));
2372900Seric 	}
2382900Seric 
2391392Seric 	if (freopen(InFileName, "r", stdin) == NULL)
2401392Seric 		syserr("Cannot reopen %s", InFileName);
2412900Seric 
2422900Seric # ifdef DEBUG
2432900Seric 	if (Debug)
2442900Seric 	{
2452900Seric 		printf("----- collected header -----\n");
2462900Seric 		for (h = Header; h != NULL; h = h->h_link)
2472900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2482900Seric 		printf("----------------------------\n");
2492900Seric 	}
2502900Seric # endif DEBUG
2512900Seric 	return (ArpaFmt ? xfrom : NULL);
2521392Seric }
2531392Seric /*
2542900Seric **  EATFROM -- chew up a UNIX style from line and process
2552900Seric **
2562900Seric **	This does indeed make some assumptions about the format
2572900Seric **	of UNIX messages.
2582900Seric **
2592900Seric **	Parameters:
2602900Seric **		fm -- the from line.
2612900Seric **
2622900Seric **	Returns:
2632900Seric **		none.
2642900Seric **
2652900Seric **	Side Effects:
2662900Seric **		extracts what information it can from the header,
2672900Seric **		such as the Date.
2682900Seric */
2692900Seric 
2702900Seric char	*MonthList[] =
2712900Seric {
2722900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2732900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2742900Seric 	NULL
2752900Seric };
2762900Seric 
2772900Seric eatfrom(fm)
2782900Seric 	char *fm;
2792900Seric {
2802900Seric 	register char *p;
2812900Seric 	register char **dt;
2822900Seric 
2832900Seric 	/* find the date part */
2842900Seric 	p = fm;
2852900Seric 	while (*p != '\0')
2862900Seric 	{
2872900Seric 		/* skip a word */
2882900Seric 		while (*p != '\0' && *p != ' ')
2892900Seric 			*p++;
2902900Seric 		while (*p == ' ')
2912900Seric 			*p++;
2922900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
2932900Seric 			continue;
2942900Seric 
2952900Seric 		/* we have a possible date */
2962900Seric 		for (dt = MonthList; *dt != NULL; dt++)
2972900Seric 			if (strncmp(*dt, p, 3) == 0)
2982900Seric 				break;
2992900Seric 
3002900Seric 		if (*dt != NULL)
3012900Seric 			break;
3022900Seric 	}
3032900Seric 
3042900Seric 	if (*p != NULL)
3052900Seric 	{
3062900Seric 		/* we have found a date */
3072900Seric 		Date = xalloc(25);
3082900Seric 		strncpy(Date, p, 25);
3092900Seric 		Date[24] = '\0';
3102900Seric 	}
3112900Seric }
3122900Seric /*
3132900Seric **  HVALUE -- return value of a header.
3142900Seric **
3152900Seric **	Parameters:
3162900Seric **		field -- the field name.
3172900Seric **
3182900Seric **	Returns:
3192900Seric **		pointer to the value part.
3202900Seric **		NULL if not found.
3212900Seric **
3222900Seric **	Side Effects:
3232900Seric **		sets the H_USED bit in the header if found.
3242900Seric */
3252900Seric 
3262900Seric char *
3272900Seric hvalue(field)
3282900Seric 	char *field;
3292900Seric {
3302900Seric 	register HDR *h;
3312900Seric 
3322900Seric 	for (h = Header; h != NULL; h = h->h_link)
3332900Seric 	{
3342900Seric 		if (strcmp(h->h_field, field) == 0)
3352900Seric 		{
3362900Seric 			h->h_flags |= H_USED;
3372900Seric 			return (h->h_value);
3382900Seric 		}
3392900Seric 	}
3402900Seric 	return (NULL);
3412900Seric }
3422900Seric /*
3431392Seric **  MAKEMSGID -- Compute a message id for this process.
3441392Seric **
3451392Seric **	This routine creates a message id for a message if
3461392Seric **	it did not have one already.  If the MESSAGEID compile
3471392Seric **	flag is set, the messageid will be added to any message
3481392Seric **	that does not already have one.  Currently it is more
3491392Seric **	of an artifact, but I suggest that if you are hacking,
3501392Seric **	you leave it in -- I may want to use it someday if
3511392Seric **	duplicate messages turn out to be a problem.
3521392Seric **
3531392Seric **	Parameters:
3541392Seric **		none.
3551392Seric **
3561392Seric **	Returns:
3572900Seric **		a message id.
3581392Seric **
3591392Seric **	Side Effects:
3602900Seric **		none.
3611392Seric */
3621392Seric 
3632900Seric char *
3641392Seric makemsgid()
3651392Seric {
3662900Seric 	static char buf[50];
3673187Seric 	extern char *expand();
3681392Seric 
3693187Seric 	expand("<$m>", buf, &buf[sizeof buf - 1]);
3702900Seric 	return (buf);
3711392Seric }
3722900Seric /*
3732900Seric **  ISHEADER -- predicate telling if argument is a header.
3742900Seric **
3752900Seric **	Parameters:
3762900Seric **		s -- string to check for possible headerness.
3772900Seric **
3782900Seric **	Returns:
3792900Seric **		TRUE if s is a header.
3802900Seric **		FALSE otherwise.
3812900Seric **
3822900Seric **	Side Effects:
3832900Seric **		none.
3842900Seric */
3852900Seric 
3862900Seric bool
3872900Seric isheader(s)
3882900Seric 	register char *s;
3892900Seric {
3902900Seric 	if (!isalnum(*s))
3912900Seric 		return (FALSE);
3922900Seric 	while (!isspace(*s) && *s != ':')
3932900Seric 		s++;
3942900Seric 	while (isspace(*s))
3952900Seric 		s++;
3962900Seric 	return (*s == ':');
3972900Seric }
398