head.c revision 1.22 1 1.22 mbalmer /* $NetBSD: head.c,v 1.22 2012/12/01 11:41:50 mbalmer 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.22 mbalmer __RCSID("$NetBSD: head.c,v 1.22 2012/12/01 11:41:50 mbalmer 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.21 christos return cmatch(date, ctype) ||
122 1.21 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.21 christos if (debug)
131 1.21 christos (void)fprintf(stderr, "\"%s\"\nnot a header because %s\n",
132 1.21 christos linebuf, reason);
133 1.20 christos #endif
134 1.1 cgd }
135 1.1 cgd
136 1.1 cgd /*
137 1.1 cgd * Collect a liberal (space, tab delimited) word into the word buffer
138 1.1 cgd * passed. Also, return a pointer to the next word following that,
139 1.11 wiz * or NULL if none follow.
140 1.1 cgd */
141 1.20 christos static const char *
142 1.15 christos nextword(const char *wp, char *wbuf)
143 1.1 cgd {
144 1.11 wiz if (wp == NULL) {
145 1.1 cgd *wbuf = 0;
146 1.20 christos return NULL;
147 1.1 cgd }
148 1.21 christos while (*wp && !is_WSP(*wp)) {
149 1.21 christos *wbuf++ = *wp;
150 1.21 christos if (*wp++ == '"') {
151 1.21 christos while (*wp && *wp != '"')
152 1.21 christos *wbuf++ = *wp++;
153 1.21 christos if (*wp == '"')
154 1.21 christos *wbuf++ = *wp++;
155 1.21 christos }
156 1.1 cgd }
157 1.1 cgd *wbuf = '\0';
158 1.21 christos wp = skip_WSP(wp);
159 1.21 christos if (*wp == '\0')
160 1.20 christos return NULL;
161 1.21 christos return wp;
162 1.20 christos }
163 1.20 christos
164 1.20 christos /*
165 1.20 christos * Copy the string on the left into the string on the right
166 1.20 christos * and bump the right (reference) string pointer by the length.
167 1.20 christos * Thus, dynamically allocate space in the right string, copying
168 1.20 christos * the left string into it.
169 1.20 christos */
170 1.20 christos static char *
171 1.20 christos copyin(const char *src, char **space)
172 1.20 christos {
173 1.20 christos char *cp;
174 1.20 christos char *begin;
175 1.20 christos
176 1.20 christos begin = cp = *space;
177 1.20 christos while ((*cp++ = *src++) != '\0')
178 1.20 christos continue;
179 1.20 christos *space = cp;
180 1.20 christos return begin;
181 1.20 christos }
182 1.20 christos
183 1.20 christos /*
184 1.20 christos * Split a headline into its useful components.
185 1.20 christos * Copy the line into dynamic string space, then set
186 1.20 christos * pointers into the copied line in the passed headline
187 1.20 christos * structure. Actually, it scans.
188 1.21 christos *
189 1.21 christos * XXX - line[], pbuf[], and word[] must be LINESIZE in length or
190 1.21 christos * overflow can occur in nextword() or copyin().
191 1.20 christos */
192 1.20 christos PUBLIC void
193 1.20 christos parse(const char line[], struct headline *hl, char pbuf[])
194 1.20 christos {
195 1.20 christos const char *cp;
196 1.20 christos char *sp;
197 1.20 christos char word[LINESIZE];
198 1.20 christos
199 1.20 christos hl->l_from = NULL;
200 1.20 christos hl->l_tty = NULL;
201 1.20 christos hl->l_date = NULL;
202 1.20 christos cp = line;
203 1.20 christos sp = pbuf;
204 1.20 christos /*
205 1.20 christos * Skip over "From" first.
206 1.20 christos */
207 1.20 christos cp = nextword(cp, word);
208 1.20 christos cp = nextword(cp, word);
209 1.20 christos if (*word)
210 1.20 christos hl->l_from = copyin(word, &sp);
211 1.20 christos if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
212 1.20 christos cp = nextword(cp, word);
213 1.20 christos hl->l_tty = copyin(word, &sp);
214 1.20 christos }
215 1.20 christos if (cp != NULL)
216 1.20 christos hl->l_date = copyin(cp, &sp);
217 1.20 christos }
218 1.20 christos
219 1.20 christos /*
220 1.20 christos * See if the passed line buffer is a mail header.
221 1.20 christos * Return true if yes. Note the extreme pains to
222 1.22 mbalmer * accommodate all funny formats.
223 1.20 christos */
224 1.20 christos PUBLIC int
225 1.20 christos ishead(const char linebuf[])
226 1.20 christos {
227 1.20 christos const char *cp;
228 1.20 christos struct headline hl;
229 1.20 christos char parbuf[LINESIZE];
230 1.20 christos
231 1.20 christos cp = linebuf;
232 1.20 christos if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
233 1.20 christos *cp++ != ' ')
234 1.20 christos return 0;
235 1.20 christos parse(linebuf, &hl, parbuf);
236 1.20 christos if (hl.l_from == NULL || hl.l_date == NULL) {
237 1.20 christos fail(linebuf, "No from or date field");
238 1.20 christos return 0;
239 1.20 christos }
240 1.20 christos if (!isdate(hl.l_date)) {
241 1.20 christos fail(linebuf, "Date field not legal date");
242 1.20 christos return 0;
243 1.20 christos }
244 1.20 christos /*
245 1.20 christos * I guess we got it!
246 1.20 christos */
247 1.20 christos return 1;
248 1.1 cgd }
249