14091Seric # include <errno.h>
24091Seric # include "sendmail.h"
34091Seric 
4*13012Seric SCCSID(@(#)headers.c	3.58		06/11/83);
54091Seric 
64091Seric /*
74091Seric **  CHOMPHEADER -- process and save a header line.
84091Seric **
94091Seric **	Called by collect and by readcf to deal with header lines.
104091Seric **
114091Seric **	Parameters:
124091Seric **		line -- header as a text line.
134091Seric **		def -- if set, this is a default value.
144091Seric **
154091Seric **	Returns:
164091Seric **		flags for this header.
174091Seric **
184091Seric **	Side Effects:
194091Seric **		The header is saved on the header list.
204319Seric **		Contents of 'line' are destroyed.
214091Seric */
224091Seric 
234091Seric chompheader(line, def)
244091Seric 	char *line;
254091Seric 	bool def;
264091Seric {
274091Seric 	register char *p;
284091Seric 	register HDR *h;
294091Seric 	HDR **hp;
304091Seric 	char *fname;
314091Seric 	char *fvalue;
324091Seric 	struct hdrinfo *hi;
339059Seric 	bool cond = FALSE;
3410689Seric 	BITMAP mopts;
357890Seric 	extern char *crackaddr();
364091Seric 
377677Seric # ifdef DEBUG
387677Seric 	if (tTd(31, 6))
397677Seric 		printf("chompheader: %s\n", line);
407677Seric # endif DEBUG
417677Seric 
424627Seric 	/* strip off options */
4310689Seric 	clrbitmap(mopts);
444627Seric 	p = line;
454627Seric 	if (*p == '?')
464627Seric 	{
474627Seric 		/* have some */
484627Seric 		register char *q = index(p + 1, *p);
494627Seric 
504627Seric 		if (q != NULL)
514627Seric 		{
524627Seric 			*q++ = '\0';
5310689Seric 			while (*++p != '\0')
5410689Seric 				setbitn(*p, mopts);
554627Seric 			p = q;
564627Seric 		}
574627Seric 		else
584627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
599059Seric 		cond = TRUE;
604627Seric 	}
614627Seric 
624091Seric 	/* find canonical name */
634627Seric 	fname = p;
644627Seric 	p = index(p, ':');
6510118Seric 	if (p == NULL)
6610118Seric 	{
6710118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
6810118Seric 		return (0);
6910118Seric 	}
704091Seric 	fvalue = &p[1];
714091Seric 	while (isspace(*--p))
724091Seric 		continue;
734091Seric 	*++p = '\0';
744091Seric 	makelower(fname);
754091Seric 
764091Seric 	/* strip field value on front */
774091Seric 	if (*fvalue == ' ')
784091Seric 		fvalue++;
794091Seric 
80*13012Seric 	/* delete default value for this header */
819382Seric 	for (hp = &CurEnv->e_header, h = CurEnv->e_header; h != NULL;
829382Seric 		hp = &h->h_link, h = h->h_link)
834091Seric 	{
84*13012Seric 		if (strcmp(fname, h->h_field) == 0 &&
85*13012Seric 		    bitset(H_DEFAULT, h->h_flags) &&
86*13012Seric 		    !bitset(H_FORCE, h->h_flags))
87*13012Seric 			h->h_value = NULL;
884091Seric 	}
894091Seric 
904091Seric 	/* see if it is a known type */
914091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
924091Seric 	{
934091Seric 		if (strcmp(hi->hi_field, fname) == 0)
944091Seric 			break;
954091Seric 	}
964091Seric 
9711414Seric 	/* see if this is a resent message */
9811930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9911414Seric 		CurEnv->e_flags |= EF_RESENT;
10011414Seric 
1014091Seric 	/* if this means "end of header" quit now */
1024091Seric 	if (bitset(H_EOH, hi->hi_flags))
1034091Seric 		return (hi->hi_flags);
1044091Seric 
10511414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10611541Seric 	if (!def && !QueueRun && strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
10711414Seric 	{
10811414Seric 		p = "resent-from";
10911414Seric 		if (!bitset(EF_RESENT, CurEnv->e_flags))
11011414Seric 			p += 7;
11111414Seric 		if (strcmp(fname, p) == 0)
11211414Seric 			return (hi->hi_flags);
11311414Seric 	}
11411414Seric 
115*13012Seric 	/* create a new node */
116*13012Seric 	h = (HDR *) xalloc(sizeof *h);
117*13012Seric 	h->h_field = newstr(fname);
118*13012Seric 	h->h_value = NULL;
119*13012Seric 	h->h_link = NULL;
120*13012Seric 	bcopy(mopts, h->h_mflags, sizeof mopts);
121*13012Seric 	*hp = h;
1228066Seric 	h->h_flags = hi->hi_flags;
1234091Seric 	if (def)
1244091Seric 		h->h_flags |= H_DEFAULT;
1259059Seric 	if (cond)
1269059Seric 		h->h_flags |= H_CHECK;
1274091Seric 	if (h->h_value != NULL)
1289351Seric 		free((char *) h->h_value);
1298066Seric 	h->h_value = newstr(fvalue);
1304091Seric 
1315937Seric 	/* hack to see if this is a new format message */
1328095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1335937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1348089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1358089Seric 	{
1369342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1378089Seric 	}
1385937Seric 
1394091Seric 	return (h->h_flags);
1404091Seric }
1414091Seric /*
1426980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1436980Seric **
1446980Seric **	This bypasses the special checking of chompheader.
1456980Seric **
1466980Seric **	Parameters:
1476980Seric **		field -- the name of the header field.
1486980Seric **		value -- the value of the field.  It must be lower-cased.
1496980Seric **		e -- the envelope to add them to.
1506980Seric **
1516980Seric **	Returns:
1526980Seric **		none.
1536980Seric **
1546980Seric **	Side Effects:
1556980Seric **		adds the field on the list of headers for this envelope.
1566980Seric */
1576980Seric 
1586980Seric addheader(field, value, e)
1596980Seric 	char *field;
1606980Seric 	char *value;
1616980Seric 	ENVELOPE *e;
1626980Seric {
1636980Seric 	register HDR *h;
1646980Seric 	register struct hdrinfo *hi;
1656980Seric 	HDR **hp;
1666980Seric 
1676980Seric 	/* find info struct */
1686980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1696980Seric 	{
1706980Seric 		if (strcmp(field, hi->hi_field) == 0)
1716980Seric 			break;
1726980Seric 	}
1736980Seric 
1746980Seric 	/* find current place in list -- keep back pointer? */
1756980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1766980Seric 	{
1776980Seric 		if (strcmp(field, h->h_field) == 0)
1786980Seric 			break;
1796980Seric 	}
1806980Seric 
1816980Seric 	/* allocate space for new header */
1826980Seric 	h = (HDR *) xalloc(sizeof *h);
1836980Seric 	h->h_field = field;
1846980Seric 	h->h_value = newstr(value);
1857368Seric 	h->h_link = *hp;
1866980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
18710689Seric 	clrbitmap(h->h_mflags);
1886980Seric 	*hp = h;
1896980Seric }
1906980Seric /*
1914091Seric **  HVALUE -- return value of a header.
1924091Seric **
1934091Seric **	Only "real" fields (i.e., ones that have not been supplied
1944091Seric **	as a default) are used.
1954091Seric **
1964091Seric **	Parameters:
1974091Seric **		field -- the field name.
1984091Seric **
1994091Seric **	Returns:
2004091Seric **		pointer to the value part.
2014091Seric **		NULL if not found.
2024091Seric **
2034091Seric **	Side Effects:
2049382Seric **		none.
2054091Seric */
2064091Seric 
2074091Seric char *
2084091Seric hvalue(field)
2094091Seric 	char *field;
2104091Seric {
2114091Seric 	register HDR *h;
2124091Seric 
2136908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2144091Seric 	{
2154091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2164091Seric 			return (h->h_value);
2174091Seric 	}
2184091Seric 	return (NULL);
2194091Seric }
2204091Seric /*
2214091Seric **  ISHEADER -- predicate telling if argument is a header.
2224091Seric **
2234319Seric **	A line is a header if it has a single word followed by
2244319Seric **	optional white space followed by a colon.
2254319Seric **
2264091Seric **	Parameters:
2274091Seric **		s -- string to check for possible headerness.
2284091Seric **
2294091Seric **	Returns:
2304091Seric **		TRUE if s is a header.
2314091Seric **		FALSE otherwise.
2324091Seric **
2334091Seric **	Side Effects:
2344091Seric **		none.
2354091Seric */
2364091Seric 
2374091Seric bool
2384091Seric isheader(s)
2394091Seric 	register char *s;
2404091Seric {
2419382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2424091Seric 		s++;
2439382Seric 
2449382Seric 	/* following technically violates RFC822 */
2454091Seric 	while (isspace(*s))
2464091Seric 		s++;
2479382Seric 
2484091Seric 	return (*s == ':');
2494091Seric }
2505919Seric /*
2517783Seric **  EATHEADER -- run through the stored header and extract info.
2527783Seric **
2537783Seric **	Parameters:
2549382Seric **		e -- the envelope to process.
2557783Seric **
2567783Seric **	Returns:
2577783Seric **		none.
2587783Seric **
2597783Seric **	Side Effects:
2607783Seric **		Sets a bunch of global variables from information
2619382Seric **			in the collected header.
2629382Seric **		Aborts the message if the hop count is exceeded.
2637783Seric */
2647783Seric 
2659382Seric eatheader(e)
2669382Seric 	register ENVELOPE *e;
2677783Seric {
2687783Seric 	register HDR *h;
2697783Seric 	register char *p;
2709382Seric 	int hopcnt = 0;
2717783Seric 
2729382Seric #ifdef DEBUG
2739382Seric 	if (tTd(32, 1))
2749382Seric 		printf("----- collected header -----\n");
2759382Seric #endif DEBUG
2769382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2777783Seric 	{
2789382Seric #ifdef DEBUG
2797783Seric 		extern char *capitalize();
2807783Seric 
2819382Seric 		if (tTd(32, 1))
2827783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2839382Seric #endif DEBUG
28411414Seric 		/* count the number of times it has been processed */
2859382Seric 		if (bitset(H_TRACE, h->h_flags))
2869382Seric 			hopcnt++;
28711414Seric 
28811414Seric 		/* send to this person if we so desire */
28911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
29011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
29111414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
29211414Seric 		{
29311414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
29411414Seric 		}
29511414Seric 
29611414Seric 		/* log the message-id */
29711290Seric #ifdef LOG
29811299Seric 		if (!QueueRun && LogLevel > 8 &&
29911299Seric 		    strcmp(h->h_field, "message-id") == 0)
30011290Seric 		{
30111290Seric 			char buf[MAXNAME];
30211290Seric 
30311290Seric 			p = h->h_value;
30411290Seric 			if (bitset(H_DEFAULT, h->h_flags))
30511290Seric 			{
30611290Seric 				expand(p, buf, &buf[sizeof buf], e);
30711290Seric 				p = buf;
30811290Seric 			}
30911290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
31011290Seric 		}
31111290Seric #endif LOG
3129382Seric 	}
3139382Seric #ifdef DEBUG
3149382Seric 	if (tTd(32, 1))
3157783Seric 		printf("----------------------------\n");
3169382Seric #endif DEBUG
3177783Seric 
3189382Seric 	/* store hop count */
3199382Seric 	if (hopcnt > e->e_hopcount)
3209382Seric 		e->e_hopcount = hopcnt;
3219382Seric 
3227783Seric 	/* message priority */
3239382Seric 	p = hvalue("precedence");
3249382Seric 	if (p != NULL)
3259382Seric 		e->e_class = priencode(p);
3267783Seric 	if (!QueueRun)
3279382Seric 		e->e_msgpriority = e->e_msgsize - e->e_class * WKPRIFACT;
3287783Seric 
3298066Seric 	/* return receipt to */
3308066Seric 	p = hvalue("return-receipt-to");
3317783Seric 	if (p != NULL)
3329382Seric 		e->e_receiptto = p;
3337783Seric 
3348253Seric 	/* errors to */
3358253Seric 	p = hvalue("errors-to");
3368253Seric 	if (p != NULL)
3379620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3388253Seric 
3397783Seric 	/* from person */
3409285Seric 	if (OpMode == MD_ARPAFTP)
3418066Seric 	{
3428066Seric 		register struct hdrinfo *hi = HdrInfo;
3437783Seric 
3448066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3458066Seric 		{
3468066Seric 			if (bitset(H_FROM, hi->hi_flags))
3478066Seric 				p = hvalue(hi->hi_field);
3488066Seric 		}
3498066Seric 		if (p != NULL)
3509382Seric 			setsender(p);
3518066Seric 	}
3528066Seric 
3537783Seric 	/* full name of from person */
3547783Seric 	p = hvalue("full-name");
3557783Seric 	if (p != NULL)
3569382Seric 		define('x', p, e);
3577783Seric 
3587783Seric 	/* date message originated */
3597783Seric 	p = hvalue("posted-date");
3607783Seric 	if (p == NULL)
3617783Seric 		p = hvalue("date");
3627783Seric 	if (p != NULL)
3637783Seric 	{
3649382Seric 		define('a', p, e);
3657783Seric 		/* we don't have a good way to do canonical conversion ....
3669382Seric 		define('d', newstr(arpatounix(p)), e);
3677783Seric 		.... so we will ignore the problem for the time being */
3687783Seric 	}
36911290Seric 
37011290Seric 	/*
37111290Seric 	**  Log collection information.
37211290Seric 	*/
37311290Seric 
37411290Seric # ifdef LOG
37511299Seric 	if (!QueueRun && LogLevel > 1)
37611290Seric 	{
37711290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
37811290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
37911290Seric 		       CurEnv->e_class);
38011290Seric 	}
38111290Seric # endif LOG
3827783Seric }
3837783Seric /*
3847783Seric **  PRIENCODE -- encode external priority names into internal values.
3857783Seric **
3867783Seric **	Parameters:
3877783Seric **		p -- priority in ascii.
3887783Seric **
3897783Seric **	Returns:
3907783Seric **		priority as a numeric level.
3917783Seric **
3927783Seric **	Side Effects:
3937783Seric **		none.
3947783Seric */
3957783Seric 
3967783Seric priencode(p)
3977783Seric 	char *p;
3987783Seric {
3998253Seric 	register int i;
4007783Seric 	extern bool sameword();
4017783Seric 
4028253Seric 	for (i = 0; i < NumPriorities; i++)
4037783Seric 	{
4048253Seric 		if (sameword(p, Priorities[i].pri_name))
4058253Seric 			return (Priorities[i].pri_val);
4067783Seric 	}
4078253Seric 
4088253Seric 	/* unknown priority */
4098253Seric 	return (0);
4107783Seric }
4117783Seric /*
4127890Seric **  CRACKADDR -- parse an address and turn it into a macro
4137783Seric **
4147783Seric **	This doesn't actually parse the address -- it just extracts
4157783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4167783Seric **	and isn't even guaranteed to leave something syntactically
4177783Seric **	identical to what it started with.  However, it does leave
4187783Seric **	something semantically identical.
4197783Seric **
4207783Seric **	The process is kind of strange.  There are a number of
4217783Seric **	interesting cases:
4227783Seric **		1.  comment <address> comment	==> comment <$g> comment
4237783Seric **		2.  address			==> address
4247783Seric **		3.  address (comment)		==> $g (comment)
4257783Seric **		4.  (comment) address		==> (comment) $g
4267783Seric **	And then there are the hard cases....
4277783Seric **		5.  add (comment) ress		==> $g (comment)
4287783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4297783Seric **		7.    .... etc ....
4307783Seric **
4317783Seric **	Parameters:
4327890Seric **		addr -- the address to be cracked.
4337783Seric **
4347783Seric **	Returns:
4357783Seric **		a pointer to the new version.
4367783Seric **
4377783Seric **	Side Effects:
4387890Seric **		none.
4397783Seric **
4407783Seric **	Warning:
4417783Seric **		The return value is saved in local storage and should
4427783Seric **		be copied if it is to be reused.
4437783Seric */
4447783Seric 
4457783Seric char *
4467890Seric crackaddr(addr)
4477890Seric 	register char *addr;
4487783Seric {
4497783Seric 	register char *p;
4507783Seric 	register int i;
4517783Seric 	static char buf[MAXNAME];
4527783Seric 	char *rhs;
4537783Seric 	bool gotaddr;
4547783Seric 	register char *bp;
4557783Seric 
4567783Seric # ifdef DEBUG
4577783Seric 	if (tTd(33, 1))
4587890Seric 		printf("crackaddr(%s)\n", addr);
4597783Seric # endif DEBUG
4607783Seric 
4617783Seric 	strcpy(buf, "");
4627783Seric 	rhs = NULL;
4637783Seric 
4648082Seric 	/* strip leading spaces */
4658082Seric 	while (*addr != '\0' && isspace(*addr))
4668082Seric 		addr++;
4678082Seric 
4687783Seric 	/*
4697783Seric 	**  See if we have anything in angle brackets.  If so, that is
4707783Seric 	**  the address part, and the rest is the comment.
4717783Seric 	*/
4727783Seric 
4737890Seric 	p = index(addr, '<');
4747783Seric 	if (p != NULL)
4757783Seric 	{
4767890Seric 		/* copy the beginning of the addr field to the buffer */
4777783Seric 		*p = '\0';
4787890Seric 		strcpy(buf, addr);
4797783Seric 		strcat(buf, "<");
4808082Seric 		*p++ = '<';
4817783Seric 
4828082Seric 		/* skip spaces */
4838082Seric 		while (isspace(*p))
4848082Seric 			p++;
4858082Seric 
4867783Seric 		/* find the matching right angle bracket */
4878082Seric 		addr = p;
4887783Seric 		for (i = 0; *p != '\0'; p++)
4897783Seric 		{
4907783Seric 			switch (*p)
4917783Seric 			{
4927783Seric 			  case '<':
4937783Seric 				i++;
4947783Seric 				break;
4957783Seric 
4967783Seric 			  case '>':
4977783Seric 				i--;
4987783Seric 				break;
4997783Seric 			}
5007783Seric 			if (i < 0)
5017783Seric 				break;
5027783Seric 		}
5037783Seric 
5047783Seric 		/* p now points to the closing quote (or a null byte) */
5057783Seric 		if (*p != '\0')
5067783Seric 		{
5077783Seric 			/* make rhs point to the extra stuff at the end */
5087783Seric 			rhs = p;
5097783Seric 			*p++ = '\0';
5107783Seric 		}
5117783Seric 	}
5127783Seric 
5137783Seric 	/*
5147944Seric 	**  Now parse the real address part.  "addr" points to the (null
5157783Seric 	**  terminated) version of what we are inerested in; rhs points
5167783Seric 	**  to the extra stuff at the end of the line, if any.
5177783Seric 	*/
5187783Seric 
5197890Seric 	p = addr;
5207783Seric 
5217783Seric 	/* now strip out comments */
5227783Seric 	bp = &buf[strlen(buf)];
5237783Seric 	gotaddr = FALSE;
5247783Seric 	for (; *p != '\0'; p++)
5257783Seric 	{
5267783Seric 		if (*p == '(')
5277783Seric 		{
5287783Seric 			/* copy to matching close paren */
5297783Seric 			*bp++ = *p++;
5307783Seric 			for (i = 0; *p != '\0'; p++)
5317783Seric 			{
5327783Seric 				*bp++ = *p;
5337783Seric 				switch (*p)
5347783Seric 				{
5357783Seric 				  case '(':
5367783Seric 					i++;
5377783Seric 					break;
5387783Seric 
5397783Seric 				  case ')':
5407783Seric 					i--;
5417783Seric 					break;
5427783Seric 				}
5437783Seric 				if (i < 0)
5447783Seric 					break;
5457783Seric 			}
5467783Seric 			continue;
5477783Seric 		}
5487783Seric 
5497783Seric 		/*
5507783Seric 		**  If this is the first "real" character we have seen,
5517783Seric 		**  then we put the "$g" in the buffer now.
5527783Seric 		*/
5537783Seric 
5547783Seric 		if (isspace(*p))
5557783Seric 			*bp++ = *p;
5567783Seric 		else if (!gotaddr)
5577783Seric 		{
5587783Seric 			strcpy(bp, "$g");
5597783Seric 			bp += 2;
5607783Seric 			gotaddr = TRUE;
5617783Seric 		}
5627783Seric 	}
5637783Seric 
5647944Seric 	/* hack, hack.... strip trailing blanks */
5657944Seric 	do
5667944Seric 	{
5677944Seric 		*bp-- = '\0';
5687944Seric 	} while (isspace(*bp));
5697944Seric 	bp++;
5707783Seric 
5717944Seric 	/* put any right hand side back on */
5727783Seric 	if (rhs != NULL)
5737783Seric 	{
5747783Seric 		*rhs = '>';
5757783Seric 		strcpy(bp, rhs);
5767783Seric 	}
5777783Seric 
5787783Seric # ifdef DEBUG
5797783Seric 	if (tTd(33, 1))
5807944Seric 		printf("crackaddr=>`%s'\n", buf);
5817783Seric # endif DEBUG
5827783Seric 
5837783Seric 	return (buf);
5847783Seric }
5859382Seric /*
5869382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5879382Seric **
5889382Seric **	Parameters:
5899382Seric **		fp -- file to put it on.
5909382Seric **		m -- mailer to use.
5919382Seric **		e -- envelope to use.
5929382Seric **
5939382Seric **	Returns:
5949382Seric **		none.
5959382Seric **
5969382Seric **	Side Effects:
5979382Seric **		none.
5989382Seric */
5999382Seric 
60010176Seric putheader(fp, m, e)
6019382Seric 	register FILE *fp;
6029382Seric 	register MAILER *m;
6039382Seric 	register ENVELOPE *e;
6049382Seric {
6059382Seric 	char buf[BUFSIZ];
6069382Seric 	register HDR *h;
6079382Seric 	extern char *arpadate();
6089382Seric 	extern char *capitalize();
6099382Seric 	char obuf[MAXLINE];
6109382Seric 
6119382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6129382Seric 	{
6139382Seric 		register char *p;
61410689Seric 		extern bool bitintersect();
6159382Seric 
6169382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
61710689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6189382Seric 			continue;
6199382Seric 
62011414Seric 		/* handle Resent-... headers specially */
62111414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
62211414Seric 			continue;
62311414Seric 
6249382Seric 		p = h->h_value;
6259382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6269382Seric 		{
6279382Seric 			/* macro expand value if generated internally */
6289382Seric 			expand(p, buf, &buf[sizeof buf], e);
6299382Seric 			p = buf;
6309382Seric 			if (p == NULL || *p == '\0')
6319382Seric 				continue;
6329382Seric 		}
6339382Seric 
6349382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6359382Seric 		{
6369382Seric 			/* address field */
6379382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6389382Seric 
6399382Seric 			if (bitset(H_FROM, h->h_flags))
6409382Seric 				oldstyle = FALSE;
64110176Seric 			commaize(h, p, fp, oldstyle, m);
6429382Seric 		}
6439382Seric 		else
6449382Seric 		{
6459382Seric 			/* vanilla header line */
64612159Seric 			register char *nlp;
64712159Seric 
64812159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
64912159Seric 			while ((nlp = index(p, '\n')) != NULL)
65012159Seric 			{
65112159Seric 				*nlp = '\0';
65212159Seric 				(void) strcat(obuf, p);
65312159Seric 				*nlp = '\n';
65412159Seric 				putline(obuf, fp, m);
65512159Seric 				p = ++nlp;
65612161Seric 				obuf[0] = '\0';
65712159Seric 			}
65812159Seric 			(void) strcat(obuf, p);
65910176Seric 			putline(obuf, fp, m);
6609382Seric 		}
6619382Seric 	}
6629382Seric }
6639382Seric /*
6649382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6659382Seric **
6669382Seric **	Parameters:
6679382Seric **		h -- the header field to output.
6689382Seric **		p -- the value to put in it.
6699382Seric **		fp -- file to put it to.
6709382Seric **		oldstyle -- TRUE if this is an old style header.
6719382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6729382Seric **			don't transform the name at all.
6739382Seric **
6749382Seric **	Returns:
6759382Seric **		none.
6769382Seric **
6779382Seric **	Side Effects:
6789382Seric **		outputs "p" to file "fp".
6799382Seric */
6809382Seric 
68110176Seric commaize(h, p, fp, oldstyle, m)
6829382Seric 	register HDR *h;
6839382Seric 	register char *p;
6849382Seric 	FILE *fp;
6859382Seric 	bool oldstyle;
6869382Seric 	register MAILER *m;
6879382Seric {
6889382Seric 	register char *obp;
6899382Seric 	int opos;
6909382Seric 	bool firstone = TRUE;
69111157Seric 	char obuf[MAXLINE + 3];
6929382Seric 
6939382Seric 	/*
6949382Seric 	**  Output the address list translated by the
6959382Seric 	**  mailer and with commas.
6969382Seric 	*/
6979382Seric 
6989382Seric # ifdef DEBUG
6999382Seric 	if (tTd(14, 2))
7009382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7019382Seric # endif DEBUG
7029382Seric 
7039382Seric 	obp = obuf;
7049382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7059382Seric 	opos = strlen(h->h_field) + 2;
7069382Seric 	obp += opos;
7079382Seric 
7089382Seric 	/*
7099382Seric 	**  Run through the list of values.
7109382Seric 	*/
7119382Seric 
7129382Seric 	while (*p != '\0')
7139382Seric 	{
7149382Seric 		register char *name;
7159382Seric 		char savechar;
7169382Seric 		extern char *remotename();
7179382Seric 		extern char *DelimChar;		/* defined in prescan */
7189382Seric 
7199382Seric 		/*
7209382Seric 		**  Find the end of the name.  New style names
7219382Seric 		**  end with a comma, old style names end with
7229382Seric 		**  a space character.  However, spaces do not
7239382Seric 		**  necessarily delimit an old-style name -- at
7249382Seric 		**  signs mean keep going.
7259382Seric 		*/
7269382Seric 
7279382Seric 		/* find end of name */
7289382Seric 		while (isspace(*p) || *p == ',')
7299382Seric 			p++;
7309382Seric 		name = p;
7319382Seric 		for (;;)
7329382Seric 		{
7339382Seric 			char *oldp;
7349382Seric 			extern bool isatword();
7359382Seric 			extern char **prescan();
7369382Seric 
7379382Seric 			(void) prescan(p, oldstyle ? ' ' : ',');
7389382Seric 			p = DelimChar;
7399382Seric 
7409382Seric 			/* look to see if we have an at sign */
7419382Seric 			oldp = p;
7429382Seric 			while (*p != '\0' && isspace(*p))
7439382Seric 				p++;
7449382Seric 
7459382Seric 			if (*p != '@' && !isatword(p))
7469382Seric 			{
7479382Seric 				p = oldp;
7489382Seric 				break;
7499382Seric 			}
7509382Seric 			p += *p == '@' ? 1 : 2;
7519382Seric 			while (*p != '\0' && isspace(*p))
7529382Seric 				p++;
7539382Seric 		}
7549382Seric 		/* at the end of one complete name */
7559382Seric 
7569382Seric 		/* strip off trailing white space */
7579382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7589382Seric 			p--;
7599382Seric 		if (++p == name)
7609382Seric 			continue;
7619382Seric 		savechar = *p;
7629382Seric 		*p = '\0';
7639382Seric 
7649382Seric 		/* translate the name to be relative */
76510309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7669382Seric 		if (*name == '\0')
7679382Seric 		{
7689382Seric 			*p = savechar;
7699382Seric 			continue;
7709382Seric 		}
7719382Seric 
7729382Seric 		/* output the name with nice formatting */
7739382Seric 		opos += qstrlen(name);
7749382Seric 		if (!firstone)
7759382Seric 			opos += 2;
7769382Seric 		if (opos > 78 && !firstone)
7779382Seric 		{
77810178Seric 			(void) strcpy(obp, ",\n");
77910176Seric 			putline(obuf, fp, m);
7809382Seric 			obp = obuf;
7819382Seric 			(void) sprintf(obp, "        ");
78210161Seric 			opos = strlen(obp);
78310161Seric 			obp += opos;
78410161Seric 			opos += qstrlen(name);
7859382Seric 		}
7869382Seric 		else if (!firstone)
7879382Seric 		{
7889382Seric 			(void) sprintf(obp, ", ");
7899382Seric 			obp += 2;
7909382Seric 		}
7919382Seric 
7929382Seric 		/* strip off quote bits as we output */
79311157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
7949382Seric 		{
7959382Seric 			if (bitset(0200, *name))
7969382Seric 				*obp++ = '\\';
7979382Seric 			*obp++ = *name++ & ~0200;
7989382Seric 		}
7999382Seric 		firstone = FALSE;
8009382Seric 		*p = savechar;
8019382Seric 	}
8029382Seric 	(void) strcpy(obp, "\n");
80310176Seric 	putline(obuf, fp, m);
8049382Seric }
8059382Seric /*
8069382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8079382Seric **
8089382Seric **	Parameters:
8099382Seric **		p -- word to check.
8109382Seric **
8119382Seric **	Returns:
8129382Seric **		TRUE -- if p is the word at.
8139382Seric **		FALSE -- otherwise.
8149382Seric **
8159382Seric **	Side Effects:
8169382Seric **		none.
8179382Seric */
8189382Seric 
8199382Seric bool
8209382Seric isatword(p)
8219382Seric 	register char *p;
8229382Seric {
8239382Seric 	extern char lower();
8249382Seric 
8259382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8269382Seric 	    p[2] != '\0' && isspace(p[2]))
8279382Seric 		return (TRUE);
8289382Seric 	return (FALSE);
8299382Seric }
830