122706Sdist /*
222706Sdist **  Sendmail
322706Sdist **  Copyright (c) 1983  Eric P. Allman
422706Sdist **  Berkeley, California
522706Sdist **
622706Sdist **  Copyright (c) 1983 Regents of the University of California.
722706Sdist **  All rights reserved.  The Berkeley software License Agreement
822706Sdist **  specifies the terms and conditions for redistribution.
922706Sdist */
1022706Sdist 
1122706Sdist #ifndef lint
12*23099Seric static char	SccsId[] = "@(#)headers.c	5.3 (Berkeley) 06/08/85";
1322706Sdist #endif not lint
1422706Sdist 
154091Seric # include <errno.h>
164091Seric # include "sendmail.h"
174091Seric 
184091Seric /*
194091Seric **  CHOMPHEADER -- process and save a header line.
204091Seric **
214091Seric **	Called by collect and by readcf to deal with header lines.
224091Seric **
234091Seric **	Parameters:
244091Seric **		line -- header as a text line.
254091Seric **		def -- if set, this is a default value.
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 
354091Seric chompheader(line, def)
364091Seric 	char *line;
374091Seric 	bool def;
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;
477890Seric 	extern char *crackaddr();
484091Seric 
497677Seric # ifdef DEBUG
507677Seric 	if (tTd(31, 6))
517677Seric 		printf("chompheader: %s\n", line);
527677Seric # endif DEBUG
537677Seric 
544627Seric 	/* strip off options */
5510689Seric 	clrbitmap(mopts);
564627Seric 	p = line;
574627Seric 	if (*p == '?')
584627Seric 	{
594627Seric 		/* have some */
604627Seric 		register char *q = index(p + 1, *p);
614627Seric 
624627Seric 		if (q != NULL)
634627Seric 		{
644627Seric 			*q++ = '\0';
6510689Seric 			while (*++p != '\0')
6610689Seric 				setbitn(*p, mopts);
674627Seric 			p = q;
684627Seric 		}
694627Seric 		else
704627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
719059Seric 		cond = TRUE;
724627Seric 	}
734627Seric 
744091Seric 	/* find canonical name */
754627Seric 	fname = p;
764627Seric 	p = index(p, ':');
7710118Seric 	if (p == NULL)
7810118Seric 	{
7910118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
8010118Seric 		return (0);
8110118Seric 	}
824091Seric 	fvalue = &p[1];
834091Seric 	while (isspace(*--p))
844091Seric 		continue;
854091Seric 	*++p = '\0';
864091Seric 	makelower(fname);
874091Seric 
884091Seric 	/* strip field value on front */
894091Seric 	if (*fvalue == ' ')
904091Seric 		fvalue++;
914091Seric 
924091Seric 	/* see if it is a known type */
934091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
944091Seric 	{
954091Seric 		if (strcmp(hi->hi_field, fname) == 0)
964091Seric 			break;
974091Seric 	}
984091Seric 
9911414Seric 	/* see if this is a resent message */
10011930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10111414Seric 		CurEnv->e_flags |= EF_RESENT;
10211414Seric 
1034091Seric 	/* if this means "end of header" quit now */
1044091Seric 	if (bitset(H_EOH, hi->hi_flags))
1054091Seric 		return (hi->hi_flags);
1064091Seric 
10711414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10814784Seric 	p = "resent-from";
10914784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
11014784Seric 		p += 7;
11114784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
11211414Seric 	{
11314784Seric 		if (strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
11411414Seric 			return (hi->hi_flags);
11511414Seric 	}
11611414Seric 
11714784Seric 	/* delete default value for this header */
11814784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
11914784Seric 	{
12014784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12114784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12214784Seric 		    !bitset(H_FORCE, h->h_flags))
12314784Seric 			h->h_value = NULL;
12414784Seric 	}
12514784Seric 
12613012Seric 	/* create a new node */
12713012Seric 	h = (HDR *) xalloc(sizeof *h);
12813012Seric 	h->h_field = newstr(fname);
12913012Seric 	h->h_value = NULL;
13013012Seric 	h->h_link = NULL;
13113012Seric 	bcopy(mopts, h->h_mflags, sizeof mopts);
13213012Seric 	*hp = h;
1338066Seric 	h->h_flags = hi->hi_flags;
1344091Seric 	if (def)
1354091Seric 		h->h_flags |= H_DEFAULT;
1369059Seric 	if (cond)
1379059Seric 		h->h_flags |= H_CHECK;
1384091Seric 	if (h->h_value != NULL)
1399351Seric 		free((char *) h->h_value);
1408066Seric 	h->h_value = newstr(fvalue);
1414091Seric 
1425937Seric 	/* hack to see if this is a new format message */
1438095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1445937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1458089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1468089Seric 	{
1479342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1488089Seric 	}
1495937Seric 
1504091Seric 	return (h->h_flags);
1514091Seric }
1524091Seric /*
1536980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1546980Seric **
1556980Seric **	This bypasses the special checking of chompheader.
1566980Seric **
1576980Seric **	Parameters:
1586980Seric **		field -- the name of the header field.
1596980Seric **		value -- the value of the field.  It must be lower-cased.
1606980Seric **		e -- the envelope to add them to.
1616980Seric **
1626980Seric **	Returns:
1636980Seric **		none.
1646980Seric **
1656980Seric **	Side Effects:
1666980Seric **		adds the field on the list of headers for this envelope.
1676980Seric */
1686980Seric 
1696980Seric addheader(field, value, e)
1706980Seric 	char *field;
1716980Seric 	char *value;
1726980Seric 	ENVELOPE *e;
1736980Seric {
1746980Seric 	register HDR *h;
1756980Seric 	register struct hdrinfo *hi;
1766980Seric 	HDR **hp;
1776980Seric 
1786980Seric 	/* find info struct */
1796980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1806980Seric 	{
1816980Seric 		if (strcmp(field, hi->hi_field) == 0)
1826980Seric 			break;
1836980Seric 	}
1846980Seric 
1856980Seric 	/* find current place in list -- keep back pointer? */
1866980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1876980Seric 	{
1886980Seric 		if (strcmp(field, h->h_field) == 0)
1896980Seric 			break;
1906980Seric 	}
1916980Seric 
1926980Seric 	/* allocate space for new header */
1936980Seric 	h = (HDR *) xalloc(sizeof *h);
1946980Seric 	h->h_field = field;
1956980Seric 	h->h_value = newstr(value);
1967368Seric 	h->h_link = *hp;
1976980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19810689Seric 	clrbitmap(h->h_mflags);
1996980Seric 	*hp = h;
2006980Seric }
2016980Seric /*
2024091Seric **  HVALUE -- return value of a header.
2034091Seric **
2044091Seric **	Only "real" fields (i.e., ones that have not been supplied
2054091Seric **	as a default) are used.
2064091Seric **
2074091Seric **	Parameters:
2084091Seric **		field -- the field name.
2094091Seric **
2104091Seric **	Returns:
2114091Seric **		pointer to the value part.
2124091Seric **		NULL if not found.
2134091Seric **
2144091Seric **	Side Effects:
2159382Seric **		none.
2164091Seric */
2174091Seric 
2184091Seric char *
2194091Seric hvalue(field)
2204091Seric 	char *field;
2214091Seric {
2224091Seric 	register HDR *h;
2234091Seric 
2246908Seric 	for (h = CurEnv->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;
2827783Seric 
2839382Seric #ifdef DEBUG
2849382Seric 	if (tTd(32, 1))
2859382Seric 		printf("----- collected header -----\n");
2869382Seric #endif DEBUG
2879382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2887783Seric 	{
2899382Seric #ifdef DEBUG
2907783Seric 		extern char *capitalize();
2917783Seric 
2929382Seric 		if (tTd(32, 1))
2937783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2949382Seric #endif DEBUG
29511414Seric 		/* count the number of times it has been processed */
2969382Seric 		if (bitset(H_TRACE, h->h_flags))
2979382Seric 			hopcnt++;
29811414Seric 
29911414Seric 		/* send to this person if we so desire */
30011414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30111414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30211414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
30311414Seric 		{
30411414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
30511414Seric 		}
30611414Seric 
30711414Seric 		/* log the message-id */
30811290Seric #ifdef LOG
30913103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31011299Seric 		    strcmp(h->h_field, "message-id") == 0)
31111290Seric 		{
31211290Seric 			char buf[MAXNAME];
31311290Seric 
31411290Seric 			p = h->h_value;
31511290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31611290Seric 			{
31711290Seric 				expand(p, buf, &buf[sizeof buf], e);
31811290Seric 				p = buf;
31911290Seric 			}
32011290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
32111290Seric 		}
32211290Seric #endif LOG
3239382Seric 	}
3249382Seric #ifdef DEBUG
3259382Seric 	if (tTd(32, 1))
3267783Seric 		printf("----------------------------\n");
3279382Seric #endif DEBUG
3287783Seric 
3299382Seric 	/* store hop count */
3309382Seric 	if (hopcnt > e->e_hopcount)
3319382Seric 		e->e_hopcount = hopcnt;
3329382Seric 
3337783Seric 	/* message priority */
3349382Seric 	p = hvalue("precedence");
3359382Seric 	if (p != NULL)
3369382Seric 		e->e_class = priencode(p);
3377783Seric 	if (!QueueRun)
33821059Seric 		e->e_msgpriority = e->e_msgsize + e->e_ctime - e->e_class * WKPRIFACT;
3397783Seric 
3408066Seric 	/* return receipt to */
3418066Seric 	p = hvalue("return-receipt-to");
3427783Seric 	if (p != NULL)
3439382Seric 		e->e_receiptto = p;
3447783Seric 
3458253Seric 	/* errors to */
3468253Seric 	p = hvalue("errors-to");
3478253Seric 	if (p != NULL)
3489620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3498253Seric 
3507783Seric 	/* from person */
3519285Seric 	if (OpMode == MD_ARPAFTP)
3528066Seric 	{
3538066Seric 		register struct hdrinfo *hi = HdrInfo;
3547783Seric 
3558066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3568066Seric 		{
3578066Seric 			if (bitset(H_FROM, hi->hi_flags))
3588066Seric 				p = hvalue(hi->hi_field);
3598066Seric 		}
3608066Seric 		if (p != NULL)
3619382Seric 			setsender(p);
3628066Seric 	}
3638066Seric 
3647783Seric 	/* full name of from person */
3657783Seric 	p = hvalue("full-name");
3667783Seric 	if (p != NULL)
3679382Seric 		define('x', p, e);
3687783Seric 
3697783Seric 	/* date message originated */
3707783Seric 	p = hvalue("posted-date");
3717783Seric 	if (p == NULL)
3727783Seric 		p = hvalue("date");
3737783Seric 	if (p != NULL)
3747783Seric 	{
3759382Seric 		define('a', p, e);
3767783Seric 		/* we don't have a good way to do canonical conversion ....
3779382Seric 		define('d', newstr(arpatounix(p)), e);
3787783Seric 		.... so we will ignore the problem for the time being */
3797783Seric 	}
38011290Seric 
38111290Seric 	/*
38211290Seric 	**  Log collection information.
38311290Seric 	*/
38411290Seric 
38511290Seric # ifdef LOG
38611299Seric 	if (!QueueRun && LogLevel > 1)
38711290Seric 	{
38811290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
38911290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
39011290Seric 		       CurEnv->e_class);
39111290Seric 	}
39211290Seric # endif LOG
3937783Seric }
3947783Seric /*
3957783Seric **  PRIENCODE -- encode external priority names into internal values.
3967783Seric **
3977783Seric **	Parameters:
3987783Seric **		p -- priority in ascii.
3997783Seric **
4007783Seric **	Returns:
4017783Seric **		priority as a numeric level.
4027783Seric **
4037783Seric **	Side Effects:
4047783Seric **		none.
4057783Seric */
4067783Seric 
4077783Seric priencode(p)
4087783Seric 	char *p;
4097783Seric {
4108253Seric 	register int i;
4117783Seric 	extern bool sameword();
4127783Seric 
4138253Seric 	for (i = 0; i < NumPriorities; i++)
4147783Seric 	{
4158253Seric 		if (sameword(p, Priorities[i].pri_name))
4168253Seric 			return (Priorities[i].pri_val);
4177783Seric 	}
4188253Seric 
4198253Seric 	/* unknown priority */
4208253Seric 	return (0);
4217783Seric }
4227783Seric /*
4237890Seric **  CRACKADDR -- parse an address and turn it into a macro
4247783Seric **
4257783Seric **	This doesn't actually parse the address -- it just extracts
4267783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4277783Seric **	and isn't even guaranteed to leave something syntactically
4287783Seric **	identical to what it started with.  However, it does leave
4297783Seric **	something semantically identical.
4307783Seric **
4317783Seric **	The process is kind of strange.  There are a number of
4327783Seric **	interesting cases:
4337783Seric **		1.  comment <address> comment	==> comment <$g> comment
4347783Seric **		2.  address			==> address
4357783Seric **		3.  address (comment)		==> $g (comment)
4367783Seric **		4.  (comment) address		==> (comment) $g
4377783Seric **	And then there are the hard cases....
4387783Seric **		5.  add (comment) ress		==> $g (comment)
4397783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4407783Seric **		7.    .... etc ....
4417783Seric **
4427783Seric **	Parameters:
4437890Seric **		addr -- the address to be cracked.
4447783Seric **
4457783Seric **	Returns:
4467783Seric **		a pointer to the new version.
4477783Seric **
4487783Seric **	Side Effects:
4497890Seric **		none.
4507783Seric **
4517783Seric **	Warning:
4527783Seric **		The return value is saved in local storage and should
4537783Seric **		be copied if it is to be reused.
4547783Seric */
4557783Seric 
4567783Seric char *
4577890Seric crackaddr(addr)
4587890Seric 	register char *addr;
4597783Seric {
4607783Seric 	register char *p;
4617783Seric 	register int i;
4627783Seric 	static char buf[MAXNAME];
4637783Seric 	char *rhs;
4647783Seric 	bool gotaddr;
4657783Seric 	register char *bp;
4667783Seric 
4677783Seric # ifdef DEBUG
4687783Seric 	if (tTd(33, 1))
4697890Seric 		printf("crackaddr(%s)\n", addr);
4707783Seric # endif DEBUG
4717783Seric 
472*23099Seric 	(void) strcpy(buf, "");
4737783Seric 	rhs = NULL;
4747783Seric 
4758082Seric 	/* strip leading spaces */
4768082Seric 	while (*addr != '\0' && isspace(*addr))
4778082Seric 		addr++;
4788082Seric 
4797783Seric 	/*
4807783Seric 	**  See if we have anything in angle brackets.  If so, that is
4817783Seric 	**  the address part, and the rest is the comment.
4827783Seric 	*/
4837783Seric 
4847890Seric 	p = index(addr, '<');
4857783Seric 	if (p != NULL)
4867783Seric 	{
4877890Seric 		/* copy the beginning of the addr field to the buffer */
4887783Seric 		*p = '\0';
489*23099Seric 		(void) strcpy(buf, addr);
490*23099Seric 		(void) strcat(buf, "<");
4918082Seric 		*p++ = '<';
4927783Seric 
4938082Seric 		/* skip spaces */
4948082Seric 		while (isspace(*p))
4958082Seric 			p++;
4968082Seric 
4977783Seric 		/* find the matching right angle bracket */
4988082Seric 		addr = p;
4997783Seric 		for (i = 0; *p != '\0'; p++)
5007783Seric 		{
5017783Seric 			switch (*p)
5027783Seric 			{
5037783Seric 			  case '<':
5047783Seric 				i++;
5057783Seric 				break;
5067783Seric 
5077783Seric 			  case '>':
5087783Seric 				i--;
5097783Seric 				break;
5107783Seric 			}
5117783Seric 			if (i < 0)
5127783Seric 				break;
5137783Seric 		}
5147783Seric 
5157783Seric 		/* p now points to the closing quote (or a null byte) */
5167783Seric 		if (*p != '\0')
5177783Seric 		{
5187783Seric 			/* make rhs point to the extra stuff at the end */
5197783Seric 			rhs = p;
5207783Seric 			*p++ = '\0';
5217783Seric 		}
5227783Seric 	}
5237783Seric 
5247783Seric 	/*
5257944Seric 	**  Now parse the real address part.  "addr" points to the (null
5267783Seric 	**  terminated) version of what we are inerested in; rhs points
5277783Seric 	**  to the extra stuff at the end of the line, if any.
5287783Seric 	*/
5297783Seric 
5307890Seric 	p = addr;
5317783Seric 
5327783Seric 	/* now strip out comments */
5337783Seric 	bp = &buf[strlen(buf)];
5347783Seric 	gotaddr = FALSE;
5357783Seric 	for (; *p != '\0'; p++)
5367783Seric 	{
5377783Seric 		if (*p == '(')
5387783Seric 		{
5397783Seric 			/* copy to matching close paren */
5407783Seric 			*bp++ = *p++;
5417783Seric 			for (i = 0; *p != '\0'; p++)
5427783Seric 			{
5437783Seric 				*bp++ = *p;
5447783Seric 				switch (*p)
5457783Seric 				{
5467783Seric 				  case '(':
5477783Seric 					i++;
5487783Seric 					break;
5497783Seric 
5507783Seric 				  case ')':
5517783Seric 					i--;
5527783Seric 					break;
5537783Seric 				}
5547783Seric 				if (i < 0)
5557783Seric 					break;
5567783Seric 			}
5577783Seric 			continue;
5587783Seric 		}
5597783Seric 
5607783Seric 		/*
5617783Seric 		**  If this is the first "real" character we have seen,
5627783Seric 		**  then we put the "$g" in the buffer now.
5637783Seric 		*/
5647783Seric 
5657783Seric 		if (isspace(*p))
5667783Seric 			*bp++ = *p;
5677783Seric 		else if (!gotaddr)
5687783Seric 		{
569*23099Seric 			(void) strcpy(bp, "\001g");
5707783Seric 			bp += 2;
5717783Seric 			gotaddr = TRUE;
5727783Seric 		}
5737783Seric 	}
5747783Seric 
5757944Seric 	/* hack, hack.... strip trailing blanks */
5767944Seric 	do
5777944Seric 	{
5787944Seric 		*bp-- = '\0';
5797944Seric 	} while (isspace(*bp));
5807944Seric 	bp++;
5817783Seric 
5827944Seric 	/* put any right hand side back on */
5837783Seric 	if (rhs != NULL)
5847783Seric 	{
5857783Seric 		*rhs = '>';
586*23099Seric 		(void) strcpy(bp, rhs);
5877783Seric 	}
5887783Seric 
5897783Seric # ifdef DEBUG
5907783Seric 	if (tTd(33, 1))
5917944Seric 		printf("crackaddr=>`%s'\n", buf);
5927783Seric # endif DEBUG
5937783Seric 
5947783Seric 	return (buf);
5957783Seric }
5969382Seric /*
5979382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5989382Seric **
5999382Seric **	Parameters:
6009382Seric **		fp -- file to put it on.
6019382Seric **		m -- mailer to use.
6029382Seric **		e -- envelope to use.
6039382Seric **
6049382Seric **	Returns:
6059382Seric **		none.
6069382Seric **
6079382Seric **	Side Effects:
6089382Seric **		none.
6099382Seric */
6109382Seric 
61110176Seric putheader(fp, m, e)
6129382Seric 	register FILE *fp;
6139382Seric 	register MAILER *m;
6149382Seric 	register ENVELOPE *e;
6159382Seric {
6169382Seric 	char buf[BUFSIZ];
6179382Seric 	register HDR *h;
6189382Seric 	extern char *arpadate();
6199382Seric 	extern char *capitalize();
6209382Seric 	char obuf[MAXLINE];
6219382Seric 
6229382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6239382Seric 	{
6249382Seric 		register char *p;
62510689Seric 		extern bool bitintersect();
6269382Seric 
6279382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
62810689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6299382Seric 			continue;
6309382Seric 
63111414Seric 		/* handle Resent-... headers specially */
63211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
63311414Seric 			continue;
63411414Seric 
6359382Seric 		p = h->h_value;
6369382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6379382Seric 		{
6389382Seric 			/* macro expand value if generated internally */
6399382Seric 			expand(p, buf, &buf[sizeof buf], e);
6409382Seric 			p = buf;
6419382Seric 			if (p == NULL || *p == '\0')
6429382Seric 				continue;
6439382Seric 		}
6449382Seric 
6459382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6469382Seric 		{
6479382Seric 			/* address field */
6489382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6499382Seric 
6509382Seric 			if (bitset(H_FROM, h->h_flags))
6519382Seric 				oldstyle = FALSE;
65210176Seric 			commaize(h, p, fp, oldstyle, m);
6539382Seric 		}
6549382Seric 		else
6559382Seric 		{
6569382Seric 			/* vanilla header line */
65712159Seric 			register char *nlp;
65812159Seric 
65912159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
66012159Seric 			while ((nlp = index(p, '\n')) != NULL)
66112159Seric 			{
66212159Seric 				*nlp = '\0';
66312159Seric 				(void) strcat(obuf, p);
66412159Seric 				*nlp = '\n';
66512159Seric 				putline(obuf, fp, m);
66612159Seric 				p = ++nlp;
66712161Seric 				obuf[0] = '\0';
66812159Seric 			}
66912159Seric 			(void) strcat(obuf, p);
67010176Seric 			putline(obuf, fp, m);
6719382Seric 		}
6729382Seric 	}
6739382Seric }
6749382Seric /*
6759382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6769382Seric **
6779382Seric **	Parameters:
6789382Seric **		h -- the header field to output.
6799382Seric **		p -- the value to put in it.
6809382Seric **		fp -- file to put it to.
6819382Seric **		oldstyle -- TRUE if this is an old style header.
6829382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6839382Seric **			don't transform the name at all.
6849382Seric **
6859382Seric **	Returns:
6869382Seric **		none.
6879382Seric **
6889382Seric **	Side Effects:
6899382Seric **		outputs "p" to file "fp".
6909382Seric */
6919382Seric 
69210176Seric commaize(h, p, fp, oldstyle, m)
6939382Seric 	register HDR *h;
6949382Seric 	register char *p;
6959382Seric 	FILE *fp;
6969382Seric 	bool oldstyle;
6979382Seric 	register MAILER *m;
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 # ifdef DEBUG
7109382Seric 	if (tTd(14, 2))
7119382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7129382Seric # endif DEBUG
7139382Seric 
7149382Seric 	obp = obuf;
7159382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7169382Seric 	opos = strlen(h->h_field) + 2;
7179382Seric 	obp += opos;
7189382Seric 
7199382Seric 	/*
7209382Seric 	**  Run through the list of values.
7219382Seric 	*/
7229382Seric 
7239382Seric 	while (*p != '\0')
7249382Seric 	{
7259382Seric 		register char *name;
7269382Seric 		char savechar;
7279382Seric 		extern char *remotename();
7289382Seric 		extern char *DelimChar;		/* defined in prescan */
7299382Seric 
7309382Seric 		/*
7319382Seric 		**  Find the end of the name.  New style names
7329382Seric 		**  end with a comma, old style names end with
7339382Seric 		**  a space character.  However, spaces do not
7349382Seric 		**  necessarily delimit an old-style name -- at
7359382Seric 		**  signs mean keep going.
7369382Seric 		*/
7379382Seric 
7389382Seric 		/* find end of name */
7399382Seric 		while (isspace(*p) || *p == ',')
7409382Seric 			p++;
7419382Seric 		name = p;
7429382Seric 		for (;;)
7439382Seric 		{
7449382Seric 			char *oldp;
74516909Seric 			char pvpbuf[PSBUFSIZE];
7469382Seric 			extern bool isatword();
7479382Seric 			extern char **prescan();
7489382Seric 
74916909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7509382Seric 			p = DelimChar;
7519382Seric 
7529382Seric 			/* look to see if we have an at sign */
7539382Seric 			oldp = p;
7549382Seric 			while (*p != '\0' && isspace(*p))
7559382Seric 				p++;
7569382Seric 
7579382Seric 			if (*p != '@' && !isatword(p))
7589382Seric 			{
7599382Seric 				p = oldp;
7609382Seric 				break;
7619382Seric 			}
7629382Seric 			p += *p == '@' ? 1 : 2;
7639382Seric 			while (*p != '\0' && isspace(*p))
7649382Seric 				p++;
7659382Seric 		}
7669382Seric 		/* at the end of one complete name */
7679382Seric 
7689382Seric 		/* strip off trailing white space */
7699382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7709382Seric 			p--;
7719382Seric 		if (++p == name)
7729382Seric 			continue;
7739382Seric 		savechar = *p;
7749382Seric 		*p = '\0';
7759382Seric 
7769382Seric 		/* translate the name to be relative */
77710309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7789382Seric 		if (*name == '\0')
7799382Seric 		{
7809382Seric 			*p = savechar;
7819382Seric 			continue;
7829382Seric 		}
7839382Seric 
7849382Seric 		/* output the name with nice formatting */
7859382Seric 		opos += qstrlen(name);
7869382Seric 		if (!firstone)
7879382Seric 			opos += 2;
7889382Seric 		if (opos > 78 && !firstone)
7899382Seric 		{
79010178Seric 			(void) strcpy(obp, ",\n");
79110176Seric 			putline(obuf, fp, m);
7929382Seric 			obp = obuf;
7939382Seric 			(void) sprintf(obp, "        ");
79410161Seric 			opos = strlen(obp);
79510161Seric 			obp += opos;
79610161Seric 			opos += qstrlen(name);
7979382Seric 		}
7989382Seric 		else if (!firstone)
7999382Seric 		{
8009382Seric 			(void) sprintf(obp, ", ");
8019382Seric 			obp += 2;
8029382Seric 		}
8039382Seric 
8049382Seric 		/* strip off quote bits as we output */
80511157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8069382Seric 		{
8079382Seric 			if (bitset(0200, *name))
8089382Seric 				*obp++ = '\\';
8099382Seric 			*obp++ = *name++ & ~0200;
8109382Seric 		}
8119382Seric 		firstone = FALSE;
8129382Seric 		*p = savechar;
8139382Seric 	}
8149382Seric 	(void) strcpy(obp, "\n");
81510176Seric 	putline(obuf, fp, m);
8169382Seric }
8179382Seric /*
8189382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8199382Seric **
8209382Seric **	Parameters:
8219382Seric **		p -- word to check.
8229382Seric **
8239382Seric **	Returns:
8249382Seric **		TRUE -- if p is the word at.
8259382Seric **		FALSE -- otherwise.
8269382Seric **
8279382Seric **	Side Effects:
8289382Seric **		none.
8299382Seric */
8309382Seric 
8319382Seric bool
8329382Seric isatword(p)
8339382Seric 	register char *p;
8349382Seric {
8359382Seric 	extern char lower();
8369382Seric 
8379382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8389382Seric 	    p[2] != '\0' && isspace(p[2]))
8399382Seric 		return (TRUE);
8409382Seric 	return (FALSE);
8419382Seric }
842