1 /* $NetBSD: head.c,v 1.19 2006/11/24 19:47:00 christos 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.19 2006/11/24 19:47:00 christos 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 * accommodate all funny formats. 54 */ 55 int 56 ishead(const char linebuf[]) 57 { 58 const 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 void 82 fail(const char linebuf[], const char reason[]) 83 { 84 #ifndef FMT_PROG 85 if (value("debug") == NULL) 86 return; 87 (void)fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, 88 reason); 89 #endif 90 } 91 92 /* 93 * Split a headline into its useful components. 94 * Copy the line into dynamic string space, then set 95 * pointers into the copied line in the passed headline 96 * structure. Actually, it scans. 97 */ 98 void 99 parse(const char line[], struct headline *hl, char pbuf[]) 100 { 101 const char *cp; 102 char *sp; 103 char word[LINESIZE]; 104 105 hl->l_from = NULL; 106 hl->l_tty = NULL; 107 hl->l_date = NULL; 108 cp = line; 109 sp = pbuf; 110 /* 111 * Skip over "From" first. 112 */ 113 cp = nextword(cp, word); 114 cp = nextword(cp, word); 115 if (*word) 116 hl->l_from = copyin(word, &sp); 117 if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') { 118 cp = nextword(cp, word); 119 hl->l_tty = copyin(word, &sp); 120 } 121 if (cp != NULL) 122 hl->l_date = copyin(cp, &sp); 123 } 124 125 /* 126 * Copy the string on the left into the string on the right 127 * and bump the right (reference) string pointer by the length. 128 * Thus, dynamically allocate space in the right string, copying 129 * the left string into it. 130 */ 131 char * 132 copyin(const char *src, char **space) 133 { 134 char *cp; 135 char *begin; 136 137 begin = cp = *space; 138 while ((*cp++ = *src++) != '\0') 139 ; 140 *space = cp; 141 return (begin); 142 } 143 144 /* 145 * Test to see if the passed string is a ctime(3) generated 146 * date string as documented in the manual. The template 147 * below is used as the criterion of correctness. 148 * Also, we check for a possible trailing time zone using 149 * the tmztype template. 150 */ 151 152 /* 153 * 'A' An upper case char 154 * 'a' A lower case char 155 * ' ' A space 156 * '0' A digit 157 * 'O' An optional digit or space 158 * ':' A colon 159 * 'N' A new line 160 */ 161 char ctype[] = "Aaa Aaa O0 00:00:00 0000"; 162 char SysV_ctype[] = "Aaa Aaa O0 00:00 0000"; 163 char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000"; 164 char SysV_tmztype[] = "Aaa Aaa O0 00:00 AAA 0000"; 165 166 int 167 isdate(const char date[]) 168 { 169 170 return cmatch(date, ctype) || 171 cmatch(date, tmztype) || 172 cmatch(date, SysV_tmztype) || cmatch(date, SysV_ctype); 173 } 174 175 /* 176 * Match the given string (cp) against the given template (tp). 177 * Return 1 if they match, 0 if they don't 178 */ 179 int 180 cmatch(const char *cp, char *tp) 181 { 182 183 while (*cp && *tp) 184 switch (*tp++) { 185 case 'a': 186 if (!islower((unsigned char)*cp++)) 187 return 0; 188 break; 189 case 'A': 190 if (!isupper((unsigned char)*cp++)) 191 return 0; 192 break; 193 case ' ': 194 if (*cp++ != ' ') 195 return 0; 196 break; 197 case '0': 198 if (!isdigit((unsigned char)*cp++)) 199 return 0; 200 break; 201 case 'O': 202 if (*cp != ' ' && !isdigit((unsigned char)*cp)) 203 return 0; 204 cp++; 205 break; 206 case ':': 207 if (*cp++ != ':') 208 return 0; 209 break; 210 case 'N': 211 if (*cp++ != '\n') 212 return 0; 213 break; 214 } 215 if (*cp || *tp) 216 return 0; 217 return (1); 218 } 219 220 /* 221 * Collect a liberal (space, tab delimited) word into the word buffer 222 * passed. Also, return a pointer to the next word following that, 223 * or NULL if none follow. 224 */ 225 const char * 226 nextword(const char *wp, char *wbuf) 227 { 228 int c; 229 230 if (wp == NULL) { 231 *wbuf = 0; 232 return (NULL); 233 } 234 while ((c = *wp++) && c != ' ' && c != '\t') { 235 *wbuf++ = c; 236 if (c == '"') { 237 while ((c = *wp++) && c != '"') 238 *wbuf++ = c; 239 if (c == '"') 240 *wbuf++ = c; 241 else 242 wp--; 243 } 244 } 245 *wbuf = '\0'; 246 for (; c == ' ' || c == '\t'; c = *wp++) 247 ; 248 if (c == 0) 249 return (NULL); 250 return (wp - 1); 251 } 252