14091Seric # include <errno.h>
24091Seric # include "sendmail.h"
34091Seric 
4*10689Seric SCCSID(@(#)headers.c	3.49		02/02/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;
34*10689Seric 	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 */
43*10689Seric 	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';
53*10689Seric 			while (*++p != '\0')
54*10689Seric 				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 
804091Seric 	/* search header list 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 	{
845187Seric 		if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags))
854091Seric 			break;
864091Seric 	}
874091Seric 
884091Seric 	/* see if it is a known type */
894091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
904091Seric 	{
914091Seric 		if (strcmp(hi->hi_field, fname) == 0)
924091Seric 			break;
934091Seric 	}
944091Seric 
954091Seric 	/* if this means "end of header" quit now */
964091Seric 	if (bitset(H_EOH, hi->hi_flags))
974091Seric 		return (hi->hi_flags);
984091Seric 
994091Seric 	/* create/fill in a new node */
1005187Seric 	if (h == NULL || bitset(H_FORCE, h->h_flags))
1014091Seric 	{
1024091Seric 		/* create a new node */
1035187Seric 		h = (HDR *) xalloc(sizeof *h);
1044091Seric 		h->h_field = newstr(fname);
1054091Seric 		h->h_value = NULL;
1065187Seric 		h->h_link = *hp;
107*10689Seric 		bcopy(mopts, h->h_mflags, sizeof mopts);
1085187Seric 		*hp = h;
1094091Seric 	}
1108066Seric 	h->h_flags = hi->hi_flags;
1114091Seric 	if (def)
1124091Seric 		h->h_flags |= H_DEFAULT;
1139059Seric 	if (cond)
1149059Seric 		h->h_flags |= H_CHECK;
1154091Seric 	if (h->h_value != NULL)
1169351Seric 		free((char *) h->h_value);
1178066Seric 	h->h_value = newstr(fvalue);
1184091Seric 
1195937Seric 	/* hack to see if this is a new format message */
1208095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1215937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1228089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1238089Seric 	{
1249342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1258089Seric 	}
1265937Seric 
1278066Seric 	/* send to this person if we so desire */
1288066Seric 	if (!def && GrabTo && bitset(H_RCPT, h->h_flags))
1299620Seric 		sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
1308066Seric 
1314091Seric 	return (h->h_flags);
1324091Seric }
1334091Seric /*
1346980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1356980Seric **
1366980Seric **	This bypasses the special checking of chompheader.
1376980Seric **
1386980Seric **	Parameters:
1396980Seric **		field -- the name of the header field.
1406980Seric **		value -- the value of the field.  It must be lower-cased.
1416980Seric **		e -- the envelope to add them to.
1426980Seric **
1436980Seric **	Returns:
1446980Seric **		none.
1456980Seric **
1466980Seric **	Side Effects:
1476980Seric **		adds the field on the list of headers for this envelope.
1486980Seric */
1496980Seric 
1506980Seric addheader(field, value, e)
1516980Seric 	char *field;
1526980Seric 	char *value;
1536980Seric 	ENVELOPE *e;
1546980Seric {
1556980Seric 	register HDR *h;
1566980Seric 	register struct hdrinfo *hi;
1576980Seric 	HDR **hp;
1586980Seric 
1596980Seric 	/* find info struct */
1606980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1616980Seric 	{
1626980Seric 		if (strcmp(field, hi->hi_field) == 0)
1636980Seric 			break;
1646980Seric 	}
1656980Seric 
1666980Seric 	/* find current place in list -- keep back pointer? */
1676980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1686980Seric 	{
1696980Seric 		if (strcmp(field, h->h_field) == 0)
1706980Seric 			break;
1716980Seric 	}
1726980Seric 
1736980Seric 	/* allocate space for new header */
1746980Seric 	h = (HDR *) xalloc(sizeof *h);
1756980Seric 	h->h_field = field;
1766980Seric 	h->h_value = newstr(value);
1777368Seric 	h->h_link = *hp;
1786980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
179*10689Seric 	clrbitmap(h->h_mflags);
1806980Seric 	*hp = h;
1816980Seric }
1826980Seric /*
1834091Seric **  HVALUE -- return value of a header.
1844091Seric **
1854091Seric **	Only "real" fields (i.e., ones that have not been supplied
1864091Seric **	as a default) are used.
1874091Seric **
1884091Seric **	Parameters:
1894091Seric **		field -- the field name.
1904091Seric **
1914091Seric **	Returns:
1924091Seric **		pointer to the value part.
1934091Seric **		NULL if not found.
1944091Seric **
1954091Seric **	Side Effects:
1969382Seric **		none.
1974091Seric */
1984091Seric 
1994091Seric char *
2004091Seric hvalue(field)
2014091Seric 	char *field;
2024091Seric {
2034091Seric 	register HDR *h;
2044091Seric 
2056908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2064091Seric 	{
2074091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2084091Seric 			return (h->h_value);
2094091Seric 	}
2104091Seric 	return (NULL);
2114091Seric }
2124091Seric /*
2134091Seric **  ISHEADER -- predicate telling if argument is a header.
2144091Seric **
2154319Seric **	A line is a header if it has a single word followed by
2164319Seric **	optional white space followed by a colon.
2174319Seric **
2184091Seric **	Parameters:
2194091Seric **		s -- string to check for possible headerness.
2204091Seric **
2214091Seric **	Returns:
2224091Seric **		TRUE if s is a header.
2234091Seric **		FALSE otherwise.
2244091Seric **
2254091Seric **	Side Effects:
2264091Seric **		none.
2274091Seric */
2284091Seric 
2294091Seric bool
2304091Seric isheader(s)
2314091Seric 	register char *s;
2324091Seric {
2339382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2344091Seric 		s++;
2359382Seric 
2369382Seric 	/* following technically violates RFC822 */
2374091Seric 	while (isspace(*s))
2384091Seric 		s++;
2399382Seric 
2404091Seric 	return (*s == ':');
2414091Seric }
2425919Seric /*
2437783Seric **  EATHEADER -- run through the stored header and extract info.
2447783Seric **
2457783Seric **	Parameters:
2469382Seric **		e -- the envelope to process.
2477783Seric **
2487783Seric **	Returns:
2497783Seric **		none.
2507783Seric **
2517783Seric **	Side Effects:
2527783Seric **		Sets a bunch of global variables from information
2539382Seric **			in the collected header.
2549382Seric **		Aborts the message if the hop count is exceeded.
2557783Seric */
2567783Seric 
2579382Seric eatheader(e)
2589382Seric 	register ENVELOPE *e;
2597783Seric {
2607783Seric 	register HDR *h;
2617783Seric 	register char *p;
2629382Seric 	int hopcnt = 0;
2637783Seric 
2649382Seric #ifdef DEBUG
2659382Seric 	if (tTd(32, 1))
2669382Seric 		printf("----- collected header -----\n");
2679382Seric #endif DEBUG
2689382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2697783Seric 	{
2709382Seric #ifdef DEBUG
2717783Seric 		extern char *capitalize();
2727783Seric 
2739382Seric 		if (tTd(32, 1))
2747783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
2759382Seric #endif DEBUG
2769382Seric 		if (bitset(H_TRACE, h->h_flags))
2779382Seric 			hopcnt++;
2789382Seric 	}
2799382Seric #ifdef DEBUG
2809382Seric 	if (tTd(32, 1))
2817783Seric 		printf("----------------------------\n");
2829382Seric #endif DEBUG
2837783Seric 
2849382Seric 	/* store hop count */
2859382Seric 	if (hopcnt > e->e_hopcount)
2869382Seric 		e->e_hopcount = hopcnt;
2879382Seric 
2887783Seric 	/* message priority */
2899382Seric 	p = hvalue("precedence");
2909382Seric 	if (p != NULL)
2919382Seric 		e->e_class = priencode(p);
2927783Seric 	if (!QueueRun)
2939382Seric 		e->e_msgpriority = e->e_msgsize - e->e_class * WKPRIFACT;
2947783Seric 
2958066Seric 	/* return receipt to */
2968066Seric 	p = hvalue("return-receipt-to");
2977783Seric 	if (p != NULL)
2989382Seric 		e->e_receiptto = p;
2997783Seric 
3008253Seric 	/* errors to */
3018253Seric 	p = hvalue("errors-to");
3028253Seric 	if (p != NULL)
3039620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3048253Seric 
3057783Seric 	/* from person */
3069285Seric 	if (OpMode == MD_ARPAFTP)
3078066Seric 	{
3088066Seric 		register struct hdrinfo *hi = HdrInfo;
3097783Seric 
3108066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3118066Seric 		{
3128066Seric 			if (bitset(H_FROM, hi->hi_flags))
3138066Seric 				p = hvalue(hi->hi_field);
3148066Seric 		}
3158066Seric 		if (p != NULL)
3169382Seric 			setsender(p);
3178066Seric 	}
3188066Seric 
3197783Seric 	/* full name of from person */
3207783Seric 	p = hvalue("full-name");
3217783Seric 	if (p != NULL)
3229382Seric 		define('x', p, e);
3237783Seric 
3247783Seric 	/* date message originated */
3257783Seric 	p = hvalue("posted-date");
3267783Seric 	if (p == NULL)
3277783Seric 		p = hvalue("date");
3287783Seric 	if (p != NULL)
3297783Seric 	{
3309382Seric 		define('a', p, e);
3317783Seric 		/* we don't have a good way to do canonical conversion ....
3329382Seric 		define('d', newstr(arpatounix(p)), e);
3337783Seric 		.... so we will ignore the problem for the time being */
3347783Seric 	}
3357783Seric }
3367783Seric /*
3377783Seric **  PRIENCODE -- encode external priority names into internal values.
3387783Seric **
3397783Seric **	Parameters:
3407783Seric **		p -- priority in ascii.
3417783Seric **
3427783Seric **	Returns:
3437783Seric **		priority as a numeric level.
3447783Seric **
3457783Seric **	Side Effects:
3467783Seric **		none.
3477783Seric */
3487783Seric 
3497783Seric priencode(p)
3507783Seric 	char *p;
3517783Seric {
3528253Seric 	register int i;
3537783Seric 	extern bool sameword();
3547783Seric 
3558253Seric 	for (i = 0; i < NumPriorities; i++)
3567783Seric 	{
3578253Seric 		if (sameword(p, Priorities[i].pri_name))
3588253Seric 			return (Priorities[i].pri_val);
3597783Seric 	}
3608253Seric 
3618253Seric 	/* unknown priority */
3628253Seric 	return (0);
3637783Seric }
3647783Seric /*
3657890Seric **  CRACKADDR -- parse an address and turn it into a macro
3667783Seric **
3677783Seric **	This doesn't actually parse the address -- it just extracts
3687783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
3697783Seric **	and isn't even guaranteed to leave something syntactically
3707783Seric **	identical to what it started with.  However, it does leave
3717783Seric **	something semantically identical.
3727783Seric **
3737783Seric **	The process is kind of strange.  There are a number of
3747783Seric **	interesting cases:
3757783Seric **		1.  comment <address> comment	==> comment <$g> comment
3767783Seric **		2.  address			==> address
3777783Seric **		3.  address (comment)		==> $g (comment)
3787783Seric **		4.  (comment) address		==> (comment) $g
3797783Seric **	And then there are the hard cases....
3807783Seric **		5.  add (comment) ress		==> $g (comment)
3817783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
3827783Seric **		7.    .... etc ....
3837783Seric **
3847783Seric **	Parameters:
3857890Seric **		addr -- the address to be cracked.
3867783Seric **
3877783Seric **	Returns:
3887783Seric **		a pointer to the new version.
3897783Seric **
3907783Seric **	Side Effects:
3917890Seric **		none.
3927783Seric **
3937783Seric **	Warning:
3947783Seric **		The return value is saved in local storage and should
3957783Seric **		be copied if it is to be reused.
3967783Seric */
3977783Seric 
3987783Seric char *
3997890Seric crackaddr(addr)
4007890Seric 	register char *addr;
4017783Seric {
4027783Seric 	register char *p;
4037783Seric 	register int i;
4047783Seric 	static char buf[MAXNAME];
4057783Seric 	char *rhs;
4067783Seric 	bool gotaddr;
4077783Seric 	register char *bp;
4087783Seric 
4097783Seric # ifdef DEBUG
4107783Seric 	if (tTd(33, 1))
4117890Seric 		printf("crackaddr(%s)\n", addr);
4127783Seric # endif DEBUG
4137783Seric 
4147783Seric 	strcpy(buf, "");
4157783Seric 	rhs = NULL;
4167783Seric 
4178082Seric 	/* strip leading spaces */
4188082Seric 	while (*addr != '\0' && isspace(*addr))
4198082Seric 		addr++;
4208082Seric 
4217783Seric 	/*
4227783Seric 	**  See if we have anything in angle brackets.  If so, that is
4237783Seric 	**  the address part, and the rest is the comment.
4247783Seric 	*/
4257783Seric 
4267890Seric 	p = index(addr, '<');
4277783Seric 	if (p != NULL)
4287783Seric 	{
4297890Seric 		/* copy the beginning of the addr field to the buffer */
4307783Seric 		*p = '\0';
4317890Seric 		strcpy(buf, addr);
4327783Seric 		strcat(buf, "<");
4338082Seric 		*p++ = '<';
4347783Seric 
4358082Seric 		/* skip spaces */
4368082Seric 		while (isspace(*p))
4378082Seric 			p++;
4388082Seric 
4397783Seric 		/* find the matching right angle bracket */
4408082Seric 		addr = p;
4417783Seric 		for (i = 0; *p != '\0'; p++)
4427783Seric 		{
4437783Seric 			switch (*p)
4447783Seric 			{
4457783Seric 			  case '<':
4467783Seric 				i++;
4477783Seric 				break;
4487783Seric 
4497783Seric 			  case '>':
4507783Seric 				i--;
4517783Seric 				break;
4527783Seric 			}
4537783Seric 			if (i < 0)
4547783Seric 				break;
4557783Seric 		}
4567783Seric 
4577783Seric 		/* p now points to the closing quote (or a null byte) */
4587783Seric 		if (*p != '\0')
4597783Seric 		{
4607783Seric 			/* make rhs point to the extra stuff at the end */
4617783Seric 			rhs = p;
4627783Seric 			*p++ = '\0';
4637783Seric 		}
4647783Seric 	}
4657783Seric 
4667783Seric 	/*
4677944Seric 	**  Now parse the real address part.  "addr" points to the (null
4687783Seric 	**  terminated) version of what we are inerested in; rhs points
4697783Seric 	**  to the extra stuff at the end of the line, if any.
4707783Seric 	*/
4717783Seric 
4727890Seric 	p = addr;
4737783Seric 
4747783Seric 	/* now strip out comments */
4757783Seric 	bp = &buf[strlen(buf)];
4767783Seric 	gotaddr = FALSE;
4777783Seric 	for (; *p != '\0'; p++)
4787783Seric 	{
4797783Seric 		if (*p == '(')
4807783Seric 		{
4817783Seric 			/* copy to matching close paren */
4827783Seric 			*bp++ = *p++;
4837783Seric 			for (i = 0; *p != '\0'; p++)
4847783Seric 			{
4857783Seric 				*bp++ = *p;
4867783Seric 				switch (*p)
4877783Seric 				{
4887783Seric 				  case '(':
4897783Seric 					i++;
4907783Seric 					break;
4917783Seric 
4927783Seric 				  case ')':
4937783Seric 					i--;
4947783Seric 					break;
4957783Seric 				}
4967783Seric 				if (i < 0)
4977783Seric 					break;
4987783Seric 			}
4997783Seric 			continue;
5007783Seric 		}
5017783Seric 
5027783Seric 		/*
5037783Seric 		**  If this is the first "real" character we have seen,
5047783Seric 		**  then we put the "$g" in the buffer now.
5057783Seric 		*/
5067783Seric 
5077783Seric 		if (isspace(*p))
5087783Seric 			*bp++ = *p;
5097783Seric 		else if (!gotaddr)
5107783Seric 		{
5117783Seric 			strcpy(bp, "$g");
5127783Seric 			bp += 2;
5137783Seric 			gotaddr = TRUE;
5147783Seric 		}
5157783Seric 	}
5167783Seric 
5177944Seric 	/* hack, hack.... strip trailing blanks */
5187944Seric 	do
5197944Seric 	{
5207944Seric 		*bp-- = '\0';
5217944Seric 	} while (isspace(*bp));
5227944Seric 	bp++;
5237783Seric 
5247944Seric 	/* put any right hand side back on */
5257783Seric 	if (rhs != NULL)
5267783Seric 	{
5277783Seric 		*rhs = '>';
5287783Seric 		strcpy(bp, rhs);
5297783Seric 	}
5307783Seric 
5317783Seric # ifdef DEBUG
5327783Seric 	if (tTd(33, 1))
5337944Seric 		printf("crackaddr=>`%s'\n", buf);
5347783Seric # endif DEBUG
5357783Seric 
5367783Seric 	return (buf);
5377783Seric }
5389382Seric /*
5399382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
5409382Seric **
5419382Seric **	Parameters:
5429382Seric **		fp -- file to put it on.
5439382Seric **		m -- mailer to use.
5449382Seric **		e -- envelope to use.
5459382Seric **
5469382Seric **	Returns:
5479382Seric **		none.
5489382Seric **
5499382Seric **	Side Effects:
5509382Seric **		none.
5519382Seric */
5529382Seric 
55310176Seric putheader(fp, m, e)
5549382Seric 	register FILE *fp;
5559382Seric 	register MAILER *m;
5569382Seric 	register ENVELOPE *e;
5579382Seric {
5589382Seric 	char buf[BUFSIZ];
5599382Seric 	register HDR *h;
5609382Seric 	extern char *arpadate();
5619382Seric 	extern char *capitalize();
5629382Seric 	char obuf[MAXLINE];
5639382Seric 
5649382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
5659382Seric 	{
5669382Seric 		register char *p;
567*10689Seric 		extern bool bitintersect();
5689382Seric 
5699382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
570*10689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
5719382Seric 			continue;
5729382Seric 
5739382Seric 		p = h->h_value;
5749382Seric 		if (bitset(H_DEFAULT, h->h_flags))
5759382Seric 		{
5769382Seric 			/* macro expand value if generated internally */
5779382Seric 			expand(p, buf, &buf[sizeof buf], e);
5789382Seric 			p = buf;
5799382Seric 			if (p == NULL || *p == '\0')
5809382Seric 				continue;
5819382Seric 		}
5829382Seric 
5839382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
5849382Seric 		{
5859382Seric 			/* address field */
5869382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
5879382Seric 
5889382Seric 			if (bitset(H_FROM, h->h_flags))
5899382Seric 				oldstyle = FALSE;
59010176Seric 			commaize(h, p, fp, oldstyle, m);
5919382Seric 		}
5929382Seric 		else
5939382Seric 		{
5949382Seric 			/* vanilla header line */
5959382Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
59610176Seric 			putline(obuf, fp, m);
5979382Seric 		}
5989382Seric 	}
5999382Seric }
6009382Seric /*
6019382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6029382Seric **
6039382Seric **	Parameters:
6049382Seric **		h -- the header field to output.
6059382Seric **		p -- the value to put in it.
6069382Seric **		fp -- file to put it to.
6079382Seric **		oldstyle -- TRUE if this is an old style header.
6089382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6099382Seric **			don't transform the name at all.
6109382Seric **
6119382Seric **	Returns:
6129382Seric **		none.
6139382Seric **
6149382Seric **	Side Effects:
6159382Seric **		outputs "p" to file "fp".
6169382Seric */
6179382Seric 
61810176Seric commaize(h, p, fp, oldstyle, m)
6199382Seric 	register HDR *h;
6209382Seric 	register char *p;
6219382Seric 	FILE *fp;
6229382Seric 	bool oldstyle;
6239382Seric 	register MAILER *m;
6249382Seric {
6259382Seric 	register char *obp;
6269382Seric 	int opos;
6279382Seric 	bool firstone = TRUE;
6289382Seric 	char obuf[MAXLINE];
6299382Seric 
6309382Seric 	/*
6319382Seric 	**  Output the address list translated by the
6329382Seric 	**  mailer and with commas.
6339382Seric 	*/
6349382Seric 
6359382Seric # ifdef DEBUG
6369382Seric 	if (tTd(14, 2))
6379382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
6389382Seric # endif DEBUG
6399382Seric 
6409382Seric 	obp = obuf;
6419382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
6429382Seric 	opos = strlen(h->h_field) + 2;
6439382Seric 	obp += opos;
6449382Seric 
6459382Seric 	/*
6469382Seric 	**  Run through the list of values.
6479382Seric 	*/
6489382Seric 
6499382Seric 	while (*p != '\0')
6509382Seric 	{
6519382Seric 		register char *name;
6529382Seric 		char savechar;
6539382Seric 		extern char *remotename();
6549382Seric 		extern char *DelimChar;		/* defined in prescan */
6559382Seric 
6569382Seric 		/*
6579382Seric 		**  Find the end of the name.  New style names
6589382Seric 		**  end with a comma, old style names end with
6599382Seric 		**  a space character.  However, spaces do not
6609382Seric 		**  necessarily delimit an old-style name -- at
6619382Seric 		**  signs mean keep going.
6629382Seric 		*/
6639382Seric 
6649382Seric 		/* find end of name */
6659382Seric 		while (isspace(*p) || *p == ',')
6669382Seric 			p++;
6679382Seric 		name = p;
6689382Seric 		for (;;)
6699382Seric 		{
6709382Seric 			char *oldp;
6719382Seric 			extern bool isatword();
6729382Seric 			extern char **prescan();
6739382Seric 
6749382Seric 			(void) prescan(p, oldstyle ? ' ' : ',');
6759382Seric 			p = DelimChar;
6769382Seric 
6779382Seric 			/* look to see if we have an at sign */
6789382Seric 			oldp = p;
6799382Seric 			while (*p != '\0' && isspace(*p))
6809382Seric 				p++;
6819382Seric 
6829382Seric 			if (*p != '@' && !isatword(p))
6839382Seric 			{
6849382Seric 				p = oldp;
6859382Seric 				break;
6869382Seric 			}
6879382Seric 			p += *p == '@' ? 1 : 2;
6889382Seric 			while (*p != '\0' && isspace(*p))
6899382Seric 				p++;
6909382Seric 		}
6919382Seric 		/* at the end of one complete name */
6929382Seric 
6939382Seric 		/* strip off trailing white space */
6949382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
6959382Seric 			p--;
6969382Seric 		if (++p == name)
6979382Seric 			continue;
6989382Seric 		savechar = *p;
6999382Seric 		*p = '\0';
7009382Seric 
7019382Seric 		/* translate the name to be relative */
70210309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7039382Seric 		if (*name == '\0')
7049382Seric 		{
7059382Seric 			*p = savechar;
7069382Seric 			continue;
7079382Seric 		}
7089382Seric 
7099382Seric 		/* output the name with nice formatting */
7109382Seric 		opos += qstrlen(name);
7119382Seric 		if (!firstone)
7129382Seric 			opos += 2;
7139382Seric 		if (opos > 78 && !firstone)
7149382Seric 		{
71510178Seric 			(void) strcpy(obp, ",\n");
71610176Seric 			putline(obuf, fp, m);
7179382Seric 			obp = obuf;
7189382Seric 			(void) sprintf(obp, "        ");
71910161Seric 			opos = strlen(obp);
72010161Seric 			obp += opos;
72110161Seric 			opos += qstrlen(name);
7229382Seric 		}
7239382Seric 		else if (!firstone)
7249382Seric 		{
7259382Seric 			(void) sprintf(obp, ", ");
7269382Seric 			obp += 2;
7279382Seric 		}
7289382Seric 
7299382Seric 		/* strip off quote bits as we output */
7309382Seric 		while (*name != '\0')
7319382Seric 		{
7329382Seric 			if (bitset(0200, *name))
7339382Seric 				*obp++ = '\\';
7349382Seric 			*obp++ = *name++ & ~0200;
7359382Seric 		}
7369382Seric 		firstone = FALSE;
7379382Seric 		*p = savechar;
7389382Seric 	}
7399382Seric 	(void) strcpy(obp, "\n");
74010176Seric 	putline(obuf, fp, m);
7419382Seric }
7429382Seric /*
7439382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
7449382Seric **
7459382Seric **	Parameters:
7469382Seric **		p -- word to check.
7479382Seric **
7489382Seric **	Returns:
7499382Seric **		TRUE -- if p is the word at.
7509382Seric **		FALSE -- otherwise.
7519382Seric **
7529382Seric **	Side Effects:
7539382Seric **		none.
7549382Seric */
7559382Seric 
7569382Seric bool
7579382Seric isatword(p)
7589382Seric 	register char *p;
7599382Seric {
7609382Seric 	extern char lower();
7619382Seric 
7629382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
7639382Seric 	    p[2] != '\0' && isspace(p[2]))
7649382Seric 		return (TRUE);
7659382Seric 	return (FALSE);
7669382Seric }
767