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