1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes #include "rcv.h"
339b50d902SRodney W. Grimes #include "extern.h"
349b50d902SRodney W. Grimes
359b50d902SRodney W. Grimes /*
369b50d902SRodney W. Grimes * Mail -- a mail program
379b50d902SRodney W. Grimes *
389b50d902SRodney W. Grimes * Routines for processing and detecting headlines.
399b50d902SRodney W. Grimes */
409b50d902SRodney W. Grimes
419b50d902SRodney W. Grimes /*
429b50d902SRodney W. Grimes * See if the passed line buffer is a mail header.
439b50d902SRodney W. Grimes * Return true if yes. Note the extreme pains to
44487ac9acSUlrich Spörlein * accommodate all funny formats.
459b50d902SRodney W. Grimes */
469b50d902SRodney W. Grimes int
ishead(char linebuf[])476d8484b0SPhilippe Charnier ishead(char linebuf[])
489b50d902SRodney W. Grimes {
499b50d902SRodney W. Grimes struct headline hl;
509b50d902SRodney W. Grimes char parbuf[BUFSIZ];
519b50d902SRodney W. Grimes
522e99d1d5SMike Heffner if (strncmp(linebuf, "From ", 5) != 0)
539b50d902SRodney W. Grimes return (0);
549b50d902SRodney W. Grimes parse(linebuf, &hl, parbuf);
552e99d1d5SMike Heffner if (hl.l_date == NULL) {
562e99d1d5SMike Heffner fail(linebuf, "No date field");
579b50d902SRodney W. Grimes return (0);
589b50d902SRodney W. Grimes }
599b50d902SRodney W. Grimes if (!isdate(hl.l_date)) {
609b50d902SRodney W. Grimes fail(linebuf, "Date field not legal date");
619b50d902SRodney W. Grimes return (0);
629b50d902SRodney W. Grimes }
639b50d902SRodney W. Grimes /*
649b50d902SRodney W. Grimes * I guess we got it!
659b50d902SRodney W. Grimes */
669b50d902SRodney W. Grimes return (1);
679b50d902SRodney W. Grimes }
689b50d902SRodney W. Grimes
699b50d902SRodney W. Grimes void
fail(const char * linebuf __unused,const char * reason __unused)706d8484b0SPhilippe Charnier fail(const char *linebuf __unused, const char *reason __unused)
719b50d902SRodney W. Grimes {
729b50d902SRodney W. Grimes
739b50d902SRodney W. Grimes /*
749ce73e90SMike Heffner if (value("debug") == NULL)
759b50d902SRodney W. Grimes return;
769b50d902SRodney W. Grimes fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
779b50d902SRodney W. Grimes */
789b50d902SRodney W. Grimes }
799b50d902SRodney W. Grimes
809b50d902SRodney W. Grimes /*
819b50d902SRodney W. Grimes * Split a headline into its useful components.
829b50d902SRodney W. Grimes * Copy the line into dynamic string space, then set
839b50d902SRodney W. Grimes * pointers into the copied line in the passed headline
849b50d902SRodney W. Grimes * structure. Actually, it scans.
859b50d902SRodney W. Grimes */
869b50d902SRodney W. Grimes void
parse(char line[],struct headline * hl,char pbuf[])876d8484b0SPhilippe Charnier parse(char line[], struct headline *hl, char pbuf[])
889b50d902SRodney W. Grimes {
899ce73e90SMike Heffner char *cp, *sp;
909b50d902SRodney W. Grimes char word[LINESIZE];
919b50d902SRodney W. Grimes
929ce73e90SMike Heffner hl->l_from = NULL;
939ce73e90SMike Heffner hl->l_tty = NULL;
949ce73e90SMike Heffner hl->l_date = NULL;
959b50d902SRodney W. Grimes cp = line;
969b50d902SRodney W. Grimes sp = pbuf;
979b50d902SRodney W. Grimes /*
989b50d902SRodney W. Grimes * Skip over "From" first.
999b50d902SRodney W. Grimes */
1009b50d902SRodney W. Grimes cp = nextword(cp, word);
1012e99d1d5SMike Heffner /*
1022e99d1d5SMike Heffner * Check for missing return-path.
1032e99d1d5SMike Heffner */
1042e99d1d5SMike Heffner if (isdate(cp)) {
1052e99d1d5SMike Heffner hl->l_date = copyin(cp, &sp);
1062e99d1d5SMike Heffner return;
1072e99d1d5SMike Heffner }
1089b50d902SRodney W. Grimes cp = nextword(cp, word);
1092e99d1d5SMike Heffner if (strlen(word) > 0)
1109b50d902SRodney W. Grimes hl->l_from = copyin(word, &sp);
1112e99d1d5SMike Heffner if (cp != NULL && strncmp(cp, "tty", 3) == 0) {
1129b50d902SRodney W. Grimes cp = nextword(cp, word);
1139b50d902SRodney W. Grimes hl->l_tty = copyin(word, &sp);
1149b50d902SRodney W. Grimes }
1159ce73e90SMike Heffner if (cp != NULL)
1169b50d902SRodney W. Grimes hl->l_date = copyin(cp, &sp);
1179b50d902SRodney W. Grimes }
1189b50d902SRodney W. Grimes
1199b50d902SRodney W. Grimes /*
1209b50d902SRodney W. Grimes * Copy the string on the left into the string on the right
1219b50d902SRodney W. Grimes * and bump the right (reference) string pointer by the length.
1229b50d902SRodney W. Grimes * Thus, dynamically allocate space in the right string, copying
1239b50d902SRodney W. Grimes * the left string into it.
1249b50d902SRodney W. Grimes */
1259b50d902SRodney W. Grimes char *
copyin(char * src,char ** space)1266d8484b0SPhilippe Charnier copyin(char *src, char **space)
1279b50d902SRodney W. Grimes {
1289ce73e90SMike Heffner char *cp, *top;
1299b50d902SRodney W. Grimes
1309b50d902SRodney W. Grimes top = cp = *space;
1319ce73e90SMike Heffner while ((*cp++ = *src++) != '\0')
1329b50d902SRodney W. Grimes ;
1339b50d902SRodney W. Grimes *space = cp;
1349b50d902SRodney W. Grimes return (top);
1359b50d902SRodney W. Grimes }
1369b50d902SRodney W. Grimes
1379b50d902SRodney W. Grimes /*
1389b50d902SRodney W. Grimes * Test to see if the passed string is a ctime(3) generated
1399b50d902SRodney W. Grimes * date string as documented in the manual. The template
1409b50d902SRodney W. Grimes * below is used as the criterion of correctness.
1419b50d902SRodney W. Grimes * Also, we check for a possible trailing time zone using
1429b50d902SRodney W. Grimes * the tmztype template.
143a9992a76SMike Heffner *
144a9992a76SMike Heffner * If the mail file is created by Sys V (Solaris), there are
145a9992a76SMike Heffner * no seconds in the time. If the mail is created by another
146a9992a76SMike Heffner * program such as imapd, it might have timezone as
147a9992a76SMike Heffner * <-|+>nnnn (-0800 for instance) at the end.
1489b50d902SRodney W. Grimes */
1499b50d902SRodney W. Grimes
1509b50d902SRodney W. Grimes /*
1519b50d902SRodney W. Grimes * 'A' An upper case char
1529b50d902SRodney W. Grimes * 'a' A lower case char
1539b50d902SRodney W. Grimes * ' ' A space
1549b50d902SRodney W. Grimes * '0' A digit
155a9992a76SMike Heffner * 'O' A digit or space
156a9992a76SMike Heffner * 'p' A punctuation char
157a9992a76SMike Heffner * 'P' A punctuation char or space
1589b50d902SRodney W. Grimes * ':' A colon
1599b50d902SRodney W. Grimes * 'N' A new line
1609b50d902SRodney W. Grimes */
161a9992a76SMike Heffner
162a9992a76SMike Heffner static char *date_formats[] = {
163a9992a76SMike Heffner "Aaa Aaa O0 00:00:00 0000", /* Mon Jan 01 23:59:59 2001 */
164a9992a76SMike Heffner "Aaa Aaa O0 00:00:00 AAA 0000", /* Mon Jan 01 23:59:59 PST 2001 */
165a9992a76SMike Heffner "Aaa Aaa O0 00:00:00 0000 p0000", /* Mon Jan 01 23:59:59 2001 -0800 */
166a9992a76SMike Heffner "Aaa Aaa O0 00:00 0000", /* Mon Jan 01 23:59 2001 */
167a9992a76SMike Heffner "Aaa Aaa O0 00:00 AAA 0000", /* Mon Jan 01 23:59 PST 2001 */
168a9992a76SMike Heffner "Aaa Aaa O0 00:00 0000 p0000", /* Mon Jan 01 23:59 2001 -0800 */
169a9992a76SMike Heffner NULL
170a9992a76SMike Heffner };
1719b50d902SRodney W. Grimes
1729b50d902SRodney W. Grimes int
isdate(char date[])1736d8484b0SPhilippe Charnier isdate(char date[])
1749b50d902SRodney W. Grimes {
175a9992a76SMike Heffner int i;
1769b50d902SRodney W. Grimes
177a9992a76SMike Heffner for(i = 0; date_formats[i] != NULL; i++) {
178a9992a76SMike Heffner if (cmatch(date, date_formats[i]))
179a9992a76SMike Heffner return (1);
180a9992a76SMike Heffner }
181a9992a76SMike Heffner return (0);
1829b50d902SRodney W. Grimes }
1839b50d902SRodney W. Grimes
1849b50d902SRodney W. Grimes /*
1859b50d902SRodney W. Grimes * Match the given string (cp) against the given template (tp).
1869b50d902SRodney W. Grimes * Return 1 if they match, 0 if they don't
1879b50d902SRodney W. Grimes */
1889b50d902SRodney W. Grimes int
cmatch(char * cp,char * tp)1896d8484b0SPhilippe Charnier cmatch(char *cp, char *tp)
1909b50d902SRodney W. Grimes {
1919b50d902SRodney W. Grimes
1929ce73e90SMike Heffner while (*cp != '\0' && *tp != '\0')
1939b50d902SRodney W. Grimes switch (*tp++) {
1949b50d902SRodney W. Grimes case 'a':
1956d48fa43SAndrey A. Chernov if (!islower((unsigned char)*cp++))
1969ce73e90SMike Heffner return (0);
1979b50d902SRodney W. Grimes break;
1989b50d902SRodney W. Grimes case 'A':
1996d48fa43SAndrey A. Chernov if (!isupper((unsigned char)*cp++))
2009ce73e90SMike Heffner return (0);
2019b50d902SRodney W. Grimes break;
2029b50d902SRodney W. Grimes case ' ':
2039b50d902SRodney W. Grimes if (*cp++ != ' ')
2049ce73e90SMike Heffner return (0);
2059b50d902SRodney W. Grimes break;
2069b50d902SRodney W. Grimes case '0':
2076d48fa43SAndrey A. Chernov if (!isdigit((unsigned char)*cp++))
2089ce73e90SMike Heffner return (0);
2099b50d902SRodney W. Grimes break;
2109b50d902SRodney W. Grimes case 'O':
2116d48fa43SAndrey A. Chernov if (*cp != ' ' && !isdigit((unsigned char)*cp))
2129ce73e90SMike Heffner return (0);
2139b50d902SRodney W. Grimes cp++;
2149b50d902SRodney W. Grimes break;
215a9992a76SMike Heffner case 'p':
2166d48fa43SAndrey A. Chernov if (!ispunct((unsigned char)*cp++))
217a9992a76SMike Heffner return (0);
218a9992a76SMike Heffner break;
219a9992a76SMike Heffner case 'P':
2206d48fa43SAndrey A. Chernov if (*cp != ' ' && !ispunct((unsigned char)*cp))
221a9992a76SMike Heffner return (0);
222a9992a76SMike Heffner cp++;
223a9992a76SMike Heffner break;
2249b50d902SRodney W. Grimes case ':':
2259b50d902SRodney W. Grimes if (*cp++ != ':')
2269ce73e90SMike Heffner return (0);
2279b50d902SRodney W. Grimes break;
2289b50d902SRodney W. Grimes case 'N':
2299b50d902SRodney W. Grimes if (*cp++ != '\n')
2309ce73e90SMike Heffner return (0);
2319b50d902SRodney W. Grimes break;
2329b50d902SRodney W. Grimes }
2339ce73e90SMike Heffner if (*cp != '\0' || *tp != '\0')
2349ce73e90SMike Heffner return (0);
2359b50d902SRodney W. Grimes return (1);
2369b50d902SRodney W. Grimes }
2379b50d902SRodney W. Grimes
2389b50d902SRodney W. Grimes /*
2399b50d902SRodney W. Grimes * Collect a liberal (space, tab delimited) word into the word buffer
2409b50d902SRodney W. Grimes * passed. Also, return a pointer to the next word following that,
2419ce73e90SMike Heffner * or NULL if none follow.
2429b50d902SRodney W. Grimes */
2439b50d902SRodney W. Grimes char *
nextword(char * wp,char * wbuf)2446d8484b0SPhilippe Charnier nextword(char *wp, char *wbuf)
2459b50d902SRodney W. Grimes {
2469ce73e90SMike Heffner int c;
2479b50d902SRodney W. Grimes
2489ce73e90SMike Heffner if (wp == NULL) {
2499ce73e90SMike Heffner *wbuf = '\0';
2509ce73e90SMike Heffner return (NULL);
2519b50d902SRodney W. Grimes }
2529ce73e90SMike Heffner while ((c = *wp++) != '\0' && c != ' ' && c != '\t') {
2539b50d902SRodney W. Grimes *wbuf++ = c;
2549b50d902SRodney W. Grimes if (c == '"') {
2559ce73e90SMike Heffner while ((c = *wp++) != '\0' && c != '"')
2569b50d902SRodney W. Grimes *wbuf++ = c;
2579b50d902SRodney W. Grimes if (c == '"')
2589b50d902SRodney W. Grimes *wbuf++ = c;
2599b50d902SRodney W. Grimes else
2609b50d902SRodney W. Grimes wp--;
2619b50d902SRodney W. Grimes }
2629b50d902SRodney W. Grimes }
2639b50d902SRodney W. Grimes *wbuf = '\0';
2649b50d902SRodney W. Grimes for (; c == ' ' || c == '\t'; c = *wp++)
2659b50d902SRodney W. Grimes ;
2669ce73e90SMike Heffner if (c == '\0')
2679ce73e90SMike Heffner return (NULL);
2689b50d902SRodney W. Grimes return (wp - 1);
2699b50d902SRodney W. Grimes }
270