File: head.c

package info (click to toggle)
axmail 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 572 kB
  • sloc: ansic: 2,417; sh: 263; makefile: 49
file content (214 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

/* head.c - parse mail headers.
   Grabbed from mailx 5.3b, reformatted, hacked and cleaned
   to compile without warnings by Heikki Hannikainen <hessu@pspt.fi> */

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of California at Berkeley. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

/*
 * Mail -- a mail program
 *
 * Routines for processing and detecting headlines.
 */

#include <stdio.h>
#include <ctype.h>

#include "head.h"
#include "defines.h"

/*
 * See if the passed line buffer is a mail header.
 * Return true if yes.  Note the extreme pains to
 * accomodate all funny formats.
 */

int ishead(char linebuf[])
{
	char *cp;
	struct headline hl;
	char parbuf[LINESIZE];

	cp = linebuf;
	if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
	    *cp++ != ' ')
		return 0;
	parse(linebuf, &hl, parbuf);
	if (hl.l_from == NULL || hl.l_date == NULL) {
		/* No from or date field */
		return 0;
	}
	if (!isdate(hl.l_date)) {
		/* Date field not legal date */
		return 0;
	}
	/*
	 * I guess we got it!
	 */
	return 1;
}

/*
 * Split a headline into its useful components.
 * Copy the line into dynamic string space, then set
 * pointers into the copied line in the passed headline
 * structure.  Actually, it scans.
 */

void parse(char line[], struct headline *hl, char pbuf[])
{
	char *cp, *sp;
	char word[LINESIZE];

	hl->l_from = NULL;
	hl->l_tty = NULL;
	hl->l_date = NULL;
	cp = line;
	sp = pbuf;
	/*
	 * Skip over "From" first.
	 */
	cp = nextword(cp, word);
	cp = nextword(cp, word);
	if (*word)
		hl->l_from = copyin(word, &sp);
	if (cp != NULL && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
		cp = nextword(cp, word);
		hl->l_tty = copyin(word, &sp);
	}
	if (cp != NULL)
		hl->l_date = copyin(cp, &sp);
}

/*
 * Copy the string on the left into the string on the right
 * and bump the right (reference) string pointer by the length.
 * Thus, dynamically allocate space in the right string, copying
 * the left string into it.
 */

char *copyin(char *src, char **space)
{
	char *cp, *top;

	top = cp = *space;
	while ((*cp++ = *src++))
		;
	*space = cp;
	return top;
}

#define	L	1		/* A lower case char */
#define	S	2		/* A space */
#define	D	3		/* A digit */
#define	O	4		/* An optional digit or space */
#define	C	5		/* A colon */
#define	N	6		/* A new line */
#define U	7		/* An upper case char */

char ctypes[] = { U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,D,D,D,D,0 };
char tmztypes[] = { U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,U,U,U,S,D,D,D,D,0 };

/*
 * Match the given string (cp) against the given template (tp).
 * Return 1 if they match, 0 if they don't
 */

int cmatch(char *cp, char *tp)
{

	while (*cp && *tp)
		switch (*tp++) {
		case L:
			if (!islower(*cp++))
				return 0;
			break;
		case U:
			if (!isupper(*cp++))
				return 0;
			break;
		case S:
			if (*cp++ != ' ')
				return 0;
			break;
		case D:
			if (!isdigit(*cp++))
				return 0;
			break;
		case O:
			if (*cp != ' ' && !isdigit(*cp))
				return 0;
			cp++;
			break;
		case C:
			if (*cp++ != ':')
				return 0;
			break;
		case N:
			if (*cp++ != '\n')
				return 0;
			break;
		}
	if (*cp || *tp)
		return 0;
	return 1;
}

/*
 * Test to see if the passed string is a ctime(3) generated
 * date string as documented in the manual.  The template
 * below is used as the criterion of correctness.
 * Also, we check for a possible trailing time zone using
 * the auxtype template.
 */

int isdate(char date[])
{

	if (cmatch(date, ctypes))
		return 1;
	return cmatch(date, tmztypes);
}

/*
 * Collect a liberal (space, tab delimited) word into the word buffer
 * passed.  Also, return a pointer to the next word following that,
 * or NULL if none follow.
 */

char *nextword(char *wp, char *wbuf)
{
	char c;

	if (wp == NULL) {
		*wbuf = 0;
		return NULL;
	}
	while ((c = *wp++) && c != ' ' && c != '\t') {
		*wbuf++ = c;
		if (c == '"') {
 			while ((c = *wp++) && c != '"')
 				*wbuf++ = c;
 			if (c == '"')
 				*wbuf++ = c;
			else
				wp--;
 		}
	}
	*wbuf = '\0';
	for (; c == ' ' || c == '\t'; c = *wp++)
		;
	if (c == 0)
		return NULL;
	return (wp - 1);
}