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*57135Seric static char sccsid[] = "@(#)headers.c 5.27 (Berkeley) 12/15/92"; 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. 2555012Seric ** e -- the envelope including this header. 264091Seric ** 274091Seric ** Returns: 284091Seric ** flags for this header. 294091Seric ** 304091Seric ** Side Effects: 314091Seric ** The header is saved on the header list. 324319Seric ** Contents of 'line' are destroyed. 334091Seric */ 344091Seric 3555012Seric chompheader(line, def, e) 364091Seric char *line; 374091Seric bool def; 3855012Seric register ENVELOPE *e; 394091Seric { 404091Seric register char *p; 414091Seric register HDR *h; 424091Seric HDR **hp; 434091Seric char *fname; 444091Seric char *fvalue; 454091Seric struct hdrinfo *hi; 469059Seric bool cond = FALSE; 4710689Seric BITMAP mopts; 484091Seric 497677Seric if (tTd(31, 6)) 507677Seric printf("chompheader: %s\n", line); 517677Seric 524627Seric /* strip off options */ 5310689Seric clrbitmap(mopts); 544627Seric p = line; 5556678Seric if (def && *p == '?') 564627Seric { 574627Seric /* have some */ 5856795Seric register char *q = strchr(p + 1, *p); 594627Seric 604627Seric if (q != NULL) 614627Seric { 624627Seric *q++ = '\0'; 6310689Seric while (*++p != '\0') 6410689Seric setbitn(*p, mopts); 654627Seric p = q; 664627Seric } 674627Seric else 6836233Skarels usrerr("chompheader: syntax error, line \"%s\"", line); 699059Seric cond = TRUE; 704627Seric } 714627Seric 724091Seric /* find canonical name */ 734627Seric fname = p; 7456795Seric p = strchr(p, ':'); 7510118Seric if (p == NULL) 7610118Seric { 7710118Seric syserr("chompheader: syntax error, line \"%s\"", line); 7810118Seric return (0); 7910118Seric } 804091Seric fvalue = &p[1]; 814091Seric while (isspace(*--p)) 824091Seric continue; 834091Seric *++p = '\0'; 844091Seric makelower(fname); 854091Seric 864091Seric /* strip field value on front */ 874091Seric if (*fvalue == ' ') 884091Seric fvalue++; 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)) 9955012Seric e->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 */ 10614784Seric p = "resent-from"; 10755012Seric if (!bitset(EF_RESENT, e->e_flags)) 10814784Seric p += 7; 10914784Seric if (!def && !QueueRun && strcmp(fname, p) == 0) 11011414Seric { 11155012Seric if (e->e_from.q_paddr != NULL && 11255012Seric strcmp(fvalue, e->e_from.q_paddr) == 0) 11311414Seric return (hi->hi_flags); 11411414Seric } 11511414Seric 11614784Seric /* delete default value for this header */ 11755012Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 11814784Seric { 11914784Seric if (strcmp(fname, h->h_field) == 0 && 12014784Seric bitset(H_DEFAULT, h->h_flags) && 12114784Seric !bitset(H_FORCE, h->h_flags)) 12214784Seric h->h_value = NULL; 12314784Seric } 12414784Seric 12513012Seric /* create a new node */ 12613012Seric h = (HDR *) xalloc(sizeof *h); 12713012Seric h->h_field = newstr(fname); 12813012Seric h->h_value = NULL; 12913012Seric h->h_link = NULL; 13023117Seric bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 13113012Seric *hp = h; 1328066Seric h->h_flags = hi->hi_flags; 1334091Seric if (def) 1344091Seric h->h_flags |= H_DEFAULT; 1359059Seric if (cond) 1369059Seric h->h_flags |= H_CHECK; 1374091Seric if (h->h_value != NULL) 1389351Seric free((char *) h->h_value); 1398066Seric h->h_value = newstr(fvalue); 1404091Seric 1415937Seric /* hack to see if this is a new format message */ 1428095Seric if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 14356795Seric (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 14456795Seric strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 1458089Seric { 14655012Seric e->e_flags &= ~EF_OLDSTYLE; 1478089Seric } 1485937Seric 1494091Seric return (h->h_flags); 1504091Seric } 1514091Seric /* 1526980Seric ** ADDHEADER -- add a header entry to the end of the queue. 1536980Seric ** 1546980Seric ** This bypasses the special checking of chompheader. 1556980Seric ** 1566980Seric ** Parameters: 1576980Seric ** field -- the name of the header field. 1586980Seric ** value -- the value of the field. It must be lower-cased. 1596980Seric ** e -- the envelope to add them to. 1606980Seric ** 1616980Seric ** Returns: 1626980Seric ** none. 1636980Seric ** 1646980Seric ** Side Effects: 1656980Seric ** adds the field on the list of headers for this envelope. 1666980Seric */ 1676980Seric 1686980Seric addheader(field, value, e) 1696980Seric char *field; 1706980Seric char *value; 1716980Seric ENVELOPE *e; 1726980Seric { 1736980Seric register HDR *h; 1746980Seric register struct hdrinfo *hi; 1756980Seric HDR **hp; 1766980Seric 1776980Seric /* find info struct */ 1786980Seric for (hi = HdrInfo; hi->hi_field != NULL; hi++) 1796980Seric { 1806980Seric if (strcmp(field, hi->hi_field) == 0) 1816980Seric break; 1826980Seric } 1836980Seric 1846980Seric /* find current place in list -- keep back pointer? */ 1856980Seric for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 1866980Seric { 1876980Seric if (strcmp(field, h->h_field) == 0) 1886980Seric break; 1896980Seric } 1906980Seric 1916980Seric /* allocate space for new header */ 1926980Seric h = (HDR *) xalloc(sizeof *h); 1936980Seric h->h_field = field; 1946980Seric h->h_value = newstr(value); 1957368Seric h->h_link = *hp; 1966980Seric h->h_flags = hi->hi_flags | H_DEFAULT; 19710689Seric clrbitmap(h->h_mflags); 1986980Seric *hp = h; 1996980Seric } 2006980Seric /* 2014091Seric ** HVALUE -- return value of a header. 2024091Seric ** 2034091Seric ** Only "real" fields (i.e., ones that have not been supplied 2044091Seric ** as a default) are used. 2054091Seric ** 2064091Seric ** Parameters: 2074091Seric ** field -- the field name. 20855012Seric ** e -- the envelope containing the header. 2094091Seric ** 2104091Seric ** Returns: 2114091Seric ** pointer to the value part. 2124091Seric ** NULL if not found. 2134091Seric ** 2144091Seric ** Side Effects: 2159382Seric ** none. 2164091Seric */ 2174091Seric 2184091Seric char * 21955012Seric hvalue(field, e) 2204091Seric char *field; 22155012Seric register ENVELOPE *e; 2224091Seric { 2234091Seric register HDR *h; 2244091Seric 22555012Seric for (h = e->e_header; h != NULL; h = h->h_link) 2264091Seric { 2274091Seric if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 2284091Seric return (h->h_value); 2294091Seric } 2304091Seric return (NULL); 2314091Seric } 2324091Seric /* 2334091Seric ** ISHEADER -- predicate telling if argument is a header. 2344091Seric ** 2354319Seric ** A line is a header if it has a single word followed by 2364319Seric ** optional white space followed by a colon. 2374319Seric ** 2384091Seric ** Parameters: 2394091Seric ** s -- string to check for possible headerness. 2404091Seric ** 2414091Seric ** Returns: 2424091Seric ** TRUE if s is a header. 2434091Seric ** FALSE otherwise. 2444091Seric ** 2454091Seric ** Side Effects: 2464091Seric ** none. 2474091Seric */ 2484091Seric 2494091Seric bool 2504091Seric isheader(s) 2514091Seric register char *s; 2524091Seric { 2539382Seric while (*s > ' ' && *s != ':' && *s != '\0') 2544091Seric s++; 2559382Seric 2569382Seric /* following technically violates RFC822 */ 2574091Seric while (isspace(*s)) 2584091Seric s++; 2599382Seric 2604091Seric return (*s == ':'); 2614091Seric } 2625919Seric /* 2637783Seric ** EATHEADER -- run through the stored header and extract info. 2647783Seric ** 2657783Seric ** Parameters: 2669382Seric ** e -- the envelope to process. 2677783Seric ** 2687783Seric ** Returns: 2697783Seric ** none. 2707783Seric ** 2717783Seric ** Side Effects: 2727783Seric ** Sets a bunch of global variables from information 2739382Seric ** in the collected header. 2749382Seric ** Aborts the message if the hop count is exceeded. 2757783Seric */ 2767783Seric 2779382Seric eatheader(e) 2789382Seric register ENVELOPE *e; 2797783Seric { 2807783Seric register HDR *h; 2817783Seric register char *p; 2829382Seric int hopcnt = 0; 2837783Seric 2849382Seric if (tTd(32, 1)) 2859382Seric printf("----- collected header -----\n"); 2869382Seric for (h = e->e_header; h != NULL; h = h->h_link) 2877783Seric { 2887783Seric extern char *capitalize(); 2897783Seric 2909382Seric if (tTd(32, 1)) 2917783Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 29211414Seric /* count the number of times it has been processed */ 2939382Seric if (bitset(H_TRACE, h->h_flags)) 2949382Seric hopcnt++; 29511414Seric 29611414Seric /* send to this person if we so desire */ 29711414Seric if (GrabTo && bitset(H_RCPT, h->h_flags) && 29811414Seric !bitset(H_DEFAULT, h->h_flags) && 29955012Seric (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 30011414Seric { 30155012Seric sendtolist(h->h_value, (ADDRESS *) NULL, 30255012Seric &e->e_sendqueue, e); 30311414Seric } 30411414Seric 30511414Seric /* log the message-id */ 30611290Seric #ifdef LOG 30713103Seric if (!QueueRun && LogLevel > 8 && h->h_value != NULL && 30811299Seric strcmp(h->h_field, "message-id") == 0) 30911290Seric { 31011290Seric char buf[MAXNAME]; 31111290Seric 31211290Seric p = h->h_value; 31311290Seric if (bitset(H_DEFAULT, h->h_flags)) 31411290Seric { 31511290Seric expand(p, buf, &buf[sizeof buf], e); 31611290Seric p = buf; 31711290Seric } 31811290Seric syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p); 31911290Seric } 32056795Seric #endif /* LOG */ 3219382Seric } 3229382Seric if (tTd(32, 1)) 3237783Seric printf("----------------------------\n"); 3247783Seric 3259382Seric /* store hop count */ 3269382Seric if (hopcnt > e->e_hopcount) 3279382Seric e->e_hopcount = hopcnt; 3289382Seric 3297783Seric /* message priority */ 33055012Seric p = hvalue("precedence", e); 3319382Seric if (p != NULL) 3329382Seric e->e_class = priencode(p); 3337783Seric if (!QueueRun) 33425013Seric e->e_msgpriority = e->e_msgsize 33524981Seric - e->e_class * WkClassFact 33624981Seric + e->e_nrcpts * WkRecipFact; 3377783Seric 3388066Seric /* return receipt to */ 33955012Seric p = hvalue("return-receipt-to", e); 3407783Seric if (p != NULL) 3419382Seric e->e_receiptto = p; 3427783Seric 3438253Seric /* errors to */ 34455012Seric p = hvalue("errors-to", e); 3458253Seric if (p != NULL) 34655012Seric sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue, e); 3478253Seric 3487783Seric /* full name of from person */ 34955012Seric p = hvalue("full-name", e); 3507783Seric if (p != NULL) 3519382Seric define('x', p, e); 3527783Seric 3537783Seric /* date message originated */ 35455012Seric p = hvalue("posted-date", e); 3557783Seric if (p == NULL) 35655012Seric p = hvalue("date", e); 3577783Seric if (p != NULL) 3587783Seric { 3599382Seric define('a', p, e); 3607783Seric /* we don't have a good way to do canonical conversion .... 3619382Seric define('d', newstr(arpatounix(p)), e); 3627783Seric .... so we will ignore the problem for the time being */ 3637783Seric } 36411290Seric 36511290Seric /* 36611290Seric ** Log collection information. 36711290Seric */ 36811290Seric 36911290Seric # ifdef LOG 37011299Seric if (!QueueRun && LogLevel > 1) 37111290Seric { 37255019Seric char hbuf[100]; 37355019Seric char *name = hbuf; 37455019Seric extern char *inet_ntoa(); 37536230Skarels 37636230Skarels if (RealHostName == NULL) 37736230Skarels name = "local"; 37836230Skarels else if (RealHostName[0] == '[') 37936230Skarels name = RealHostName; 38036230Skarels else 38154999Seric (void)sprintf(hbuf, "%.80s (%s)", 38236230Skarels RealHostName, inet_ntoa(RealHostAddr.sin_addr)); 38336230Skarels syslog(LOG_INFO, 38436230Skarels "%s: from=%s, size=%ld, class=%d, received from %s\n", 38555012Seric e->e_id, e->e_from.q_paddr, e->e_msgsize, 38655012Seric e->e_class, name); 38711290Seric } 38856795Seric # endif /* LOG */ 3897783Seric } 3907783Seric /* 3917783Seric ** PRIENCODE -- encode external priority names into internal values. 3927783Seric ** 3937783Seric ** Parameters: 3947783Seric ** p -- priority in ascii. 3957783Seric ** 3967783Seric ** Returns: 3977783Seric ** priority as a numeric level. 3987783Seric ** 3997783Seric ** Side Effects: 4007783Seric ** none. 4017783Seric */ 4027783Seric 4037783Seric priencode(p) 4047783Seric char *p; 4057783Seric { 4068253Seric register int i; 4077783Seric 4088253Seric for (i = 0; i < NumPriorities; i++) 4097783Seric { 41033725Sbostic if (!strcasecmp(p, Priorities[i].pri_name)) 4118253Seric return (Priorities[i].pri_val); 4127783Seric } 4138253Seric 4148253Seric /* unknown priority */ 4158253Seric return (0); 4167783Seric } 4177783Seric /* 4187890Seric ** CRACKADDR -- parse an address and turn it into a macro 4197783Seric ** 4207783Seric ** This doesn't actually parse the address -- it just extracts 4217783Seric ** it and replaces it with "$g". The parse is totally ad hoc 4227783Seric ** and isn't even guaranteed to leave something syntactically 4237783Seric ** identical to what it started with. However, it does leave 4247783Seric ** something semantically identical. 4257783Seric ** 42651379Seric ** This algorithm has been cleaned up to handle a wider range 42751379Seric ** of cases -- notably quoted and backslash escaped strings. 42851379Seric ** This modification makes it substantially better at preserving 42951379Seric ** the original syntax. 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; 45051379Seric register char c; 45151379Seric int cmtlev; 45256764Seric int realcmtlev; 45356764Seric int anglelev, realanglelev; 45451379Seric int copylev; 45551379Seric bool qmode; 45656764Seric bool realqmode; 45756764Seric bool skipping; 45851379Seric bool putgmac = FALSE; 45956735Seric bool quoteit = FALSE; 46051379Seric register char *bp; 46156764Seric char *buflim; 4627783Seric static char buf[MAXNAME]; 4637783Seric 4647783Seric if (tTd(33, 1)) 4657890Seric printf("crackaddr(%s)\n", addr); 4667783Seric 4678082Seric /* strip leading spaces */ 4688082Seric while (*addr != '\0' && isspace(*addr)) 4698082Seric addr++; 4708082Seric 4717783Seric /* 47251379Seric ** Start by assuming we have no angle brackets. This will be 47351379Seric ** adjusted later if we find them. 4747783Seric */ 4757783Seric 47651379Seric bp = buf; 47756764Seric buflim = &buf[sizeof buf - 5]; 47851379Seric p = addr; 47956764Seric copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 48056764Seric qmode = realqmode = FALSE; 48151379Seric 48251379Seric while ((c = *p++) != '\0') 4837783Seric { 48456764Seric /* 48556764Seric ** If the buffer is overful, go into a special "skipping" 48656764Seric ** mode that tries to keep legal syntax but doesn't actually 48756764Seric ** output things. 48856764Seric */ 4897783Seric 49056764Seric skipping = bp >= buflim; 49156735Seric 49256764Seric if (copylev > 0 && !skipping) 49356764Seric *bp++ = c; 49456735Seric 49551379Seric /* check for backslash escapes */ 49651379Seric if (c == '\\') 4977783Seric { 49851379Seric if ((c = *p++) == '\0') 4997783Seric { 50051379Seric /* too far */ 50151379Seric p--; 50251379Seric goto putg; 5037783Seric } 50456764Seric if (copylev > 0 && !skipping) 50551379Seric *bp++ = c; 50651379Seric goto putg; 5077783Seric } 5087783Seric 50951379Seric /* check for quoted strings */ 51051379Seric if (c == '"') 5117783Seric { 51251379Seric qmode = !qmode; 51356764Seric if (copylev > 0 && !skipping) 51456764Seric realqmode = !realqmode; 51551379Seric continue; 5167783Seric } 51751379Seric if (qmode) 51851379Seric goto putg; 5197783Seric 52051379Seric /* check for comments */ 52151379Seric if (c == '(') 5227783Seric { 52351379Seric cmtlev++; 52456764Seric 52556764Seric /* allow space for closing paren */ 52656764Seric if (!skipping) 52756764Seric { 52856764Seric buflim--; 52956764Seric realcmtlev++; 53056764Seric if (copylev++ <= 0) 53156764Seric { 53256764Seric *bp++ = ' '; 53356764Seric *bp++ = c; 53456764Seric } 53556764Seric } 53651379Seric } 53751379Seric if (cmtlev > 0) 53851379Seric { 53951379Seric if (c == ')') 5407783Seric { 54151379Seric cmtlev--; 54251379Seric copylev--; 54356764Seric if (!skipping) 54456764Seric { 54556764Seric realcmtlev--; 54656764Seric buflim++; 54756764Seric } 5487783Seric } 5497783Seric continue; 5507783Seric } 55156764Seric else if (c == ')') 55256764Seric { 55356764Seric /* syntax error: unmatched ) */ 55456764Seric if (!skipping) 55556764Seric bp--; 55656764Seric } 5577783Seric 55856764Seric 55956764Seric /* check for characters that may have to be quoted */ 56057123Seric if (strchr(".'@,;:\\[]<>()", c) != NULL) 56156764Seric { 56256764Seric /* 56356764Seric ** If these occur as the phrase part of a <> 56456764Seric ** construct, but are not inside of () or already 56556764Seric ** quoted, they will have to be quoted. Note that 56656764Seric ** now (but don't actually do the quoting). 56756764Seric */ 56856764Seric 56956764Seric if (cmtlev <= 0 && !qmode) 57056764Seric quoteit = TRUE; 57156764Seric } 57256764Seric 57351379Seric /* check for angle brackets */ 57451379Seric if (c == '<') 57551379Seric { 57656735Seric register char *q; 57756735Seric 57851379Seric /* oops -- have to change our mind */ 57956764Seric anglelev++; 58056764Seric if (!skipping) 58156764Seric realanglelev++; 58256764Seric 58356735Seric bp = buf; 58456735Seric if (quoteit) 58556735Seric { 58656735Seric *bp++ = '"'; 58756735Seric 58856735Seric /* back up over the '<' and any spaces */ 58956735Seric --p; 59056735Seric while (isspace(*--p)) 59156735Seric continue; 59256735Seric p++; 59356735Seric } 59456735Seric for (q = addr; q < p; ) 59556735Seric { 59656735Seric c = *q++; 59756764Seric if (bp < buflim) 59856764Seric { 59956764Seric if (quoteit && c == '"') 60056764Seric *bp++ = '\\'; 60156764Seric *bp++ = c; 60256764Seric } 60356735Seric } 60456735Seric if (quoteit) 60556735Seric { 60656735Seric *bp++ = '"'; 60756764Seric while ((c = *p++) != '<') 60856764Seric { 60956764Seric if (bp < buflim) 61056764Seric *bp++ = c; 61156764Seric } 61256764Seric *bp++ = c; 61356735Seric } 61451379Seric copylev = 0; 61556735Seric putgmac = quoteit = FALSE; 61651379Seric continue; 61751379Seric } 6187783Seric 61951379Seric if (c == '>') 6207783Seric { 62156764Seric if (anglelev > 0) 62256764Seric { 62356764Seric anglelev--; 62456764Seric if (!skipping) 62556764Seric { 62656764Seric realanglelev--; 62756764Seric buflim++; 62856764Seric } 62956764Seric } 63056764Seric else if (!skipping) 63156764Seric { 63256764Seric /* syntax error: unmatched > */ 63356764Seric if (copylev > 0) 63456764Seric bp--; 63556764Seric continue; 63656764Seric } 63751379Seric if (copylev++ <= 0) 63851379Seric *bp++ = c; 63951379Seric continue; 6407783Seric } 64151379Seric 64251379Seric /* must be a real address character */ 64351379Seric putg: 64451379Seric if (copylev <= 0 && !putgmac) 64551379Seric { 64651379Seric *bp++ = '\001'; 64751379Seric *bp++ = 'g'; 64851379Seric putgmac = TRUE; 64951379Seric } 6507783Seric } 6517783Seric 65256764Seric /* repair any syntactic damage */ 65356764Seric if (realqmode) 65456764Seric *bp++ = '"'; 65556764Seric while (realcmtlev-- > 0) 65656764Seric *bp++ = ')'; 65756764Seric while (realanglelev-- > 0) 65856764Seric *bp++ = '>'; 65951379Seric *bp++ = '\0'; 6607783Seric 6617783Seric if (tTd(33, 1)) 6627944Seric printf("crackaddr=>`%s'\n", buf); 6637783Seric 6647783Seric return (buf); 6657783Seric } 6669382Seric /* 6679382Seric ** PUTHEADER -- put the header part of a message from the in-core copy 6689382Seric ** 6699382Seric ** Parameters: 6709382Seric ** fp -- file to put it on. 6719382Seric ** m -- mailer to use. 6729382Seric ** e -- envelope to use. 6739382Seric ** 6749382Seric ** Returns: 6759382Seric ** none. 6769382Seric ** 6779382Seric ** Side Effects: 6789382Seric ** none. 6799382Seric */ 6809382Seric 68110176Seric putheader(fp, m, e) 6829382Seric register FILE *fp; 6839382Seric register MAILER *m; 6849382Seric register ENVELOPE *e; 6859382Seric { 686*57135Seric char buf[MAX(MAXLINE,BUFSIZ)]; 6879382Seric register HDR *h; 6889382Seric extern char *arpadate(); 6899382Seric extern char *capitalize(); 690*57135Seric char obuf[MAXLINE]; 6919382Seric 6929382Seric for (h = e->e_header; h != NULL; h = h->h_link) 6939382Seric { 6949382Seric register char *p; 69510689Seric extern bool bitintersect(); 6969382Seric 6979382Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 69810689Seric !bitintersect(h->h_mflags, m->m_flags)) 6999382Seric continue; 7009382Seric 70111414Seric /* handle Resent-... headers specially */ 70211414Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 70311414Seric continue; 70411414Seric 7059382Seric p = h->h_value; 7069382Seric if (bitset(H_DEFAULT, h->h_flags)) 7079382Seric { 7089382Seric /* macro expand value if generated internally */ 7099382Seric expand(p, buf, &buf[sizeof buf], e); 7109382Seric p = buf; 7119382Seric if (p == NULL || *p == '\0') 7129382Seric continue; 7139382Seric } 7149382Seric 7159382Seric if (bitset(H_FROM|H_RCPT, h->h_flags)) 7169382Seric { 7179382Seric /* address field */ 7189382Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 7199382Seric 7209382Seric if (bitset(H_FROM, h->h_flags)) 7219382Seric oldstyle = FALSE; 72255012Seric commaize(h, p, fp, oldstyle, m, e); 7239382Seric } 7249382Seric else 7259382Seric { 7269382Seric /* vanilla header line */ 72712159Seric register char *nlp; 72812159Seric 72912159Seric (void) sprintf(obuf, "%s: ", capitalize(h->h_field)); 73056795Seric while ((nlp = strchr(p, '\n')) != NULL) 73112159Seric { 73212159Seric *nlp = '\0'; 73312159Seric (void) strcat(obuf, p); 73412159Seric *nlp = '\n'; 73512159Seric putline(obuf, fp, m); 73612159Seric p = ++nlp; 73712161Seric obuf[0] = '\0'; 73812159Seric } 73912159Seric (void) strcat(obuf, p); 74010176Seric putline(obuf, fp, m); 7419382Seric } 7429382Seric } 7439382Seric } 7449382Seric /* 7459382Seric ** COMMAIZE -- output a header field, making a comma-translated list. 7469382Seric ** 7479382Seric ** Parameters: 7489382Seric ** h -- the header field to output. 7499382Seric ** p -- the value to put in it. 7509382Seric ** fp -- file to put it to. 7519382Seric ** oldstyle -- TRUE if this is an old style header. 7529382Seric ** m -- a pointer to the mailer descriptor. If NULL, 7539382Seric ** don't transform the name at all. 75455012Seric ** e -- the envelope containing the message. 7559382Seric ** 7569382Seric ** Returns: 7579382Seric ** none. 7589382Seric ** 7599382Seric ** Side Effects: 7609382Seric ** outputs "p" to file "fp". 7619382Seric */ 7629382Seric 76355012Seric commaize(h, p, fp, oldstyle, m, e) 7649382Seric register HDR *h; 7659382Seric register char *p; 7669382Seric FILE *fp; 7679382Seric bool oldstyle; 7689382Seric register MAILER *m; 76955012Seric register ENVELOPE *e; 7709382Seric { 7719382Seric register char *obp; 7729382Seric int opos; 7739382Seric bool firstone = TRUE; 77411157Seric char obuf[MAXLINE + 3]; 7759382Seric 7769382Seric /* 7779382Seric ** Output the address list translated by the 7789382Seric ** mailer and with commas. 7799382Seric */ 7809382Seric 7819382Seric if (tTd(14, 2)) 7829382Seric printf("commaize(%s: %s)\n", h->h_field, p); 7839382Seric 7849382Seric obp = obuf; 7859382Seric (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 7869382Seric opos = strlen(h->h_field) + 2; 7879382Seric obp += opos; 7889382Seric 7899382Seric /* 7909382Seric ** Run through the list of values. 7919382Seric */ 7929382Seric 7939382Seric while (*p != '\0') 7949382Seric { 7959382Seric register char *name; 79654983Seric register int c; 7979382Seric char savechar; 7989382Seric extern char *remotename(); 7999382Seric extern char *DelimChar; /* defined in prescan */ 8009382Seric 8019382Seric /* 8029382Seric ** Find the end of the name. New style names 8039382Seric ** end with a comma, old style names end with 8049382Seric ** a space character. However, spaces do not 8059382Seric ** necessarily delimit an old-style name -- at 8069382Seric ** signs mean keep going. 8079382Seric */ 8089382Seric 8099382Seric /* find end of name */ 8109382Seric while (isspace(*p) || *p == ',') 8119382Seric p++; 8129382Seric name = p; 8139382Seric for (;;) 8149382Seric { 8159382Seric char *oldp; 81616909Seric char pvpbuf[PSBUFSIZE]; 8179382Seric extern bool isatword(); 8189382Seric extern char **prescan(); 8199382Seric 82016909Seric (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf); 8219382Seric p = DelimChar; 8229382Seric 8239382Seric /* look to see if we have an at sign */ 8249382Seric oldp = p; 8259382Seric while (*p != '\0' && isspace(*p)) 8269382Seric p++; 8279382Seric 8289382Seric if (*p != '@' && !isatword(p)) 8299382Seric { 8309382Seric p = oldp; 8319382Seric break; 8329382Seric } 8339382Seric p += *p == '@' ? 1 : 2; 8349382Seric while (*p != '\0' && isspace(*p)) 8359382Seric p++; 8369382Seric } 8379382Seric /* at the end of one complete name */ 8389382Seric 8399382Seric /* strip off trailing white space */ 8409382Seric while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 8419382Seric p--; 8429382Seric if (++p == name) 8439382Seric continue; 8449382Seric savechar = *p; 8459382Seric *p = '\0'; 8469382Seric 8479382Seric /* translate the name to be relative */ 84855012Seric name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE, e); 8499382Seric if (*name == '\0') 8509382Seric { 8519382Seric *p = savechar; 8529382Seric continue; 8539382Seric } 8549382Seric 8559382Seric /* output the name with nice formatting */ 85654983Seric opos += strlen(name); 8579382Seric if (!firstone) 8589382Seric opos += 2; 8599382Seric if (opos > 78 && !firstone) 8609382Seric { 86110178Seric (void) strcpy(obp, ",\n"); 86210176Seric putline(obuf, fp, m); 8639382Seric obp = obuf; 8649382Seric (void) sprintf(obp, " "); 86510161Seric opos = strlen(obp); 86610161Seric obp += opos; 86754983Seric opos += strlen(name); 8689382Seric } 8699382Seric else if (!firstone) 8709382Seric { 8719382Seric (void) sprintf(obp, ", "); 8729382Seric obp += 2; 8739382Seric } 8749382Seric 8759382Seric /* strip off quote bits as we output */ 87654983Seric while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 8779382Seric { 87854983Seric if (bitnset(M_7BITS, m->m_flags)) 87954983Seric c &= 0177; 88054983Seric *obp++ = c; 8819382Seric } 8829382Seric firstone = FALSE; 8839382Seric *p = savechar; 8849382Seric } 8859382Seric (void) strcpy(obp, "\n"); 88610176Seric putline(obuf, fp, m); 8879382Seric } 8889382Seric /* 8899382Seric ** ISATWORD -- tell if the word we are pointing to is "at". 8909382Seric ** 8919382Seric ** Parameters: 8929382Seric ** p -- word to check. 8939382Seric ** 8949382Seric ** Returns: 8959382Seric ** TRUE -- if p is the word at. 8969382Seric ** FALSE -- otherwise. 8979382Seric ** 8989382Seric ** Side Effects: 8999382Seric ** none. 9009382Seric */ 9019382Seric 9029382Seric bool 9039382Seric isatword(p) 9049382Seric register char *p; 9059382Seric { 9069382Seric extern char lower(); 9079382Seric 9089382Seric if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 9099382Seric p[2] != '\0' && isspace(p[2])) 9109382Seric return (TRUE); 9119382Seric return (FALSE); 9129382Seric } 913