122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*57232Seric static char sccsid[] = "@(#)headers.c	5.30 (Berkeley) 12/20/92";
1133729Sbostic #endif /* not lint */
1222706Sdist 
134091Seric # include <errno.h>
144091Seric # include "sendmail.h"
154091Seric 
164091Seric /*
174091Seric **  CHOMPHEADER -- process and save a header line.
184091Seric **
194091Seric **	Called by collect and by readcf to deal with header lines.
204091Seric **
214091Seric **	Parameters:
224091Seric **		line -- header as a text line.
234091Seric **		def -- if set, this is a default value.
2455012Seric **		e -- the envelope including this header.
254091Seric **
264091Seric **	Returns:
274091Seric **		flags for this header.
284091Seric **
294091Seric **	Side Effects:
304091Seric **		The header is saved on the header list.
314319Seric **		Contents of 'line' are destroyed.
324091Seric */
334091Seric 
3455012Seric chompheader(line, def, e)
354091Seric 	char *line;
364091Seric 	bool def;
3755012Seric 	register ENVELOPE *e;
384091Seric {
394091Seric 	register char *p;
404091Seric 	register HDR *h;
414091Seric 	HDR **hp;
424091Seric 	char *fname;
434091Seric 	char *fvalue;
444091Seric 	struct hdrinfo *hi;
459059Seric 	bool cond = FALSE;
4610689Seric 	BITMAP mopts;
474091Seric 
487677Seric 	if (tTd(31, 6))
497677Seric 		printf("chompheader: %s\n", line);
507677Seric 
514627Seric 	/* strip off options */
5210689Seric 	clrbitmap(mopts);
534627Seric 	p = line;
5456678Seric 	if (def && *p == '?')
554627Seric 	{
564627Seric 		/* have some */
5756795Seric 		register char *q = strchr(p + 1, *p);
584627Seric 
594627Seric 		if (q != NULL)
604627Seric 		{
614627Seric 			*q++ = '\0';
6210689Seric 			while (*++p != '\0')
6310689Seric 				setbitn(*p, mopts);
644627Seric 			p = q;
654627Seric 		}
664627Seric 		else
6736233Skarels 			usrerr("chompheader: syntax error, line \"%s\"", line);
689059Seric 		cond = TRUE;
694627Seric 	}
704627Seric 
714091Seric 	/* find canonical name */
724627Seric 	fname = p;
7356795Seric 	p = strchr(p, ':');
7410118Seric 	if (p == NULL)
7510118Seric 	{
7610118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
7710118Seric 		return (0);
7810118Seric 	}
794091Seric 	fvalue = &p[1];
804091Seric 	while (isspace(*--p))
814091Seric 		continue;
824091Seric 	*++p = '\0';
834091Seric 	makelower(fname);
844091Seric 
854091Seric 	/* strip field value on front */
864091Seric 	if (*fvalue == ' ')
874091Seric 		fvalue++;
884091Seric 
894091Seric 	/* see if it is a known type */
904091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
914091Seric 	{
924091Seric 		if (strcmp(hi->hi_field, fname) == 0)
934091Seric 			break;
944091Seric 	}
954091Seric 
9611414Seric 	/* see if this is a resent message */
9711930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9855012Seric 		e->e_flags |= EF_RESENT;
9911414Seric 
1004091Seric 	/* if this means "end of header" quit now */
1014091Seric 	if (bitset(H_EOH, hi->hi_flags))
1024091Seric 		return (hi->hi_flags);
1034091Seric 
10411414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10514784Seric 	p = "resent-from";
10655012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10714784Seric 		p += 7;
10814784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
10911414Seric 	{
11055012Seric 		if (e->e_from.q_paddr != NULL &&
11155012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11211414Seric 			return (hi->hi_flags);
11311414Seric 	}
11411414Seric 
11514784Seric 	/* delete default value for this header */
11655012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11714784Seric 	{
11814784Seric 		if (strcmp(fname, h->h_field) == 0 &&
11914784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12014784Seric 		    !bitset(H_FORCE, h->h_flags))
12114784Seric 			h->h_value = NULL;
12214784Seric 	}
12314784Seric 
12413012Seric 	/* create a new node */
12513012Seric 	h = (HDR *) xalloc(sizeof *h);
12613012Seric 	h->h_field = newstr(fname);
12713012Seric 	h->h_value = NULL;
12813012Seric 	h->h_link = NULL;
12923117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13013012Seric 	*hp = h;
1318066Seric 	h->h_flags = hi->hi_flags;
1324091Seric 	if (def)
1334091Seric 		h->h_flags |= H_DEFAULT;
1349059Seric 	if (cond)
1359059Seric 		h->h_flags |= H_CHECK;
1364091Seric 	if (h->h_value != NULL)
1379351Seric 		free((char *) h->h_value);
1388066Seric 	h->h_value = newstr(fvalue);
1394091Seric 
1405937Seric 	/* hack to see if this is a new format message */
1418095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14256795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14356795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1448089Seric 	{
14555012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1468089Seric 	}
1475937Seric 
1484091Seric 	return (h->h_flags);
1494091Seric }
1504091Seric /*
1516980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1526980Seric **
1536980Seric **	This bypasses the special checking of chompheader.
1546980Seric **
1556980Seric **	Parameters:
1566980Seric **		field -- the name of the header field.
1576980Seric **		value -- the value of the field.  It must be lower-cased.
1586980Seric **		e -- the envelope to add them to.
1596980Seric **
1606980Seric **	Returns:
1616980Seric **		none.
1626980Seric **
1636980Seric **	Side Effects:
1646980Seric **		adds the field on the list of headers for this envelope.
1656980Seric */
1666980Seric 
1676980Seric addheader(field, value, e)
1686980Seric 	char *field;
1696980Seric 	char *value;
1706980Seric 	ENVELOPE *e;
1716980Seric {
1726980Seric 	register HDR *h;
1736980Seric 	register struct hdrinfo *hi;
1746980Seric 	HDR **hp;
1756980Seric 
1766980Seric 	/* find info struct */
1776980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1786980Seric 	{
1796980Seric 		if (strcmp(field, hi->hi_field) == 0)
1806980Seric 			break;
1816980Seric 	}
1826980Seric 
1836980Seric 	/* find current place in list -- keep back pointer? */
1846980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1856980Seric 	{
1866980Seric 		if (strcmp(field, h->h_field) == 0)
1876980Seric 			break;
1886980Seric 	}
1896980Seric 
1906980Seric 	/* allocate space for new header */
1916980Seric 	h = (HDR *) xalloc(sizeof *h);
1926980Seric 	h->h_field = field;
1936980Seric 	h->h_value = newstr(value);
1947368Seric 	h->h_link = *hp;
1956980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19610689Seric 	clrbitmap(h->h_mflags);
1976980Seric 	*hp = h;
1986980Seric }
1996980Seric /*
2004091Seric **  HVALUE -- return value of a header.
2014091Seric **
2024091Seric **	Only "real" fields (i.e., ones that have not been supplied
2034091Seric **	as a default) are used.
2044091Seric **
2054091Seric **	Parameters:
2064091Seric **		field -- the field name.
20755012Seric **		e -- the envelope containing the header.
2084091Seric **
2094091Seric **	Returns:
2104091Seric **		pointer to the value part.
2114091Seric **		NULL if not found.
2124091Seric **
2134091Seric **	Side Effects:
2149382Seric **		none.
2154091Seric */
2164091Seric 
2174091Seric char *
21855012Seric hvalue(field, e)
2194091Seric 	char *field;
22055012Seric 	register ENVELOPE *e;
2214091Seric {
2224091Seric 	register HDR *h;
2234091Seric 
22455012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2254091Seric 	{
2264091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2274091Seric 			return (h->h_value);
2284091Seric 	}
2294091Seric 	return (NULL);
2304091Seric }
2314091Seric /*
2324091Seric **  ISHEADER -- predicate telling if argument is a header.
2334091Seric **
2344319Seric **	A line is a header if it has a single word followed by
2354319Seric **	optional white space followed by a colon.
2364319Seric **
2374091Seric **	Parameters:
2384091Seric **		s -- string to check for possible headerness.
2394091Seric **
2404091Seric **	Returns:
2414091Seric **		TRUE if s is a header.
2424091Seric **		FALSE otherwise.
2434091Seric **
2444091Seric **	Side Effects:
2454091Seric **		none.
2464091Seric */
2474091Seric 
2484091Seric bool
2494091Seric isheader(s)
2504091Seric 	register char *s;
2514091Seric {
2529382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2534091Seric 		s++;
2549382Seric 
2559382Seric 	/* following technically violates RFC822 */
2564091Seric 	while (isspace(*s))
2574091Seric 		s++;
2589382Seric 
2594091Seric 	return (*s == ':');
2604091Seric }
2615919Seric /*
2627783Seric **  EATHEADER -- run through the stored header and extract info.
2637783Seric **
2647783Seric **	Parameters:
2659382Seric **		e -- the envelope to process.
2667783Seric **
2677783Seric **	Returns:
2687783Seric **		none.
2697783Seric **
2707783Seric **	Side Effects:
2717783Seric **		Sets a bunch of global variables from information
2729382Seric **			in the collected header.
2739382Seric **		Aborts the message if the hop count is exceeded.
2747783Seric */
2757783Seric 
2769382Seric eatheader(e)
2779382Seric 	register ENVELOPE *e;
2787783Seric {
2797783Seric 	register HDR *h;
2807783Seric 	register char *p;
2819382Seric 	int hopcnt = 0;
28257208Seric 	char *msgid;
28357208Seric 	char msgidbuf[MAXNAME];
2847783Seric 
2859382Seric 	if (tTd(32, 1))
2869382Seric 		printf("----- collected header -----\n");
28757208Seric 	msgid = "<none>";
2889382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2897783Seric 	{
2907783Seric 		extern char *capitalize();
2917783Seric 
2929382Seric 		if (tTd(32, 1))
2937783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
29411414Seric 		/* count the number of times it has been processed */
2959382Seric 		if (bitset(H_TRACE, h->h_flags))
2969382Seric 			hopcnt++;
29711414Seric 
29811414Seric 		/* send to this person if we so desire */
29911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30155012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
30211414Seric 		{
30355012Seric 			sendtolist(h->h_value, (ADDRESS *) NULL,
30455012Seric 				   &e->e_sendqueue, e);
30511414Seric 		}
30611414Seric 
30757208Seric 		/* save the message-id for logging */
30857208Seric 		if (!QueueRun && h->h_value != NULL &&
30911299Seric 		    strcmp(h->h_field, "message-id") == 0)
31011290Seric 		{
31157208Seric 			msgid = h->h_value;
31211290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31311290Seric 			{
31457208Seric 				expand(msgid, msgidbuf,
31557208Seric 					&msgidbuf[sizeof msgidbuf], e);
31657208Seric 				msgid = msgidbuf;
31711290Seric 			}
31811290Seric 		}
3199382Seric 	}
3209382Seric 	if (tTd(32, 1))
3217783Seric 		printf("----------------------------\n");
3227783Seric 
3239382Seric 	/* store hop count */
3249382Seric 	if (hopcnt > e->e_hopcount)
3259382Seric 		e->e_hopcount = hopcnt;
3269382Seric 
3277783Seric 	/* message priority */
32855012Seric 	p = hvalue("precedence", e);
3299382Seric 	if (p != NULL)
3309382Seric 		e->e_class = priencode(p);
3317783Seric 	if (!QueueRun)
33225013Seric 		e->e_msgpriority = e->e_msgsize
33324981Seric 				 - e->e_class * WkClassFact
33424981Seric 				 + e->e_nrcpts * WkRecipFact;
3357783Seric 
3368066Seric 	/* return receipt to */
33755012Seric 	p = hvalue("return-receipt-to", e);
3387783Seric 	if (p != NULL)
3399382Seric 		e->e_receiptto = p;
3407783Seric 
3418253Seric 	/* errors to */
34255012Seric 	p = hvalue("errors-to", e);
3438253Seric 	if (p != NULL)
34455012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue, e);
3458253Seric 
3467783Seric 	/* full name of from person */
34755012Seric 	p = hvalue("full-name", e);
3487783Seric 	if (p != NULL)
3499382Seric 		define('x', p, e);
3507783Seric 
3517783Seric 	/* date message originated */
35255012Seric 	p = hvalue("posted-date", e);
3537783Seric 	if (p == NULL)
35455012Seric 		p = hvalue("date", e);
3557783Seric 	if (p != NULL)
3567783Seric 	{
3579382Seric 		define('a', p, e);
3587783Seric 		/* we don't have a good way to do canonical conversion ....
3599382Seric 		define('d', newstr(arpatounix(p)), e);
3607783Seric 		.... so we will ignore the problem for the time being */
3617783Seric 	}
36211290Seric 
36311290Seric 	/*
36411290Seric 	**  Log collection information.
36511290Seric 	*/
36611290Seric 
36711290Seric # ifdef LOG
36811299Seric 	if (!QueueRun && LogLevel > 1)
36911290Seric 	{
370*57232Seric 		char hbuf[MAXNAME];
37155019Seric 		char *name = hbuf;
37255019Seric 		extern char *inet_ntoa();
37336230Skarels 
37436230Skarels 		if (RealHostName == NULL)
37536230Skarels 			name = "local";
37636230Skarels 		else if (RealHostName[0] == '[')
37736230Skarels 			name = RealHostName;
37836230Skarels 		else
37954999Seric 			(void)sprintf(hbuf, "%.80s (%s)",
38036230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
38136230Skarels 		syslog(LOG_INFO,
38257208Seric 		    "%s: from=%s, size=%ld, class=%d, msgid=%s, received from %s\n",
38355012Seric 		    e->e_id, e->e_from.q_paddr, e->e_msgsize,
38457208Seric 		    e->e_class, msgid, name);
38511290Seric 	}
38656795Seric # endif /* LOG */
3877783Seric }
3887783Seric /*
3897783Seric **  PRIENCODE -- encode external priority names into internal values.
3907783Seric **
3917783Seric **	Parameters:
3927783Seric **		p -- priority in ascii.
3937783Seric **
3947783Seric **	Returns:
3957783Seric **		priority as a numeric level.
3967783Seric **
3977783Seric **	Side Effects:
3987783Seric **		none.
3997783Seric */
4007783Seric 
4017783Seric priencode(p)
4027783Seric 	char *p;
4037783Seric {
4048253Seric 	register int i;
4057783Seric 
4068253Seric 	for (i = 0; i < NumPriorities; i++)
4077783Seric 	{
40833725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4098253Seric 			return (Priorities[i].pri_val);
4107783Seric 	}
4118253Seric 
4128253Seric 	/* unknown priority */
4138253Seric 	return (0);
4147783Seric }
4157783Seric /*
4167890Seric **  CRACKADDR -- parse an address and turn it into a macro
4177783Seric **
4187783Seric **	This doesn't actually parse the address -- it just extracts
4197783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4207783Seric **	and isn't even guaranteed to leave something syntactically
4217783Seric **	identical to what it started with.  However, it does leave
4227783Seric **	something semantically identical.
4237783Seric **
42451379Seric **	This algorithm has been cleaned up to handle a wider range
42551379Seric **	of cases -- notably quoted and backslash escaped strings.
42651379Seric **	This modification makes it substantially better at preserving
42751379Seric **	the original syntax.
4287783Seric **
4297783Seric **	Parameters:
4307890Seric **		addr -- the address to be cracked.
4317783Seric **
4327783Seric **	Returns:
4337783Seric **		a pointer to the new version.
4347783Seric **
4357783Seric **	Side Effects:
4367890Seric **		none.
4377783Seric **
4387783Seric **	Warning:
4397783Seric **		The return value is saved in local storage and should
4407783Seric **		be copied if it is to be reused.
4417783Seric */
4427783Seric 
4437783Seric char *
4447890Seric crackaddr(addr)
4457890Seric 	register char *addr;
4467783Seric {
4477783Seric 	register char *p;
44851379Seric 	register char c;
44951379Seric 	int cmtlev;
45056764Seric 	int realcmtlev;
45156764Seric 	int anglelev, realanglelev;
45251379Seric 	int copylev;
45351379Seric 	bool qmode;
45456764Seric 	bool realqmode;
45556764Seric 	bool skipping;
45651379Seric 	bool putgmac = FALSE;
45756735Seric 	bool quoteit = FALSE;
45851379Seric 	register char *bp;
45956764Seric 	char *buflim;
4607783Seric 	static char buf[MAXNAME];
4617783Seric 
4627783Seric 	if (tTd(33, 1))
4637890Seric 		printf("crackaddr(%s)\n", addr);
4647783Seric 
4658082Seric 	/* strip leading spaces */
4668082Seric 	while (*addr != '\0' && isspace(*addr))
4678082Seric 		addr++;
4688082Seric 
4697783Seric 	/*
47051379Seric 	**  Start by assuming we have no angle brackets.  This will be
47151379Seric 	**  adjusted later if we find them.
4727783Seric 	*/
4737783Seric 
47451379Seric 	bp = buf;
47556764Seric 	buflim = &buf[sizeof buf - 5];
47651379Seric 	p = addr;
47756764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
47856764Seric 	qmode = realqmode = FALSE;
47951379Seric 
48051379Seric 	while ((c = *p++) != '\0')
4817783Seric 	{
48256764Seric 		/*
48356764Seric 		**  If the buffer is overful, go into a special "skipping"
48456764Seric 		**  mode that tries to keep legal syntax but doesn't actually
48556764Seric 		**  output things.
48656764Seric 		*/
4877783Seric 
48856764Seric 		skipping = bp >= buflim;
48956735Seric 
49056764Seric 		if (copylev > 0 && !skipping)
49156764Seric 			*bp++ = c;
49256735Seric 
49351379Seric 		/* check for backslash escapes */
49451379Seric 		if (c == '\\')
4957783Seric 		{
49651379Seric 			if ((c = *p++) == '\0')
4977783Seric 			{
49851379Seric 				/* too far */
49951379Seric 				p--;
50051379Seric 				goto putg;
5017783Seric 			}
50256764Seric 			if (copylev > 0 && !skipping)
50351379Seric 				*bp++ = c;
50451379Seric 			goto putg;
5057783Seric 		}
5067783Seric 
50751379Seric 		/* check for quoted strings */
50851379Seric 		if (c == '"')
5097783Seric 		{
51051379Seric 			qmode = !qmode;
51156764Seric 			if (copylev > 0 && !skipping)
51256764Seric 				realqmode = !realqmode;
51351379Seric 			continue;
5147783Seric 		}
51551379Seric 		if (qmode)
51651379Seric 			goto putg;
5177783Seric 
51851379Seric 		/* check for comments */
51951379Seric 		if (c == '(')
5207783Seric 		{
52151379Seric 			cmtlev++;
52256764Seric 
52356764Seric 			/* allow space for closing paren */
52456764Seric 			if (!skipping)
52556764Seric 			{
52656764Seric 				buflim--;
52756764Seric 				realcmtlev++;
52856764Seric 				if (copylev++ <= 0)
52956764Seric 				{
53056764Seric 					*bp++ = ' ';
53156764Seric 					*bp++ = c;
53256764Seric 				}
53356764Seric 			}
53451379Seric 		}
53551379Seric 		if (cmtlev > 0)
53651379Seric 		{
53751379Seric 			if (c == ')')
5387783Seric 			{
53951379Seric 				cmtlev--;
54051379Seric 				copylev--;
54156764Seric 				if (!skipping)
54256764Seric 				{
54356764Seric 					realcmtlev--;
54456764Seric 					buflim++;
54556764Seric 				}
5467783Seric 			}
5477783Seric 			continue;
5487783Seric 		}
54956764Seric 		else if (c == ')')
55056764Seric 		{
55156764Seric 			/* syntax error: unmatched ) */
55256764Seric 			if (!skipping)
55356764Seric 				bp--;
55456764Seric 		}
5557783Seric 
55656764Seric 
55756764Seric 		/* check for characters that may have to be quoted */
55857175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
55956764Seric 		{
56056764Seric 			/*
56156764Seric 			**  If these occur as the phrase part of a <>
56256764Seric 			**  construct, but are not inside of () or already
56356764Seric 			**  quoted, they will have to be quoted.  Note that
56456764Seric 			**  now (but don't actually do the quoting).
56556764Seric 			*/
56656764Seric 
56756764Seric 			if (cmtlev <= 0 && !qmode)
56856764Seric 				quoteit = TRUE;
56956764Seric 		}
57056764Seric 
57151379Seric 		/* check for angle brackets */
57251379Seric 		if (c == '<')
57351379Seric 		{
57456735Seric 			register char *q;
57556735Seric 
57651379Seric 			/* oops -- have to change our mind */
57756764Seric 			anglelev++;
57856764Seric 			if (!skipping)
57956764Seric 				realanglelev++;
58056764Seric 
58156735Seric 			bp = buf;
58256735Seric 			if (quoteit)
58356735Seric 			{
58456735Seric 				*bp++ = '"';
58556735Seric 
58656735Seric 				/* back up over the '<' and any spaces */
58756735Seric 				--p;
58856735Seric 				while (isspace(*--p))
58956735Seric 					continue;
59056735Seric 				p++;
59156735Seric 			}
59256735Seric 			for (q = addr; q < p; )
59356735Seric 			{
59456735Seric 				c = *q++;
59556764Seric 				if (bp < buflim)
59656764Seric 				{
59756764Seric 					if (quoteit && c == '"')
59856764Seric 						*bp++ = '\\';
59956764Seric 					*bp++ = c;
60056764Seric 				}
60156735Seric 			}
60256735Seric 			if (quoteit)
60356735Seric 			{
60456735Seric 				*bp++ = '"';
60556764Seric 				while ((c = *p++) != '<')
60656764Seric 				{
60756764Seric 					if (bp < buflim)
60856764Seric 						*bp++ = c;
60956764Seric 				}
61056764Seric 				*bp++ = c;
61156735Seric 			}
61251379Seric 			copylev = 0;
61356735Seric 			putgmac = quoteit = FALSE;
61451379Seric 			continue;
61551379Seric 		}
6167783Seric 
61751379Seric 		if (c == '>')
6187783Seric 		{
61956764Seric 			if (anglelev > 0)
62056764Seric 			{
62156764Seric 				anglelev--;
62256764Seric 				if (!skipping)
62356764Seric 				{
62456764Seric 					realanglelev--;
62556764Seric 					buflim++;
62656764Seric 				}
62756764Seric 			}
62856764Seric 			else if (!skipping)
62956764Seric 			{
63056764Seric 				/* syntax error: unmatched > */
63156764Seric 				if (copylev > 0)
63256764Seric 					bp--;
63356764Seric 				continue;
63456764Seric 			}
63551379Seric 			if (copylev++ <= 0)
63651379Seric 				*bp++ = c;
63751379Seric 			continue;
6387783Seric 		}
63951379Seric 
64051379Seric 		/* must be a real address character */
64151379Seric 	putg:
64251379Seric 		if (copylev <= 0 && !putgmac)
64351379Seric 		{
64451379Seric 			*bp++ = '\001';
64551379Seric 			*bp++ = 'g';
64651379Seric 			putgmac = TRUE;
64751379Seric 		}
6487783Seric 	}
6497783Seric 
65056764Seric 	/* repair any syntactic damage */
65156764Seric 	if (realqmode)
65256764Seric 		*bp++ = '"';
65356764Seric 	while (realcmtlev-- > 0)
65456764Seric 		*bp++ = ')';
65556764Seric 	while (realanglelev-- > 0)
65656764Seric 		*bp++ = '>';
65751379Seric 	*bp++ = '\0';
6587783Seric 
6597783Seric 	if (tTd(33, 1))
6607944Seric 		printf("crackaddr=>`%s'\n", buf);
6617783Seric 
6627783Seric 	return (buf);
6637783Seric }
6649382Seric /*
6659382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6669382Seric **
6679382Seric **	Parameters:
6689382Seric **		fp -- file to put it on.
6699382Seric **		m -- mailer to use.
6709382Seric **		e -- envelope to use.
6719382Seric **
6729382Seric **	Returns:
6739382Seric **		none.
6749382Seric **
6759382Seric **	Side Effects:
6769382Seric **		none.
6779382Seric */
6789382Seric 
67910176Seric putheader(fp, m, e)
6809382Seric 	register FILE *fp;
6819382Seric 	register MAILER *m;
6829382Seric 	register ENVELOPE *e;
6839382Seric {
68457135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
6859382Seric 	register HDR *h;
6869382Seric 	extern char *arpadate();
6879382Seric 	extern char *capitalize();
68857135Seric 	char obuf[MAXLINE];
6899382Seric 
6909382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6919382Seric 	{
6929382Seric 		register char *p;
69310689Seric 		extern bool bitintersect();
6949382Seric 
6959382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
69610689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6979382Seric 			continue;
6989382Seric 
69911414Seric 		/* handle Resent-... headers specially */
70011414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
70111414Seric 			continue;
70211414Seric 
7039382Seric 		p = h->h_value;
7049382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7059382Seric 		{
7069382Seric 			/* macro expand value if generated internally */
7079382Seric 			expand(p, buf, &buf[sizeof buf], e);
7089382Seric 			p = buf;
7099382Seric 			if (p == NULL || *p == '\0')
7109382Seric 				continue;
7119382Seric 		}
7129382Seric 
7139382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7149382Seric 		{
7159382Seric 			/* address field */
7169382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7179382Seric 
7189382Seric 			if (bitset(H_FROM, h->h_flags))
7199382Seric 				oldstyle = FALSE;
72055012Seric 			commaize(h, p, fp, oldstyle, m, e);
7219382Seric 		}
7229382Seric 		else
7239382Seric 		{
7249382Seric 			/* vanilla header line */
72512159Seric 			register char *nlp;
72612159Seric 
72712159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
72856795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
72912159Seric 			{
73012159Seric 				*nlp = '\0';
73112159Seric 				(void) strcat(obuf, p);
73212159Seric 				*nlp = '\n';
73312159Seric 				putline(obuf, fp, m);
73412159Seric 				p = ++nlp;
73512161Seric 				obuf[0] = '\0';
73612159Seric 			}
73712159Seric 			(void) strcat(obuf, p);
73810176Seric 			putline(obuf, fp, m);
7399382Seric 		}
7409382Seric 	}
7419382Seric }
7429382Seric /*
7439382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7449382Seric **
7459382Seric **	Parameters:
7469382Seric **		h -- the header field to output.
7479382Seric **		p -- the value to put in it.
7489382Seric **		fp -- file to put it to.
7499382Seric **		oldstyle -- TRUE if this is an old style header.
7509382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7519382Seric **			don't transform the name at all.
75255012Seric **		e -- the envelope containing the message.
7539382Seric **
7549382Seric **	Returns:
7559382Seric **		none.
7569382Seric **
7579382Seric **	Side Effects:
7589382Seric **		outputs "p" to file "fp".
7599382Seric */
7609382Seric 
76155012Seric commaize(h, p, fp, oldstyle, m, e)
7629382Seric 	register HDR *h;
7639382Seric 	register char *p;
7649382Seric 	FILE *fp;
7659382Seric 	bool oldstyle;
7669382Seric 	register MAILER *m;
76755012Seric 	register ENVELOPE *e;
7689382Seric {
7699382Seric 	register char *obp;
7709382Seric 	int opos;
7719382Seric 	bool firstone = TRUE;
77211157Seric 	char obuf[MAXLINE + 3];
7739382Seric 
7749382Seric 	/*
7759382Seric 	**  Output the address list translated by the
7769382Seric 	**  mailer and with commas.
7779382Seric 	*/
7789382Seric 
7799382Seric 	if (tTd(14, 2))
7809382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7819382Seric 
7829382Seric 	obp = obuf;
7839382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7849382Seric 	opos = strlen(h->h_field) + 2;
7859382Seric 	obp += opos;
7869382Seric 
7879382Seric 	/*
7889382Seric 	**  Run through the list of values.
7899382Seric 	*/
7909382Seric 
7919382Seric 	while (*p != '\0')
7929382Seric 	{
7939382Seric 		register char *name;
79454983Seric 		register int c;
7959382Seric 		char savechar;
7969382Seric 		extern char *remotename();
7979382Seric 		extern char *DelimChar;		/* defined in prescan */
7989382Seric 
7999382Seric 		/*
8009382Seric 		**  Find the end of the name.  New style names
8019382Seric 		**  end with a comma, old style names end with
8029382Seric 		**  a space character.  However, spaces do not
8039382Seric 		**  necessarily delimit an old-style name -- at
8049382Seric 		**  signs mean keep going.
8059382Seric 		*/
8069382Seric 
8079382Seric 		/* find end of name */
8089382Seric 		while (isspace(*p) || *p == ',')
8099382Seric 			p++;
8109382Seric 		name = p;
8119382Seric 		for (;;)
8129382Seric 		{
8139382Seric 			char *oldp;
81416909Seric 			char pvpbuf[PSBUFSIZE];
8159382Seric 			extern bool isatword();
8169382Seric 			extern char **prescan();
8179382Seric 
81816909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
8199382Seric 			p = DelimChar;
8209382Seric 
8219382Seric 			/* look to see if we have an at sign */
8229382Seric 			oldp = p;
8239382Seric 			while (*p != '\0' && isspace(*p))
8249382Seric 				p++;
8259382Seric 
8269382Seric 			if (*p != '@' && !isatword(p))
8279382Seric 			{
8289382Seric 				p = oldp;
8299382Seric 				break;
8309382Seric 			}
8319382Seric 			p += *p == '@' ? 1 : 2;
8329382Seric 			while (*p != '\0' && isspace(*p))
8339382Seric 				p++;
8349382Seric 		}
8359382Seric 		/* at the end of one complete name */
8369382Seric 
8379382Seric 		/* strip off trailing white space */
8389382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
8399382Seric 			p--;
8409382Seric 		if (++p == name)
8419382Seric 			continue;
8429382Seric 		savechar = *p;
8439382Seric 		*p = '\0';
8449382Seric 
8459382Seric 		/* translate the name to be relative */
84655012Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE, e);
8479382Seric 		if (*name == '\0')
8489382Seric 		{
8499382Seric 			*p = savechar;
8509382Seric 			continue;
8519382Seric 		}
8529382Seric 
8539382Seric 		/* output the name with nice formatting */
85454983Seric 		opos += strlen(name);
8559382Seric 		if (!firstone)
8569382Seric 			opos += 2;
8579382Seric 		if (opos > 78 && !firstone)
8589382Seric 		{
85910178Seric 			(void) strcpy(obp, ",\n");
86010176Seric 			putline(obuf, fp, m);
8619382Seric 			obp = obuf;
8629382Seric 			(void) sprintf(obp, "        ");
86310161Seric 			opos = strlen(obp);
86410161Seric 			obp += opos;
86554983Seric 			opos += strlen(name);
8669382Seric 		}
8679382Seric 		else if (!firstone)
8689382Seric 		{
8699382Seric 			(void) sprintf(obp, ", ");
8709382Seric 			obp += 2;
8719382Seric 		}
8729382Seric 
8739382Seric 		/* strip off quote bits as we output */
87454983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
8759382Seric 		{
87654983Seric 			if (bitnset(M_7BITS, m->m_flags))
87754983Seric 				c &= 0177;
87854983Seric 			*obp++ = c;
8799382Seric 		}
8809382Seric 		firstone = FALSE;
8819382Seric 		*p = savechar;
8829382Seric 	}
8839382Seric 	(void) strcpy(obp, "\n");
88410176Seric 	putline(obuf, fp, m);
8859382Seric }
8869382Seric /*
8879382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8889382Seric **
8899382Seric **	Parameters:
8909382Seric **		p -- word to check.
8919382Seric **
8929382Seric **	Returns:
8939382Seric **		TRUE -- if p is the word at.
8949382Seric **		FALSE -- otherwise.
8959382Seric **
8969382Seric **	Side Effects:
8979382Seric **		none.
8989382Seric */
8999382Seric 
9009382Seric bool
9019382Seric isatword(p)
9029382Seric 	register char *p;
9039382Seric {
9049382Seric 	extern char lower();
9059382Seric 
9069382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
9079382Seric 	    p[2] != '\0' && isspace(p[2]))
9089382Seric 		return (TRUE);
9099382Seric 	return (FALSE);
9109382Seric }
911