Fix spelling.
[dragonfly.git] / usr.bin / mail / head.c
blob4d71a4f7c51309835fb4950ad6ca04546cc4c3a5
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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.5 2004/09/08 03:01:11 joerg Exp $
38 #include "rcv.h"
39 #include "extern.h"
42 * Mail -- a mail program
44 * Routines for processing and detecting headlines.
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.
52 int
53 ishead(char *linebuf)
55 struct headline hl;
56 char parbuf[BUFSIZ];
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);
65 if (!isdate(hl.l_date)) {
66 fail(linebuf, "Date field not legal date");
67 return (0);
70 * I guess we got it!
72 return (1);
75 /*ARGSUSED*/
76 void
77 fail(const char *linebuf, const char *reason)
80 if (value("debug") == NULL)
81 return;
82 fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
87 * Split a headline into its useful components.
88 * Copy the line into dynamic string space, then set
89 * pointers into the copied line in the passed headline
90 * structure. Actually, it scans.
92 void
93 parse(char *line, struct headline *hl, char *pbuf)
95 char *cp, *sp;
96 char word[LINESIZE];
98 hl->l_from = NULL;
99 hl->l_tty = NULL;
100 hl->l_date = NULL;
101 cp = line;
102 sp = pbuf;
104 * Skip over "From" first.
106 cp = nextword(cp, word);
108 * Check for missing return-path.
110 if (isdate(cp)) {
111 hl->l_date = copyin(cp, &sp);
112 return;
114 cp = nextword(cp, word);
115 if (strlen(word) > 0)
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);
121 if (cp != NULL)
122 hl->l_date = copyin(cp, &sp);
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.
131 char *
132 copyin(char *src, char **space)
134 char *cp, *top;
136 top = cp = *space;
137 while ((*cp++ = *src++) != '\0')
139 *space = cp;
140 return (top);
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.
150 * If the mail file is created by Sys V (Solaris), there are
151 * no seconds in the time. If the mail is created by another
152 * program such as imapd, it might have timezone as
153 * <-|+>nnnn (-0800 for instance) at the end.
157 * 'A' An upper case char
158 * 'a' A lower case char
159 * ' ' A space
160 * '0' A digit
161 * 'O' A digit or space
162 * 'p' A punctuation char
163 * 'P' A punctuation char or space
164 * ':' A colon
165 * 'N' A new line
168 static char *date_formats[] = {
169 "Aaa Aaa O0 00:00:00 0000", /* Mon Jan 01 23:59:59 2001 */
170 "Aaa Aaa O0 00:00:00 AAA 0000", /* Mon Jan 01 23:59:59 PST 2001 */
171 "Aaa Aaa O0 00:00:00 0000 p0000", /* Mon Jan 01 23:59:59 2001 -0800 */
172 "Aaa Aaa O0 00:00 0000", /* Mon Jan 01 23:59 2001 */
173 "Aaa Aaa O0 00:00 AAA 0000", /* Mon Jan 01 23:59 PST 2001 */
174 "Aaa Aaa O0 00:00 0000 p0000", /* Mon Jan 01 23:59 2001 -0800 */
175 NULL
179 isdate(char *date)
181 int i;
183 for(i = 0; date_formats[i] != NULL; i++) {
184 if (cmatch(date, date_formats[i]))
185 return (1);
187 return (0);
191 * Match the given string (cp) against the given template (tp).
192 * Return 1 if they match, 0 if they don't
195 cmatch(char *cp, char *tp)
198 while (*cp != '\0' && *tp != '\0')
199 switch (*tp++) {
200 case 'a':
201 if (!islower((unsigned char)*cp++))
202 return (0);
203 break;
204 case 'A':
205 if (!isupper((unsigned char)*cp++))
206 return (0);
207 break;
208 case ' ':
209 if (*cp++ != ' ')
210 return (0);
211 break;
212 case '0':
213 if (!isdigit((unsigned char)*cp++))
214 return (0);
215 break;
216 case 'O':
217 if (*cp != ' ' && !isdigit((unsigned char)*cp))
218 return (0);
219 cp++;
220 break;
221 case 'p':
222 if (!ispunct((unsigned char)*cp++))
223 return (0);
224 break;
225 case 'P':
226 if (*cp != ' ' && !ispunct((unsigned char)*cp))
227 return (0);
228 cp++;
229 break;
230 case ':':
231 if (*cp++ != ':')
232 return (0);
233 break;
234 case 'N':
235 if (*cp++ != '\n')
236 return (0);
237 break;
239 if (*cp != '\0' || *tp != '\0')
240 return (0);
241 return (1);
245 * Collect a liberal (space, tab delimited) word into the word buffer
246 * passed. Also, return a pointer to the next word following that,
247 * or NULL if none follow.
249 char *
250 nextword(char *wp, char *wbuf)
252 int c;
254 if (wp == NULL) {
255 *wbuf = '\0';
256 return (NULL);
258 while ((c = *wp++) != '\0' && c != ' ' && c != '\t') {
259 *wbuf++ = c;
260 if (c == '"') {
261 while ((c = *wp++) != '\0' && c != '"')
262 *wbuf++ = c;
263 if (c == '"')
264 *wbuf++ = c;
265 else
266 wp--;
269 *wbuf = '\0';
270 for (; c == ' ' || c == '\t'; c = *wp++)
272 if (c == '\0')
273 return (NULL);
274 return (wp - 1);