1*22706Sdist /*
2*22706Sdist **  Sendmail
3*22706Sdist **  Copyright (c) 1983  Eric P. Allman
4*22706Sdist **  Berkeley, California
5*22706Sdist **
6*22706Sdist **  Copyright (c) 1983 Regents of the University of California.
7*22706Sdist **  All rights reserved.  The Berkeley software License Agreement
8*22706Sdist **  specifies the terms and conditions for redistribution.
9*22706Sdist */
10*22706Sdist 
11*22706Sdist #ifndef lint
12*22706Sdist static char	SccsId[] = "@(#)headers.c	5.1 (Berkeley) 06/07/85";
13*22706Sdist #endif not lint
14*22706Sdist 
154091Seric # include <errno.h>
164091Seric # include "sendmail.h"
174091Seric 
18*22706Sdist SCCSID(@(#)headers.c	5.1		06/07/85);
194091Seric 
204091Seric /*
214091Seric **  CHOMPHEADER -- process and save a header line.
224091Seric **
234091Seric **	Called by collect and by readcf to deal with header lines.
244091Seric **
254091Seric **	Parameters:
264091Seric **		line -- header as a text line.
274091Seric **		def -- if set, this is a default value.
284091Seric **
294091Seric **	Returns:
304091Seric **		flags for this header.
314091Seric **
324091Seric **	Side Effects:
334091Seric **		The header is saved on the header list.
344319Seric **		Contents of 'line' are destroyed.
354091Seric */
364091Seric 
374091Seric chompheader(line, def)
384091Seric 	char *line;
394091Seric 	bool def;
404091Seric {
414091Seric 	register char *p;
424091Seric 	register HDR *h;
434091Seric 	HDR **hp;
444091Seric 	char *fname;
454091Seric 	char *fvalue;
464091Seric 	struct hdrinfo *hi;
479059Seric 	bool cond = FALSE;
4810689Seric 	BITMAP mopts;
497890Seric 	extern char *crackaddr();
504091Seric 
517677Seric # ifdef DEBUG
527677Seric 	if (tTd(31, 6))
537677Seric 		printf("chompheader: %s\n", line);
547677Seric # endif DEBUG
557677Seric 
564627Seric 	/* strip off options */
5710689Seric 	clrbitmap(mopts);
584627Seric 	p = line;
594627Seric 	if (*p == '?')
604627Seric 	{
614627Seric 		/* have some */
624627Seric 		register char *q = index(p + 1, *p);
634627Seric 
644627Seric 		if (q != NULL)
654627Seric 		{
664627Seric 			*q++ = '\0';
6710689Seric 			while (*++p != '\0')
6810689Seric 				setbitn(*p, mopts);
694627Seric 			p = q;
704627Seric 		}
714627Seric 		else
724627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
739059Seric 		cond = TRUE;
744627Seric 	}
754627Seric 
764091Seric 	/* find canonical name */
774627Seric 	fname = p;
784627Seric 	p = index(p, ':');
7910118Seric 	if (p == NULL)
8010118Seric 	{
8110118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
8210118Seric 		return (0);
8310118Seric 	}
844091Seric 	fvalue = &p[1];
854091Seric 	while (isspace(*--p))
864091Seric 		continue;
874091Seric 	*++p = '\0';
884091Seric 	makelower(fname);
894091Seric 
904091Seric 	/* strip field value on front */
914091Seric 	if (*fvalue == ' ')
924091Seric 		fvalue++;
934091Seric 
944091Seric 	/* see if it is a known type */
954091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
964091Seric 	{
974091Seric 		if (strcmp(hi->hi_field, fname) == 0)
984091Seric 			break;
994091Seric 	}
1004091Seric 
10111414Seric 	/* see if this is a resent message */
10211930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10311414Seric 		CurEnv->e_flags |= EF_RESENT;
10411414Seric 
1054091Seric 	/* if this means "end of header" quit now */
1064091Seric 	if (bitset(H_EOH, hi->hi_flags))
1074091Seric 		return (hi->hi_flags);
1084091Seric 
10911414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
11014784Seric 	p = "resent-from";
11114784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
11214784Seric 		p += 7;
11314784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
11411414Seric 	{
11514788Seric 		ADDRESS fromaddr;
11614788Seric 
11714784Seric 		if (strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
11811414Seric 			return (hi->hi_flags);
11911414Seric 	}
12011414Seric 
12114784Seric 	/* delete default value for this header */
12214784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
12314784Seric 	{
12414784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12514784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12614784Seric 		    !bitset(H_FORCE, h->h_flags))
12714784Seric 			h->h_value = NULL;
12814784Seric 	}
12914784Seric 
13013012Seric 	/* create a new node */
13113012Seric 	h = (HDR *) xalloc(sizeof *h);
13213012Seric 	h->h_field = newstr(fname);
13313012Seric 	h->h_value = NULL;
13413012Seric 	h->h_link = NULL;
13513012Seric 	bcopy(mopts, h->h_mflags, sizeof mopts);
13613012Seric 	*hp = h;
1378066Seric 	h->h_flags = hi->hi_flags;
1384091Seric 	if (def)
1394091Seric 		h->h_flags |= H_DEFAULT;
1409059Seric 	if (cond)
1419059Seric 		h->h_flags |= H_CHECK;
1424091Seric 	if (h->h_value != NULL)
1439351Seric 		free((char *) h->h_value);
1448066Seric 	h->h_value = newstr(fvalue);
1454091Seric 
1465937Seric 	/* hack to see if this is a new format message */
1478095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1485937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1498089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1508089Seric 	{
1519342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1528089Seric 	}
1535937Seric 
1544091Seric 	return (h->h_flags);
1554091Seric }
1564091Seric /*
1576980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1586980Seric **
1596980Seric **	This bypasses the special checking of chompheader.
1606980Seric **
1616980Seric **	Parameters:
1626980Seric **		field -- the name of the header field.
1636980Seric **		value -- the value of the field.  It must be lower-cased.
1646980Seric **		e -- the envelope to add them to.
1656980Seric **
1666980Seric **	Returns:
1676980Seric **		none.
1686980Seric **
1696980Seric **	Side Effects:
1706980Seric **		adds the field on the list of headers for this envelope.
1716980Seric */
1726980Seric 
1736980Seric addheader(field, value, e)
1746980Seric 	char *field;
1756980Seric 	char *value;
1766980Seric 	ENVELOPE *e;
1776980Seric {
1786980Seric 	register HDR *h;
1796980Seric 	register struct hdrinfo *hi;
1806980Seric 	HDR **hp;
1816980Seric 
1826980Seric 	/* find info struct */
1836980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1846980Seric 	{
1856980Seric 		if (strcmp(field, hi->hi_field) == 0)
1866980Seric 			break;
1876980Seric 	}
1886980Seric 
1896980Seric 	/* find current place in list -- keep back pointer? */
1906980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1916980Seric 	{
1926980Seric 		if (strcmp(field, h->h_field) == 0)
1936980Seric 			break;
1946980Seric 	}
1956980Seric 
1966980Seric 	/* allocate space for new header */
1976980Seric 	h = (HDR *) xalloc(sizeof *h);
1986980Seric 	h->h_field = field;
1996980Seric 	h->h_value = newstr(value);
2007368Seric 	h->h_link = *hp;
2016980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20210689Seric 	clrbitmap(h->h_mflags);
2036980Seric 	*hp = h;
2046980Seric }
2056980Seric /*
2064091Seric **  HVALUE -- return value of a header.
2074091Seric **
2084091Seric **	Only "real" fields (i.e., ones that have not been supplied
2094091Seric **	as a default) are used.
2104091Seric **
2114091Seric **	Parameters:
2124091Seric **		field -- the field name.
2134091Seric **
2144091Seric **	Returns:
2154091Seric **		pointer to the value part.
2164091Seric **		NULL if not found.
2174091Seric **
2184091Seric **	Side Effects:
2199382Seric **		none.
2204091Seric */
2214091Seric 
2224091Seric char *
2234091Seric hvalue(field)
2244091Seric 	char *field;
2254091Seric {
2264091Seric 	register HDR *h;
2274091Seric 
2286908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2294091Seric 	{
2304091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2314091Seric 			return (h->h_value);
2324091Seric 	}
2334091Seric 	return (NULL);
2344091Seric }
2354091Seric /*
2364091Seric **  ISHEADER -- predicate telling if argument is a header.
2374091Seric **
2384319Seric **	A line is a header if it has a single word followed by
2394319Seric **	optional white space followed by a colon.
2404319Seric **
2414091Seric **	Parameters:
2424091Seric **		s -- string to check for possible headerness.
2434091Seric **
2444091Seric **	Returns:
2454091Seric **		TRUE if s is a header.
2464091Seric **		FALSE otherwise.
2474091Seric **
2484091Seric **	Side Effects:
2494091Seric **		none.
2504091Seric */
2514091Seric 
2524091Seric bool
2534091Seric isheader(s)
2544091Seric 	register char *s;
2554091Seric {
2569382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2574091Seric 		s++;
2589382Seric 
2599382Seric 	/* following technically violates RFC822 */
2604091Seric 	while (isspace(*s))
2614091Seric 		s++;
2629382Seric 
2634091Seric 	return (*s == ':');
2644091Seric }
2655919Seric /*
2667783Seric **  EATHEADER -- run through the stored header and extract info.
2677783Seric **
2687783Seric **	Parameters:
2699382Seric **		e -- the envelope to process.
2707783Seric **
2717783Seric **	Returns:
2727783Seric **		none.
2737783Seric **
2747783Seric **	Side Effects:
2757783Seric **		Sets a bunch of global variables from information
2769382Seric **			in the collected header.
2779382Seric **		Aborts the message if the hop count is exceeded.
2787783Seric */
2797783Seric 
2809382Seric eatheader(e)
2819382Seric 	register ENVELOPE *e;
2827783Seric {
2837783Seric 	register HDR *h;
2847783Seric 	register char *p;
2859382Seric 	int hopcnt = 0;
2867783Seric 
2879382Seric #ifdef DEBUG
2889382Seric 	if (tTd(32, 1))
2899382Seric 		printf("----- collected header -----\n");
2909382Seric #endif DEBUG
2919382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2927783Seric 	{
2939382Seric #ifdef DEBUG
2947783Seric 		extern char *capitalize();
2957783Seric 
2969382Seric 		if (tTd(32, 1))
2977783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2989382Seric #endif DEBUG
29911414Seric 		/* count the number of times it has been processed */
3009382Seric 		if (bitset(H_TRACE, h->h_flags))
3019382Seric 			hopcnt++;
30211414Seric 
30311414Seric 		/* send to this person if we so desire */
30411414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30511414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30611414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
30711414Seric 		{
30811414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
30911414Seric 		}
31011414Seric 
31111414Seric 		/* log the message-id */
31211290Seric #ifdef LOG
31313103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31411299Seric 		    strcmp(h->h_field, "message-id") == 0)
31511290Seric 		{
31611290Seric 			char buf[MAXNAME];
31711290Seric 
31811290Seric 			p = h->h_value;
31911290Seric 			if (bitset(H_DEFAULT, h->h_flags))
32011290Seric 			{
32111290Seric 				expand(p, buf, &buf[sizeof buf], e);
32211290Seric 				p = buf;
32311290Seric 			}
32411290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
32511290Seric 		}
32611290Seric #endif LOG
3279382Seric 	}
3289382Seric #ifdef DEBUG
3299382Seric 	if (tTd(32, 1))
3307783Seric 		printf("----------------------------\n");
3319382Seric #endif DEBUG
3327783Seric 
3339382Seric 	/* store hop count */
3349382Seric 	if (hopcnt > e->e_hopcount)
3359382Seric 		e->e_hopcount = hopcnt;
3369382Seric 
3377783Seric 	/* message priority */
3389382Seric 	p = hvalue("precedence");
3399382Seric 	if (p != NULL)
3409382Seric 		e->e_class = priencode(p);
3417783Seric 	if (!QueueRun)
34221059Seric 		e->e_msgpriority = e->e_msgsize + e->e_ctime - e->e_class * WKPRIFACT;
3437783Seric 
3448066Seric 	/* return receipt to */
3458066Seric 	p = hvalue("return-receipt-to");
3467783Seric 	if (p != NULL)
3479382Seric 		e->e_receiptto = p;
3487783Seric 
3498253Seric 	/* errors to */
3508253Seric 	p = hvalue("errors-to");
3518253Seric 	if (p != NULL)
3529620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3538253Seric 
3547783Seric 	/* from person */
3559285Seric 	if (OpMode == MD_ARPAFTP)
3568066Seric 	{
3578066Seric 		register struct hdrinfo *hi = HdrInfo;
3587783Seric 
3598066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3608066Seric 		{
3618066Seric 			if (bitset(H_FROM, hi->hi_flags))
3628066Seric 				p = hvalue(hi->hi_field);
3638066Seric 		}
3648066Seric 		if (p != NULL)
3659382Seric 			setsender(p);
3668066Seric 	}
3678066Seric 
3687783Seric 	/* full name of from person */
3697783Seric 	p = hvalue("full-name");
3707783Seric 	if (p != NULL)
3719382Seric 		define('x', p, e);
3727783Seric 
3737783Seric 	/* date message originated */
3747783Seric 	p = hvalue("posted-date");
3757783Seric 	if (p == NULL)
3767783Seric 		p = hvalue("date");
3777783Seric 	if (p != NULL)
3787783Seric 	{
3799382Seric 		define('a', p, e);
3807783Seric 		/* we don't have a good way to do canonical conversion ....
3819382Seric 		define('d', newstr(arpatounix(p)), e);
3827783Seric 		.... so we will ignore the problem for the time being */
3837783Seric 	}
38411290Seric 
38511290Seric 	/*
38611290Seric 	**  Log collection information.
38711290Seric 	*/
38811290Seric 
38911290Seric # ifdef LOG
39011299Seric 	if (!QueueRun && LogLevel > 1)
39111290Seric 	{
39211290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
39311290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
39411290Seric 		       CurEnv->e_class);
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 	extern bool sameword();
4167783Seric 
4178253Seric 	for (i = 0; i < NumPriorities; i++)
4187783Seric 	{
4198253Seric 		if (sameword(p, Priorities[i].pri_name))
4208253Seric 			return (Priorities[i].pri_val);
4217783Seric 	}
4228253Seric 
4238253Seric 	/* unknown priority */
4248253Seric 	return (0);
4257783Seric }
4267783Seric /*
4277890Seric **  CRACKADDR -- parse an address and turn it into a macro
4287783Seric **
4297783Seric **	This doesn't actually parse the address -- it just extracts
4307783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4317783Seric **	and isn't even guaranteed to leave something syntactically
4327783Seric **	identical to what it started with.  However, it does leave
4337783Seric **	something semantically identical.
4347783Seric **
4357783Seric **	The process is kind of strange.  There are a number of
4367783Seric **	interesting cases:
4377783Seric **		1.  comment <address> comment	==> comment <$g> comment
4387783Seric **		2.  address			==> address
4397783Seric **		3.  address (comment)		==> $g (comment)
4407783Seric **		4.  (comment) address		==> (comment) $g
4417783Seric **	And then there are the hard cases....
4427783Seric **		5.  add (comment) ress		==> $g (comment)
4437783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4447783Seric **		7.    .... etc ....
4457783Seric **
4467783Seric **	Parameters:
4477890Seric **		addr -- the address to be cracked.
4487783Seric **
4497783Seric **	Returns:
4507783Seric **		a pointer to the new version.
4517783Seric **
4527783Seric **	Side Effects:
4537890Seric **		none.
4547783Seric **
4557783Seric **	Warning:
4567783Seric **		The return value is saved in local storage and should
4577783Seric **		be copied if it is to be reused.
4587783Seric */
4597783Seric 
4607783Seric char *
4617890Seric crackaddr(addr)
4627890Seric 	register char *addr;
4637783Seric {
4647783Seric 	register char *p;
4657783Seric 	register int i;
4667783Seric 	static char buf[MAXNAME];
4677783Seric 	char *rhs;
4687783Seric 	bool gotaddr;
4697783Seric 	register char *bp;
4707783Seric 
4717783Seric # ifdef DEBUG
4727783Seric 	if (tTd(33, 1))
4737890Seric 		printf("crackaddr(%s)\n", addr);
4747783Seric # endif DEBUG
4757783Seric 
4767783Seric 	strcpy(buf, "");
4777783Seric 	rhs = NULL;
4787783Seric 
4798082Seric 	/* strip leading spaces */
4808082Seric 	while (*addr != '\0' && isspace(*addr))
4818082Seric 		addr++;
4828082Seric 
4837783Seric 	/*
4847783Seric 	**  See if we have anything in angle brackets.  If so, that is
4857783Seric 	**  the address part, and the rest is the comment.
4867783Seric 	*/
4877783Seric 
4887890Seric 	p = index(addr, '<');
4897783Seric 	if (p != NULL)
4907783Seric 	{
4917890Seric 		/* copy the beginning of the addr field to the buffer */
4927783Seric 		*p = '\0';
4937890Seric 		strcpy(buf, addr);
4947783Seric 		strcat(buf, "<");
4958082Seric 		*p++ = '<';
4967783Seric 
4978082Seric 		/* skip spaces */
4988082Seric 		while (isspace(*p))
4998082Seric 			p++;
5008082Seric 
5017783Seric 		/* find the matching right angle bracket */
5028082Seric 		addr = p;
5037783Seric 		for (i = 0; *p != '\0'; p++)
5047783Seric 		{
5057783Seric 			switch (*p)
5067783Seric 			{
5077783Seric 			  case '<':
5087783Seric 				i++;
5097783Seric 				break;
5107783Seric 
5117783Seric 			  case '>':
5127783Seric 				i--;
5137783Seric 				break;
5147783Seric 			}
5157783Seric 			if (i < 0)
5167783Seric 				break;
5177783Seric 		}
5187783Seric 
5197783Seric 		/* p now points to the closing quote (or a null byte) */
5207783Seric 		if (*p != '\0')
5217783Seric 		{
5227783Seric 			/* make rhs point to the extra stuff at the end */
5237783Seric 			rhs = p;
5247783Seric 			*p++ = '\0';
5257783Seric 		}
5267783Seric 	}
5277783Seric 
5287783Seric 	/*
5297944Seric 	**  Now parse the real address part.  "addr" points to the (null
5307783Seric 	**  terminated) version of what we are inerested in; rhs points
5317783Seric 	**  to the extra stuff at the end of the line, if any.
5327783Seric 	*/
5337783Seric 
5347890Seric 	p = addr;
5357783Seric 
5367783Seric 	/* now strip out comments */
5377783Seric 	bp = &buf[strlen(buf)];
5387783Seric 	gotaddr = FALSE;
5397783Seric 	for (; *p != '\0'; p++)
5407783Seric 	{
5417783Seric 		if (*p == '(')
5427783Seric 		{
5437783Seric 			/* copy to matching close paren */
5447783Seric 			*bp++ = *p++;
5457783Seric 			for (i = 0; *p != '\0'; p++)
5467783Seric 			{
5477783Seric 				*bp++ = *p;
5487783Seric 				switch (*p)
5497783Seric 				{
5507783Seric 				  case '(':
5517783Seric 					i++;
5527783Seric 					break;
5537783Seric 
5547783Seric 				  case ')':
5557783Seric 					i--;
5567783Seric 					break;
5577783Seric 				}
5587783Seric 				if (i < 0)
5597783Seric 					break;
5607783Seric 			}
5617783Seric 			continue;
5627783Seric 		}
5637783Seric 
5647783Seric 		/*
5657783Seric 		**  If this is the first "real" character we have seen,
5667783Seric 		**  then we put the "$g" in the buffer now.
5677783Seric 		*/
5687783Seric 
5697783Seric 		if (isspace(*p))
5707783Seric 			*bp++ = *p;
5717783Seric 		else if (!gotaddr)
5727783Seric 		{
57316148Seric 			strcpy(bp, "\001g");
5747783Seric 			bp += 2;
5757783Seric 			gotaddr = TRUE;
5767783Seric 		}
5777783Seric 	}
5787783Seric 
5797944Seric 	/* hack, hack.... strip trailing blanks */
5807944Seric 	do
5817944Seric 	{
5827944Seric 		*bp-- = '\0';
5837944Seric 	} while (isspace(*bp));
5847944Seric 	bp++;
5857783Seric 
5867944Seric 	/* put any right hand side back on */
5877783Seric 	if (rhs != NULL)
5887783Seric 	{
5897783Seric 		*rhs = '>';
5907783Seric 		strcpy(bp, rhs);
5917783Seric 	}
5927783Seric 
5937783Seric # ifdef DEBUG
5947783Seric 	if (tTd(33, 1))
5957944Seric 		printf("crackaddr=>`%s'\n", buf);
5967783Seric # endif DEBUG
5977783Seric 
5987783Seric 	return (buf);
5997783Seric }
6009382Seric /*
6019382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6029382Seric **
6039382Seric **	Parameters:
6049382Seric **		fp -- file to put it on.
6059382Seric **		m -- mailer to use.
6069382Seric **		e -- envelope to use.
6079382Seric **
6089382Seric **	Returns:
6099382Seric **		none.
6109382Seric **
6119382Seric **	Side Effects:
6129382Seric **		none.
6139382Seric */
6149382Seric 
61510176Seric putheader(fp, m, e)
6169382Seric 	register FILE *fp;
6179382Seric 	register MAILER *m;
6189382Seric 	register ENVELOPE *e;
6199382Seric {
6209382Seric 	char buf[BUFSIZ];
6219382Seric 	register HDR *h;
6229382Seric 	extern char *arpadate();
6239382Seric 	extern char *capitalize();
6249382Seric 	char obuf[MAXLINE];
6259382Seric 
6269382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6279382Seric 	{
6289382Seric 		register char *p;
62910689Seric 		extern bool bitintersect();
6309382Seric 
6319382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
63210689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6339382Seric 			continue;
6349382Seric 
63511414Seric 		/* handle Resent-... headers specially */
63611414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
63711414Seric 			continue;
63811414Seric 
6399382Seric 		p = h->h_value;
6409382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6419382Seric 		{
6429382Seric 			/* macro expand value if generated internally */
6439382Seric 			expand(p, buf, &buf[sizeof buf], e);
6449382Seric 			p = buf;
6459382Seric 			if (p == NULL || *p == '\0')
6469382Seric 				continue;
6479382Seric 		}
6489382Seric 
6499382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6509382Seric 		{
6519382Seric 			/* address field */
6529382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6539382Seric 
6549382Seric 			if (bitset(H_FROM, h->h_flags))
6559382Seric 				oldstyle = FALSE;
65610176Seric 			commaize(h, p, fp, oldstyle, m);
6579382Seric 		}
6589382Seric 		else
6599382Seric 		{
6609382Seric 			/* vanilla header line */
66112159Seric 			register char *nlp;
66212159Seric 
66312159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
66412159Seric 			while ((nlp = index(p, '\n')) != NULL)
66512159Seric 			{
66612159Seric 				*nlp = '\0';
66712159Seric 				(void) strcat(obuf, p);
66812159Seric 				*nlp = '\n';
66912159Seric 				putline(obuf, fp, m);
67012159Seric 				p = ++nlp;
67112161Seric 				obuf[0] = '\0';
67212159Seric 			}
67312159Seric 			(void) strcat(obuf, p);
67410176Seric 			putline(obuf, fp, m);
6759382Seric 		}
6769382Seric 	}
6779382Seric }
6789382Seric /*
6799382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6809382Seric **
6819382Seric **	Parameters:
6829382Seric **		h -- the header field to output.
6839382Seric **		p -- the value to put in it.
6849382Seric **		fp -- file to put it to.
6859382Seric **		oldstyle -- TRUE if this is an old style header.
6869382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6879382Seric **			don't transform the name at all.
6889382Seric **
6899382Seric **	Returns:
6909382Seric **		none.
6919382Seric **
6929382Seric **	Side Effects:
6939382Seric **		outputs "p" to file "fp".
6949382Seric */
6959382Seric 
69610176Seric commaize(h, p, fp, oldstyle, m)
6979382Seric 	register HDR *h;
6989382Seric 	register char *p;
6999382Seric 	FILE *fp;
7009382Seric 	bool oldstyle;
7019382Seric 	register MAILER *m;
7029382Seric {
7039382Seric 	register char *obp;
7049382Seric 	int opos;
7059382Seric 	bool firstone = TRUE;
70611157Seric 	char obuf[MAXLINE + 3];
7079382Seric 
7089382Seric 	/*
7099382Seric 	**  Output the address list translated by the
7109382Seric 	**  mailer and with commas.
7119382Seric 	*/
7129382Seric 
7139382Seric # ifdef DEBUG
7149382Seric 	if (tTd(14, 2))
7159382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7169382Seric # endif DEBUG
7179382Seric 
7189382Seric 	obp = obuf;
7199382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7209382Seric 	opos = strlen(h->h_field) + 2;
7219382Seric 	obp += opos;
7229382Seric 
7239382Seric 	/*
7249382Seric 	**  Run through the list of values.
7259382Seric 	*/
7269382Seric 
7279382Seric 	while (*p != '\0')
7289382Seric 	{
7299382Seric 		register char *name;
7309382Seric 		char savechar;
7319382Seric 		extern char *remotename();
7329382Seric 		extern char *DelimChar;		/* defined in prescan */
7339382Seric 
7349382Seric 		/*
7359382Seric 		**  Find the end of the name.  New style names
7369382Seric 		**  end with a comma, old style names end with
7379382Seric 		**  a space character.  However, spaces do not
7389382Seric 		**  necessarily delimit an old-style name -- at
7399382Seric 		**  signs mean keep going.
7409382Seric 		*/
7419382Seric 
7429382Seric 		/* find end of name */
7439382Seric 		while (isspace(*p) || *p == ',')
7449382Seric 			p++;
7459382Seric 		name = p;
7469382Seric 		for (;;)
7479382Seric 		{
7489382Seric 			char *oldp;
74916909Seric 			char pvpbuf[PSBUFSIZE];
7509382Seric 			extern bool isatword();
7519382Seric 			extern char **prescan();
7529382Seric 
75316909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7549382Seric 			p = DelimChar;
7559382Seric 
7569382Seric 			/* look to see if we have an at sign */
7579382Seric 			oldp = p;
7589382Seric 			while (*p != '\0' && isspace(*p))
7599382Seric 				p++;
7609382Seric 
7619382Seric 			if (*p != '@' && !isatword(p))
7629382Seric 			{
7639382Seric 				p = oldp;
7649382Seric 				break;
7659382Seric 			}
7669382Seric 			p += *p == '@' ? 1 : 2;
7679382Seric 			while (*p != '\0' && isspace(*p))
7689382Seric 				p++;
7699382Seric 		}
7709382Seric 		/* at the end of one complete name */
7719382Seric 
7729382Seric 		/* strip off trailing white space */
7739382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7749382Seric 			p--;
7759382Seric 		if (++p == name)
7769382Seric 			continue;
7779382Seric 		savechar = *p;
7789382Seric 		*p = '\0';
7799382Seric 
7809382Seric 		/* translate the name to be relative */
78110309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7829382Seric 		if (*name == '\0')
7839382Seric 		{
7849382Seric 			*p = savechar;
7859382Seric 			continue;
7869382Seric 		}
7879382Seric 
7889382Seric 		/* output the name with nice formatting */
7899382Seric 		opos += qstrlen(name);
7909382Seric 		if (!firstone)
7919382Seric 			opos += 2;
7929382Seric 		if (opos > 78 && !firstone)
7939382Seric 		{
79410178Seric 			(void) strcpy(obp, ",\n");
79510176Seric 			putline(obuf, fp, m);
7969382Seric 			obp = obuf;
7979382Seric 			(void) sprintf(obp, "        ");
79810161Seric 			opos = strlen(obp);
79910161Seric 			obp += opos;
80010161Seric 			opos += qstrlen(name);
8019382Seric 		}
8029382Seric 		else if (!firstone)
8039382Seric 		{
8049382Seric 			(void) sprintf(obp, ", ");
8059382Seric 			obp += 2;
8069382Seric 		}
8079382Seric 
8089382Seric 		/* strip off quote bits as we output */
80911157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8109382Seric 		{
8119382Seric 			if (bitset(0200, *name))
8129382Seric 				*obp++ = '\\';
8139382Seric 			*obp++ = *name++ & ~0200;
8149382Seric 		}
8159382Seric 		firstone = FALSE;
8169382Seric 		*p = savechar;
8179382Seric 	}
8189382Seric 	(void) strcpy(obp, "\n");
81910176Seric 	putline(obuf, fp, m);
8209382Seric }
8219382Seric /*
8229382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8239382Seric **
8249382Seric **	Parameters:
8259382Seric **		p -- word to check.
8269382Seric **
8279382Seric **	Returns:
8289382Seric **		TRUE -- if p is the word at.
8299382Seric **		FALSE -- otherwise.
8309382Seric **
8319382Seric **	Side Effects:
8329382Seric **		none.
8339382Seric */
8349382Seric 
8359382Seric bool
8369382Seric isatword(p)
8379382Seric 	register char *p;
8389382Seric {
8399382Seric 	extern char lower();
8409382Seric 
8419382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8429382Seric 	    p[2] != '\0' && isspace(p[2]))
8439382Seric 		return (TRUE);
8449382Seric 	return (FALSE);
8459382Seric }
846