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