head.c revision 1.23 1 1.23 christos /* $NetBSD: head.c,v 1.23 2013/01/15 17:25:42 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.23 christos __RCSID("$NetBSD: head.c,v 1.23 2013/01/15 17:25:42 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.23 christos cmatch(const char *cp, const 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.23 christos case '+':
90 1.23 christos if (*cp != '+' && *cp != '-')
91 1.23 christos return 0;
92 1.23 christos cp++;
93 1.23 christos break;
94 1.1 cgd }
95 1.1 cgd if (*cp || *tp)
96 1.1 cgd return 0;
97 1.20 christos return 1;
98 1.20 christos }
99 1.20 christos
100 1.20 christos /*
101 1.20 christos * Test to see if the passed string is a ctime(3) generated
102 1.20 christos * date string as documented in the manual. The template
103 1.20 christos * below is used as the criterion of correctness.
104 1.20 christos * Also, we check for a possible trailing time zone using
105 1.20 christos * the tmztype template.
106 1.20 christos */
107 1.20 christos
108 1.20 christos /*
109 1.20 christos * 'A' An upper case char
110 1.20 christos * 'a' A lower case char
111 1.20 christos * ' ' A space
112 1.20 christos * '0' A digit
113 1.20 christos * 'O' An optional digit or space
114 1.20 christos * ':' A colon
115 1.20 christos * 'N' A new line
116 1.23 christos * '+' A plus or minus sign
117 1.20 christos */
118 1.23 christos static const char *datetypes[] = {
119 1.23 christos "Aaa Aaa O0 00:00:00 0000", /* BSD ctype */
120 1.23 christos "Aaa Aaa O0 00:00 0000", /* SysV ctype */
121 1.23 christos "Aaa Aaa O0 00:00:00 AAA 0000", /* BSD tmztype */
122 1.23 christos "Aaa Aaa O0 00:00 AAA 0000", /* SysV tmztype */
123 1.23 christos "Aaa Aaa O0 00:00:00 0000 +0000", /* RFC822 type */
124 1.23 christos "Aaa Aaa O0 00:00:00 0000 AAA", /* RFC822 alttype */
125 1.23 christos };
126 1.20 christos
127 1.20 christos static int
128 1.20 christos isdate(const char date[])
129 1.20 christos {
130 1.20 christos
131 1.23 christos for (size_t i = 0; i < __arraycount(datetypes); i++)
132 1.23 christos if (cmatch(date, datetypes[i]))
133 1.23 christos return 1;
134 1.23 christos return 0;
135 1.20 christos }
136 1.20 christos
137 1.20 christos static void
138 1.20 christos fail(const char linebuf[], const char reason[])
139 1.20 christos {
140 1.20 christos #ifndef FMT_PROG
141 1.21 christos if (debug)
142 1.21 christos (void)fprintf(stderr, "\"%s\"\nnot a header because %s\n",
143 1.21 christos linebuf, reason);
144 1.20 christos #endif
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd /*
148 1.1 cgd * Collect a liberal (space, tab delimited) word into the word buffer
149 1.1 cgd * passed. Also, return a pointer to the next word following that,
150 1.11 wiz * or NULL if none follow.
151 1.1 cgd */
152 1.20 christos static const char *
153 1.15 christos nextword(const char *wp, char *wbuf)
154 1.1 cgd {
155 1.11 wiz if (wp == NULL) {
156 1.1 cgd *wbuf = 0;
157 1.20 christos return NULL;
158 1.1 cgd }
159 1.21 christos while (*wp && !is_WSP(*wp)) {
160 1.21 christos *wbuf++ = *wp;
161 1.21 christos if (*wp++ == '"') {
162 1.21 christos while (*wp && *wp != '"')
163 1.21 christos *wbuf++ = *wp++;
164 1.21 christos if (*wp == '"')
165 1.21 christos *wbuf++ = *wp++;
166 1.21 christos }
167 1.1 cgd }
168 1.1 cgd *wbuf = '\0';
169 1.21 christos wp = skip_WSP(wp);
170 1.21 christos if (*wp == '\0')
171 1.20 christos return NULL;
172 1.21 christos return wp;
173 1.20 christos }
174 1.20 christos
175 1.20 christos /*
176 1.20 christos * Copy the string on the left into the string on the right
177 1.20 christos * and bump the right (reference) string pointer by the length.
178 1.20 christos * Thus, dynamically allocate space in the right string, copying
179 1.20 christos * the left string into it.
180 1.20 christos */
181 1.20 christos static char *
182 1.20 christos copyin(const char *src, char **space)
183 1.20 christos {
184 1.20 christos char *cp;
185 1.20 christos char *begin;
186 1.20 christos
187 1.20 christos begin = cp = *space;
188 1.20 christos while ((*cp++ = *src++) != '\0')
189 1.20 christos continue;
190 1.20 christos *space = cp;
191 1.20 christos return begin;
192 1.20 christos }
193 1.20 christos
194 1.20 christos /*
195 1.20 christos * Split a headline into its useful components.
196 1.20 christos * Copy the line into dynamic string space, then set
197 1.20 christos * pointers into the copied line in the passed headline
198 1.20 christos * structure. Actually, it scans.
199 1.21 christos *
200 1.21 christos * XXX - line[], pbuf[], and word[] must be LINESIZE in length or
201 1.21 christos * overflow can occur in nextword() or copyin().
202 1.20 christos */
203 1.20 christos PUBLIC void
204 1.20 christos parse(const char line[], struct headline *hl, char pbuf[])
205 1.20 christos {
206 1.20 christos const char *cp;
207 1.20 christos char *sp;
208 1.20 christos char word[LINESIZE];
209 1.20 christos
210 1.20 christos hl->l_from = NULL;
211 1.20 christos hl->l_tty = NULL;
212 1.20 christos hl->l_date = NULL;
213 1.20 christos cp = line;
214 1.20 christos sp = pbuf;
215 1.20 christos /*
216 1.20 christos * Skip over "From" first.
217 1.20 christos */
218 1.20 christos cp = nextword(cp, word);
219 1.20 christos cp = nextword(cp, word);
220 1.20 christos if (*word)
221 1.20 christos hl->l_from = copyin(word, &sp);
222 1.20 christos if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
223 1.20 christos cp = nextword(cp, word);
224 1.20 christos hl->l_tty = copyin(word, &sp);
225 1.20 christos }
226 1.20 christos if (cp != NULL)
227 1.20 christos hl->l_date = copyin(cp, &sp);
228 1.20 christos }
229 1.20 christos
230 1.20 christos /*
231 1.20 christos * See if the passed line buffer is a mail header.
232 1.20 christos * Return true if yes. Note the extreme pains to
233 1.22 mbalmer * accommodate all funny formats.
234 1.20 christos */
235 1.20 christos PUBLIC int
236 1.20 christos ishead(const char linebuf[])
237 1.20 christos {
238 1.20 christos const char *cp;
239 1.20 christos struct headline hl;
240 1.20 christos char parbuf[LINESIZE];
241 1.20 christos
242 1.20 christos cp = linebuf;
243 1.20 christos if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
244 1.20 christos *cp++ != ' ')
245 1.20 christos return 0;
246 1.20 christos parse(linebuf, &hl, parbuf);
247 1.20 christos if (hl.l_from == NULL || hl.l_date == NULL) {
248 1.20 christos fail(linebuf, "No from or date field");
249 1.20 christos return 0;
250 1.20 christos }
251 1.20 christos if (!isdate(hl.l_date)) {
252 1.20 christos fail(linebuf, "Date field not legal date");
253 1.20 christos return 0;
254 1.20 christos }
255 1.20 christos /*
256 1.20 christos * I guess we got it!
257 1.20 christos */
258 1.20 christos return 1;
259 1.1 cgd }
260