xref: /openbsd-src/usr.bin/mail/head.c (revision 5b133f3f277e80f096764111e64f3a1284acb179)
1 /*	$OpenBSD: head.c,v 1.13 2023/03/08 04:43:11 guenther 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 #include "rcv.h"
34 #include "extern.h"
35 
36 /*
37  * Mail -- a mail program
38  *
39  * Routines for processing and detecting headlines.
40  */
41 
42 /*
43  * See if the passed line buffer is a mail header.
44  * Return true if yes.  Note the extreme pains to
45  * accommodate all funny formats.
46  */
47 int
ishead(char * linebuf)48 ishead(char *linebuf)
49 {
50 	char *cp;
51 	struct headline hl;
52 	char parbuf[BUFSIZ];
53 
54 	cp = linebuf;
55 	if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
56 	    *cp++ != ' ')
57 		return(0);
58 	parse(linebuf, &hl, parbuf);
59 	if (hl.l_from == NULL || hl.l_date == NULL) {
60 		fail(linebuf, "No from or date field");
61 		return(0);
62 	}
63 	if (!isdate(hl.l_date)) {
64 		fail(linebuf, "Date field not legal date");
65 		return(0);
66 	}
67 	/*
68 	 * I guess we got it!
69 	 */
70 	return(1);
71 }
72 
73 void
fail(char * linebuf,char * reason)74 fail(char *linebuf, char *reason)
75 {
76 
77 	/*
78 	if (value("debug") == NULL)
79 		return;
80 	fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
81 	*/
82 }
83 
84 /*
85  * Split a headline into its useful components.
86  * Copy the line into dynamic string space, then set
87  * pointers into the copied line in the passed headline
88  * structure.  Actually, it scans.
89  */
90 void
parse(char * line,struct headline * hl,char * pbuf)91 parse(char *line, struct headline *hl, char *pbuf)
92 {
93 	char *cp, *sp;
94 	char word[LINESIZE];
95 
96 	hl->l_from = NULL;
97 	hl->l_tty = NULL;
98 	hl->l_date = NULL;
99 	cp = line;
100 	sp = pbuf;
101 	/*
102 	 * Skip over "From" first.
103 	 */
104 	cp = nextword(cp, word);
105 	cp = nextword(cp, word);
106 	if (*word)
107 		hl->l_from = copyin(word, &sp);
108 	if (cp != NULL && strncmp(cp, "tty", 3) == 0) {
109 		cp = nextword(cp, word);
110 		hl->l_tty = copyin(word, &sp);
111 	}
112 	if (cp != NULL)
113 		hl->l_date = copyin(cp, &sp);
114 }
115 
116 /*
117  * Copy the string on the left into the string on the right
118  * and bump the right (reference) string pointer by the length.
119  * Thus, dynamically allocate space in the right string, copying
120  * the left string into it.
121  */
122 char *
copyin(char * src,char ** space)123 copyin(char *src, char **space)
124 {
125 	char *cp, *top;
126 
127 	top = cp = *space;
128 	while ((*cp++ = *src++) != '\0')
129 		;
130 	*space = cp;
131 	return(top);
132 }
133 
134 /*
135  * Test to see if the passed string is a ctime(3) generated
136  * date string as documented in the manual.  The template
137  * below is used as the criterion of correctness.
138  * Also, we check for a possible trailing time zone using
139  * the tmztype template.
140  */
141 
142 /*
143  * 'A'	An upper case char
144  * 'a'	A lower case char
145  * ' '	A space
146  * '0'	A digit
147  * 'O'	A digit or space
148  * 'p'	A punctuation char
149  * 'P'	A punctuation char or space
150  * ':'	A colon
151  * 'N'	A new line
152  */
153 
154 /*
155  * Yuck.  If the mail file is created by Sys V (Solaris),
156  * there are no seconds in the time...
157  */
158 
159 /*
160  * If the mail is created by another program such as imapd, it might
161  * have timezone as <-|+>nnnn (-0800 for instance) at the end.
162  */
163 
164 static char *date_formats[] = {
165 	"Aaa Aaa O0 00:00:00 0000",	   /* Mon Jan 01 23:59:59 2001 */
166 	"Aaa Aaa O0 00:00:00 AAA 0000",	   /* Mon Jan 01 23:59:59 PST 2001 */
167 	"Aaa Aaa O0 00:00:00 0000 p0000",  /* Mon Jan 01 23:59:59 2001 -0800 */
168 	"Aaa Aaa O0 00:00 0000",	   /* Mon Jan 01 23:59 2001 */
169 	"Aaa Aaa O0 00:00 AAA 0000",	   /* Mon Jan 01 23:59 PST 2001 */
170 	"Aaa Aaa O0 00:00 0000 p0000",	   /* Mon Jan 01 23:59 2001 -0800 */
171 	""
172 };
173 
174 int
isdate(char * date)175 isdate(char *date)
176 {
177 	int i;
178 
179 	for(i = 0; *date_formats[i]; i++) {
180 		if (cmatch(date, date_formats[i]))
181 			return 1;
182 	}
183 	return 0;
184 }
185 
186 /*
187  * Match the given string (cp) against the given template (tp).
188  * Return 1 if they match, 0 if they don't
189  */
190 int
cmatch(char * cp,char * tp)191 cmatch(char *cp, char *tp)
192 {
193 
194 	while (*cp && *tp)
195 		switch (*tp++) {
196 		case 'a':
197 			if (!islower((unsigned char)*cp++))
198 				return(0);
199 			break;
200 		case 'A':
201 			if (!isupper((unsigned char)*cp++))
202 				return(0);
203 			break;
204 		case ' ':
205 			if (*cp++ != ' ')
206 				return(0);
207 			break;
208 		case '0':
209 			if (!isdigit((unsigned char)*cp++))
210 				return(0);
211 			break;
212 		case 'O':
213 			if (*cp != ' ' && !isdigit((unsigned char)*cp))
214 				return(0);
215 			cp++;
216 			break;
217 		case 'p':
218 			if (!ispunct((unsigned char)*cp++))
219 				return(0);
220 			break;
221 		case 'P':
222 			if (*cp != ' ' && !ispunct((unsigned char)*cp))
223 				return(0);
224 			cp++;
225 			break;
226 		case ':':
227 			if (*cp++ != ':')
228 				return(0);
229 			break;
230 		case 'N':
231 			if (*cp++ != '\n')
232 				return(0);
233 			break;
234 		}
235 	if (*cp || *tp)
236 		return(0);
237 	return(1);
238 }
239 
240 /*
241  * Collect a liberal (space, tab delimited) word into the word buffer
242  * passed.  Also, return a pointer to the next word following that,
243  * or NULL if none follow.
244  */
245 char *
nextword(char * wp,char * wbuf)246 nextword(char *wp, char *wbuf)
247 {
248 	int c;
249 
250 	if (wp == NULL) {
251 		*wbuf = 0;
252 		return(NULL);
253 	}
254 	while ((c = (unsigned char)*wp++) && c != ' ' && c != '\t') {
255 		*wbuf++ = c;
256 		if (c == '"') {
257  			while ((c = (unsigned char)*wp++) && c != '"')
258  				*wbuf++ = c;
259  			if (c == '"')
260  				*wbuf++ = c;
261 			else
262 				wp--;
263  		}
264 	}
265 	*wbuf = '\0';
266 	for (; c == ' ' || c == '\t'; c = (unsigned char)*wp++)
267 		;
268 	if (c == 0)
269 		return(NULL);
270 	return(wp - 1);
271 }
272