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