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