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 #if 0 36 static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 4/20/95"; 37 #endif 38 #endif /* not lint */ 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "rcv.h" 43 #include "extern.h" 44 45 /* 46 * Mail -- a mail program 47 * 48 * Routines for processing and detecting headlines. 49 */ 50 51 /* 52 * See if the passed line buffer is a mail header. 53 * Return true if yes. Note the extreme pains to 54 * accomodate all funny formats. 55 */ 56 int 57 ishead(linebuf) 58 char linebuf[]; 59 { 60 struct headline hl; 61 char parbuf[BUFSIZ]; 62 63 if (strncmp(linebuf, "From ", 5) != 0) 64 return (0); 65 parse(linebuf, &hl, parbuf); 66 if (hl.l_date == NULL) { 67 fail(linebuf, "No 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 const char *linebuf, *reason; 84 { 85 86 /* 87 if (value("debug") == NULL) 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 struct headline *hl; 103 { 104 char *cp, *sp; 105 char word[LINESIZE]; 106 107 hl->l_from = NULL; 108 hl->l_tty = NULL; 109 hl->l_date = NULL; 110 cp = line; 111 sp = pbuf; 112 /* 113 * Skip over "From" first. 114 */ 115 cp = nextword(cp, word); 116 /* 117 * Check for missing return-path. 118 */ 119 if (isdate(cp)) { 120 hl->l_date = copyin(cp, &sp); 121 return; 122 } 123 cp = nextword(cp, word); 124 if (strlen(word) > 0) 125 hl->l_from = copyin(word, &sp); 126 if (cp != NULL && strncmp(cp, "tty", 3) == 0) { 127 cp = nextword(cp, word); 128 hl->l_tty = copyin(word, &sp); 129 } 130 if (cp != NULL) 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, *top; 146 147 top = cp = *space; 148 while ((*cp++ = *src++) != '\0') 149 ; 150 *space = cp; 151 return (top); 152 } 153 154 /* 155 * Test to see if the passed string is a ctime(3) generated 156 * date string as documented in the manual. The template 157 * below is used as the criterion of correctness. 158 * Also, we check for a possible trailing time zone using 159 * the tmztype template. 160 * 161 * If the mail file is created by Sys V (Solaris), there are 162 * no seconds in the time. If the mail is created by another 163 * program such as imapd, it might have timezone as 164 * <-|+>nnnn (-0800 for instance) at the end. 165 */ 166 167 /* 168 * 'A' An upper case char 169 * 'a' A lower case char 170 * ' ' A space 171 * '0' A digit 172 * 'O' A digit or space 173 * 'p' A punctuation char 174 * 'P' A punctuation char or space 175 * ':' A colon 176 * 'N' A new line 177 */ 178 179 static char *date_formats[] = { 180 "Aaa Aaa O0 00:00:00 0000", /* Mon Jan 01 23:59:59 2001 */ 181 "Aaa Aaa O0 00:00:00 AAA 0000", /* Mon Jan 01 23:59:59 PST 2001 */ 182 "Aaa Aaa O0 00:00:00 0000 p0000", /* Mon Jan 01 23:59:59 2001 -0800 */ 183 "Aaa Aaa O0 00:00 0000", /* Mon Jan 01 23:59 2001 */ 184 "Aaa Aaa O0 00:00 AAA 0000", /* Mon Jan 01 23:59 PST 2001 */ 185 "Aaa Aaa O0 00:00 0000 p0000", /* Mon Jan 01 23:59 2001 -0800 */ 186 NULL 187 }; 188 189 int 190 isdate(date) 191 char date[]; 192 { 193 int i; 194 195 for(i = 0; date_formats[i] != NULL; i++) { 196 if (cmatch(date, date_formats[i])) 197 return (1); 198 } 199 return (0); 200 } 201 202 /* 203 * Match the given string (cp) against the given template (tp). 204 * Return 1 if they match, 0 if they don't 205 */ 206 int 207 cmatch(cp, tp) 208 char *cp, *tp; 209 { 210 211 while (*cp != '\0' && *tp != '\0') 212 switch (*tp++) { 213 case 'a': 214 if (!islower((unsigned char)*cp++)) 215 return (0); 216 break; 217 case 'A': 218 if (!isupper((unsigned char)*cp++)) 219 return (0); 220 break; 221 case ' ': 222 if (*cp++ != ' ') 223 return (0); 224 break; 225 case '0': 226 if (!isdigit((unsigned char)*cp++)) 227 return (0); 228 break; 229 case 'O': 230 if (*cp != ' ' && !isdigit((unsigned char)*cp)) 231 return (0); 232 cp++; 233 break; 234 case 'p': 235 if (!ispunct((unsigned char)*cp++)) 236 return (0); 237 break; 238 case 'P': 239 if (*cp != ' ' && !ispunct((unsigned char)*cp)) 240 return (0); 241 cp++; 242 break; 243 case ':': 244 if (*cp++ != ':') 245 return (0); 246 break; 247 case 'N': 248 if (*cp++ != '\n') 249 return (0); 250 break; 251 } 252 if (*cp != '\0' || *tp != '\0') 253 return (0); 254 return (1); 255 } 256 257 /* 258 * Collect a liberal (space, tab delimited) word into the word buffer 259 * passed. Also, return a pointer to the next word following that, 260 * or NULL if none follow. 261 */ 262 char * 263 nextword(wp, wbuf) 264 char *wp, *wbuf; 265 { 266 int c; 267 268 if (wp == NULL) { 269 *wbuf = '\0'; 270 return (NULL); 271 } 272 while ((c = *wp++) != '\0' && c != ' ' && c != '\t') { 273 *wbuf++ = c; 274 if (c == '"') { 275 while ((c = *wp++) != '\0' && c != '"') 276 *wbuf++ = c; 277 if (c == '"') 278 *wbuf++ = c; 279 else 280 wp--; 281 } 282 } 283 *wbuf = '\0'; 284 for (; c == ' ' || c == '\t'; c = *wp++) 285 ; 286 if (c == '\0') 287 return (NULL); 288 return (wp - 1); 289 } 290