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*25013Seric static char	SccsId[] = "@(#)headers.c	5.7 (Berkeley) 09/21/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 	{
11324941Seric 		if (CurEnv->e_from.q_paddr != NULL &&
11424941Seric 		    strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
11511414Seric 			return (hi->hi_flags);
11611414Seric 	}
11711414Seric 
11814784Seric 	/* delete default value for this header */
11914784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
12014784Seric 	{
12114784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12214784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12314784Seric 		    !bitset(H_FORCE, h->h_flags))
12414784Seric 			h->h_value = NULL;
12514784Seric 	}
12614784Seric 
12713012Seric 	/* create a new node */
12813012Seric 	h = (HDR *) xalloc(sizeof *h);
12913012Seric 	h->h_field = newstr(fname);
13013012Seric 	h->h_value = NULL;
13113012Seric 	h->h_link = NULL;
13223117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13313012Seric 	*hp = h;
1348066Seric 	h->h_flags = hi->hi_flags;
1354091Seric 	if (def)
1364091Seric 		h->h_flags |= H_DEFAULT;
1379059Seric 	if (cond)
1389059Seric 		h->h_flags |= H_CHECK;
1394091Seric 	if (h->h_value != NULL)
1409351Seric 		free((char *) h->h_value);
1418066Seric 	h->h_value = newstr(fvalue);
1424091Seric 
1435937Seric 	/* hack to see if this is a new format message */
1448095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1455937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1468089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1478089Seric 	{
1489342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1498089Seric 	}
1505937Seric 
1514091Seric 	return (h->h_flags);
1524091Seric }
1534091Seric /*
1546980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1556980Seric **
1566980Seric **	This bypasses the special checking of chompheader.
1576980Seric **
1586980Seric **	Parameters:
1596980Seric **		field -- the name of the header field.
1606980Seric **		value -- the value of the field.  It must be lower-cased.
1616980Seric **		e -- the envelope to add them to.
1626980Seric **
1636980Seric **	Returns:
1646980Seric **		none.
1656980Seric **
1666980Seric **	Side Effects:
1676980Seric **		adds the field on the list of headers for this envelope.
1686980Seric */
1696980Seric 
1706980Seric addheader(field, value, e)
1716980Seric 	char *field;
1726980Seric 	char *value;
1736980Seric 	ENVELOPE *e;
1746980Seric {
1756980Seric 	register HDR *h;
1766980Seric 	register struct hdrinfo *hi;
1776980Seric 	HDR **hp;
1786980Seric 
1796980Seric 	/* find info struct */
1806980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1816980Seric 	{
1826980Seric 		if (strcmp(field, hi->hi_field) == 0)
1836980Seric 			break;
1846980Seric 	}
1856980Seric 
1866980Seric 	/* find current place in list -- keep back pointer? */
1876980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1886980Seric 	{
1896980Seric 		if (strcmp(field, h->h_field) == 0)
1906980Seric 			break;
1916980Seric 	}
1926980Seric 
1936980Seric 	/* allocate space for new header */
1946980Seric 	h = (HDR *) xalloc(sizeof *h);
1956980Seric 	h->h_field = field;
1966980Seric 	h->h_value = newstr(value);
1977368Seric 	h->h_link = *hp;
1986980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19910689Seric 	clrbitmap(h->h_mflags);
2006980Seric 	*hp = h;
2016980Seric }
2026980Seric /*
2034091Seric **  HVALUE -- return value of a header.
2044091Seric **
2054091Seric **	Only "real" fields (i.e., ones that have not been supplied
2064091Seric **	as a default) are used.
2074091Seric **
2084091Seric **	Parameters:
2094091Seric **		field -- the field name.
2104091Seric **
2114091Seric **	Returns:
2124091Seric **		pointer to the value part.
2134091Seric **		NULL if not found.
2144091Seric **
2154091Seric **	Side Effects:
2169382Seric **		none.
2174091Seric */
2184091Seric 
2194091Seric char *
2204091Seric hvalue(field)
2214091Seric 	char *field;
2224091Seric {
2234091Seric 	register HDR *h;
2244091Seric 
2256908Seric 	for (h = CurEnv->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 #ifdef DEBUG
2859382Seric 	if (tTd(32, 1))
2869382Seric 		printf("----- collected header -----\n");
2879382Seric #endif DEBUG
2889382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2897783Seric 	{
2909382Seric #ifdef DEBUG
2917783Seric 		extern char *capitalize();
2927783Seric 
2939382Seric 		if (tTd(32, 1))
2947783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2959382Seric #endif DEBUG
29611414Seric 		/* count the number of times it has been processed */
2979382Seric 		if (bitset(H_TRACE, h->h_flags))
2989382Seric 			hopcnt++;
29911414Seric 
30011414Seric 		/* send to this person if we so desire */
30111414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30211414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30311414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
30411414Seric 		{
30511414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
30611414Seric 		}
30711414Seric 
30811414Seric 		/* log the message-id */
30911290Seric #ifdef LOG
31013103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31111299Seric 		    strcmp(h->h_field, "message-id") == 0)
31211290Seric 		{
31311290Seric 			char buf[MAXNAME];
31411290Seric 
31511290Seric 			p = h->h_value;
31611290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31711290Seric 			{
31811290Seric 				expand(p, buf, &buf[sizeof buf], e);
31911290Seric 				p = buf;
32011290Seric 			}
32111290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
32211290Seric 		}
32311290Seric #endif LOG
3249382Seric 	}
3259382Seric #ifdef DEBUG
3269382Seric 	if (tTd(32, 1))
3277783Seric 		printf("----------------------------\n");
3289382Seric #endif DEBUG
3297783Seric 
3309382Seric 	/* store hop count */
3319382Seric 	if (hopcnt > e->e_hopcount)
3329382Seric 		e->e_hopcount = hopcnt;
3339382Seric 
3347783Seric 	/* message priority */
3359382Seric 	p = hvalue("precedence");
3369382Seric 	if (p != NULL)
3379382Seric 		e->e_class = priencode(p);
3387783Seric 	if (!QueueRun)
339*25013Seric 		e->e_msgpriority = e->e_msgsize
34024981Seric 				 - e->e_class * WkClassFact
34124981Seric 				 + e->e_nrcpts * WkRecipFact;
3427783Seric 
3438066Seric 	/* return receipt to */
3448066Seric 	p = hvalue("return-receipt-to");
3457783Seric 	if (p != NULL)
3469382Seric 		e->e_receiptto = p;
3477783Seric 
3488253Seric 	/* errors to */
3498253Seric 	p = hvalue("errors-to");
3508253Seric 	if (p != NULL)
3519620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3528253Seric 
3537783Seric 	/* from person */
3549285Seric 	if (OpMode == MD_ARPAFTP)
3558066Seric 	{
3568066Seric 		register struct hdrinfo *hi = HdrInfo;
3577783Seric 
3588066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3598066Seric 		{
3608066Seric 			if (bitset(H_FROM, hi->hi_flags))
3618066Seric 				p = hvalue(hi->hi_field);
3628066Seric 		}
3638066Seric 		if (p != NULL)
3649382Seric 			setsender(p);
3658066Seric 	}
3668066Seric 
3677783Seric 	/* full name of from person */
3687783Seric 	p = hvalue("full-name");
3697783Seric 	if (p != NULL)
3709382Seric 		define('x', p, e);
3717783Seric 
3727783Seric 	/* date message originated */
3737783Seric 	p = hvalue("posted-date");
3747783Seric 	if (p == NULL)
3757783Seric 		p = hvalue("date");
3767783Seric 	if (p != NULL)
3777783Seric 	{
3789382Seric 		define('a', p, e);
3797783Seric 		/* we don't have a good way to do canonical conversion ....
3809382Seric 		define('d', newstr(arpatounix(p)), e);
3817783Seric 		.... so we will ignore the problem for the time being */
3827783Seric 	}
38311290Seric 
38411290Seric 	/*
38511290Seric 	**  Log collection information.
38611290Seric 	*/
38711290Seric 
38811290Seric # ifdef LOG
38911299Seric 	if (!QueueRun && LogLevel > 1)
39011290Seric 	{
39111290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
39211290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
39311290Seric 		       CurEnv->e_class);
39411290Seric 	}
39511290Seric # endif LOG
3967783Seric }
3977783Seric /*
3987783Seric **  PRIENCODE -- encode external priority names into internal values.
3997783Seric **
4007783Seric **	Parameters:
4017783Seric **		p -- priority in ascii.
4027783Seric **
4037783Seric **	Returns:
4047783Seric **		priority as a numeric level.
4057783Seric **
4067783Seric **	Side Effects:
4077783Seric **		none.
4087783Seric */
4097783Seric 
4107783Seric priencode(p)
4117783Seric 	char *p;
4127783Seric {
4138253Seric 	register int i;
4147783Seric 	extern bool sameword();
4157783Seric 
4168253Seric 	for (i = 0; i < NumPriorities; i++)
4177783Seric 	{
4188253Seric 		if (sameword(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 # ifdef DEBUG
4717783Seric 	if (tTd(33, 1))
4727890Seric 		printf("crackaddr(%s)\n", addr);
4737783Seric # endif DEBUG
4747783Seric 
47523099Seric 	(void) strcpy(buf, "");
4767783Seric 	rhs = NULL;
4777783Seric 
4788082Seric 	/* strip leading spaces */
4798082Seric 	while (*addr != '\0' && isspace(*addr))
4808082Seric 		addr++;
4818082Seric 
4827783Seric 	/*
4837783Seric 	**  See if we have anything in angle brackets.  If so, that is
4847783Seric 	**  the address part, and the rest is the comment.
4857783Seric 	*/
4867783Seric 
4877890Seric 	p = index(addr, '<');
4887783Seric 	if (p != NULL)
4897783Seric 	{
4907890Seric 		/* copy the beginning of the addr field to the buffer */
4917783Seric 		*p = '\0';
49223099Seric 		(void) strcpy(buf, addr);
49323099Seric 		(void) strcat(buf, "<");
4948082Seric 		*p++ = '<';
4957783Seric 
4968082Seric 		/* skip spaces */
4978082Seric 		while (isspace(*p))
4988082Seric 			p++;
4998082Seric 
5007783Seric 		/* find the matching right angle bracket */
5018082Seric 		addr = p;
5027783Seric 		for (i = 0; *p != '\0'; p++)
5037783Seric 		{
5047783Seric 			switch (*p)
5057783Seric 			{
5067783Seric 			  case '<':
5077783Seric 				i++;
5087783Seric 				break;
5097783Seric 
5107783Seric 			  case '>':
5117783Seric 				i--;
5127783Seric 				break;
5137783Seric 			}
5147783Seric 			if (i < 0)
5157783Seric 				break;
5167783Seric 		}
5177783Seric 
5187783Seric 		/* p now points to the closing quote (or a null byte) */
5197783Seric 		if (*p != '\0')
5207783Seric 		{
5217783Seric 			/* make rhs point to the extra stuff at the end */
5227783Seric 			rhs = p;
5237783Seric 			*p++ = '\0';
5247783Seric 		}
5257783Seric 	}
5267783Seric 
5277783Seric 	/*
5287944Seric 	**  Now parse the real address part.  "addr" points to the (null
5297783Seric 	**  terminated) version of what we are inerested in; rhs points
5307783Seric 	**  to the extra stuff at the end of the line, if any.
5317783Seric 	*/
5327783Seric 
5337890Seric 	p = addr;
5347783Seric 
5357783Seric 	/* now strip out comments */
5367783Seric 	bp = &buf[strlen(buf)];
5377783Seric 	gotaddr = FALSE;
5387783Seric 	for (; *p != '\0'; p++)
5397783Seric 	{
5407783Seric 		if (*p == '(')
5417783Seric 		{
5427783Seric 			/* copy to matching close paren */
5437783Seric 			*bp++ = *p++;
5447783Seric 			for (i = 0; *p != '\0'; p++)
5457783Seric 			{
5467783Seric 				*bp++ = *p;
5477783Seric 				switch (*p)
5487783Seric 				{
5497783Seric 				  case '(':
5507783Seric 					i++;
5517783Seric 					break;
5527783Seric 
5537783Seric 				  case ')':
5547783Seric 					i--;
5557783Seric 					break;
5567783Seric 				}
5577783Seric 				if (i < 0)
5587783Seric 					break;
5597783Seric 			}
5607783Seric 			continue;
5617783Seric 		}
5627783Seric 
5637783Seric 		/*
5647783Seric 		**  If this is the first "real" character we have seen,
5657783Seric 		**  then we put the "$g" in the buffer now.
5667783Seric 		*/
5677783Seric 
5687783Seric 		if (isspace(*p))
5697783Seric 			*bp++ = *p;
5707783Seric 		else if (!gotaddr)
5717783Seric 		{
57223099Seric 			(void) strcpy(bp, "\001g");
5737783Seric 			bp += 2;
5747783Seric 			gotaddr = TRUE;
5757783Seric 		}
5767783Seric 	}
5777783Seric 
5787944Seric 	/* hack, hack.... strip trailing blanks */
5797944Seric 	do
5807944Seric 	{
5817944Seric 		*bp-- = '\0';
5827944Seric 	} while (isspace(*bp));
5837944Seric 	bp++;
5847783Seric 
5857944Seric 	/* put any right hand side back on */
5867783Seric 	if (rhs != NULL)
5877783Seric 	{
5887783Seric 		*rhs = '>';
58923099Seric 		(void) strcpy(bp, rhs);
5907783Seric 	}
5917783Seric 
5927783Seric # ifdef DEBUG
5937783Seric 	if (tTd(33, 1))
5947944Seric 		printf("crackaddr=>`%s'\n", buf);
5957783Seric # endif DEBUG
5967783Seric 
5977783Seric 	return (buf);
5987783Seric }
5999382Seric /*
6009382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6019382Seric **
6029382Seric **	Parameters:
6039382Seric **		fp -- file to put it on.
6049382Seric **		m -- mailer to use.
6059382Seric **		e -- envelope to use.
6069382Seric **
6079382Seric **	Returns:
6089382Seric **		none.
6099382Seric **
6109382Seric **	Side Effects:
6119382Seric **		none.
6129382Seric */
6139382Seric 
61410176Seric putheader(fp, m, e)
6159382Seric 	register FILE *fp;
6169382Seric 	register MAILER *m;
6179382Seric 	register ENVELOPE *e;
6189382Seric {
6199382Seric 	char buf[BUFSIZ];
6209382Seric 	register HDR *h;
6219382Seric 	extern char *arpadate();
6229382Seric 	extern char *capitalize();
6239382Seric 	char obuf[MAXLINE];
6249382Seric 
6259382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6269382Seric 	{
6279382Seric 		register char *p;
62810689Seric 		extern bool bitintersect();
6299382Seric 
6309382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
63110689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6329382Seric 			continue;
6339382Seric 
63411414Seric 		/* handle Resent-... headers specially */
63511414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
63611414Seric 			continue;
63711414Seric 
6389382Seric 		p = h->h_value;
6399382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6409382Seric 		{
6419382Seric 			/* macro expand value if generated internally */
6429382Seric 			expand(p, buf, &buf[sizeof buf], e);
6439382Seric 			p = buf;
6449382Seric 			if (p == NULL || *p == '\0')
6459382Seric 				continue;
6469382Seric 		}
6479382Seric 
6489382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6499382Seric 		{
6509382Seric 			/* address field */
6519382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6529382Seric 
6539382Seric 			if (bitset(H_FROM, h->h_flags))
6549382Seric 				oldstyle = FALSE;
65510176Seric 			commaize(h, p, fp, oldstyle, m);
6569382Seric 		}
6579382Seric 		else
6589382Seric 		{
6599382Seric 			/* vanilla header line */
66012159Seric 			register char *nlp;
66112159Seric 
66212159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
66312159Seric 			while ((nlp = index(p, '\n')) != NULL)
66412159Seric 			{
66512159Seric 				*nlp = '\0';
66612159Seric 				(void) strcat(obuf, p);
66712159Seric 				*nlp = '\n';
66812159Seric 				putline(obuf, fp, m);
66912159Seric 				p = ++nlp;
67012161Seric 				obuf[0] = '\0';
67112159Seric 			}
67212159Seric 			(void) strcat(obuf, p);
67310176Seric 			putline(obuf, fp, m);
6749382Seric 		}
6759382Seric 	}
6769382Seric }
6779382Seric /*
6789382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6799382Seric **
6809382Seric **	Parameters:
6819382Seric **		h -- the header field to output.
6829382Seric **		p -- the value to put in it.
6839382Seric **		fp -- file to put it to.
6849382Seric **		oldstyle -- TRUE if this is an old style header.
6859382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6869382Seric **			don't transform the name at all.
6879382Seric **
6889382Seric **	Returns:
6899382Seric **		none.
6909382Seric **
6919382Seric **	Side Effects:
6929382Seric **		outputs "p" to file "fp".
6939382Seric */
6949382Seric 
69510176Seric commaize(h, p, fp, oldstyle, m)
6969382Seric 	register HDR *h;
6979382Seric 	register char *p;
6989382Seric 	FILE *fp;
6999382Seric 	bool oldstyle;
7009382Seric 	register MAILER *m;
7019382Seric {
7029382Seric 	register char *obp;
7039382Seric 	int opos;
7049382Seric 	bool firstone = TRUE;
70511157Seric 	char obuf[MAXLINE + 3];
7069382Seric 
7079382Seric 	/*
7089382Seric 	**  Output the address list translated by the
7099382Seric 	**  mailer and with commas.
7109382Seric 	*/
7119382Seric 
7129382Seric # ifdef DEBUG
7139382Seric 	if (tTd(14, 2))
7149382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7159382Seric # endif DEBUG
7169382Seric 
7179382Seric 	obp = obuf;
7189382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7199382Seric 	opos = strlen(h->h_field) + 2;
7209382Seric 	obp += opos;
7219382Seric 
7229382Seric 	/*
7239382Seric 	**  Run through the list of values.
7249382Seric 	*/
7259382Seric 
7269382Seric 	while (*p != '\0')
7279382Seric 	{
7289382Seric 		register char *name;
7299382Seric 		char savechar;
7309382Seric 		extern char *remotename();
7319382Seric 		extern char *DelimChar;		/* defined in prescan */
7329382Seric 
7339382Seric 		/*
7349382Seric 		**  Find the end of the name.  New style names
7359382Seric 		**  end with a comma, old style names end with
7369382Seric 		**  a space character.  However, spaces do not
7379382Seric 		**  necessarily delimit an old-style name -- at
7389382Seric 		**  signs mean keep going.
7399382Seric 		*/
7409382Seric 
7419382Seric 		/* find end of name */
7429382Seric 		while (isspace(*p) || *p == ',')
7439382Seric 			p++;
7449382Seric 		name = p;
7459382Seric 		for (;;)
7469382Seric 		{
7479382Seric 			char *oldp;
74816909Seric 			char pvpbuf[PSBUFSIZE];
7499382Seric 			extern bool isatword();
7509382Seric 			extern char **prescan();
7519382Seric 
75216909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7539382Seric 			p = DelimChar;
7549382Seric 
7559382Seric 			/* look to see if we have an at sign */
7569382Seric 			oldp = p;
7579382Seric 			while (*p != '\0' && isspace(*p))
7589382Seric 				p++;
7599382Seric 
7609382Seric 			if (*p != '@' && !isatword(p))
7619382Seric 			{
7629382Seric 				p = oldp;
7639382Seric 				break;
7649382Seric 			}
7659382Seric 			p += *p == '@' ? 1 : 2;
7669382Seric 			while (*p != '\0' && isspace(*p))
7679382Seric 				p++;
7689382Seric 		}
7699382Seric 		/* at the end of one complete name */
7709382Seric 
7719382Seric 		/* strip off trailing white space */
7729382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7739382Seric 			p--;
7749382Seric 		if (++p == name)
7759382Seric 			continue;
7769382Seric 		savechar = *p;
7779382Seric 		*p = '\0';
7789382Seric 
7799382Seric 		/* translate the name to be relative */
78010309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7819382Seric 		if (*name == '\0')
7829382Seric 		{
7839382Seric 			*p = savechar;
7849382Seric 			continue;
7859382Seric 		}
7869382Seric 
7879382Seric 		/* output the name with nice formatting */
7889382Seric 		opos += qstrlen(name);
7899382Seric 		if (!firstone)
7909382Seric 			opos += 2;
7919382Seric 		if (opos > 78 && !firstone)
7929382Seric 		{
79310178Seric 			(void) strcpy(obp, ",\n");
79410176Seric 			putline(obuf, fp, m);
7959382Seric 			obp = obuf;
7969382Seric 			(void) sprintf(obp, "        ");
79710161Seric 			opos = strlen(obp);
79810161Seric 			obp += opos;
79910161Seric 			opos += qstrlen(name);
8009382Seric 		}
8019382Seric 		else if (!firstone)
8029382Seric 		{
8039382Seric 			(void) sprintf(obp, ", ");
8049382Seric 			obp += 2;
8059382Seric 		}
8069382Seric 
8079382Seric 		/* strip off quote bits as we output */
80811157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8099382Seric 		{
8109382Seric 			if (bitset(0200, *name))
8119382Seric 				*obp++ = '\\';
8129382Seric 			*obp++ = *name++ & ~0200;
8139382Seric 		}
8149382Seric 		firstone = FALSE;
8159382Seric 		*p = savechar;
8169382Seric 	}
8179382Seric 	(void) strcpy(obp, "\n");
81810176Seric 	putline(obuf, fp, m);
8199382Seric }
8209382Seric /*
8219382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8229382Seric **
8239382Seric **	Parameters:
8249382Seric **		p -- word to check.
8259382Seric **
8269382Seric **	Returns:
8279382Seric **		TRUE -- if p is the word at.
8289382Seric **		FALSE -- otherwise.
8299382Seric **
8309382Seric **	Side Effects:
8319382Seric **		none.
8329382Seric */
8339382Seric 
8349382Seric bool
8359382Seric isatword(p)
8369382Seric 	register char *p;
8379382Seric {
8389382Seric 	extern char lower();
8399382Seric 
8409382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8419382Seric 	    p[2] != '\0' && isspace(p[2]))
8429382Seric 		return (TRUE);
8439382Seric 	return (FALSE);
8449382Seric }
845