xref: /freebsd/usr.bin/mail/head.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)head.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include "rcv.h"
399b50d902SRodney W. Grimes #include "extern.h"
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes /*
429b50d902SRodney W. Grimes  * Mail -- a mail program
439b50d902SRodney W. Grimes  *
449b50d902SRodney W. Grimes  * Routines for processing and detecting headlines.
459b50d902SRodney W. Grimes  */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes /*
489b50d902SRodney W. Grimes  * See if the passed line buffer is a mail header.
499b50d902SRodney W. Grimes  * Return true if yes.  Note the extreme pains to
509b50d902SRodney W. Grimes  * accomodate all funny formats.
519b50d902SRodney W. Grimes  */
529b50d902SRodney W. Grimes int
539b50d902SRodney W. Grimes ishead(linebuf)
549b50d902SRodney W. Grimes 	char linebuf[];
559b50d902SRodney W. Grimes {
569b50d902SRodney W. Grimes 	register char *cp;
579b50d902SRodney W. Grimes 	struct headline hl;
589b50d902SRodney W. Grimes 	char parbuf[BUFSIZ];
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes 	cp = linebuf;
619b50d902SRodney W. Grimes 	if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
629b50d902SRodney W. Grimes 	    *cp++ != ' ')
639b50d902SRodney W. Grimes 		return (0);
649b50d902SRodney W. Grimes 	parse(linebuf, &hl, parbuf);
659b50d902SRodney W. Grimes 	if (hl.l_from == NOSTR || hl.l_date == NOSTR) {
669b50d902SRodney W. Grimes 		fail(linebuf, "No from or date field");
679b50d902SRodney W. Grimes 		return (0);
689b50d902SRodney W. Grimes 	}
699b50d902SRodney W. Grimes 	if (!isdate(hl.l_date)) {
709b50d902SRodney W. Grimes 		fail(linebuf, "Date field not legal date");
719b50d902SRodney W. Grimes 		return (0);
729b50d902SRodney W. Grimes 	}
739b50d902SRodney W. Grimes 	/*
749b50d902SRodney W. Grimes 	 * I guess we got it!
759b50d902SRodney W. Grimes 	 */
769b50d902SRodney W. Grimes 	return (1);
779b50d902SRodney W. Grimes }
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes /*ARGSUSED*/
809b50d902SRodney W. Grimes void
819b50d902SRodney W. Grimes fail(linebuf, reason)
829b50d902SRodney W. Grimes 	char linebuf[], reason[];
839b50d902SRodney W. Grimes {
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	/*
869b50d902SRodney W. Grimes 	if (value("debug") == NOSTR)
879b50d902SRodney W. Grimes 		return;
889b50d902SRodney W. Grimes 	fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
899b50d902SRodney W. Grimes 	*/
909b50d902SRodney W. Grimes }
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes /*
939b50d902SRodney W. Grimes  * Split a headline into its useful components.
949b50d902SRodney W. Grimes  * Copy the line into dynamic string space, then set
959b50d902SRodney W. Grimes  * pointers into the copied line in the passed headline
969b50d902SRodney W. Grimes  * structure.  Actually, it scans.
979b50d902SRodney W. Grimes  */
989b50d902SRodney W. Grimes void
999b50d902SRodney W. Grimes parse(line, hl, pbuf)
1009b50d902SRodney W. Grimes 	char line[], pbuf[];
1019b50d902SRodney W. Grimes 	register struct headline *hl;
1029b50d902SRodney W. Grimes {
1039b50d902SRodney W. Grimes 	register char *cp;
1049b50d902SRodney W. Grimes 	char *sp;
1059b50d902SRodney W. Grimes 	char word[LINESIZE];
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes 	hl->l_from = NOSTR;
1089b50d902SRodney W. Grimes 	hl->l_tty = NOSTR;
1099b50d902SRodney W. Grimes 	hl->l_date = NOSTR;
1109b50d902SRodney W. Grimes 	cp = line;
1119b50d902SRodney W. Grimes 	sp = pbuf;
1129b50d902SRodney W. Grimes 	/*
1139b50d902SRodney W. Grimes 	 * Skip over "From" first.
1149b50d902SRodney W. Grimes 	 */
1159b50d902SRodney W. Grimes 	cp = nextword(cp, word);
1169b50d902SRodney W. Grimes 	cp = nextword(cp, word);
1179b50d902SRodney W. Grimes 	if (*word)
1189b50d902SRodney W. Grimes 		hl->l_from = copyin(word, &sp);
1199b50d902SRodney W. Grimes 	if (cp != NOSTR && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
1209b50d902SRodney W. Grimes 		cp = nextword(cp, word);
1219b50d902SRodney W. Grimes 		hl->l_tty = copyin(word, &sp);
1229b50d902SRodney W. Grimes 	}
1239b50d902SRodney W. Grimes 	if (cp != NOSTR)
1249b50d902SRodney W. Grimes 		hl->l_date = copyin(cp, &sp);
1259b50d902SRodney W. Grimes }
1269b50d902SRodney W. Grimes 
1279b50d902SRodney W. Grimes /*
1289b50d902SRodney W. Grimes  * Copy the string on the left into the string on the right
1299b50d902SRodney W. Grimes  * and bump the right (reference) string pointer by the length.
1309b50d902SRodney W. Grimes  * Thus, dynamically allocate space in the right string, copying
1319b50d902SRodney W. Grimes  * the left string into it.
1329b50d902SRodney W. Grimes  */
1339b50d902SRodney W. Grimes char *
1349b50d902SRodney W. Grimes copyin(src, space)
1359b50d902SRodney W. Grimes 	register char *src;
1369b50d902SRodney W. Grimes 	char **space;
1379b50d902SRodney W. Grimes {
1389b50d902SRodney W. Grimes 	register char *cp;
1399b50d902SRodney W. Grimes 	char *top;
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes 	top = cp = *space;
1429b50d902SRodney W. Grimes 	while (*cp++ = *src++)
1439b50d902SRodney W. Grimes 		;
1449b50d902SRodney W. Grimes 	*space = cp;
1459b50d902SRodney W. Grimes 	return (top);
1469b50d902SRodney W. Grimes }
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes /*
1499b50d902SRodney W. Grimes  * Test to see if the passed string is a ctime(3) generated
1509b50d902SRodney W. Grimes  * date string as documented in the manual.  The template
1519b50d902SRodney W. Grimes  * below is used as the criterion of correctness.
1529b50d902SRodney W. Grimes  * Also, we check for a possible trailing time zone using
1539b50d902SRodney W. Grimes  * the tmztype template.
1549b50d902SRodney W. Grimes  */
1559b50d902SRodney W. Grimes 
1569b50d902SRodney W. Grimes /*
1579b50d902SRodney W. Grimes  * 'A'	An upper case char
1589b50d902SRodney W. Grimes  * 'a'	A lower case char
1599b50d902SRodney W. Grimes  * ' '	A space
1609b50d902SRodney W. Grimes  * '0'	A digit
1619b50d902SRodney W. Grimes  * 'O'	An optional digit or space
1629b50d902SRodney W. Grimes  * ':'	A colon
1639b50d902SRodney W. Grimes  * 'N'	A new line
1649b50d902SRodney W. Grimes  */
1659b50d902SRodney W. Grimes char ctype[] = "Aaa Aaa O0 00:00:00 0000";
1669b50d902SRodney W. Grimes char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000";
1679b50d902SRodney W. Grimes 
1689b50d902SRodney W. Grimes int
1699b50d902SRodney W. Grimes isdate(date)
1709b50d902SRodney W. Grimes 	char date[];
1719b50d902SRodney W. Grimes {
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes 	return cmatch(date, ctype) || cmatch(date, tmztype);
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes /*
1779b50d902SRodney W. Grimes  * Match the given string (cp) against the given template (tp).
1789b50d902SRodney W. Grimes  * Return 1 if they match, 0 if they don't
1799b50d902SRodney W. Grimes  */
1809b50d902SRodney W. Grimes int
1819b50d902SRodney W. Grimes cmatch(cp, tp)
1829b50d902SRodney W. Grimes 	register char *cp, *tp;
1839b50d902SRodney W. Grimes {
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 	while (*cp && *tp)
1869b50d902SRodney W. Grimes 		switch (*tp++) {
1879b50d902SRodney W. Grimes 		case 'a':
1889b50d902SRodney W. Grimes 			if (!islower(*cp++))
1899b50d902SRodney W. Grimes 				return 0;
1909b50d902SRodney W. Grimes 			break;
1919b50d902SRodney W. Grimes 		case 'A':
1929b50d902SRodney W. Grimes 			if (!isupper(*cp++))
1939b50d902SRodney W. Grimes 				return 0;
1949b50d902SRodney W. Grimes 			break;
1959b50d902SRodney W. Grimes 		case ' ':
1969b50d902SRodney W. Grimes 			if (*cp++ != ' ')
1979b50d902SRodney W. Grimes 				return 0;
1989b50d902SRodney W. Grimes 			break;
1999b50d902SRodney W. Grimes 		case '0':
2009b50d902SRodney W. Grimes 			if (!isdigit(*cp++))
2019b50d902SRodney W. Grimes 				return 0;
2029b50d902SRodney W. Grimes 			break;
2039b50d902SRodney W. Grimes 		case 'O':
2049b50d902SRodney W. Grimes 			if (*cp != ' ' && !isdigit(*cp))
2059b50d902SRodney W. Grimes 				return 0;
2069b50d902SRodney W. Grimes 			cp++;
2079b50d902SRodney W. Grimes 			break;
2089b50d902SRodney W. Grimes 		case ':':
2099b50d902SRodney W. Grimes 			if (*cp++ != ':')
2109b50d902SRodney W. Grimes 				return 0;
2119b50d902SRodney W. Grimes 			break;
2129b50d902SRodney W. Grimes 		case 'N':
2139b50d902SRodney W. Grimes 			if (*cp++ != '\n')
2149b50d902SRodney W. Grimes 				return 0;
2159b50d902SRodney W. Grimes 			break;
2169b50d902SRodney W. Grimes 		}
2179b50d902SRodney W. Grimes 	if (*cp || *tp)
2189b50d902SRodney W. Grimes 		return 0;
2199b50d902SRodney W. Grimes 	return (1);
2209b50d902SRodney W. Grimes }
2219b50d902SRodney W. Grimes 
2229b50d902SRodney W. Grimes /*
2239b50d902SRodney W. Grimes  * Collect a liberal (space, tab delimited) word into the word buffer
2249b50d902SRodney W. Grimes  * passed.  Also, return a pointer to the next word following that,
2259b50d902SRodney W. Grimes  * or NOSTR if none follow.
2269b50d902SRodney W. Grimes  */
2279b50d902SRodney W. Grimes char *
2289b50d902SRodney W. Grimes nextword(wp, wbuf)
2299b50d902SRodney W. Grimes 	register char *wp, *wbuf;
2309b50d902SRodney W. Grimes {
2319b50d902SRodney W. Grimes 	register c;
2329b50d902SRodney W. Grimes 
2339b50d902SRodney W. Grimes 	if (wp == NOSTR) {
2349b50d902SRodney W. Grimes 		*wbuf = 0;
2359b50d902SRodney W. Grimes 		return (NOSTR);
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 	while ((c = *wp++) && c != ' ' && c != '\t') {
2389b50d902SRodney W. Grimes 		*wbuf++ = c;
2399b50d902SRodney W. Grimes 		if (c == '"') {
2409b50d902SRodney W. Grimes  			while ((c = *wp++) && c != '"')
2419b50d902SRodney W. Grimes  				*wbuf++ = c;
2429b50d902SRodney W. Grimes  			if (c == '"')
2439b50d902SRodney W. Grimes  				*wbuf++ = c;
2449b50d902SRodney W. Grimes 			else
2459b50d902SRodney W. Grimes 				wp--;
2469b50d902SRodney W. Grimes  		}
2479b50d902SRodney W. Grimes 	}
2489b50d902SRodney W. Grimes 	*wbuf = '\0';
2499b50d902SRodney W. Grimes 	for (; c == ' ' || c == '\t'; c = *wp++)
2509b50d902SRodney W. Grimes 		;
2519b50d902SRodney W. Grimes 	if (c == 0)
2529b50d902SRodney W. Grimes 		return (NOSTR);
2539b50d902SRodney W. Grimes 	return (wp - 1);
2549b50d902SRodney W. Grimes }
255