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