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