summary refs log tree commit diff
path: root/blaze822.c
blob: f55c00de1ffa35478acb9defefb9292eddafc85f (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// memmem
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <time.h>

//          WSP            =  SP / HTAB
#define iswsp(c)  (((c) == ' ' || (c) == '\t'))

#define bufsiz 4096

struct message {
	char *msg;
	char *end;
};

static long
parse_posint(char **s, size_t minn, size_t maxn)
{
        long n;
        char *end;

        errno = 0;
        n = strtol(*s, &end, 10);
        if (errno) {
//                perror("strtol");
		return -1;
        }
        if (n < (long)minn || n > (long)maxn) {
//                fprintf(stderr, "number outside %zd <= n < %zd\n", minn, maxn);
		return -1;
        }
        *s = end;
        return n;
}

time_t
blaze822_date(char *s) {
	struct tm tm;
	int c;

#if 0
#define i4(m) (s[0] && (s[0]|0x20) == m[0] && \
               s[1] && (s[1]|0x20) == m[1] && \
               s[2] && (s[2]|0x20) == m[2] && \
               s[3] && (s[3]|0x20) == m[3] && (s = s+4) )

#define i3(m) (s[0] && (s[0]|0x20) == m[0] && \
               s[1] && (s[1]|0x20) == m[1] && \
               s[2] && (s[2]|0x20) == m[2] && (s = s+3) )
#endif

#define i4(m) (((uint32_t) m[0]<<24 | m[1]<<16 | m[2]<<8 | m[3]) == \
	       ((uint32_t) s[0]<<24 | s[1]<<16 | s[2]<<8 | s[3] | 0x20202020) \
	       && (s += 4))

#define i3(m) (((uint32_t) m[0]<<24 | m[1]<<16 | m[2]<<8) == \
	       ((uint32_t) s[0]<<24 | s[1]<<16 | s[2]<<8 | 0x20202000) \
	       && (s += 3))

	while (iswsp(*s))
		s++;
	if (i4("mon,") || i4("tue,") || i4("wed,") || i4("thu,") ||
	    i4("fri,") || i4("sat,") || i4("sun,"))
		while (iswsp(*s))
			s++;

	if ((c = parse_posint(&s, 1, 31)) < 0) goto fail;
	tm.tm_mday = c;

	while (iswsp(*s))
		s++;
	
	if      (i3("jan")) tm.tm_mon = 0;
	else if (i3("feb")) tm.tm_mon = 1;
	else if (i3("mar")) tm.tm_mon = 2;
	else if (i3("apr")) tm.tm_mon = 3;
	else if (i3("may")) tm.tm_mon = 4;
	else if (i3("jul")) tm.tm_mon = 5;
	else if (i3("jun")) tm.tm_mon = 6;
	else if (i3("aug")) tm.tm_mon = 7;
	else if (i3("sep")) tm.tm_mon = 8;
	else if (i3("oct")) tm.tm_mon = 9;
	else if (i3("nov")) tm.tm_mon = 10;
	else if (i3("dec")) tm.tm_mon = 11;
	else goto fail;

	while (iswsp(*s))
		s++;
	
	if ((c = parse_posint(&s, 1000, 9999)) > 0) {
		tm.tm_year = c - 1900;
	} else if ((c = parse_posint(&s, 0, 49)) > 0) {
		tm.tm_year = c + 100;
	} else if ((c = parse_posint(&s, 50, 99)) > 0) {
		tm.tm_year = c;
	} else goto fail;

	while (iswsp(*s))
		s++;

	if ((c = parse_posint(&s, 0, 24)) < 0) goto fail;
	tm.tm_hour = c;
	if (*s++ != ':') goto fail;
	if ((c = parse_posint(&s, 0, 59)) < 0) goto fail;
	tm.tm_min = c;
	if (*s++ == ':') {
		if ((c = parse_posint(&s, 0, 61)) < 0) goto fail;
		tm.tm_sec = c;
	}

	while (iswsp(*s))
		s++;

	if (*s == '+' || *s == '-') {
		int neg = (*s == '-');
		s++;
		if ((c = parse_posint(&s, 0, 10000)) < 0) goto fail;
		if (neg) {
                        tm.tm_hour += c / 100;
                        tm.tm_min  += c % 100;
		} else {
                        tm.tm_hour -= c / 100;
                        tm.tm_min  -= c % 100;
                }
	}

	tm.tm_isdst = -1;

	time_t r = mktime(&tm);
	return r;

fail:
	return -1;
}

char *
blaze822_addr(char *s, char **dispo, char **addro)
{
	static char disp[1024];
	static char addr[1024];
//	char *disp = disp+sizeof disp;
//	char *addr = addr+sizeof addr;
	char *c, *e;

//	printf("RAW : |%s|\n", s);
	
	while (iswsp(*s))
		s++;
	
	c = disp;
	e = disp + sizeof disp;

	*disp = 0;
	*addr = 0;

	while (*s) {
		if (*s == '<') {
			char *c = addr;
			char *e = addr + sizeof addr;

			s++;
			while (*s && c < e && *s != '>')
				*c++ = *s++;
			if (*s == '>')
				s++;
			*c = 0;
		} else if (*s == '"') {
			s++;
			while (*s && c < e && *s != '"')
				*c++ = *s++;
			if (*s == '"')
				s++;
		} else if (*s == '(') {
			s++;

			if (!*addr) {   // assume: user@host (name)
				*c-- = 0;
				while (c > disp && iswsp(*c))
					*c-- = 0;
				strcpy(addr, disp);
				c = disp;
				*c = 0;
			}

			while (*s && c < e && *s != ')')
				*c++ = *s++;
			if (*s == ')')
				s++;
		} else if (*s == ',') {
			s++;
			break;
		} else {
			*c++ = *s++;
		}
	}

	*c-- = 0;
	// strip trailing ws
	while (c > disp && iswsp(*c))
		*c-- = 0;

	if (*disp && !*addr && strchr(disp, '@')) {
		// just mail address was given
		strcpy(addr, disp);
		*disp = 0;
	}

//	printf("DISP :: |%s|\n", disp);
//	printf("ADDR :: |%s|\n", addr);

	if (dispo) *dispo = disp;
	if (addro) *addro = addr;

	return s;
}

struct message *
blaze822(char *file)
{
	int fd;
	ssize_t rd;
	char *buf;
	ssize_t bufalloc;
	ssize_t used;
	char *end;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
//		perror("open");
		return 0;
	}

	buf = malloc(3);
	if (!buf)
		return 0;
	buf[0] = '\n';
	buf[1] = '\n';
	buf[2] = '\n';
	bufalloc = 3;
	used = 3;

	while (1) {
		bufalloc += bufsiz;
		buf = realloc(buf, bufalloc);
		if (!buf) {
			close(fd);
			return 0;
		}

		rd = read(fd, buf+used, bufalloc-used);
		if (rd == 0) {
			end = buf+used;
			break;
		}
		if (rd < 0) {
			free(buf);
			close(fd);
			return 0;
		}

		if ((end = memmem(buf-1+used, rd+1, "\n\n", 2)) ||
		    (end = memmem(buf-3+used, rd+3, "\r\n\r\n", 4))) {
			used += rd;
			break;
		}

		used += rd;
	}
	close(fd);

	end++;
	*end = 0;   // dereferencing *end is safe

	char *s;
	for (s = buf; s < end; s++) {
		if (*s == 0)   // sanitize nul bytes in headers
			*s = ' ';

		if (*s == '\r') {
			if (*(s+1) == '\n') {
				*s++ = '\n';
			} else {
				*s = ' ';
			}
		}

		if (iswsp(*s)) {
			// change prior \n to spaces
			int j;
			for (j = 1; s - j >= buf && *(s-j) == '\n'; j++)
				*(s-j) = ' ';
		}

		if (*s == '\n') {
			s++;
			if (iswsp(*s)) {
				*(s-1) = ' ';
			} else {			
				*(s-1) = 0;
				if (s-2 > buf && *(s-2) == '\n')   // ex-crlf
					*(s-2) = 0;
				while (s < end && *s != ':') {
					*s = tolower(*s);
					s++;
				}
			}
		}
	}

	buf[0] = 0;
	buf[1] = 0;
	buf[2] = 0;

	struct message *mesg = malloc(sizeof (struct message));
	if (!mesg) {
		free(buf);
		return 0;
	}

	mesg->msg = buf;
	mesg->end = end;

	return mesg;
}

void
blaze822_free(struct message *mesg)
{
	free(mesg->msg);
	free(mesg);
}

char *
blaze822_hdr_(struct message *mesg, char *hdr, size_t hdrlen)
{
	char *v;

	v = memmem(mesg->msg, mesg->end - mesg->msg, hdr, hdrlen);
	if (!v)
		return 0;
	v += hdrlen;
	while (*v && iswsp(*v))
		v++;
	return v;
}

int
blaze822_loop(int argc, char *argv[], void (*cb)(char *))
{
	char *line = 0;
	size_t linelen = 0;
	ssize_t rd;
	int i = 0;

	if (argc == 0 || (argc == 1 && strcmp(argv[0], "-") == 0)) {
		while ((rd = getdelim(&line, &linelen, '\n', stdin)) != -1) {
			if (line[rd-1] == '\n')
				line[rd-1] = 0;
			cb(line);
			i++;
		}
	} else {
		for (i = 0; i < argc; i++)
			cb(argv[i]);
	}

	return i;
}