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