1 /* $OpenBSD: head.c,v 1.6 2001/01/19 04:11:28 millert Exp $ */ 2 /* $NetBSD: head.c,v 1.6 1996/12/28 07:11:03 tls Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 4/20/95"; 40 #else 41 static char rcsid[] = "$OpenBSD: head.c,v 1.6 2001/01/19 04:11:28 millert 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 == NULL || hl.l_date == NULL) { 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") == NULL) 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, *sp; 111 char word[LINESIZE]; 112 113 hl->l_from = NULL; 114 hl->l_tty = NULL; 115 hl->l_date = NULL; 116 cp = line; 117 sp = pbuf; 118 /* 119 * Skip over "From" first. 120 */ 121 cp = nextword(cp, word); 122 cp = nextword(cp, word); 123 if (*word) 124 hl->l_from = copyin(word, &sp); 125 if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') { 126 cp = nextword(cp, word); 127 hl->l_tty = copyin(word, &sp); 128 } 129 if (cp != NULL) 130 hl->l_date = copyin(cp, &sp); 131 } 132 133 /* 134 * Copy the string on the left into the string on the right 135 * and bump the right (reference) string pointer by the length. 136 * Thus, dynamically allocate space in the right string, copying 137 * the left string into it. 138 */ 139 char * 140 copyin(src, space) 141 char *src; 142 char **space; 143 { 144 char *cp, *top; 145 146 top = cp = *space; 147 while ((*cp++ = *src++) != '\0') 148 ; 149 *space = cp; 150 return(top); 151 } 152 153 /* 154 * Test to see if the passed string is a ctime(3) generated 155 * date string as documented in the manual. The template 156 * below is used as the criterion of correctness. 157 * Also, we check for a possible trailing time zone using 158 * the tmztype template. 159 */ 160 161 /* 162 * 'A' An upper case char 163 * 'a' A lower case char 164 * ' ' A space 165 * '0' A digit 166 * 'O' A digit or space 167 * 'p' A punctuation char 168 * 'P' A punctuation char or space 169 * ':' A colon 170 * 'N' A new line 171 */ 172 173 /* 174 * Yuck. If the mail file is created by Sys V (Solaris), 175 * there are no seconds in the time... 176 */ 177 178 /* 179 * If the mail is created by another program such as imapd, it might 180 * have timezone as <-|+>nnnn (-0800 for instance) at the end. 181 */ 182 183 static char *date_formats[] = { 184 "Aaa Aaa O0 00:00:00 0000", /* Mon Jan 01 23:59:59 2001 */ 185 "Aaa Aaa O0 00:00:00 AAA 0000", /* Mon Jan 01 23:59:59 PST 2001 */ 186 "Aaa Aaa O0 00:00:00 0000 p0000", /* Mon Jan 01 23:59:59 2001 -0800 */ 187 "Aaa Aaa O0 00:00 0000", /* Mon Jan 01 23:59 2001 */ 188 "Aaa Aaa O0 00:00 AAA 0000", /* Mon Jan 01 23:59 PST 2001 */ 189 "Aaa Aaa O0 00:00 0000 p0000", /* Mon Jan 01 23:59 2001 -0800 */ 190 "" 191 }; 192 193 int 194 isdate(date) 195 char date[]; 196 { 197 int i; 198 199 for(i = 0; *date_formats[i]; i++) { 200 if (cmatch(date, date_formats[i])) 201 return 1; 202 } 203 return 0; 204 } 205 206 /* 207 * Match the given string (cp) against the given template (tp). 208 * Return 1 if they match, 0 if they don't 209 */ 210 int 211 cmatch(cp, tp) 212 char *cp, *tp; 213 { 214 215 while (*cp && *tp) 216 switch (*tp++) { 217 case 'a': 218 if (!islower(*cp++)) 219 return(0); 220 break; 221 case 'A': 222 if (!isupper(*cp++)) 223 return(0); 224 break; 225 case ' ': 226 if (*cp++ != ' ') 227 return(0); 228 break; 229 case '0': 230 if (!isdigit(*cp++)) 231 return(0); 232 break; 233 case 'O': 234 if (*cp != ' ' && !isdigit(*cp)) 235 return(0); 236 cp++; 237 break; 238 case 'p': 239 if (!ispunct(*cp++)) 240 return(0); 241 break; 242 case 'P': 243 if (*cp != ' ' && !ispunct(*cp)) 244 return(0); 245 cp++; 246 break; 247 case ':': 248 if (*cp++ != ':') 249 return(0); 250 break; 251 case 'N': 252 if (*cp++ != '\n') 253 return(0); 254 break; 255 } 256 if (*cp || *tp) 257 return(0); 258 return(1); 259 } 260 261 /* 262 * Collect a liberal (space, tab delimited) word into the word buffer 263 * passed. Also, return a pointer to the next word following that, 264 * or NULL if none follow. 265 */ 266 char * 267 nextword(wp, wbuf) 268 char *wp, *wbuf; 269 { 270 int c; 271 272 if (wp == NULL) { 273 *wbuf = 0; 274 return(NULL); 275 } 276 while ((c = *wp++) && c != ' ' && c != '\t') { 277 *wbuf++ = c; 278 if (c == '"') { 279 while ((c = *wp++) && c != '"') 280 *wbuf++ = c; 281 if (c == '"') 282 *wbuf++ = c; 283 else 284 wp--; 285 } 286 } 287 *wbuf = '\0'; 288 for (; c == ' ' || c == '\t'; c = *wp++) 289 ; 290 if (c == 0) 291 return(NULL); 292 return(wp - 1); 293 } 294