122706Sdist /*
2*33729Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*33729Sbostic  * All rights reserved.
4*33729Sbostic  *
5*33729Sbostic  * Redistribution and use in source and binary forms are permitted
6*33729Sbostic  * provided that this notice is preserved and that due credit is given
7*33729Sbostic  * to the University of California at Berkeley. The name of the University
8*33729Sbostic  * may not be used to endorse or promote products derived from this
9*33729Sbostic  * software without specific prior written permission. This software
10*33729Sbostic  * is provided ``as is'' without express or implied warranty.
11*33729Sbostic  *
12*33729Sbostic  *  Sendmail
13*33729Sbostic  *  Copyright (c) 1983  Eric P. Allman
14*33729Sbostic  *  Berkeley, California
15*33729Sbostic  */
1622706Sdist 
1722706Sdist #ifndef lint
18*33729Sbostic static char sccsid[] = "@(#)headers.c	5.9 (Berkeley) 03/13/88";
19*33729Sbostic #endif /* not lint */
2022706Sdist 
214091Seric # include <errno.h>
224091Seric # include "sendmail.h"
234091Seric 
244091Seric /*
254091Seric **  CHOMPHEADER -- process and save a header line.
264091Seric **
274091Seric **	Called by collect and by readcf to deal with header lines.
284091Seric **
294091Seric **	Parameters:
304091Seric **		line -- header as a text line.
314091Seric **		def -- if set, this is a default value.
324091Seric **
334091Seric **	Returns:
344091Seric **		flags for this header.
354091Seric **
364091Seric **	Side Effects:
374091Seric **		The header is saved on the header list.
384319Seric **		Contents of 'line' are destroyed.
394091Seric */
404091Seric 
414091Seric chompheader(line, def)
424091Seric 	char *line;
434091Seric 	bool def;
444091Seric {
454091Seric 	register char *p;
464091Seric 	register HDR *h;
474091Seric 	HDR **hp;
484091Seric 	char *fname;
494091Seric 	char *fvalue;
504091Seric 	struct hdrinfo *hi;
519059Seric 	bool cond = FALSE;
5210689Seric 	BITMAP mopts;
537890Seric 	extern char *crackaddr();
544091Seric 
557677Seric # ifdef DEBUG
567677Seric 	if (tTd(31, 6))
577677Seric 		printf("chompheader: %s\n", line);
587677Seric # endif DEBUG
597677Seric 
604627Seric 	/* strip off options */
6110689Seric 	clrbitmap(mopts);
624627Seric 	p = line;
634627Seric 	if (*p == '?')
644627Seric 	{
654627Seric 		/* have some */
664627Seric 		register char *q = index(p + 1, *p);
674627Seric 
684627Seric 		if (q != NULL)
694627Seric 		{
704627Seric 			*q++ = '\0';
7110689Seric 			while (*++p != '\0')
7210689Seric 				setbitn(*p, mopts);
734627Seric 			p = q;
744627Seric 		}
754627Seric 		else
764627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
779059Seric 		cond = TRUE;
784627Seric 	}
794627Seric 
804091Seric 	/* find canonical name */
814627Seric 	fname = p;
824627Seric 	p = index(p, ':');
8310118Seric 	if (p == NULL)
8410118Seric 	{
8510118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
8610118Seric 		return (0);
8710118Seric 	}
884091Seric 	fvalue = &p[1];
894091Seric 	while (isspace(*--p))
904091Seric 		continue;
914091Seric 	*++p = '\0';
924091Seric 	makelower(fname);
934091Seric 
944091Seric 	/* strip field value on front */
954091Seric 	if (*fvalue == ' ')
964091Seric 		fvalue++;
974091Seric 
984091Seric 	/* see if it is a known type */
994091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1004091Seric 	{
1014091Seric 		if (strcmp(hi->hi_field, fname) == 0)
1024091Seric 			break;
1034091Seric 	}
1044091Seric 
10511414Seric 	/* see if this is a resent message */
10611930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10711414Seric 		CurEnv->e_flags |= EF_RESENT;
10811414Seric 
1094091Seric 	/* if this means "end of header" quit now */
1104091Seric 	if (bitset(H_EOH, hi->hi_flags))
1114091Seric 		return (hi->hi_flags);
1124091Seric 
11311414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
11414784Seric 	p = "resent-from";
11514784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
11614784Seric 		p += 7;
11714784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
11811414Seric 	{
11924941Seric 		if (CurEnv->e_from.q_paddr != NULL &&
12024941Seric 		    strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
12111414Seric 			return (hi->hi_flags);
12211414Seric 	}
12311414Seric 
12414784Seric 	/* delete default value for this header */
12514784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
12614784Seric 	{
12714784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12814784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12914784Seric 		    !bitset(H_FORCE, h->h_flags))
13014784Seric 			h->h_value = NULL;
13114784Seric 	}
13214784Seric 
13313012Seric 	/* create a new node */
13413012Seric 	h = (HDR *) xalloc(sizeof *h);
13513012Seric 	h->h_field = newstr(fname);
13613012Seric 	h->h_value = NULL;
13713012Seric 	h->h_link = NULL;
13823117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13913012Seric 	*hp = h;
1408066Seric 	h->h_flags = hi->hi_flags;
1414091Seric 	if (def)
1424091Seric 		h->h_flags |= H_DEFAULT;
1439059Seric 	if (cond)
1449059Seric 		h->h_flags |= H_CHECK;
1454091Seric 	if (h->h_value != NULL)
1469351Seric 		free((char *) h->h_value);
1478066Seric 	h->h_value = newstr(fvalue);
1484091Seric 
1495937Seric 	/* hack to see if this is a new format message */
1508095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1515937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1528089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1538089Seric 	{
1549342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1558089Seric 	}
1565937Seric 
1574091Seric 	return (h->h_flags);
1584091Seric }
1594091Seric /*
1606980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1616980Seric **
1626980Seric **	This bypasses the special checking of chompheader.
1636980Seric **
1646980Seric **	Parameters:
1656980Seric **		field -- the name of the header field.
1666980Seric **		value -- the value of the field.  It must be lower-cased.
1676980Seric **		e -- the envelope to add them to.
1686980Seric **
1696980Seric **	Returns:
1706980Seric **		none.
1716980Seric **
1726980Seric **	Side Effects:
1736980Seric **		adds the field on the list of headers for this envelope.
1746980Seric */
1756980Seric 
1766980Seric addheader(field, value, e)
1776980Seric 	char *field;
1786980Seric 	char *value;
1796980Seric 	ENVELOPE *e;
1806980Seric {
1816980Seric 	register HDR *h;
1826980Seric 	register struct hdrinfo *hi;
1836980Seric 	HDR **hp;
1846980Seric 
1856980Seric 	/* find info struct */
1866980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1876980Seric 	{
1886980Seric 		if (strcmp(field, hi->hi_field) == 0)
1896980Seric 			break;
1906980Seric 	}
1916980Seric 
1926980Seric 	/* find current place in list -- keep back pointer? */
1936980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1946980Seric 	{
1956980Seric 		if (strcmp(field, h->h_field) == 0)
1966980Seric 			break;
1976980Seric 	}
1986980Seric 
1996980Seric 	/* allocate space for new header */
2006980Seric 	h = (HDR *) xalloc(sizeof *h);
2016980Seric 	h->h_field = field;
2026980Seric 	h->h_value = newstr(value);
2037368Seric 	h->h_link = *hp;
2046980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20510689Seric 	clrbitmap(h->h_mflags);
2066980Seric 	*hp = h;
2076980Seric }
2086980Seric /*
2094091Seric **  HVALUE -- return value of a header.
2104091Seric **
2114091Seric **	Only "real" fields (i.e., ones that have not been supplied
2124091Seric **	as a default) are used.
2134091Seric **
2144091Seric **	Parameters:
2154091Seric **		field -- the field name.
2164091Seric **
2174091Seric **	Returns:
2184091Seric **		pointer to the value part.
2194091Seric **		NULL if not found.
2204091Seric **
2214091Seric **	Side Effects:
2229382Seric **		none.
2234091Seric */
2244091Seric 
2254091Seric char *
2264091Seric hvalue(field)
2274091Seric 	char *field;
2284091Seric {
2294091Seric 	register HDR *h;
2304091Seric 
2316908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2324091Seric 	{
2334091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2344091Seric 			return (h->h_value);
2354091Seric 	}
2364091Seric 	return (NULL);
2374091Seric }
2384091Seric /*
2394091Seric **  ISHEADER -- predicate telling if argument is a header.
2404091Seric **
2414319Seric **	A line is a header if it has a single word followed by
2424319Seric **	optional white space followed by a colon.
2434319Seric **
2444091Seric **	Parameters:
2454091Seric **		s -- string to check for possible headerness.
2464091Seric **
2474091Seric **	Returns:
2484091Seric **		TRUE if s is a header.
2494091Seric **		FALSE otherwise.
2504091Seric **
2514091Seric **	Side Effects:
2524091Seric **		none.
2534091Seric */
2544091Seric 
2554091Seric bool
2564091Seric isheader(s)
2574091Seric 	register char *s;
2584091Seric {
2599382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2604091Seric 		s++;
2619382Seric 
2629382Seric 	/* following technically violates RFC822 */
2634091Seric 	while (isspace(*s))
2644091Seric 		s++;
2659382Seric 
2664091Seric 	return (*s == ':');
2674091Seric }
2685919Seric /*
2697783Seric **  EATHEADER -- run through the stored header and extract info.
2707783Seric **
2717783Seric **	Parameters:
2729382Seric **		e -- the envelope to process.
2737783Seric **
2747783Seric **	Returns:
2757783Seric **		none.
2767783Seric **
2777783Seric **	Side Effects:
2787783Seric **		Sets a bunch of global variables from information
2799382Seric **			in the collected header.
2809382Seric **		Aborts the message if the hop count is exceeded.
2817783Seric */
2827783Seric 
2839382Seric eatheader(e)
2849382Seric 	register ENVELOPE *e;
2857783Seric {
2867783Seric 	register HDR *h;
2877783Seric 	register char *p;
2889382Seric 	int hopcnt = 0;
2897783Seric 
2909382Seric #ifdef DEBUG
2919382Seric 	if (tTd(32, 1))
2929382Seric 		printf("----- collected header -----\n");
2939382Seric #endif DEBUG
2949382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2957783Seric 	{
2969382Seric #ifdef DEBUG
2977783Seric 		extern char *capitalize();
2987783Seric 
2999382Seric 		if (tTd(32, 1))
3007783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
3019382Seric #endif DEBUG
30211414Seric 		/* count the number of times it has been processed */
3039382Seric 		if (bitset(H_TRACE, h->h_flags))
3049382Seric 			hopcnt++;
30511414Seric 
30611414Seric 		/* send to this person if we so desire */
30711414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30811414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30911414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
31011414Seric 		{
31111414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
31211414Seric 		}
31311414Seric 
31411414Seric 		/* log the message-id */
31511290Seric #ifdef LOG
31613103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31711299Seric 		    strcmp(h->h_field, "message-id") == 0)
31811290Seric 		{
31911290Seric 			char buf[MAXNAME];
32011290Seric 
32111290Seric 			p = h->h_value;
32211290Seric 			if (bitset(H_DEFAULT, h->h_flags))
32311290Seric 			{
32411290Seric 				expand(p, buf, &buf[sizeof buf], e);
32511290Seric 				p = buf;
32611290Seric 			}
32711290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
32811290Seric 		}
32911290Seric #endif LOG
3309382Seric 	}
3319382Seric #ifdef DEBUG
3329382Seric 	if (tTd(32, 1))
3337783Seric 		printf("----------------------------\n");
3349382Seric #endif DEBUG
3357783Seric 
3369382Seric 	/* store hop count */
3379382Seric 	if (hopcnt > e->e_hopcount)
3389382Seric 		e->e_hopcount = hopcnt;
3399382Seric 
3407783Seric 	/* message priority */
3419382Seric 	p = hvalue("precedence");
3429382Seric 	if (p != NULL)
3439382Seric 		e->e_class = priencode(p);
3447783Seric 	if (!QueueRun)
34525013Seric 		e->e_msgpriority = e->e_msgsize
34624981Seric 				 - e->e_class * WkClassFact
34724981Seric 				 + e->e_nrcpts * WkRecipFact;
3487783Seric 
3498066Seric 	/* return receipt to */
3508066Seric 	p = hvalue("return-receipt-to");
3517783Seric 	if (p != NULL)
3529382Seric 		e->e_receiptto = p;
3537783Seric 
3548253Seric 	/* errors to */
3558253Seric 	p = hvalue("errors-to");
3568253Seric 	if (p != NULL)
3579620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3588253Seric 
3597783Seric 	/* from person */
3609285Seric 	if (OpMode == MD_ARPAFTP)
3618066Seric 	{
3628066Seric 		register struct hdrinfo *hi = HdrInfo;
3637783Seric 
3648066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3658066Seric 		{
3668066Seric 			if (bitset(H_FROM, hi->hi_flags))
3678066Seric 				p = hvalue(hi->hi_field);
3688066Seric 		}
3698066Seric 		if (p != NULL)
3709382Seric 			setsender(p);
3718066Seric 	}
3728066Seric 
3737783Seric 	/* full name of from person */
3747783Seric 	p = hvalue("full-name");
3757783Seric 	if (p != NULL)
3769382Seric 		define('x', p, e);
3777783Seric 
3787783Seric 	/* date message originated */
3797783Seric 	p = hvalue("posted-date");
3807783Seric 	if (p == NULL)
3817783Seric 		p = hvalue("date");
3827783Seric 	if (p != NULL)
3837783Seric 	{
3849382Seric 		define('a', p, e);
3857783Seric 		/* we don't have a good way to do canonical conversion ....
3869382Seric 		define('d', newstr(arpatounix(p)), e);
3877783Seric 		.... so we will ignore the problem for the time being */
3887783Seric 	}
38911290Seric 
39011290Seric 	/*
39111290Seric 	**  Log collection information.
39211290Seric 	*/
39311290Seric 
39411290Seric # ifdef LOG
39511299Seric 	if (!QueueRun && LogLevel > 1)
39611290Seric 	{
39711290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
39811290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
39911290Seric 		       CurEnv->e_class);
40011290Seric 	}
40111290Seric # endif LOG
4027783Seric }
4037783Seric /*
4047783Seric **  PRIENCODE -- encode external priority names into internal values.
4057783Seric **
4067783Seric **	Parameters:
4077783Seric **		p -- priority in ascii.
4087783Seric **
4097783Seric **	Returns:
4107783Seric **		priority as a numeric level.
4117783Seric **
4127783Seric **	Side Effects:
4137783Seric **		none.
4147783Seric */
4157783Seric 
4167783Seric priencode(p)
4177783Seric 	char *p;
4187783Seric {
4198253Seric 	register int i;
4207783Seric 
4218253Seric 	for (i = 0; i < NumPriorities; i++)
4227783Seric 	{
42333725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4248253Seric 			return (Priorities[i].pri_val);
4257783Seric 	}
4268253Seric 
4278253Seric 	/* unknown priority */
4288253Seric 	return (0);
4297783Seric }
4307783Seric /*
4317890Seric **  CRACKADDR -- parse an address and turn it into a macro
4327783Seric **
4337783Seric **	This doesn't actually parse the address -- it just extracts
4347783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4357783Seric **	and isn't even guaranteed to leave something syntactically
4367783Seric **	identical to what it started with.  However, it does leave
4377783Seric **	something semantically identical.
4387783Seric **
4397783Seric **	The process is kind of strange.  There are a number of
4407783Seric **	interesting cases:
4417783Seric **		1.  comment <address> comment	==> comment <$g> comment
4427783Seric **		2.  address			==> address
4437783Seric **		3.  address (comment)		==> $g (comment)
4447783Seric **		4.  (comment) address		==> (comment) $g
4457783Seric **	And then there are the hard cases....
4467783Seric **		5.  add (comment) ress		==> $g (comment)
4477783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4487783Seric **		7.    .... etc ....
4497783Seric **
4507783Seric **	Parameters:
4517890Seric **		addr -- the address to be cracked.
4527783Seric **
4537783Seric **	Returns:
4547783Seric **		a pointer to the new version.
4557783Seric **
4567783Seric **	Side Effects:
4577890Seric **		none.
4587783Seric **
4597783Seric **	Warning:
4607783Seric **		The return value is saved in local storage and should
4617783Seric **		be copied if it is to be reused.
4627783Seric */
4637783Seric 
4647783Seric char *
4657890Seric crackaddr(addr)
4667890Seric 	register char *addr;
4677783Seric {
4687783Seric 	register char *p;
4697783Seric 	register int i;
4707783Seric 	static char buf[MAXNAME];
4717783Seric 	char *rhs;
4727783Seric 	bool gotaddr;
4737783Seric 	register char *bp;
4747783Seric 
4757783Seric # ifdef DEBUG
4767783Seric 	if (tTd(33, 1))
4777890Seric 		printf("crackaddr(%s)\n", addr);
4787783Seric # endif DEBUG
4797783Seric 
48023099Seric 	(void) strcpy(buf, "");
4817783Seric 	rhs = NULL;
4827783Seric 
4838082Seric 	/* strip leading spaces */
4848082Seric 	while (*addr != '\0' && isspace(*addr))
4858082Seric 		addr++;
4868082Seric 
4877783Seric 	/*
4887783Seric 	**  See if we have anything in angle brackets.  If so, that is
4897783Seric 	**  the address part, and the rest is the comment.
4907783Seric 	*/
4917783Seric 
4927890Seric 	p = index(addr, '<');
4937783Seric 	if (p != NULL)
4947783Seric 	{
4957890Seric 		/* copy the beginning of the addr field to the buffer */
4967783Seric 		*p = '\0';
49723099Seric 		(void) strcpy(buf, addr);
49823099Seric 		(void) strcat(buf, "<");
4998082Seric 		*p++ = '<';
5007783Seric 
5018082Seric 		/* skip spaces */
5028082Seric 		while (isspace(*p))
5038082Seric 			p++;
5048082Seric 
5057783Seric 		/* find the matching right angle bracket */
5068082Seric 		addr = p;
5077783Seric 		for (i = 0; *p != '\0'; p++)
5087783Seric 		{
5097783Seric 			switch (*p)
5107783Seric 			{
5117783Seric 			  case '<':
5127783Seric 				i++;
5137783Seric 				break;
5147783Seric 
5157783Seric 			  case '>':
5167783Seric 				i--;
5177783Seric 				break;
5187783Seric 			}
5197783Seric 			if (i < 0)
5207783Seric 				break;
5217783Seric 		}
5227783Seric 
5237783Seric 		/* p now points to the closing quote (or a null byte) */
5247783Seric 		if (*p != '\0')
5257783Seric 		{
5267783Seric 			/* make rhs point to the extra stuff at the end */
5277783Seric 			rhs = p;
5287783Seric 			*p++ = '\0';
5297783Seric 		}
5307783Seric 	}
5317783Seric 
5327783Seric 	/*
5337944Seric 	**  Now parse the real address part.  "addr" points to the (null
5347783Seric 	**  terminated) version of what we are inerested in; rhs points
5357783Seric 	**  to the extra stuff at the end of the line, if any.
5367783Seric 	*/
5377783Seric 
5387890Seric 	p = addr;
5397783Seric 
5407783Seric 	/* now strip out comments */
5417783Seric 	bp = &buf[strlen(buf)];
5427783Seric 	gotaddr = FALSE;
5437783Seric 	for (; *p != '\0'; p++)
5447783Seric 	{
5457783Seric 		if (*p == '(')
5467783Seric 		{
5477783Seric 			/* copy to matching close paren */
5487783Seric 			*bp++ = *p++;
5497783Seric 			for (i = 0; *p != '\0'; p++)
5507783Seric 			{
5517783Seric 				*bp++ = *p;
5527783Seric 				switch (*p)
5537783Seric 				{
5547783Seric 				  case '(':
5557783Seric 					i++;
5567783Seric 					break;
5577783Seric 
5587783Seric 				  case ')':
5597783Seric 					i--;
5607783Seric 					break;
5617783Seric 				}
5627783Seric 				if (i < 0)
5637783Seric 					break;
5647783Seric 			}
5657783Seric 			continue;
5667783Seric 		}
5677783Seric 
5687783Seric 		/*
5697783Seric 		**  If this is the first "real" character we have seen,
5707783Seric 		**  then we put the "$g" in the buffer now.
5717783Seric 		*/
5727783Seric 
5737783Seric 		if (isspace(*p))
5747783Seric 			*bp++ = *p;
5757783Seric 		else if (!gotaddr)
5767783Seric 		{
57723099Seric 			(void) strcpy(bp, "\001g");
5787783Seric 			bp += 2;
5797783Seric 			gotaddr = TRUE;
5807783Seric 		}
5817783Seric 	}
5827783Seric 
5837944Seric 	/* hack, hack.... strip trailing blanks */
5847944Seric 	do
5857944Seric 	{
5867944Seric 		*bp-- = '\0';
5877944Seric 	} while (isspace(*bp));
5887944Seric 	bp++;
5897783Seric 
5907944Seric 	/* put any right hand side back on */
5917783Seric 	if (rhs != NULL)
5927783Seric 	{
5937783Seric 		*rhs = '>';
59423099Seric 		(void) strcpy(bp, rhs);
5957783Seric 	}
5967783Seric 
5977783Seric # ifdef DEBUG
5987783Seric 	if (tTd(33, 1))
5997944Seric 		printf("crackaddr=>`%s'\n", buf);
6007783Seric # endif DEBUG
6017783Seric 
6027783Seric 	return (buf);
6037783Seric }
6049382Seric /*
6059382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6069382Seric **
6079382Seric **	Parameters:
6089382Seric **		fp -- file to put it on.
6099382Seric **		m -- mailer to use.
6109382Seric **		e -- envelope to use.
6119382Seric **
6129382Seric **	Returns:
6139382Seric **		none.
6149382Seric **
6159382Seric **	Side Effects:
6169382Seric **		none.
6179382Seric */
6189382Seric 
61910176Seric putheader(fp, m, e)
6209382Seric 	register FILE *fp;
6219382Seric 	register MAILER *m;
6229382Seric 	register ENVELOPE *e;
6239382Seric {
6249382Seric 	char buf[BUFSIZ];
6259382Seric 	register HDR *h;
6269382Seric 	extern char *arpadate();
6279382Seric 	extern char *capitalize();
6289382Seric 	char obuf[MAXLINE];
6299382Seric 
6309382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6319382Seric 	{
6329382Seric 		register char *p;
63310689Seric 		extern bool bitintersect();
6349382Seric 
6359382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
63610689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6379382Seric 			continue;
6389382Seric 
63911414Seric 		/* handle Resent-... headers specially */
64011414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
64111414Seric 			continue;
64211414Seric 
6439382Seric 		p = h->h_value;
6449382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6459382Seric 		{
6469382Seric 			/* macro expand value if generated internally */
6479382Seric 			expand(p, buf, &buf[sizeof buf], e);
6489382Seric 			p = buf;
6499382Seric 			if (p == NULL || *p == '\0')
6509382Seric 				continue;
6519382Seric 		}
6529382Seric 
6539382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6549382Seric 		{
6559382Seric 			/* address field */
6569382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6579382Seric 
6589382Seric 			if (bitset(H_FROM, h->h_flags))
6599382Seric 				oldstyle = FALSE;
66010176Seric 			commaize(h, p, fp, oldstyle, m);
6619382Seric 		}
6629382Seric 		else
6639382Seric 		{
6649382Seric 			/* vanilla header line */
66512159Seric 			register char *nlp;
66612159Seric 
66712159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
66812159Seric 			while ((nlp = index(p, '\n')) != NULL)
66912159Seric 			{
67012159Seric 				*nlp = '\0';
67112159Seric 				(void) strcat(obuf, p);
67212159Seric 				*nlp = '\n';
67312159Seric 				putline(obuf, fp, m);
67412159Seric 				p = ++nlp;
67512161Seric 				obuf[0] = '\0';
67612159Seric 			}
67712159Seric 			(void) strcat(obuf, p);
67810176Seric 			putline(obuf, fp, m);
6799382Seric 		}
6809382Seric 	}
6819382Seric }
6829382Seric /*
6839382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6849382Seric **
6859382Seric **	Parameters:
6869382Seric **		h -- the header field to output.
6879382Seric **		p -- the value to put in it.
6889382Seric **		fp -- file to put it to.
6899382Seric **		oldstyle -- TRUE if this is an old style header.
6909382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6919382Seric **			don't transform the name at all.
6929382Seric **
6939382Seric **	Returns:
6949382Seric **		none.
6959382Seric **
6969382Seric **	Side Effects:
6979382Seric **		outputs "p" to file "fp".
6989382Seric */
6999382Seric 
70010176Seric commaize(h, p, fp, oldstyle, m)
7019382Seric 	register HDR *h;
7029382Seric 	register char *p;
7039382Seric 	FILE *fp;
7049382Seric 	bool oldstyle;
7059382Seric 	register MAILER *m;
7069382Seric {
7079382Seric 	register char *obp;
7089382Seric 	int opos;
7099382Seric 	bool firstone = TRUE;
71011157Seric 	char obuf[MAXLINE + 3];
7119382Seric 
7129382Seric 	/*
7139382Seric 	**  Output the address list translated by the
7149382Seric 	**  mailer and with commas.
7159382Seric 	*/
7169382Seric 
7179382Seric # ifdef DEBUG
7189382Seric 	if (tTd(14, 2))
7199382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7209382Seric # endif DEBUG
7219382Seric 
7229382Seric 	obp = obuf;
7239382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7249382Seric 	opos = strlen(h->h_field) + 2;
7259382Seric 	obp += opos;
7269382Seric 
7279382Seric 	/*
7289382Seric 	**  Run through the list of values.
7299382Seric 	*/
7309382Seric 
7319382Seric 	while (*p != '\0')
7329382Seric 	{
7339382Seric 		register char *name;
7349382Seric 		char savechar;
7359382Seric 		extern char *remotename();
7369382Seric 		extern char *DelimChar;		/* defined in prescan */
7379382Seric 
7389382Seric 		/*
7399382Seric 		**  Find the end of the name.  New style names
7409382Seric 		**  end with a comma, old style names end with
7419382Seric 		**  a space character.  However, spaces do not
7429382Seric 		**  necessarily delimit an old-style name -- at
7439382Seric 		**  signs mean keep going.
7449382Seric 		*/
7459382Seric 
7469382Seric 		/* find end of name */
7479382Seric 		while (isspace(*p) || *p == ',')
7489382Seric 			p++;
7499382Seric 		name = p;
7509382Seric 		for (;;)
7519382Seric 		{
7529382Seric 			char *oldp;
75316909Seric 			char pvpbuf[PSBUFSIZE];
7549382Seric 			extern bool isatword();
7559382Seric 			extern char **prescan();
7569382Seric 
75716909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7589382Seric 			p = DelimChar;
7599382Seric 
7609382Seric 			/* look to see if we have an at sign */
7619382Seric 			oldp = p;
7629382Seric 			while (*p != '\0' && isspace(*p))
7639382Seric 				p++;
7649382Seric 
7659382Seric 			if (*p != '@' && !isatword(p))
7669382Seric 			{
7679382Seric 				p = oldp;
7689382Seric 				break;
7699382Seric 			}
7709382Seric 			p += *p == '@' ? 1 : 2;
7719382Seric 			while (*p != '\0' && isspace(*p))
7729382Seric 				p++;
7739382Seric 		}
7749382Seric 		/* at the end of one complete name */
7759382Seric 
7769382Seric 		/* strip off trailing white space */
7779382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7789382Seric 			p--;
7799382Seric 		if (++p == name)
7809382Seric 			continue;
7819382Seric 		savechar = *p;
7829382Seric 		*p = '\0';
7839382Seric 
7849382Seric 		/* translate the name to be relative */
78510309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7869382Seric 		if (*name == '\0')
7879382Seric 		{
7889382Seric 			*p = savechar;
7899382Seric 			continue;
7909382Seric 		}
7919382Seric 
7929382Seric 		/* output the name with nice formatting */
7939382Seric 		opos += qstrlen(name);
7949382Seric 		if (!firstone)
7959382Seric 			opos += 2;
7969382Seric 		if (opos > 78 && !firstone)
7979382Seric 		{
79810178Seric 			(void) strcpy(obp, ",\n");
79910176Seric 			putline(obuf, fp, m);
8009382Seric 			obp = obuf;
8019382Seric 			(void) sprintf(obp, "        ");
80210161Seric 			opos = strlen(obp);
80310161Seric 			obp += opos;
80410161Seric 			opos += qstrlen(name);
8059382Seric 		}
8069382Seric 		else if (!firstone)
8079382Seric 		{
8089382Seric 			(void) sprintf(obp, ", ");
8099382Seric 			obp += 2;
8109382Seric 		}
8119382Seric 
8129382Seric 		/* strip off quote bits as we output */
81311157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8149382Seric 		{
8159382Seric 			if (bitset(0200, *name))
8169382Seric 				*obp++ = '\\';
8179382Seric 			*obp++ = *name++ & ~0200;
8189382Seric 		}
8199382Seric 		firstone = FALSE;
8209382Seric 		*p = savechar;
8219382Seric 	}
8229382Seric 	(void) strcpy(obp, "\n");
82310176Seric 	putline(obuf, fp, m);
8249382Seric }
8259382Seric /*
8269382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8279382Seric **
8289382Seric **	Parameters:
8299382Seric **		p -- word to check.
8309382Seric **
8319382Seric **	Returns:
8329382Seric **		TRUE -- if p is the word at.
8339382Seric **		FALSE -- otherwise.
8349382Seric **
8359382Seric **	Side Effects:
8369382Seric **		none.
8379382Seric */
8389382Seric 
8399382Seric bool
8409382Seric isatword(p)
8419382Seric 	register char *p;
8429382Seric {
8439382Seric 	extern char lower();
8449382Seric 
8459382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8469382Seric 	    p[2] != '\0' && isspace(p[2]))
8479382Seric 		return (TRUE);
8489382Seric 	return (FALSE);
8499382Seric }
850