1/*
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 4/20/95";
37#endif
38__attribute__((__used__))
39static const char rcsid[] =
40  "$FreeBSD: src/usr.bin/mail/head.c,v 1.8 2003/01/09 05:08:37 mikeh Exp $";
41#endif /* not lint */
42
43#include <sys/cdefs.h>
44
45#include "rcv.h"
46#include "extern.h"
47
48/*
49 * Mail -- a mail program
50 *
51 * Routines for processing and detecting headlines.
52 */
53
54/*
55 * See if the passed line buffer is a mail header.
56 * Return true if yes.  Note the extreme pains to
57 * accomodate all funny formats.
58 */
59int
60ishead(linebuf)
61	char linebuf[];
62{
63	struct headline hl;
64	char parbuf[BUFSIZ];
65
66	if (strncmp(linebuf, "From ", 5) != 0)
67		return (0);
68	parse(linebuf, &hl, parbuf);
69	if (hl.l_date == NULL) {
70		fail(linebuf, "No date field");
71		return (0);
72	}
73	if (!isdate(hl.l_date)) {
74		fail(linebuf, "Date field not legal date");
75		return (0);
76	}
77	/*
78	 * I guess we got it!
79	 */
80	return (1);
81}
82
83/*ARGSUSED*/
84void
85fail(linebuf, reason)
86	const char *linebuf, *reason;
87{
88
89	/*
90	if (value("debug") == NULL)
91		return;
92	fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
93	*/
94}
95
96/*
97 * Split a headline into its useful components.
98 * Copy the line into dynamic string space, then set
99 * pointers into the copied line in the passed headline
100 * structure.  Actually, it scans.
101 */
102void
103parse(line, hl, pbuf)
104	char line[], pbuf[];
105	struct headline *hl;
106{
107	char *cp, *sp;
108	char word[LINESIZE];
109
110	hl->l_from = NULL;
111	hl->l_tty = NULL;
112	hl->l_date = NULL;
113	cp = line;
114	sp = pbuf;
115	/*
116	 * Skip over "From" first.
117	 */
118	cp = nextword(cp, word);
119	/*
120	 * Check for missing return-path.
121	 */
122	if (isdate(cp)) {
123		hl->l_date = copyin(cp, &sp);
124		return;
125	}
126	cp = nextword(cp, word);
127	if (strlen(word) > 0)
128		hl->l_from = copyin(word, &sp);
129	if (cp != NULL && strncmp(cp, "tty", 3) == 0) {
130		cp = nextword(cp, word);
131		hl->l_tty = copyin(word, &sp);
132	}
133	if (cp != NULL)
134		hl->l_date = copyin(cp, &sp);
135}
136
137/*
138 * Copy the string on the left into the string on the right
139 * and bump the right (reference) string pointer by the length.
140 * Thus, dynamically allocate space in the right string, copying
141 * the left string into it.
142 */
143char *
144copyin(src, space)
145	char *src;
146	char **space;
147{
148	char *cp, *top;
149
150	top = cp = *space;
151	while ((*cp++ = *src++) != '\0')
152		;
153	*space = cp;
154	return (top);
155}
156
157/*
158 * Test to see if the passed string is a ctime(3) generated
159 * date string as documented in the manual.  The template
160 * below is used as the criterion of correctness.
161 * Also, we check for a possible trailing time zone using
162 * the tmztype template.
163 *
164 * If the mail file is created by Sys V (Solaris), there are
165 * no seconds in the time. If the mail is created by another
166 * program such as imapd, it might have timezone as
167 * <-|+>nnnn (-0800 for instance) at the end.
168 */
169
170/*
171 * 'A'	An upper case char
172 * 'a'	A lower case char
173 * ' '	A space
174 * '0'	A digit
175 * 'O'	A digit or space
176 * 'p'	A punctuation char
177 * 'P'	A punctuation char or space
178 * ':'	A colon
179 * 'N'	A new line
180 */
181
182static char *date_formats[] = {
183	"Aaa Aaa O0 00:00:00 0000",	   /* Mon Jan 01 23:59:59 2001 */
184	"Aaa Aaa O0 00:00:00 AAA 0000",	   /* Mon Jan 01 23:59:59 PST 2001 */
185	"Aaa Aaa O0 00:00:00 0000 p0000",  /* Mon Jan 01 23:59:59 2001 -0800 */
186	"Aaa Aaa O0 00:00 0000",	   /* Mon Jan 01 23:59 2001 */
187	"Aaa Aaa O0 00:00 AAA 0000",	   /* Mon Jan 01 23:59 PST 2001 */
188	"Aaa Aaa O0 00:00 0000 p0000",	   /* Mon Jan 01 23:59 2001 -0800 */
189	NULL
190};
191
192int
193isdate(date)
194	char date[];
195{
196	int i;
197
198	for(i = 0; date_formats[i] != NULL; i++) {
199		if (cmatch(date, date_formats[i]))
200			return (1);
201	}
202	return (0);
203}
204
205/*
206 * Match the given string (cp) against the given template (tp).
207 * Return 1 if they match, 0 if they don't
208 */
209int
210cmatch(cp, tp)
211	char *cp, *tp;
212{
213
214	while (*cp != '\0' && *tp != '\0')
215		switch (*tp++) {
216		case 'a':
217			if (!islower((unsigned char)*cp++))
218				return (0);
219			break;
220		case 'A':
221			if (!isupper((unsigned char)*cp++))
222				return (0);
223			break;
224		case ' ':
225			if (*cp++ != ' ')
226				return (0);
227			break;
228		case '0':
229			if (!isdigit((unsigned char)*cp++))
230				return (0);
231			break;
232		case 'O':
233			if (*cp != ' ' && !isdigit((unsigned char)*cp))
234				return (0);
235			cp++;
236			break;
237		case 'p':
238			if (!ispunct((unsigned char)*cp++))
239				return (0);
240			break;
241		case 'P':
242			if (*cp != ' ' && !ispunct((unsigned char)*cp))
243				return (0);
244			cp++;
245			break;
246		case ':':
247			if (*cp++ != ':')
248				return (0);
249			break;
250		case 'N':
251			if (*cp++ != '\n')
252				return (0);
253			break;
254		}
255	if (*cp != '\0' || *tp != '\0')
256		return (0);
257	return (1);
258}
259
260/*
261 * Collect a liberal (space, tab delimited) word into the word buffer
262 * passed.  Also, return a pointer to the next word following that,
263 * or NULL if none follow.
264 */
265char *
266nextword(wp, wbuf)
267	char *wp, *wbuf;
268{
269	int c;
270
271	if (wp == NULL) {
272		*wbuf = '\0';
273		return (NULL);
274	}
275	while ((c = *wp++) != '\0' && c != ' ' && c != '\t') {
276		*wbuf++ = c;
277		if (c == '"') {
278 			while ((c = *wp++) != '\0' && c != '"')
279 				*wbuf++ = c;
280 			if (c == '"')
281 				*wbuf++ = c;
282			else
283				wp--;
284 		}
285	}
286	*wbuf = '\0';
287	for (; c == ' ' || c == '\t'; c = *wp++)
288		;
289	if (c == '\0')
290		return (NULL);
291	return (wp - 1);
292}
293