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