1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * 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 /*static char sccsid[] = "from: @(#)head.c 5.7 (Berkeley) 6/1/90";*/ 36 static char rcsid[] = "$Id: head.c,v 1.2 1993/08/01 18:13:04 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include "rcv.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 ishead(linebuf) 53 char linebuf[]; 54 { 55 register char *cp; 56 struct headline hl; 57 char parbuf[BUFSIZ]; 58 59 cp = linebuf; 60 if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' || 61 *cp++ != ' ') 62 return (0); 63 parse(linebuf, &hl, parbuf); 64 if (hl.l_from == NOSTR || hl.l_date == NOSTR) { 65 fail(linebuf, "No from or date field"); 66 return (0); 67 } 68 if (!isdate(hl.l_date)) { 69 fail(linebuf, "Date field not legal date"); 70 return (0); 71 } 72 /* 73 * I guess we got it! 74 */ 75 return (1); 76 } 77 78 /*ARGSUSED*/ 79 fail(linebuf, reason) 80 char linebuf[], reason[]; 81 { 82 83 /* 84 if (value("debug") == NOSTR) 85 return; 86 fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason); 87 */ 88 } 89 90 /* 91 * Split a headline into its useful components. 92 * Copy the line into dynamic string space, then set 93 * pointers into the copied line in the passed headline 94 * structure. Actually, it scans. 95 */ 96 parse(line, hl, pbuf) 97 char line[], pbuf[]; 98 register struct headline *hl; 99 { 100 register char *cp; 101 char *sp; 102 char word[LINESIZE]; 103 104 hl->l_from = NOSTR; 105 hl->l_tty = NOSTR; 106 hl->l_date = NOSTR; 107 cp = line; 108 sp = pbuf; 109 /* 110 * Skip over "From" first. 111 */ 112 cp = nextword(cp, word); 113 cp = nextword(cp, word); 114 if (*word) 115 hl->l_from = copyin(word, &sp); 116 if (cp != NOSTR && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') { 117 cp = nextword(cp, word); 118 hl->l_tty = copyin(word, &sp); 119 } 120 if (cp != NOSTR) 121 hl->l_date = copyin(cp, &sp); 122 } 123 124 /* 125 * Copy the string on the left into the string on the right 126 * and bump the right (reference) string pointer by the length. 127 * Thus, dynamically allocate space in the right string, copying 128 * the left string into it. 129 */ 130 char * 131 copyin(src, space) 132 register char *src; 133 char **space; 134 { 135 register char *cp; 136 char *top; 137 138 top = cp = *space; 139 while (*cp++ = *src++) 140 ; 141 *space = cp; 142 return (top); 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 tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000"; 164 165 isdate(date) 166 char date[]; 167 { 168 169 return cmatch(date, ctype) || cmatch(date, tmztype); 170 } 171 172 /* 173 * Match the given string (cp) against the given template (tp). 174 * Return 1 if they match, 0 if they don't 175 */ 176 cmatch(cp, tp) 177 register char *cp, *tp; 178 { 179 180 while (*cp && *tp) 181 switch (*tp++) { 182 case 'a': 183 if (!islower(*cp++)) 184 return 0; 185 break; 186 case 'A': 187 if (!isupper(*cp++)) 188 return 0; 189 break; 190 case ' ': 191 if (*cp++ != ' ') 192 return 0; 193 break; 194 case '0': 195 if (!isdigit(*cp++)) 196 return 0; 197 break; 198 case 'O': 199 if (*cp != ' ' && !isdigit(*cp)) 200 return 0; 201 cp++; 202 break; 203 case ':': 204 if (*cp++ != ':') 205 return 0; 206 break; 207 case 'N': 208 if (*cp++ != '\n') 209 return 0; 210 break; 211 } 212 if (*cp || *tp) 213 return 0; 214 return (1); 215 } 216 217 /* 218 * Collect a liberal (space, tab delimited) word into the word buffer 219 * passed. Also, return a pointer to the next word following that, 220 * or NOSTR if none follow. 221 */ 222 char * 223 nextword(wp, wbuf) 224 register char *wp, *wbuf; 225 { 226 register c; 227 228 if (wp == NOSTR) { 229 *wbuf = 0; 230 return (NOSTR); 231 } 232 while ((c = *wp++) && c != ' ' && c != '\t') { 233 *wbuf++ = c; 234 if (c == '"') { 235 while ((c = *wp++) && c != '"') 236 *wbuf++ = c; 237 if (c == '"') 238 *wbuf++ = c; 239 else 240 wp--; 241 } 242 } 243 *wbuf = '\0'; 244 for (; c == ' ' || c == '\t'; c = *wp++) 245 ; 246 if (c == 0) 247 return (NOSTR); 248 return (wp - 1); 249 } 250