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