1263382Smarcel/*
2263382Smarcel * Copyright (c) 1980, 1993
3263382Smarcel *	The Regents of the University of California.  All rights reserved.
4263382Smarcel *
5263382Smarcel * Redistribution and use in source and binary forms, with or without
6263382Smarcel * modification, are permitted provided that the following conditions
7263382Smarcel * are met:
8263382Smarcel * 1. Redistributions of source code must retain the above copyright
9263382Smarcel *    notice, this list of conditions and the following disclaimer.
10263382Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11263382Smarcel *    notice, this list of conditions and the following disclaimer in the
12263382Smarcel *    documentation and/or other materials provided with the distribution.
13263382Smarcel * 4. Neither the name of the University nor the names of its contributors
14263382Smarcel *    may be used to endorse or promote products derived from this software
15263382Smarcel *    without specific prior written permission.
16263382Smarcel *
17263382Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18263382Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19263382Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20263382Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21263382Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22263382Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23263382Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24263382Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25263382Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26263382Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27263382Smarcel * SUCH DAMAGE.
28263382Smarcel */
29263382Smarcel
30263382Smarcel#ifndef lint
31263382Smarcel#if 0
32263442Smarcelstatic char sccsid[] = "@(#)head.c	8.2 (Berkeley) 4/20/95";
33263442Smarcel#endif
34263382Smarcel#endif /* not lint */
35263382Smarcel#include <sys/cdefs.h>
36263414Smarcel__FBSDID("$FreeBSD$");
37263382Smarcel
38263382Smarcel#include "rcv.h"
39263382Smarcel#include "extern.h"
40263382Smarcel
41263382Smarcel/*
42263382Smarcel * Mail -- a mail program
43263382Smarcel *
44263461Smarcel * Routines for processing and detecting headlines.
45263653Smarcel */
46263653Smarcel
47263466Smarcel/*
48263382Smarcel * See if the passed line buffer is a mail header.
49263382Smarcel * Return true if yes.  Note the extreme pains to
50263382Smarcel * accomodate all funny formats.
51263382Smarcel */
52263382Smarcelint
53269177Smarcelishead(char linebuf[])
54263831Smarcel{
55263831Smarcel	struct headline hl;
56263709Smarcel	char parbuf[BUFSIZ];
57263709Smarcel
58263709Smarcel	if (strncmp(linebuf, "From ", 5) != 0)
59263709Smarcel		return (0);
60263709Smarcel	parse(linebuf, &hl, parbuf);
61263653Smarcel	if (hl.l_date == NULL) {
62263844Smarcel		fail(linebuf, "No date field");
63263844Smarcel		return (0);
64263844Smarcel	}
65263844Smarcel	if (!isdate(hl.l_date)) {
66263844Smarcel		fail(linebuf, "Date field not legal date");
67263844Smarcel		return (0);
68263844Smarcel	}
69272030Smarcel	/*
70272030Smarcel	 * I guess we got it!
71272030Smarcel	 */
72272030Smarcel	return (1);
73272030Smarcel}
74272030Smarcel
75272030Smarcelvoid
76272030Smarcelfail(const char *linebuf __unused, const char *reason __unused)
77272030Smarcel{
78272030Smarcel
79272030Smarcel	/*
80272030Smarcel	if (value("debug") == NULL)
81272030Smarcel		return;
82272030Smarcel	fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
83272030Smarcel	*/
84268161Smarcel}
85268161Smarcel
86268161Smarcel/*
87268161Smarcel * Split a headline into its useful components.
88268161Smarcel * Copy the line into dynamic string space, then set
89263653Smarcel * pointers into the copied line in the passed headline
90272776Smarcel * structure.  Actually, it scans.
91272776Smarcel */
92269177Smarcelvoid
93269177Smarcelparse(char line[], struct headline *hl, char pbuf[])
94269177Smarcel{
95263382Smarcel	char *cp, *sp;
96	char word[LINESIZE];
97
98	hl->l_from = NULL;
99	hl->l_tty = NULL;
100	hl->l_date = NULL;
101	cp = line;
102	sp = pbuf;
103	/*
104	 * Skip over "From" first.
105	 */
106	cp = nextword(cp, word);
107	/*
108	 * Check for missing return-path.
109	 */
110	if (isdate(cp)) {
111		hl->l_date = copyin(cp, &sp);
112		return;
113	}
114	cp = nextword(cp, word);
115	if (strlen(word) > 0)
116		hl->l_from = copyin(word, &sp);
117	if (cp != NULL && strncmp(cp, "tty", 3) == 0) {
118		cp = nextword(cp, word);
119		hl->l_tty = copyin(word, &sp);
120	}
121	if (cp != NULL)
122		hl->l_date = copyin(cp, &sp);
123}
124
125/*
126 * Copy the string on the left into the string on the right
127 * and bump the right (reference) string pointer by the length.
128 * Thus, dynamically allocate space in the right string, copying
129 * the left string into it.
130 */
131char *
132copyin(char *src, char **space)
133{
134	char *cp, *top;
135
136	top = cp = *space;
137	while ((*cp++ = *src++) != '\0')
138		;
139	*space = cp;
140	return (top);
141}
142
143/*
144 * Test to see if the passed string is a ctime(3) generated
145 * date string as documented in the manual.  The template
146 * below is used as the criterion of correctness.
147 * Also, we check for a possible trailing time zone using
148 * the tmztype template.
149 *
150 * If the mail file is created by Sys V (Solaris), there are
151 * no seconds in the time. If the mail is created by another
152 * program such as imapd, it might have timezone as
153 * <-|+>nnnn (-0800 for instance) at the end.
154 */
155
156/*
157 * 'A'	An upper case char
158 * 'a'	A lower case char
159 * ' '	A space
160 * '0'	A digit
161 * 'O'	A digit or space
162 * 'p'	A punctuation char
163 * 'P'	A punctuation char or space
164 * ':'	A colon
165 * 'N'	A new line
166 */
167
168static char *date_formats[] = {
169	"Aaa Aaa O0 00:00:00 0000",	   /* Mon Jan 01 23:59:59 2001 */
170	"Aaa Aaa O0 00:00:00 AAA 0000",	   /* Mon Jan 01 23:59:59 PST 2001 */
171	"Aaa Aaa O0 00:00:00 0000 p0000",  /* Mon Jan 01 23:59:59 2001 -0800 */
172	"Aaa Aaa O0 00:00 0000",	   /* Mon Jan 01 23:59 2001 */
173	"Aaa Aaa O0 00:00 AAA 0000",	   /* Mon Jan 01 23:59 PST 2001 */
174	"Aaa Aaa O0 00:00 0000 p0000",	   /* Mon Jan 01 23:59 2001 -0800 */
175	NULL
176};
177
178int
179isdate(char date[])
180{
181	int i;
182
183	for(i = 0; date_formats[i] != NULL; i++) {
184		if (cmatch(date, date_formats[i]))
185			return (1);
186	}
187	return (0);
188}
189
190/*
191 * Match the given string (cp) against the given template (tp).
192 * Return 1 if they match, 0 if they don't
193 */
194int
195cmatch(char *cp, char *tp)
196{
197
198	while (*cp != '\0' && *tp != '\0')
199		switch (*tp++) {
200		case 'a':
201			if (!islower((unsigned char)*cp++))
202				return (0);
203			break;
204		case 'A':
205			if (!isupper((unsigned char)*cp++))
206				return (0);
207			break;
208		case ' ':
209			if (*cp++ != ' ')
210				return (0);
211			break;
212		case '0':
213			if (!isdigit((unsigned char)*cp++))
214				return (0);
215			break;
216		case 'O':
217			if (*cp != ' ' && !isdigit((unsigned char)*cp))
218				return (0);
219			cp++;
220			break;
221		case 'p':
222			if (!ispunct((unsigned char)*cp++))
223				return (0);
224			break;
225		case 'P':
226			if (*cp != ' ' && !ispunct((unsigned char)*cp))
227				return (0);
228			cp++;
229			break;
230		case ':':
231			if (*cp++ != ':')
232				return (0);
233			break;
234		case 'N':
235			if (*cp++ != '\n')
236				return (0);
237			break;
238		}
239	if (*cp != '\0' || *tp != '\0')
240		return (0);
241	return (1);
242}
243
244/*
245 * Collect a liberal (space, tab delimited) word into the word buffer
246 * passed.  Also, return a pointer to the next word following that,
247 * or NULL if none follow.
248 */
249char *
250nextword(char *wp, char *wbuf)
251{
252	int c;
253
254	if (wp == NULL) {
255		*wbuf = '\0';
256		return (NULL);
257	}
258	while ((c = *wp++) != '\0' && c != ' ' && c != '\t') {
259		*wbuf++ = c;
260		if (c == '"') {
261 			while ((c = *wp++) != '\0' && c != '"')
262 				*wbuf++ = c;
263 			if (c == '"')
264 				*wbuf++ = c;
265			else
266				wp--;
267 		}
268	}
269	*wbuf = '\0';
270	for (; c == ' ' || c == '\t'; c = *wp++)
271		;
272	if (c == '\0')
273		return (NULL);
274	return (wp - 1);
275}
276