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 * @(#)head.c 8.2 (Berkeley) 4/20/95 34 * $FreeBSD: src/usr.bin/mail/head.c,v 1.1.1.1.14.4 2003/01/06 05:46:03 mikeh Exp $ 35 * $DragonFly: src/usr.bin/mail/head.c,v 1.2 2003/06/17 04:29:28 dillon Exp $ 36 */ 37 38 #include "rcv.h" 39 #include "extern.h" 40 41 /* 42 * Mail -- a mail program 43 * 44 * Routines for processing and detecting headlines. 45 */ 46 47 /* 48 * See if the passed line buffer is a mail header. 49 * Return true if yes. Note the extreme pains to 50 * accomodate all funny formats. 51 */ 52 int 53 ishead(linebuf) 54 char linebuf[]; 55 { 56 struct headline hl; 57 char parbuf[BUFSIZ]; 58 59 if (strncmp(linebuf, "From ", 5)) 60 return (0); 61 parse(linebuf, &hl, parbuf); 62 if (hl.l_from == NULL || hl.l_date == NULL) { 63 fail(linebuf, "No from or date field"); 64 return (0); 65 } 66 if (!isdate(hl.l_date)) { 67 fail(linebuf, "Date field not legal date"); 68 return (0); 69 } 70 /* 71 * I guess we got it! 72 */ 73 return (1); 74 } 75 76 /*ARGSUSED*/ 77 void 78 fail(linebuf, reason) 79 const char *linebuf, *reason; 80 { 81 82 /* 83 if (value("debug") == NULL) 84 return; 85 fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason); 86 */ 87 } 88 89 /* 90 * Split a headline into its useful components. 91 * Copy the line into dynamic string space, then set 92 * pointers into the copied line in the passed headline 93 * structure. Actually, it scans. 94 */ 95 void 96 parse(line, hl, pbuf) 97 char line[], pbuf[]; 98 struct headline *hl; 99 { 100 char *cp, *sp; 101 char word[LINESIZE]; 102 103 hl->l_from = NULL; 104 hl->l_tty = NULL; 105 hl->l_date = NULL; 106 cp = line; 107 sp = pbuf; 108 /* 109 * Skip over "From" first. 110 */ 111 cp = nextword(cp, word); 112 cp = nextword(cp, word); 113 if (*word != '\0') 114 hl->l_from = copyin(word, &sp); 115 if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') { 116 cp = nextword(cp, word); 117 hl->l_tty = copyin(word, &sp); 118 } 119 if (cp != NULL) 120 hl->l_date = copyin(cp, &sp); 121 } 122 123 /* 124 * Copy the string on the left into the string on the right 125 * and bump the right (reference) string pointer by the length. 126 * Thus, dynamically allocate space in the right string, copying 127 * the left string into it. 128 */ 129 char * 130 copyin(src, space) 131 char *src; 132 char **space; 133 { 134 char *cp, *top; 135 136 top = cp = *space; 137 while ((*cp++ = *src++) != '\0') 138 ; 139 *space = cp; 140 return (top); 141 } 142 143 /* 144 * Test to see if the passed string is a ctime(3) generated 145 * date string as documented in the manual. The template 146 * below is used as the criterion of correctness. 147 * Also, we check for a possible trailing time zone using 148 * the tmztype template. 149 * 150 * If the mail file is created by Sys V (Solaris), there are 151 * no seconds in the time. If the mail is created by another 152 * program such as imapd, it might have timezone as 153 * <-|+>nnnn (-0800 for instance) at the end. 154 */ 155 156 /* 157 * 'A' An upper case char 158 * 'a' A lower case char 159 * ' ' A space 160 * '0' A digit 161 * 'O' A digit or space 162 * 'p' A punctuation char 163 * 'P' A punctuation char or space 164 * ':' A colon 165 * 'N' A new line 166 */ 167 168 static char *date_formats[] = { 169 "Aaa Aaa O0 00:00:00 0000", /* Mon Jan 01 23:59:59 2001 */ 170 "Aaa Aaa O0 00:00:00 AAA 0000", /* Mon Jan 01 23:59:59 PST 2001 */ 171 "Aaa Aaa O0 00:00:00 0000 p0000", /* Mon Jan 01 23:59:59 2001 -0800 */ 172 "Aaa Aaa O0 00:00 0000", /* Mon Jan 01 23:59 2001 */ 173 "Aaa Aaa O0 00:00 AAA 0000", /* Mon Jan 01 23:59 PST 2001 */ 174 "Aaa Aaa O0 00:00 0000 p0000", /* Mon Jan 01 23:59 2001 -0800 */ 175 NULL 176 }; 177 178 int 179 isdate(date) 180 char date[]; 181 { 182 int i; 183 184 for(i = 0; date_formats[i] != NULL; i++) { 185 if (cmatch(date, date_formats[i])) 186 return (1); 187 } 188 return (0); 189 } 190 191 /* 192 * Match the given string (cp) against the given template (tp). 193 * Return 1 if they match, 0 if they don't 194 */ 195 int 196 cmatch(cp, tp) 197 char *cp, *tp; 198 { 199 200 while (*cp != '\0' && *tp != '\0') 201 switch (*tp++) { 202 case 'a': 203 if (!islower((unsigned char)*cp++)) 204 return (0); 205 break; 206 case 'A': 207 if (!isupper((unsigned char)*cp++)) 208 return (0); 209 break; 210 case ' ': 211 if (*cp++ != ' ') 212 return (0); 213 break; 214 case '0': 215 if (!isdigit((unsigned char)*cp++)) 216 return (0); 217 break; 218 case 'O': 219 if (*cp != ' ' && !isdigit((unsigned char)*cp)) 220 return (0); 221 cp++; 222 break; 223 case 'p': 224 if (!ispunct((unsigned char)*cp++)) 225 return (0); 226 break; 227 case 'P': 228 if (*cp != ' ' && !ispunct((unsigned char)*cp)) 229 return (0); 230 cp++; 231 break; 232 case ':': 233 if (*cp++ != ':') 234 return (0); 235 break; 236 case 'N': 237 if (*cp++ != '\n') 238 return (0); 239 break; 240 } 241 if (*cp != '\0' || *tp != '\0') 242 return (0); 243 return (1); 244 } 245 246 /* 247 * Collect a liberal (space, tab delimited) word into the word buffer 248 * passed. Also, return a pointer to the next word following that, 249 * or NULL if none follow. 250 */ 251 char * 252 nextword(wp, wbuf) 253 char *wp, *wbuf; 254 { 255 int c; 256 257 if (wp == NULL) { 258 *wbuf = '\0'; 259 return (NULL); 260 } 261 while ((c = *wp++) != '\0' && c != ' ' && c != '\t') { 262 *wbuf++ = c; 263 if (c == '"') { 264 while ((c = *wp++) != '\0' && c != '"') 265 *wbuf++ = c; 266 if (c == '"') 267 *wbuf++ = c; 268 else 269 wp--; 270 } 271 } 272 *wbuf = '\0'; 273 for (; c == ' ' || c == '\t'; c = *wp++) 274 ; 275 if (c == '\0') 276 return (NULL); 277 return (wp - 1); 278 } 279