122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
6*42826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*42826Sbostic static char sccsid[] = "@(#)headers.c	5.15 (Berkeley) 06/01/90";
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.
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 
344091Seric chompheader(line, def)
354091Seric 	char *line;
364091Seric 	bool def;
374091Seric {
384091Seric 	register char *p;
394091Seric 	register HDR *h;
404091Seric 	HDR **hp;
414091Seric 	char *fname;
424091Seric 	char *fvalue;
434091Seric 	struct hdrinfo *hi;
449059Seric 	bool cond = FALSE;
4510689Seric 	BITMAP mopts;
467890Seric 	extern char *crackaddr();
474091Seric 
487677Seric 	if (tTd(31, 6))
497677Seric 		printf("chompheader: %s\n", line);
507677Seric 
514627Seric 	/* strip off options */
5210689Seric 	clrbitmap(mopts);
534627Seric 	p = line;
544627Seric 	if (*p == '?')
554627Seric 	{
564627Seric 		/* have some */
574627Seric 		register char *q = index(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;
734627Seric 	p = index(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))
9811414Seric 		CurEnv->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";
10614784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
10714784Seric 		p += 7;
10814784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
10911414Seric 	{
11024941Seric 		if (CurEnv->e_from.q_paddr != NULL &&
11124941Seric 		    strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
11211414Seric 			return (hi->hi_flags);
11311414Seric 	}
11411414Seric 
11514784Seric 	/* delete default value for this header */
11614784Seric 	for (hp = &CurEnv->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) &&
1425937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1438089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1448089Seric 	{
1459342Seric 		CurEnv->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.
2074091Seric **
2084091Seric **	Returns:
2094091Seric **		pointer to the value part.
2104091Seric **		NULL if not found.
2114091Seric **
2124091Seric **	Side Effects:
2139382Seric **		none.
2144091Seric */
2154091Seric 
2164091Seric char *
2174091Seric hvalue(field)
2184091Seric 	char *field;
2194091Seric {
2204091Seric 	register HDR *h;
2214091Seric 
2226908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2234091Seric 	{
2244091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2254091Seric 			return (h->h_value);
2264091Seric 	}
2274091Seric 	return (NULL);
2284091Seric }
2294091Seric /*
2304091Seric **  ISHEADER -- predicate telling if argument is a header.
2314091Seric **
2324319Seric **	A line is a header if it has a single word followed by
2334319Seric **	optional white space followed by a colon.
2344319Seric **
2354091Seric **	Parameters:
2364091Seric **		s -- string to check for possible headerness.
2374091Seric **
2384091Seric **	Returns:
2394091Seric **		TRUE if s is a header.
2404091Seric **		FALSE otherwise.
2414091Seric **
2424091Seric **	Side Effects:
2434091Seric **		none.
2444091Seric */
2454091Seric 
2464091Seric bool
2474091Seric isheader(s)
2484091Seric 	register char *s;
2494091Seric {
2509382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2514091Seric 		s++;
2529382Seric 
2539382Seric 	/* following technically violates RFC822 */
2544091Seric 	while (isspace(*s))
2554091Seric 		s++;
2569382Seric 
2574091Seric 	return (*s == ':');
2584091Seric }
2595919Seric /*
2607783Seric **  EATHEADER -- run through the stored header and extract info.
2617783Seric **
2627783Seric **	Parameters:
2639382Seric **		e -- the envelope to process.
2647783Seric **
2657783Seric **	Returns:
2667783Seric **		none.
2677783Seric **
2687783Seric **	Side Effects:
2697783Seric **		Sets a bunch of global variables from information
2709382Seric **			in the collected header.
2719382Seric **		Aborts the message if the hop count is exceeded.
2727783Seric */
2737783Seric 
2749382Seric eatheader(e)
2759382Seric 	register ENVELOPE *e;
2767783Seric {
2777783Seric 	register HDR *h;
2787783Seric 	register char *p;
2799382Seric 	int hopcnt = 0;
2807783Seric 
2819382Seric 	if (tTd(32, 1))
2829382Seric 		printf("----- collected header -----\n");
2839382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2847783Seric 	{
2857783Seric 		extern char *capitalize();
2867783Seric 
2879382Seric 		if (tTd(32, 1))
2887783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
28911414Seric 		/* count the number of times it has been processed */
2909382Seric 		if (bitset(H_TRACE, h->h_flags))
2919382Seric 			hopcnt++;
29211414Seric 
29311414Seric 		/* send to this person if we so desire */
29411414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
29511414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
29611414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
29711414Seric 		{
29811414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
29911414Seric 		}
30011414Seric 
30111414Seric 		/* log the message-id */
30211290Seric #ifdef LOG
30313103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
30411299Seric 		    strcmp(h->h_field, "message-id") == 0)
30511290Seric 		{
30611290Seric 			char buf[MAXNAME];
30711290Seric 
30811290Seric 			p = h->h_value;
30911290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31011290Seric 			{
31111290Seric 				expand(p, buf, &buf[sizeof buf], e);
31211290Seric 				p = buf;
31311290Seric 			}
31411290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
31511290Seric 		}
31611290Seric #endif LOG
3179382Seric 	}
3189382Seric 	if (tTd(32, 1))
3197783Seric 		printf("----------------------------\n");
3207783Seric 
3219382Seric 	/* store hop count */
3229382Seric 	if (hopcnt > e->e_hopcount)
3239382Seric 		e->e_hopcount = hopcnt;
3249382Seric 
3257783Seric 	/* message priority */
3269382Seric 	p = hvalue("precedence");
3279382Seric 	if (p != NULL)
3289382Seric 		e->e_class = priencode(p);
3297783Seric 	if (!QueueRun)
33025013Seric 		e->e_msgpriority = e->e_msgsize
33124981Seric 				 - e->e_class * WkClassFact
33224981Seric 				 + e->e_nrcpts * WkRecipFact;
3337783Seric 
3348066Seric 	/* return receipt to */
3358066Seric 	p = hvalue("return-receipt-to");
3367783Seric 	if (p != NULL)
3379382Seric 		e->e_receiptto = p;
3387783Seric 
3398253Seric 	/* errors to */
3408253Seric 	p = hvalue("errors-to");
3418253Seric 	if (p != NULL)
3429620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3438253Seric 
3447783Seric 	/* from person */
3459285Seric 	if (OpMode == MD_ARPAFTP)
3468066Seric 	{
3478066Seric 		register struct hdrinfo *hi = HdrInfo;
3487783Seric 
3498066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3508066Seric 		{
3518066Seric 			if (bitset(H_FROM, hi->hi_flags))
3528066Seric 				p = hvalue(hi->hi_field);
3538066Seric 		}
3548066Seric 		if (p != NULL)
3559382Seric 			setsender(p);
3568066Seric 	}
3578066Seric 
3587783Seric 	/* full name of from person */
3597783Seric 	p = hvalue("full-name");
3607783Seric 	if (p != NULL)
3619382Seric 		define('x', p, e);
3627783Seric 
3637783Seric 	/* date message originated */
3647783Seric 	p = hvalue("posted-date");
3657783Seric 	if (p == NULL)
3667783Seric 		p = hvalue("date");
3677783Seric 	if (p != NULL)
3687783Seric 	{
3699382Seric 		define('a', p, e);
3707783Seric 		/* we don't have a good way to do canonical conversion ....
3719382Seric 		define('d', newstr(arpatounix(p)), e);
3727783Seric 		.... so we will ignore the problem for the time being */
3737783Seric 	}
37411290Seric 
37511290Seric 	/*
37611290Seric 	**  Log collection information.
37711290Seric 	*/
37811290Seric 
37911290Seric # ifdef LOG
38011299Seric 	if (!QueueRun && LogLevel > 1)
38111290Seric 	{
38236230Skarels 		char hbuf[100], *name = hbuf;
38336230Skarels 
38436230Skarels 		if (RealHostName == NULL)
38536230Skarels 			name = "local";
38636230Skarels 		else if (RealHostName[0] == '[')
38736230Skarels 			name = RealHostName;
38836230Skarels 		else
38936230Skarels 			(void)sprintf(hbuf, "%.90s (%s)",
39036230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
39136230Skarels 		syslog(LOG_INFO,
39236230Skarels 		    "%s: from=%s, size=%ld, class=%d, received from %s\n",
39336230Skarels 		    CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
39436230Skarels 		    CurEnv->e_class, name);
39511290Seric 	}
39611290Seric # endif LOG
3977783Seric }
3987783Seric /*
3997783Seric **  PRIENCODE -- encode external priority names into internal values.
4007783Seric **
4017783Seric **	Parameters:
4027783Seric **		p -- priority in ascii.
4037783Seric **
4047783Seric **	Returns:
4057783Seric **		priority as a numeric level.
4067783Seric **
4077783Seric **	Side Effects:
4087783Seric **		none.
4097783Seric */
4107783Seric 
4117783Seric priencode(p)
4127783Seric 	char *p;
4137783Seric {
4148253Seric 	register int i;
4157783Seric 
4168253Seric 	for (i = 0; i < NumPriorities; i++)
4177783Seric 	{
41833725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4198253Seric 			return (Priorities[i].pri_val);
4207783Seric 	}
4218253Seric 
4228253Seric 	/* unknown priority */
4238253Seric 	return (0);
4247783Seric }
4257783Seric /*
4267890Seric **  CRACKADDR -- parse an address and turn it into a macro
4277783Seric **
4287783Seric **	This doesn't actually parse the address -- it just extracts
4297783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4307783Seric **	and isn't even guaranteed to leave something syntactically
4317783Seric **	identical to what it started with.  However, it does leave
4327783Seric **	something semantically identical.
4337783Seric **
4347783Seric **	The process is kind of strange.  There are a number of
4357783Seric **	interesting cases:
4367783Seric **		1.  comment <address> comment	==> comment <$g> comment
4377783Seric **		2.  address			==> address
4387783Seric **		3.  address (comment)		==> $g (comment)
4397783Seric **		4.  (comment) address		==> (comment) $g
4407783Seric **	And then there are the hard cases....
4417783Seric **		5.  add (comment) ress		==> $g (comment)
4427783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4437783Seric **		7.    .... etc ....
4447783Seric **
4457783Seric **	Parameters:
4467890Seric **		addr -- the address to be cracked.
4477783Seric **
4487783Seric **	Returns:
4497783Seric **		a pointer to the new version.
4507783Seric **
4517783Seric **	Side Effects:
4527890Seric **		none.
4537783Seric **
4547783Seric **	Warning:
4557783Seric **		The return value is saved in local storage and should
4567783Seric **		be copied if it is to be reused.
4577783Seric */
4587783Seric 
4597783Seric char *
4607890Seric crackaddr(addr)
4617890Seric 	register char *addr;
4627783Seric {
4637783Seric 	register char *p;
4647783Seric 	register int i;
4657783Seric 	static char buf[MAXNAME];
4667783Seric 	char *rhs;
4677783Seric 	bool gotaddr;
4687783Seric 	register char *bp;
4697783Seric 
4707783Seric 	if (tTd(33, 1))
4717890Seric 		printf("crackaddr(%s)\n", addr);
4727783Seric 
47323099Seric 	(void) strcpy(buf, "");
4747783Seric 	rhs = NULL;
4757783Seric 
4768082Seric 	/* strip leading spaces */
4778082Seric 	while (*addr != '\0' && isspace(*addr))
4788082Seric 		addr++;
4798082Seric 
4807783Seric 	/*
4817783Seric 	**  See if we have anything in angle brackets.  If so, that is
4827783Seric 	**  the address part, and the rest is the comment.
4837783Seric 	*/
4847783Seric 
4857890Seric 	p = index(addr, '<');
4867783Seric 	if (p != NULL)
4877783Seric 	{
4887890Seric 		/* copy the beginning of the addr field to the buffer */
4897783Seric 		*p = '\0';
49023099Seric 		(void) strcpy(buf, addr);
49123099Seric 		(void) strcat(buf, "<");
4928082Seric 		*p++ = '<';
4937783Seric 
4948082Seric 		/* skip spaces */
4958082Seric 		while (isspace(*p))
4968082Seric 			p++;
4978082Seric 
4987783Seric 		/* find the matching right angle bracket */
4998082Seric 		addr = p;
5007783Seric 		for (i = 0; *p != '\0'; p++)
5017783Seric 		{
5027783Seric 			switch (*p)
5037783Seric 			{
5047783Seric 			  case '<':
5057783Seric 				i++;
5067783Seric 				break;
5077783Seric 
5087783Seric 			  case '>':
5097783Seric 				i--;
5107783Seric 				break;
5117783Seric 			}
5127783Seric 			if (i < 0)
5137783Seric 				break;
5147783Seric 		}
5157783Seric 
5167783Seric 		/* p now points to the closing quote (or a null byte) */
5177783Seric 		if (*p != '\0')
5187783Seric 		{
5197783Seric 			/* make rhs point to the extra stuff at the end */
5207783Seric 			rhs = p;
5217783Seric 			*p++ = '\0';
5227783Seric 		}
5237783Seric 	}
5247783Seric 
5257783Seric 	/*
5267944Seric 	**  Now parse the real address part.  "addr" points to the (null
5277783Seric 	**  terminated) version of what we are inerested in; rhs points
5287783Seric 	**  to the extra stuff at the end of the line, if any.
5297783Seric 	*/
5307783Seric 
5317890Seric 	p = addr;
5327783Seric 
5337783Seric 	/* now strip out comments */
5347783Seric 	bp = &buf[strlen(buf)];
5357783Seric 	gotaddr = FALSE;
5367783Seric 	for (; *p != '\0'; p++)
5377783Seric 	{
5387783Seric 		if (*p == '(')
5397783Seric 		{
5407783Seric 			/* copy to matching close paren */
5417783Seric 			*bp++ = *p++;
5427783Seric 			for (i = 0; *p != '\0'; p++)
5437783Seric 			{
5447783Seric 				*bp++ = *p;
5457783Seric 				switch (*p)
5467783Seric 				{
5477783Seric 				  case '(':
5487783Seric 					i++;
5497783Seric 					break;
5507783Seric 
5517783Seric 				  case ')':
5527783Seric 					i--;
5537783Seric 					break;
5547783Seric 				}
5557783Seric 				if (i < 0)
5567783Seric 					break;
5577783Seric 			}
5587783Seric 			continue;
5597783Seric 		}
5607783Seric 
5617783Seric 		/*
5627783Seric 		**  If this is the first "real" character we have seen,
5637783Seric 		**  then we put the "$g" in the buffer now.
5647783Seric 		*/
5657783Seric 
5667783Seric 		if (isspace(*p))
5677783Seric 			*bp++ = *p;
5687783Seric 		else if (!gotaddr)
5697783Seric 		{
57023099Seric 			(void) strcpy(bp, "\001g");
5717783Seric 			bp += 2;
5727783Seric 			gotaddr = TRUE;
5737783Seric 		}
5747783Seric 	}
5757783Seric 
5767944Seric 	/* hack, hack.... strip trailing blanks */
5777944Seric 	do
5787944Seric 	{
5797944Seric 		*bp-- = '\0';
5807944Seric 	} while (isspace(*bp));
5817944Seric 	bp++;
5827783Seric 
5837944Seric 	/* put any right hand side back on */
5847783Seric 	if (rhs != NULL)
5857783Seric 	{
5867783Seric 		*rhs = '>';
58723099Seric 		(void) strcpy(bp, rhs);
5887783Seric 	}
5897783Seric 
5907783Seric 	if (tTd(33, 1))
5917944Seric 		printf("crackaddr=>`%s'\n", buf);
5927783Seric 
5937783Seric 	return (buf);
5947783Seric }
5959382Seric /*
5969382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5979382Seric **
5989382Seric **	Parameters:
5999382Seric **		fp -- file to put it on.
6009382Seric **		m -- mailer to use.
6019382Seric **		e -- envelope to use.
6029382Seric **
6039382Seric **	Returns:
6049382Seric **		none.
6059382Seric **
6069382Seric **	Side Effects:
6079382Seric **		none.
6089382Seric */
6099382Seric 
61010176Seric putheader(fp, m, e)
6119382Seric 	register FILE *fp;
6129382Seric 	register MAILER *m;
6139382Seric 	register ENVELOPE *e;
6149382Seric {
61540960Sbostic 	char buf[MAX(MAXFIELD,BUFSIZ)];
6169382Seric 	register HDR *h;
6179382Seric 	extern char *arpadate();
6189382Seric 	extern char *capitalize();
61940960Sbostic 	char obuf[MAX(MAXFIELD,MAXLINE)];
6209382Seric 
6219382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6229382Seric 	{
6239382Seric 		register char *p;
62410689Seric 		extern bool bitintersect();
6259382Seric 
6269382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
62710689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6289382Seric 			continue;
6299382Seric 
63011414Seric 		/* handle Resent-... headers specially */
63111414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
63211414Seric 			continue;
63311414Seric 
6349382Seric 		p = h->h_value;
6359382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6369382Seric 		{
6379382Seric 			/* macro expand value if generated internally */
6389382Seric 			expand(p, buf, &buf[sizeof buf], e);
6399382Seric 			p = buf;
6409382Seric 			if (p == NULL || *p == '\0')
6419382Seric 				continue;
6429382Seric 		}
6439382Seric 
6449382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6459382Seric 		{
6469382Seric 			/* address field */
6479382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6489382Seric 
6499382Seric 			if (bitset(H_FROM, h->h_flags))
6509382Seric 				oldstyle = FALSE;
65110176Seric 			commaize(h, p, fp, oldstyle, m);
6529382Seric 		}
6539382Seric 		else
6549382Seric 		{
6559382Seric 			/* vanilla header line */
65612159Seric 			register char *nlp;
65712159Seric 
65812159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
65912159Seric 			while ((nlp = index(p, '\n')) != NULL)
66012159Seric 			{
66112159Seric 				*nlp = '\0';
66212159Seric 				(void) strcat(obuf, p);
66312159Seric 				*nlp = '\n';
66412159Seric 				putline(obuf, fp, m);
66512159Seric 				p = ++nlp;
66612161Seric 				obuf[0] = '\0';
66712159Seric 			}
66812159Seric 			(void) strcat(obuf, p);
66910176Seric 			putline(obuf, fp, m);
6709382Seric 		}
6719382Seric 	}
6729382Seric }
6739382Seric /*
6749382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6759382Seric **
6769382Seric **	Parameters:
6779382Seric **		h -- the header field to output.
6789382Seric **		p -- the value to put in it.
6799382Seric **		fp -- file to put it to.
6809382Seric **		oldstyle -- TRUE if this is an old style header.
6819382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6829382Seric **			don't transform the name at all.
6839382Seric **
6849382Seric **	Returns:
6859382Seric **		none.
6869382Seric **
6879382Seric **	Side Effects:
6889382Seric **		outputs "p" to file "fp".
6899382Seric */
6909382Seric 
69110176Seric commaize(h, p, fp, oldstyle, m)
6929382Seric 	register HDR *h;
6939382Seric 	register char *p;
6949382Seric 	FILE *fp;
6959382Seric 	bool oldstyle;
6969382Seric 	register MAILER *m;
6979382Seric {
6989382Seric 	register char *obp;
6999382Seric 	int opos;
7009382Seric 	bool firstone = TRUE;
70111157Seric 	char obuf[MAXLINE + 3];
7029382Seric 
7039382Seric 	/*
7049382Seric 	**  Output the address list translated by the
7059382Seric 	**  mailer and with commas.
7069382Seric 	*/
7079382Seric 
7089382Seric 	if (tTd(14, 2))
7099382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7109382Seric 
7119382Seric 	obp = obuf;
7129382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7139382Seric 	opos = strlen(h->h_field) + 2;
7149382Seric 	obp += opos;
7159382Seric 
7169382Seric 	/*
7179382Seric 	**  Run through the list of values.
7189382Seric 	*/
7199382Seric 
7209382Seric 	while (*p != '\0')
7219382Seric 	{
7229382Seric 		register char *name;
7239382Seric 		char savechar;
7249382Seric 		extern char *remotename();
7259382Seric 		extern char *DelimChar;		/* defined in prescan */
7269382Seric 
7279382Seric 		/*
7289382Seric 		**  Find the end of the name.  New style names
7299382Seric 		**  end with a comma, old style names end with
7309382Seric 		**  a space character.  However, spaces do not
7319382Seric 		**  necessarily delimit an old-style name -- at
7329382Seric 		**  signs mean keep going.
7339382Seric 		*/
7349382Seric 
7359382Seric 		/* find end of name */
7369382Seric 		while (isspace(*p) || *p == ',')
7379382Seric 			p++;
7389382Seric 		name = p;
7399382Seric 		for (;;)
7409382Seric 		{
7419382Seric 			char *oldp;
74216909Seric 			char pvpbuf[PSBUFSIZE];
7439382Seric 			extern bool isatword();
7449382Seric 			extern char **prescan();
7459382Seric 
74616909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7479382Seric 			p = DelimChar;
7489382Seric 
7499382Seric 			/* look to see if we have an at sign */
7509382Seric 			oldp = p;
7519382Seric 			while (*p != '\0' && isspace(*p))
7529382Seric 				p++;
7539382Seric 
7549382Seric 			if (*p != '@' && !isatword(p))
7559382Seric 			{
7569382Seric 				p = oldp;
7579382Seric 				break;
7589382Seric 			}
7599382Seric 			p += *p == '@' ? 1 : 2;
7609382Seric 			while (*p != '\0' && isspace(*p))
7619382Seric 				p++;
7629382Seric 		}
7639382Seric 		/* at the end of one complete name */
7649382Seric 
7659382Seric 		/* strip off trailing white space */
7669382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7679382Seric 			p--;
7689382Seric 		if (++p == name)
7699382Seric 			continue;
7709382Seric 		savechar = *p;
7719382Seric 		*p = '\0';
7729382Seric 
7739382Seric 		/* translate the name to be relative */
77410309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7759382Seric 		if (*name == '\0')
7769382Seric 		{
7779382Seric 			*p = savechar;
7789382Seric 			continue;
7799382Seric 		}
7809382Seric 
7819382Seric 		/* output the name with nice formatting */
7829382Seric 		opos += qstrlen(name);
7839382Seric 		if (!firstone)
7849382Seric 			opos += 2;
7859382Seric 		if (opos > 78 && !firstone)
7869382Seric 		{
78710178Seric 			(void) strcpy(obp, ",\n");
78810176Seric 			putline(obuf, fp, m);
7899382Seric 			obp = obuf;
7909382Seric 			(void) sprintf(obp, "        ");
79110161Seric 			opos = strlen(obp);
79210161Seric 			obp += opos;
79310161Seric 			opos += qstrlen(name);
7949382Seric 		}
7959382Seric 		else if (!firstone)
7969382Seric 		{
7979382Seric 			(void) sprintf(obp, ", ");
7989382Seric 			obp += 2;
7999382Seric 		}
8009382Seric 
8019382Seric 		/* strip off quote bits as we output */
80211157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8039382Seric 		{
8049382Seric 			if (bitset(0200, *name))
8059382Seric 				*obp++ = '\\';
8069382Seric 			*obp++ = *name++ & ~0200;
8079382Seric 		}
8089382Seric 		firstone = FALSE;
8099382Seric 		*p = savechar;
8109382Seric 	}
8119382Seric 	(void) strcpy(obp, "\n");
81210176Seric 	putline(obuf, fp, m);
8139382Seric }
8149382Seric /*
8159382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8169382Seric **
8179382Seric **	Parameters:
8189382Seric **		p -- word to check.
8199382Seric **
8209382Seric **	Returns:
8219382Seric **		TRUE -- if p is the word at.
8229382Seric **		FALSE -- otherwise.
8239382Seric **
8249382Seric **	Side Effects:
8259382Seric **		none.
8269382Seric */
8279382Seric 
8289382Seric bool
8299382Seric isatword(p)
8309382Seric 	register char *p;
8319382Seric {
8329382Seric 	extern char lower();
8339382Seric 
8349382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8359382Seric 	    p[2] != '\0' && isspace(p[2]))
8369382Seric 		return (TRUE);
8379382Seric 	return (FALSE);
8389382Seric }
839