xref: /dragonfly/usr.bin/mail/head.c (revision 2b3f93ea)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)head.c	8.2 (Berkeley) 4/20/95
30  * $FreeBSD: src/usr.bin/mail/head.c,v 1.1.1.1.14.5 2003/06/17 04:25:07 mikeh Exp $
31  * $DragonFly: src/usr.bin/mail/head.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
32  */
33 
34 #include "rcv.h"
35 #include "extern.h"
36 
37 /*
38  * Mail -- a mail program
39  *
40  * Routines for processing and detecting headlines.
41  */
42 
43 /*
44  * See if the passed line buffer is a mail header.
45  * Return true if yes.  Note the extreme pains to
46  * accomodate all funny formats.
47  */
48 int
49 ishead(char *linebuf)
50 {
51 	struct headline hl;
52 	char parbuf[BUFSIZ];
53 
54 	if (strncmp(linebuf, "From ", 5) != 0)
55 		return (0);
56 	parse(linebuf, &hl, parbuf);
57 	if (hl.l_date == NULL) {
58 		fail(linebuf, "No date field");
59 		return (0);
60 	}
61 	if (!isdate(hl.l_date)) {
62 		fail(linebuf, "Date field not legal date");
63 		return (0);
64 	}
65 	/*
66 	 * I guess we got it!
67 	 */
68 	return (1);
69 }
70 
71 /*ARGSUSED*/
72 void
73 fail(const char *linebuf, const char *reason)
74 {
75 	/*
76 	if (value("debug") == NULL)
77 		return;
78 	fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
79 	*/
80 }
81 
82 /*
83  * Split a headline into its useful components.
84  * Copy the line into dynamic string space, then set
85  * pointers into the copied line in the passed headline
86  * structure.  Actually, it scans.
87  */
88 void
89 parse(char *line, struct headline *hl, char *pbuf)
90 {
91 	char *cp, *sp;
92 	char word[LINESIZE];
93 
94 	hl->l_from = NULL;
95 	hl->l_tty = NULL;
96 	hl->l_date = NULL;
97 	cp = line;
98 	sp = pbuf;
99 	/*
100 	 * Skip over "From" first.
101 	 */
102 	cp = nextword(cp, word);
103 	/*
104 	 * Check for missing return-path.
105 	 */
106 	if (isdate(cp)) {
107 		hl->l_date = copyin(cp, &sp);
108 		return;
109 	}
110 	cp = nextword(cp, word);
111 	if (strlen(word) > 0)
112 		hl->l_from = copyin(word, &sp);
113 	if (cp != NULL && strncmp(cp, "tty", 3) == 0) {
114 		cp = nextword(cp, word);
115 		hl->l_tty = copyin(word, &sp);
116 	}
117 	if (cp != NULL)
118 		hl->l_date = copyin(cp, &sp);
119 }
120 
121 /*
122  * Copy the string on the left into the string on the right
123  * and bump the right (reference) string pointer by the length.
124  * Thus, dynamically allocate space in the right string, copying
125  * the left string into it.
126  */
127 char *
128 copyin(char *src, char **space)
129 {
130 	char *cp, *top;
131 
132 	top = cp = *space;
133 	while ((*cp++ = *src++) != '\0')
134 		;
135 	*space = cp;
136 	return (top);
137 }
138 
139 /*
140  * Test to see if the passed string is a ctime(3) generated
141  * date string as documented in the manual.  The template
142  * below is used as the criterion of correctness.
143  * Also, we check for a possible trailing time zone using
144  * the tmztype template.
145  *
146  * If the mail file is created by Sys V (Solaris), there are
147  * no seconds in the time. If the mail is created by another
148  * program such as imapd, it might have timezone as
149  * <-|+>nnnn (-0800 for instance) at the end.
150  */
151 
152 /*
153  * 'A'	An upper case char
154  * 'a'	A lower case char
155  * ' '	A space
156  * '0'	A digit
157  * 'O'	A digit or space
158  * 'p'	A punctuation char
159  * 'P'	A punctuation char or space
160  * ':'	A colon
161  * 'N'	A new line
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 	NULL
172 };
173 
174 int
175 isdate(char *date)
176 {
177 	int i;
178 
179 	for(i = 0; date_formats[i] != NULL; 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
191 cmatch(char *cp, char *tp)
192 {
193 
194 	while (*cp != '\0' && *tp != '\0')
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 != '\0' || *tp != '\0')
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 *
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 = *wp++) != '\0' && c != ' ' && c != '\t') {
255 		*wbuf++ = c;
256 		if (c == '"') {
257  			while ((c = *wp++) != '\0' && 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 = *wp++)
267 		;
268 	if (c == '\0')
269 		return (NULL);
270 	return (wp - 1);
271 }
272