122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*51379Seric static char sccsid[] = "@(#)headers.c	5.16 (Berkeley) 10/20/91";
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 **
434*51379Seric **	This algorithm has been cleaned up to handle a wider range
435*51379Seric **	of cases -- notably quoted and backslash escaped strings.
436*51379Seric **	This modification makes it substantially better at preserving
437*51379Seric **	the original syntax.
4387783Seric **
4397783Seric **	Parameters:
4407890Seric **		addr -- the address to be cracked.
4417783Seric **
4427783Seric **	Returns:
4437783Seric **		a pointer to the new version.
4447783Seric **
4457783Seric **	Side Effects:
4467890Seric **		none.
4477783Seric **
4487783Seric **	Warning:
4497783Seric **		The return value is saved in local storage and should
4507783Seric **		be copied if it is to be reused.
4517783Seric */
4527783Seric 
4537783Seric char *
4547890Seric crackaddr(addr)
4557890Seric 	register char *addr;
4567783Seric {
4577783Seric 	register char *p;
458*51379Seric 	register char c;
459*51379Seric 	int cmtlev;
460*51379Seric 	int copylev;
461*51379Seric 	bool qmode;
462*51379Seric 	bool putgmac = FALSE;
463*51379Seric 	register char *bp;
4647783Seric 	static char buf[MAXNAME];
4657783Seric 
4667783Seric 	if (tTd(33, 1))
4677890Seric 		printf("crackaddr(%s)\n", addr);
4687783Seric 
4698082Seric 	/* strip leading spaces */
4708082Seric 	while (*addr != '\0' && isspace(*addr))
4718082Seric 		addr++;
4728082Seric 
4737783Seric 	/*
474*51379Seric 	**  Start by assuming we have no angle brackets.  This will be
475*51379Seric 	**  adjusted later if we find them.
4767783Seric 	*/
4777783Seric 
478*51379Seric 	bp = buf;
479*51379Seric 	p = addr;
480*51379Seric 	copylev = cmtlev = 0;
481*51379Seric 	qmode = FALSE;
482*51379Seric 
483*51379Seric 	while ((c = *p++) != '\0')
4847783Seric 	{
485*51379Seric 		if (copylev > 0 || c == ' ')
486*51379Seric 			*bp++ = c;
4877783Seric 
488*51379Seric 		/* check for backslash escapes */
489*51379Seric 		if (c == '\\')
4907783Seric 		{
491*51379Seric 			if ((c = *p++) == '\0')
4927783Seric 			{
493*51379Seric 				/* too far */
494*51379Seric 				p--;
495*51379Seric 				goto putg;
4967783Seric 			}
497*51379Seric 			if (copylev > 0)
498*51379Seric 				*bp++ = c;
499*51379Seric 			goto putg;
5007783Seric 		}
5017783Seric 
502*51379Seric 		/* check for quoted strings */
503*51379Seric 		if (c == '"')
5047783Seric 		{
505*51379Seric 			qmode = !qmode;
506*51379Seric 			continue;
5077783Seric 		}
508*51379Seric 		if (qmode)
509*51379Seric 			goto putg;
5107783Seric 
511*51379Seric 		/* check for comments */
512*51379Seric 		if (c == '(')
5137783Seric 		{
514*51379Seric 			cmtlev++;
515*51379Seric 			if (copylev++ <= 0)
516*51379Seric 				*bp++ = c;
517*51379Seric 		}
518*51379Seric 		if (cmtlev > 0)
519*51379Seric 		{
520*51379Seric 			if (c == ')')
5217783Seric 			{
522*51379Seric 				cmtlev--;
523*51379Seric 				copylev--;
5247783Seric 			}
5257783Seric 			continue;
5267783Seric 		}
5277783Seric 
528*51379Seric 		/* check for angle brackets */
529*51379Seric 		if (c == '<')
530*51379Seric 		{
531*51379Seric 			/* oops -- have to change our mind */
532*51379Seric 			bcopy(addr, buf, p - addr);
533*51379Seric 			bp = &buf[p - addr];
534*51379Seric 			copylev = 0;
535*51379Seric 			putgmac = FALSE;
536*51379Seric 			continue;
537*51379Seric 		}
5387783Seric 
539*51379Seric 		if (c == '>')
5407783Seric 		{
541*51379Seric 			if (copylev++ <= 0)
542*51379Seric 				*bp++ = c;
543*51379Seric 			continue;
5447783Seric 		}
545*51379Seric 
546*51379Seric 		/* must be a real address character */
547*51379Seric 	putg:
548*51379Seric 		if (copylev <= 0 && !putgmac)
549*51379Seric 		{
550*51379Seric 			*bp++ = '\001';
551*51379Seric 			*bp++ = 'g';
552*51379Seric 			putgmac = TRUE;
553*51379Seric 		}
5547783Seric 	}
5557783Seric 
556*51379Seric 	*bp++ = '\0';
5577783Seric 
5587783Seric 	if (tTd(33, 1))
5597944Seric 		printf("crackaddr=>`%s'\n", buf);
5607783Seric 
5617783Seric 	return (buf);
5627783Seric }
5639382Seric /*
5649382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5659382Seric **
5669382Seric **	Parameters:
5679382Seric **		fp -- file to put it on.
5689382Seric **		m -- mailer to use.
5699382Seric **		e -- envelope to use.
5709382Seric **
5719382Seric **	Returns:
5729382Seric **		none.
5739382Seric **
5749382Seric **	Side Effects:
5759382Seric **		none.
5769382Seric */
5779382Seric 
57810176Seric putheader(fp, m, e)
5799382Seric 	register FILE *fp;
5809382Seric 	register MAILER *m;
5819382Seric 	register ENVELOPE *e;
5829382Seric {
58340960Sbostic 	char buf[MAX(MAXFIELD,BUFSIZ)];
5849382Seric 	register HDR *h;
5859382Seric 	extern char *arpadate();
5869382Seric 	extern char *capitalize();
58740960Sbostic 	char obuf[MAX(MAXFIELD,MAXLINE)];
5889382Seric 
5899382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
5909382Seric 	{
5919382Seric 		register char *p;
59210689Seric 		extern bool bitintersect();
5939382Seric 
5949382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
59510689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
5969382Seric 			continue;
5979382Seric 
59811414Seric 		/* handle Resent-... headers specially */
59911414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
60011414Seric 			continue;
60111414Seric 
6029382Seric 		p = h->h_value;
6039382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6049382Seric 		{
6059382Seric 			/* macro expand value if generated internally */
6069382Seric 			expand(p, buf, &buf[sizeof buf], e);
6079382Seric 			p = buf;
6089382Seric 			if (p == NULL || *p == '\0')
6099382Seric 				continue;
6109382Seric 		}
6119382Seric 
6129382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6139382Seric 		{
6149382Seric 			/* address field */
6159382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6169382Seric 
6179382Seric 			if (bitset(H_FROM, h->h_flags))
6189382Seric 				oldstyle = FALSE;
61910176Seric 			commaize(h, p, fp, oldstyle, m);
6209382Seric 		}
6219382Seric 		else
6229382Seric 		{
6239382Seric 			/* vanilla header line */
62412159Seric 			register char *nlp;
62512159Seric 
62612159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
62712159Seric 			while ((nlp = index(p, '\n')) != NULL)
62812159Seric 			{
62912159Seric 				*nlp = '\0';
63012159Seric 				(void) strcat(obuf, p);
63112159Seric 				*nlp = '\n';
63212159Seric 				putline(obuf, fp, m);
63312159Seric 				p = ++nlp;
63412161Seric 				obuf[0] = '\0';
63512159Seric 			}
63612159Seric 			(void) strcat(obuf, p);
63710176Seric 			putline(obuf, fp, m);
6389382Seric 		}
6399382Seric 	}
6409382Seric }
6419382Seric /*
6429382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6439382Seric **
6449382Seric **	Parameters:
6459382Seric **		h -- the header field to output.
6469382Seric **		p -- the value to put in it.
6479382Seric **		fp -- file to put it to.
6489382Seric **		oldstyle -- TRUE if this is an old style header.
6499382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6509382Seric **			don't transform the name at all.
6519382Seric **
6529382Seric **	Returns:
6539382Seric **		none.
6549382Seric **
6559382Seric **	Side Effects:
6569382Seric **		outputs "p" to file "fp".
6579382Seric */
6589382Seric 
65910176Seric commaize(h, p, fp, oldstyle, m)
6609382Seric 	register HDR *h;
6619382Seric 	register char *p;
6629382Seric 	FILE *fp;
6639382Seric 	bool oldstyle;
6649382Seric 	register MAILER *m;
6659382Seric {
6669382Seric 	register char *obp;
6679382Seric 	int opos;
6689382Seric 	bool firstone = TRUE;
66911157Seric 	char obuf[MAXLINE + 3];
6709382Seric 
6719382Seric 	/*
6729382Seric 	**  Output the address list translated by the
6739382Seric 	**  mailer and with commas.
6749382Seric 	*/
6759382Seric 
6769382Seric 	if (tTd(14, 2))
6779382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
6789382Seric 
6799382Seric 	obp = obuf;
6809382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
6819382Seric 	opos = strlen(h->h_field) + 2;
6829382Seric 	obp += opos;
6839382Seric 
6849382Seric 	/*
6859382Seric 	**  Run through the list of values.
6869382Seric 	*/
6879382Seric 
6889382Seric 	while (*p != '\0')
6899382Seric 	{
6909382Seric 		register char *name;
6919382Seric 		char savechar;
6929382Seric 		extern char *remotename();
6939382Seric 		extern char *DelimChar;		/* defined in prescan */
6949382Seric 
6959382Seric 		/*
6969382Seric 		**  Find the end of the name.  New style names
6979382Seric 		**  end with a comma, old style names end with
6989382Seric 		**  a space character.  However, spaces do not
6999382Seric 		**  necessarily delimit an old-style name -- at
7009382Seric 		**  signs mean keep going.
7019382Seric 		*/
7029382Seric 
7039382Seric 		/* find end of name */
7049382Seric 		while (isspace(*p) || *p == ',')
7059382Seric 			p++;
7069382Seric 		name = p;
7079382Seric 		for (;;)
7089382Seric 		{
7099382Seric 			char *oldp;
71016909Seric 			char pvpbuf[PSBUFSIZE];
7119382Seric 			extern bool isatword();
7129382Seric 			extern char **prescan();
7139382Seric 
71416909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7159382Seric 			p = DelimChar;
7169382Seric 
7179382Seric 			/* look to see if we have an at sign */
7189382Seric 			oldp = p;
7199382Seric 			while (*p != '\0' && isspace(*p))
7209382Seric 				p++;
7219382Seric 
7229382Seric 			if (*p != '@' && !isatword(p))
7239382Seric 			{
7249382Seric 				p = oldp;
7259382Seric 				break;
7269382Seric 			}
7279382Seric 			p += *p == '@' ? 1 : 2;
7289382Seric 			while (*p != '\0' && isspace(*p))
7299382Seric 				p++;
7309382Seric 		}
7319382Seric 		/* at the end of one complete name */
7329382Seric 
7339382Seric 		/* strip off trailing white space */
7349382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7359382Seric 			p--;
7369382Seric 		if (++p == name)
7379382Seric 			continue;
7389382Seric 		savechar = *p;
7399382Seric 		*p = '\0';
7409382Seric 
7419382Seric 		/* translate the name to be relative */
74210309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7439382Seric 		if (*name == '\0')
7449382Seric 		{
7459382Seric 			*p = savechar;
7469382Seric 			continue;
7479382Seric 		}
7489382Seric 
7499382Seric 		/* output the name with nice formatting */
7509382Seric 		opos += qstrlen(name);
7519382Seric 		if (!firstone)
7529382Seric 			opos += 2;
7539382Seric 		if (opos > 78 && !firstone)
7549382Seric 		{
75510178Seric 			(void) strcpy(obp, ",\n");
75610176Seric 			putline(obuf, fp, m);
7579382Seric 			obp = obuf;
7589382Seric 			(void) sprintf(obp, "        ");
75910161Seric 			opos = strlen(obp);
76010161Seric 			obp += opos;
76110161Seric 			opos += qstrlen(name);
7629382Seric 		}
7639382Seric 		else if (!firstone)
7649382Seric 		{
7659382Seric 			(void) sprintf(obp, ", ");
7669382Seric 			obp += 2;
7679382Seric 		}
7689382Seric 
7699382Seric 		/* strip off quote bits as we output */
77011157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
7719382Seric 		{
7729382Seric 			if (bitset(0200, *name))
7739382Seric 				*obp++ = '\\';
7749382Seric 			*obp++ = *name++ & ~0200;
7759382Seric 		}
7769382Seric 		firstone = FALSE;
7779382Seric 		*p = savechar;
7789382Seric 	}
7799382Seric 	(void) strcpy(obp, "\n");
78010176Seric 	putline(obuf, fp, m);
7819382Seric }
7829382Seric /*
7839382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
7849382Seric **
7859382Seric **	Parameters:
7869382Seric **		p -- word to check.
7879382Seric **
7889382Seric **	Returns:
7899382Seric **		TRUE -- if p is the word at.
7909382Seric **		FALSE -- otherwise.
7919382Seric **
7929382Seric **	Side Effects:
7939382Seric **		none.
7949382Seric */
7959382Seric 
7969382Seric bool
7979382Seric isatword(p)
7989382Seric 	register char *p;
7999382Seric {
8009382Seric 	extern char lower();
8019382Seric 
8029382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8039382Seric 	    p[2] != '\0' && isspace(p[2]))
8049382Seric 		return (TRUE);
8059382Seric 	return (FALSE);
8069382Seric }
807