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*56735Seric static char sccsid[] = "@(#)headers.c	5.23 (Berkeley) 11/13/92";
1133729Sbostic #endif /* not lint */
1222706Sdist 
1340960Sbostic # include <sys/param.h>
144091Seric # include <errno.h>
154091Seric # include "sendmail.h"
164091Seric 
174091Seric /*
184091Seric **  CHOMPHEADER -- process and save a header line.
194091Seric **
204091Seric **	Called by collect and by readcf to deal with header lines.
214091Seric **
224091Seric **	Parameters:
234091Seric **		line -- header as a text line.
244091Seric **		def -- if set, this is a default value.
2555012Seric **		e -- the envelope including this header.
264091Seric **
274091Seric **	Returns:
284091Seric **		flags for this header.
294091Seric **
304091Seric **	Side Effects:
314091Seric **		The header is saved on the header list.
324319Seric **		Contents of 'line' are destroyed.
334091Seric */
344091Seric 
3555012Seric chompheader(line, def, e)
364091Seric 	char *line;
374091Seric 	bool def;
3855012Seric 	register ENVELOPE *e;
394091Seric {
404091Seric 	register char *p;
414091Seric 	register HDR *h;
424091Seric 	HDR **hp;
434091Seric 	char *fname;
444091Seric 	char *fvalue;
454091Seric 	struct hdrinfo *hi;
469059Seric 	bool cond = FALSE;
4710689Seric 	BITMAP mopts;
484091Seric 
497677Seric 	if (tTd(31, 6))
507677Seric 		printf("chompheader: %s\n", line);
517677Seric 
524627Seric 	/* strip off options */
5310689Seric 	clrbitmap(mopts);
544627Seric 	p = line;
5556678Seric 	if (def && *p == '?')
564627Seric 	{
574627Seric 		/* have some */
584627Seric 		register char *q = index(p + 1, *p);
594627Seric 
604627Seric 		if (q != NULL)
614627Seric 		{
624627Seric 			*q++ = '\0';
6310689Seric 			while (*++p != '\0')
6410689Seric 				setbitn(*p, mopts);
654627Seric 			p = q;
664627Seric 		}
674627Seric 		else
6836233Skarels 			usrerr("chompheader: syntax error, line \"%s\"", line);
699059Seric 		cond = TRUE;
704627Seric 	}
714627Seric 
724091Seric 	/* find canonical name */
734627Seric 	fname = p;
744627Seric 	p = index(p, ':');
7510118Seric 	if (p == NULL)
7610118Seric 	{
7710118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
7810118Seric 		return (0);
7910118Seric 	}
804091Seric 	fvalue = &p[1];
814091Seric 	while (isspace(*--p))
824091Seric 		continue;
834091Seric 	*++p = '\0';
844091Seric 	makelower(fname);
854091Seric 
864091Seric 	/* strip field value on front */
874091Seric 	if (*fvalue == ' ')
884091Seric 		fvalue++;
894091Seric 
904091Seric 	/* see if it is a known type */
914091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
924091Seric 	{
934091Seric 		if (strcmp(hi->hi_field, fname) == 0)
944091Seric 			break;
954091Seric 	}
964091Seric 
9711414Seric 	/* see if this is a resent message */
9811930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9955012Seric 		e->e_flags |= EF_RESENT;
10011414Seric 
1014091Seric 	/* if this means "end of header" quit now */
1024091Seric 	if (bitset(H_EOH, hi->hi_flags))
1034091Seric 		return (hi->hi_flags);
1044091Seric 
10511414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10614784Seric 	p = "resent-from";
10755012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10814784Seric 		p += 7;
10914784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
11011414Seric 	{
11155012Seric 		if (e->e_from.q_paddr != NULL &&
11255012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11311414Seric 			return (hi->hi_flags);
11411414Seric 	}
11511414Seric 
11614784Seric 	/* delete default value for this header */
11755012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11814784Seric 	{
11914784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12114784Seric 		    !bitset(H_FORCE, h->h_flags))
12214784Seric 			h->h_value = NULL;
12314784Seric 	}
12414784Seric 
12513012Seric 	/* create a new node */
12613012Seric 	h = (HDR *) xalloc(sizeof *h);
12713012Seric 	h->h_field = newstr(fname);
12813012Seric 	h->h_value = NULL;
12913012Seric 	h->h_link = NULL;
13023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13113012Seric 	*hp = h;
1328066Seric 	h->h_flags = hi->hi_flags;
1334091Seric 	if (def)
1344091Seric 		h->h_flags |= H_DEFAULT;
1359059Seric 	if (cond)
1369059Seric 		h->h_flags |= H_CHECK;
1374091Seric 	if (h->h_value != NULL)
1389351Seric 		free((char *) h->h_value);
1398066Seric 	h->h_value = newstr(fvalue);
1404091Seric 
1415937Seric 	/* hack to see if this is a new format message */
1428095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1435937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1448089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1458089Seric 	{
14655012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1478089Seric 	}
1485937Seric 
1494091Seric 	return (h->h_flags);
1504091Seric }
1514091Seric /*
1526980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1536980Seric **
1546980Seric **	This bypasses the special checking of chompheader.
1556980Seric **
1566980Seric **	Parameters:
1576980Seric **		field -- the name of the header field.
1586980Seric **		value -- the value of the field.  It must be lower-cased.
1596980Seric **		e -- the envelope to add them to.
1606980Seric **
1616980Seric **	Returns:
1626980Seric **		none.
1636980Seric **
1646980Seric **	Side Effects:
1656980Seric **		adds the field on the list of headers for this envelope.
1666980Seric */
1676980Seric 
1686980Seric addheader(field, value, e)
1696980Seric 	char *field;
1706980Seric 	char *value;
1716980Seric 	ENVELOPE *e;
1726980Seric {
1736980Seric 	register HDR *h;
1746980Seric 	register struct hdrinfo *hi;
1756980Seric 	HDR **hp;
1766980Seric 
1776980Seric 	/* find info struct */
1786980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1796980Seric 	{
1806980Seric 		if (strcmp(field, hi->hi_field) == 0)
1816980Seric 			break;
1826980Seric 	}
1836980Seric 
1846980Seric 	/* find current place in list -- keep back pointer? */
1856980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1866980Seric 	{
1876980Seric 		if (strcmp(field, h->h_field) == 0)
1886980Seric 			break;
1896980Seric 	}
1906980Seric 
1916980Seric 	/* allocate space for new header */
1926980Seric 	h = (HDR *) xalloc(sizeof *h);
1936980Seric 	h->h_field = field;
1946980Seric 	h->h_value = newstr(value);
1957368Seric 	h->h_link = *hp;
1966980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19710689Seric 	clrbitmap(h->h_mflags);
1986980Seric 	*hp = h;
1996980Seric }
2006980Seric /*
2014091Seric **  HVALUE -- return value of a header.
2024091Seric **
2034091Seric **	Only "real" fields (i.e., ones that have not been supplied
2044091Seric **	as a default) are used.
2054091Seric **
2064091Seric **	Parameters:
2074091Seric **		field -- the field name.
20855012Seric **		e -- the envelope containing the header.
2094091Seric **
2104091Seric **	Returns:
2114091Seric **		pointer to the value part.
2124091Seric **		NULL if not found.
2134091Seric **
2144091Seric **	Side Effects:
2159382Seric **		none.
2164091Seric */
2174091Seric 
2184091Seric char *
21955012Seric hvalue(field, e)
2204091Seric 	char *field;
22155012Seric 	register ENVELOPE *e;
2224091Seric {
2234091Seric 	register HDR *h;
2244091Seric 
22555012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2264091Seric 	{
2274091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2284091Seric 			return (h->h_value);
2294091Seric 	}
2304091Seric 	return (NULL);
2314091Seric }
2324091Seric /*
2334091Seric **  ISHEADER -- predicate telling if argument is a header.
2344091Seric **
2354319Seric **	A line is a header if it has a single word followed by
2364319Seric **	optional white space followed by a colon.
2374319Seric **
2384091Seric **	Parameters:
2394091Seric **		s -- string to check for possible headerness.
2404091Seric **
2414091Seric **	Returns:
2424091Seric **		TRUE if s is a header.
2434091Seric **		FALSE otherwise.
2444091Seric **
2454091Seric **	Side Effects:
2464091Seric **		none.
2474091Seric */
2484091Seric 
2494091Seric bool
2504091Seric isheader(s)
2514091Seric 	register char *s;
2524091Seric {
2539382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2544091Seric 		s++;
2559382Seric 
2569382Seric 	/* following technically violates RFC822 */
2574091Seric 	while (isspace(*s))
2584091Seric 		s++;
2599382Seric 
2604091Seric 	return (*s == ':');
2614091Seric }
2625919Seric /*
2637783Seric **  EATHEADER -- run through the stored header and extract info.
2647783Seric **
2657783Seric **	Parameters:
2669382Seric **		e -- the envelope to process.
2677783Seric **
2687783Seric **	Returns:
2697783Seric **		none.
2707783Seric **
2717783Seric **	Side Effects:
2727783Seric **		Sets a bunch of global variables from information
2739382Seric **			in the collected header.
2749382Seric **		Aborts the message if the hop count is exceeded.
2757783Seric */
2767783Seric 
2779382Seric eatheader(e)
2789382Seric 	register ENVELOPE *e;
2797783Seric {
2807783Seric 	register HDR *h;
2817783Seric 	register char *p;
2829382Seric 	int hopcnt = 0;
2837783Seric 
2849382Seric 	if (tTd(32, 1))
2859382Seric 		printf("----- collected header -----\n");
2869382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2877783Seric 	{
2887783Seric 		extern char *capitalize();
2897783Seric 
2909382Seric 		if (tTd(32, 1))
2917783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
29211414Seric 		/* count the number of times it has been processed */
2939382Seric 		if (bitset(H_TRACE, h->h_flags))
2949382Seric 			hopcnt++;
29511414Seric 
29611414Seric 		/* send to this person if we so desire */
29711414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
29811414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
29955012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
30011414Seric 		{
30155012Seric 			sendtolist(h->h_value, (ADDRESS *) NULL,
30255012Seric 				   &e->e_sendqueue, e);
30311414Seric 		}
30411414Seric 
30511414Seric 		/* log the message-id */
30611290Seric #ifdef LOG
30713103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
30811299Seric 		    strcmp(h->h_field, "message-id") == 0)
30911290Seric 		{
31011290Seric 			char buf[MAXNAME];
31111290Seric 
31211290Seric 			p = h->h_value;
31311290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31411290Seric 			{
31511290Seric 				expand(p, buf, &buf[sizeof buf], e);
31611290Seric 				p = buf;
31711290Seric 			}
31811290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
31911290Seric 		}
32011290Seric #endif LOG
3219382Seric 	}
3229382Seric 	if (tTd(32, 1))
3237783Seric 		printf("----------------------------\n");
3247783Seric 
3259382Seric 	/* store hop count */
3269382Seric 	if (hopcnt > e->e_hopcount)
3279382Seric 		e->e_hopcount = hopcnt;
3289382Seric 
3297783Seric 	/* message priority */
33055012Seric 	p = hvalue("precedence", e);
3319382Seric 	if (p != NULL)
3329382Seric 		e->e_class = priencode(p);
3337783Seric 	if (!QueueRun)
33425013Seric 		e->e_msgpriority = e->e_msgsize
33524981Seric 				 - e->e_class * WkClassFact
33624981Seric 				 + e->e_nrcpts * WkRecipFact;
3377783Seric 
3388066Seric 	/* return receipt to */
33955012Seric 	p = hvalue("return-receipt-to", e);
3407783Seric 	if (p != NULL)
3419382Seric 		e->e_receiptto = p;
3427783Seric 
3438253Seric 	/* errors to */
34455012Seric 	p = hvalue("errors-to", e);
3458253Seric 	if (p != NULL)
34655012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue, e);
3478253Seric 
3487783Seric 	/* full name of from person */
34955012Seric 	p = hvalue("full-name", e);
3507783Seric 	if (p != NULL)
3519382Seric 		define('x', p, e);
3527783Seric 
3537783Seric 	/* date message originated */
35455012Seric 	p = hvalue("posted-date", e);
3557783Seric 	if (p == NULL)
35655012Seric 		p = hvalue("date", e);
3577783Seric 	if (p != NULL)
3587783Seric 	{
3599382Seric 		define('a', p, e);
3607783Seric 		/* we don't have a good way to do canonical conversion ....
3619382Seric 		define('d', newstr(arpatounix(p)), e);
3627783Seric 		.... so we will ignore the problem for the time being */
3637783Seric 	}
36411290Seric 
36511290Seric 	/*
36611290Seric 	**  Log collection information.
36711290Seric 	*/
36811290Seric 
36911290Seric # ifdef LOG
37011299Seric 	if (!QueueRun && LogLevel > 1)
37111290Seric 	{
37255019Seric 		char hbuf[100];
37355019Seric 		char *name = hbuf;
37455019Seric 		extern char *inet_ntoa();
37536230Skarels 
37636230Skarels 		if (RealHostName == NULL)
37736230Skarels 			name = "local";
37836230Skarels 		else if (RealHostName[0] == '[')
37936230Skarels 			name = RealHostName;
38036230Skarels 		else
38154999Seric 			(void)sprintf(hbuf, "%.80s (%s)",
38236230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
38336230Skarels 		syslog(LOG_INFO,
38436230Skarels 		    "%s: from=%s, size=%ld, class=%d, received from %s\n",
38555012Seric 		    e->e_id, e->e_from.q_paddr, e->e_msgsize,
38655012Seric 		    e->e_class, name);
38711290Seric 	}
38811290Seric # endif LOG
3897783Seric }
3907783Seric /*
3917783Seric **  PRIENCODE -- encode external priority names into internal values.
3927783Seric **
3937783Seric **	Parameters:
3947783Seric **		p -- priority in ascii.
3957783Seric **
3967783Seric **	Returns:
3977783Seric **		priority as a numeric level.
3987783Seric **
3997783Seric **	Side Effects:
4007783Seric **		none.
4017783Seric */
4027783Seric 
4037783Seric priencode(p)
4047783Seric 	char *p;
4057783Seric {
4068253Seric 	register int i;
4077783Seric 
4088253Seric 	for (i = 0; i < NumPriorities; i++)
4097783Seric 	{
41033725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4118253Seric 			return (Priorities[i].pri_val);
4127783Seric 	}
4138253Seric 
4148253Seric 	/* unknown priority */
4158253Seric 	return (0);
4167783Seric }
4177783Seric /*
4187890Seric **  CRACKADDR -- parse an address and turn it into a macro
4197783Seric **
4207783Seric **	This doesn't actually parse the address -- it just extracts
4217783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4227783Seric **	and isn't even guaranteed to leave something syntactically
4237783Seric **	identical to what it started with.  However, it does leave
4247783Seric **	something semantically identical.
4257783Seric **
42651379Seric **	This algorithm has been cleaned up to handle a wider range
42751379Seric **	of cases -- notably quoted and backslash escaped strings.
42851379Seric **	This modification makes it substantially better at preserving
42951379Seric **	the original syntax.
4307783Seric **
4317783Seric **	Parameters:
4327890Seric **		addr -- the address to be cracked.
4337783Seric **
4347783Seric **	Returns:
4357783Seric **		a pointer to the new version.
4367783Seric **
4377783Seric **	Side Effects:
4387890Seric **		none.
4397783Seric **
4407783Seric **	Warning:
4417783Seric **		The return value is saved in local storage and should
4427783Seric **		be copied if it is to be reused.
4437783Seric */
4447783Seric 
4457783Seric char *
4467890Seric crackaddr(addr)
4477890Seric 	register char *addr;
4487783Seric {
4497783Seric 	register char *p;
45051379Seric 	register char c;
45151379Seric 	int cmtlev;
45251379Seric 	int copylev;
45351379Seric 	bool qmode;
45451379Seric 	bool putgmac = FALSE;
455*56735Seric 	bool quoteit = FALSE;
45651379Seric 	register char *bp;
4577783Seric 	static char buf[MAXNAME];
4587783Seric 
4597783Seric 	if (tTd(33, 1))
4607890Seric 		printf("crackaddr(%s)\n", addr);
4617783Seric 
4628082Seric 	/* strip leading spaces */
4638082Seric 	while (*addr != '\0' && isspace(*addr))
4648082Seric 		addr++;
4658082Seric 
4667783Seric 	/*
46751379Seric 	**  Start by assuming we have no angle brackets.  This will be
46851379Seric 	**  adjusted later if we find them.
4697783Seric 	*/
4707783Seric 
47151379Seric 	bp = buf;
47251379Seric 	p = addr;
47351379Seric 	copylev = cmtlev = 0;
47451379Seric 	qmode = FALSE;
47551379Seric 
47651379Seric 	while ((c = *p++) != '\0')
4777783Seric 	{
47851379Seric 		if (copylev > 0 || c == ' ')
47951379Seric 			*bp++ = c;
4807783Seric 
481*56735Seric 		/* check for characters that may have to be quoted */
482*56735Seric 		if (index(".'@,;:[]", c) != NULL)
483*56735Seric 		{
484*56735Seric 			/*
485*56735Seric 			**  If these occur as the phrase part of a <>
486*56735Seric 			**  construct, but are not inside of () or already
487*56735Seric 			**  quoted, they will have to be quoted.  Note that
488*56735Seric 			**  now (but don't actually do the quoting).
489*56735Seric 			*/
490*56735Seric 
491*56735Seric 			if (cmtlev <= 0 && !qmode)
492*56735Seric 				quoteit = TRUE;
493*56735Seric 		}
494*56735Seric 
49551379Seric 		/* check for backslash escapes */
49651379Seric 		if (c == '\\')
4977783Seric 		{
49851379Seric 			if ((c = *p++) == '\0')
4997783Seric 			{
50051379Seric 				/* too far */
50151379Seric 				p--;
50251379Seric 				goto putg;
5037783Seric 			}
50451379Seric 			if (copylev > 0)
50551379Seric 				*bp++ = c;
50651379Seric 			goto putg;
5077783Seric 		}
5087783Seric 
50951379Seric 		/* check for quoted strings */
51051379Seric 		if (c == '"')
5117783Seric 		{
51251379Seric 			qmode = !qmode;
51351379Seric 			continue;
5147783Seric 		}
51551379Seric 		if (qmode)
51651379Seric 			goto putg;
5177783Seric 
51851379Seric 		/* check for comments */
51951379Seric 		if (c == '(')
5207783Seric 		{
52151379Seric 			cmtlev++;
52251379Seric 			if (copylev++ <= 0)
52351379Seric 				*bp++ = c;
52451379Seric 		}
52551379Seric 		if (cmtlev > 0)
52651379Seric 		{
52751379Seric 			if (c == ')')
5287783Seric 			{
52951379Seric 				cmtlev--;
53051379Seric 				copylev--;
5317783Seric 			}
5327783Seric 			continue;
5337783Seric 		}
5347783Seric 
53551379Seric 		/* check for angle brackets */
53651379Seric 		if (c == '<')
53751379Seric 		{
538*56735Seric 			register char *q;
539*56735Seric 
54051379Seric 			/* oops -- have to change our mind */
541*56735Seric 			bp = buf;
542*56735Seric 			if (quoteit)
543*56735Seric 			{
544*56735Seric 				*bp++ = '"';
545*56735Seric 
546*56735Seric 				/* back up over the '<' and any spaces */
547*56735Seric 				--p;
548*56735Seric 				while (isspace(*--p))
549*56735Seric 					continue;
550*56735Seric 				p++;
551*56735Seric 			}
552*56735Seric 			for (q = addr; q < p; )
553*56735Seric 			{
554*56735Seric 				c = *q++;
555*56735Seric 				if (quoteit && c == '"')
556*56735Seric 					*bp++ = '\\';
557*56735Seric 				*bp++ = c;
558*56735Seric 			}
559*56735Seric 			if (quoteit)
560*56735Seric 			{
561*56735Seric 				*bp++ = '"';
562*56735Seric 				while ((*bp++ = *p++) != '<')
563*56735Seric 					continue;
564*56735Seric 			}
56551379Seric 			copylev = 0;
566*56735Seric 			putgmac = quoteit = FALSE;
56751379Seric 			continue;
56851379Seric 		}
5697783Seric 
57051379Seric 		if (c == '>')
5717783Seric 		{
57251379Seric 			if (copylev++ <= 0)
57351379Seric 				*bp++ = c;
57451379Seric 			continue;
5757783Seric 		}
57651379Seric 
57751379Seric 		/* must be a real address character */
57851379Seric 	putg:
57951379Seric 		if (copylev <= 0 && !putgmac)
58051379Seric 		{
58151379Seric 			*bp++ = '\001';
58251379Seric 			*bp++ = 'g';
58351379Seric 			putgmac = TRUE;
58451379Seric 		}
5857783Seric 	}
5867783Seric 
58751379Seric 	*bp++ = '\0';
5887783Seric 
5897783Seric 	if (tTd(33, 1))
5907944Seric 		printf("crackaddr=>`%s'\n", buf);
5917783Seric 
5927783Seric 	return (buf);
5937783Seric }
5949382Seric /*
5959382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5969382Seric **
5979382Seric **	Parameters:
5989382Seric **		fp -- file to put it on.
5999382Seric **		m -- mailer to use.
6009382Seric **		e -- envelope to use.
6019382Seric **
6029382Seric **	Returns:
6039382Seric **		none.
6049382Seric **
6059382Seric **	Side Effects:
6069382Seric **		none.
6079382Seric */
6089382Seric 
60910176Seric putheader(fp, m, e)
6109382Seric 	register FILE *fp;
6119382Seric 	register MAILER *m;
6129382Seric 	register ENVELOPE *e;
6139382Seric {
61440960Sbostic 	char buf[MAX(MAXFIELD,BUFSIZ)];
6159382Seric 	register HDR *h;
6169382Seric 	extern char *arpadate();
6179382Seric 	extern char *capitalize();
61840960Sbostic 	char obuf[MAX(MAXFIELD,MAXLINE)];
6199382Seric 
6209382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6219382Seric 	{
6229382Seric 		register char *p;
62310689Seric 		extern bool bitintersect();
6249382Seric 
6259382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
62610689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6279382Seric 			continue;
6289382Seric 
62911414Seric 		/* handle Resent-... headers specially */
63011414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
63111414Seric 			continue;
63211414Seric 
6339382Seric 		p = h->h_value;
6349382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6359382Seric 		{
6369382Seric 			/* macro expand value if generated internally */
6379382Seric 			expand(p, buf, &buf[sizeof buf], e);
6389382Seric 			p = buf;
6399382Seric 			if (p == NULL || *p == '\0')
6409382Seric 				continue;
6419382Seric 		}
6429382Seric 
6439382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6449382Seric 		{
6459382Seric 			/* address field */
6469382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6479382Seric 
6489382Seric 			if (bitset(H_FROM, h->h_flags))
6499382Seric 				oldstyle = FALSE;
65055012Seric 			commaize(h, p, fp, oldstyle, m, e);
6519382Seric 		}
6529382Seric 		else
6539382Seric 		{
6549382Seric 			/* vanilla header line */
65512159Seric 			register char *nlp;
65612159Seric 
65712159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
65812159Seric 			while ((nlp = index(p, '\n')) != NULL)
65912159Seric 			{
66012159Seric 				*nlp = '\0';
66112159Seric 				(void) strcat(obuf, p);
66212159Seric 				*nlp = '\n';
66312159Seric 				putline(obuf, fp, m);
66412159Seric 				p = ++nlp;
66512161Seric 				obuf[0] = '\0';
66612159Seric 			}
66712159Seric 			(void) strcat(obuf, p);
66810176Seric 			putline(obuf, fp, m);
6699382Seric 		}
6709382Seric 	}
6719382Seric }
6729382Seric /*
6739382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6749382Seric **
6759382Seric **	Parameters:
6769382Seric **		h -- the header field to output.
6779382Seric **		p -- the value to put in it.
6789382Seric **		fp -- file to put it to.
6799382Seric **		oldstyle -- TRUE if this is an old style header.
6809382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6819382Seric **			don't transform the name at all.
68255012Seric **		e -- the envelope containing the message.
6839382Seric **
6849382Seric **	Returns:
6859382Seric **		none.
6869382Seric **
6879382Seric **	Side Effects:
6889382Seric **		outputs "p" to file "fp".
6899382Seric */
6909382Seric 
69155012Seric commaize(h, p, fp, oldstyle, m, e)
6929382Seric 	register HDR *h;
6939382Seric 	register char *p;
6949382Seric 	FILE *fp;
6959382Seric 	bool oldstyle;
6969382Seric 	register MAILER *m;
69755012Seric 	register ENVELOPE *e;
6989382Seric {
6999382Seric 	register char *obp;
7009382Seric 	int opos;
7019382Seric 	bool firstone = TRUE;
70211157Seric 	char obuf[MAXLINE + 3];
7039382Seric 
7049382Seric 	/*
7059382Seric 	**  Output the address list translated by the
7069382Seric 	**  mailer and with commas.
7079382Seric 	*/
7089382Seric 
7099382Seric 	if (tTd(14, 2))
7109382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7119382Seric 
7129382Seric 	obp = obuf;
7139382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7149382Seric 	opos = strlen(h->h_field) + 2;
7159382Seric 	obp += opos;
7169382Seric 
7179382Seric 	/*
7189382Seric 	**  Run through the list of values.
7199382Seric 	*/
7209382Seric 
7219382Seric 	while (*p != '\0')
7229382Seric 	{
7239382Seric 		register char *name;
72454983Seric 		register int c;
7259382Seric 		char savechar;
7269382Seric 		extern char *remotename();
7279382Seric 		extern char *DelimChar;		/* defined in prescan */
7289382Seric 
7299382Seric 		/*
7309382Seric 		**  Find the end of the name.  New style names
7319382Seric 		**  end with a comma, old style names end with
7329382Seric 		**  a space character.  However, spaces do not
7339382Seric 		**  necessarily delimit an old-style name -- at
7349382Seric 		**  signs mean keep going.
7359382Seric 		*/
7369382Seric 
7379382Seric 		/* find end of name */
7389382Seric 		while (isspace(*p) || *p == ',')
7399382Seric 			p++;
7409382Seric 		name = p;
7419382Seric 		for (;;)
7429382Seric 		{
7439382Seric 			char *oldp;
74416909Seric 			char pvpbuf[PSBUFSIZE];
7459382Seric 			extern bool isatword();
7469382Seric 			extern char **prescan();
7479382Seric 
74816909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7499382Seric 			p = DelimChar;
7509382Seric 
7519382Seric 			/* look to see if we have an at sign */
7529382Seric 			oldp = p;
7539382Seric 			while (*p != '\0' && isspace(*p))
7549382Seric 				p++;
7559382Seric 
7569382Seric 			if (*p != '@' && !isatword(p))
7579382Seric 			{
7589382Seric 				p = oldp;
7599382Seric 				break;
7609382Seric 			}
7619382Seric 			p += *p == '@' ? 1 : 2;
7629382Seric 			while (*p != '\0' && isspace(*p))
7639382Seric 				p++;
7649382Seric 		}
7659382Seric 		/* at the end of one complete name */
7669382Seric 
7679382Seric 		/* strip off trailing white space */
7689382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7699382Seric 			p--;
7709382Seric 		if (++p == name)
7719382Seric 			continue;
7729382Seric 		savechar = *p;
7739382Seric 		*p = '\0';
7749382Seric 
7759382Seric 		/* translate the name to be relative */
77655012Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE, e);
7779382Seric 		if (*name == '\0')
7789382Seric 		{
7799382Seric 			*p = savechar;
7809382Seric 			continue;
7819382Seric 		}
7829382Seric 
7839382Seric 		/* output the name with nice formatting */
78454983Seric 		opos += strlen(name);
7859382Seric 		if (!firstone)
7869382Seric 			opos += 2;
7879382Seric 		if (opos > 78 && !firstone)
7889382Seric 		{
78910178Seric 			(void) strcpy(obp, ",\n");
79010176Seric 			putline(obuf, fp, m);
7919382Seric 			obp = obuf;
7929382Seric 			(void) sprintf(obp, "        ");
79310161Seric 			opos = strlen(obp);
79410161Seric 			obp += opos;
79554983Seric 			opos += strlen(name);
7969382Seric 		}
7979382Seric 		else if (!firstone)
7989382Seric 		{
7999382Seric 			(void) sprintf(obp, ", ");
8009382Seric 			obp += 2;
8019382Seric 		}
8029382Seric 
8039382Seric 		/* strip off quote bits as we output */
80454983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
8059382Seric 		{
80654983Seric 			if (bitnset(M_7BITS, m->m_flags))
80754983Seric 				c &= 0177;
80854983Seric 			*obp++ = c;
8099382Seric 		}
8109382Seric 		firstone = FALSE;
8119382Seric 		*p = savechar;
8129382Seric 	}
8139382Seric 	(void) strcpy(obp, "\n");
81410176Seric 	putline(obuf, fp, m);
8159382Seric }
8169382Seric /*
8179382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8189382Seric **
8199382Seric **	Parameters:
8209382Seric **		p -- word to check.
8219382Seric **
8229382Seric **	Returns:
8239382Seric **		TRUE -- if p is the word at.
8249382Seric **		FALSE -- otherwise.
8259382Seric **
8269382Seric **	Side Effects:
8279382Seric **		none.
8289382Seric */
8299382Seric 
8309382Seric bool
8319382Seric isatword(p)
8329382Seric 	register char *p;
8339382Seric {
8349382Seric 	extern char lower();
8359382Seric 
8369382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8379382Seric 	    p[2] != '\0' && isspace(p[2]))
8389382Seric 		return (TRUE);
8399382Seric 	return (FALSE);
8409382Seric }
841