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