about summary refs log tree commit diff
path: root/magrep.c
blob: 72ce1bb8ef246d8dfda5cdcb35d466f061284fe0 (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
#include <sys/stat.h>
#include <sys/types.h>

#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include "blaze822.h"
#include "xpledge.h"

static int aflag;
static int cflag;
static int dflag;
static int hflag;
static int iflag;
static int lflag;
static int oflag;
static int pflag;
static int qflag;
static int vflag;
static long mflag;
static long matches;

static regex_t pattern;
static char *header;
static char *curfile;

int
match(char *file, char *hdr, char *s)
{
	if (oflag && !cflag && !qflag && !vflag && !lflag) {
		regmatch_t pmatch = {0};
		int len, matched;
		matched = 0;
		while (*s && regexec(&pattern, s, 1, &pmatch, 0) == 0) {
			s += pmatch.rm_so;
			if (!(len = pmatch.rm_eo-pmatch.rm_so)) {
				s += 1;
				continue;
			}
			if (pflag)
				printf("%s: %s: ", file, hdr);
			else if (hflag)
				printf("%s: ", hdr);
			printf("%.*s\n", len, s);
			s += len;
			matched++;
		}
		return (matched && matches++);
	} else if (vflag ^ (regexec(&pattern, s, 0, 0, 0) == 0)) {
		if (qflag)
			exit(0);
		matches++;
		if (!cflag) {
			if (vflag || !hflag)
				printf("%s", file);
			if (pflag && !vflag)
				printf(": %s: %s", hdr, s);
			else if (hflag && !vflag)
				printf("%s: %s", hdr, s);
			putchar('\n');
		}
		if (mflag && matches >= mflag)
			exit(0);
		return 1;
	}

	return 0;
}

blaze822_mime_action
match_part(int depth, struct message *msg, char *body, size_t bodylen)
{
	(void)depth;

	char *ct = blaze822_hdr(msg, "content-type");

	blaze822_mime_action r = MIME_CONTINUE;

	if (!ct || strncmp(ct, "text/plain", 10) == 0) {
		char *charset = 0, *cs, *cse;
		if (blaze822_mime_parameter(ct, "charset", &cs, &cse))
			charset = strndup(cs, cse-cs);
		if (!charset ||
		    strcasecmp(charset, "utf-8") == 0 ||
		    strcasecmp(charset, "utf8") == 0 ||
		    strcasecmp(charset, "us-ascii") == 0) {
			if ((hflag || pflag) && !cflag && !oflag && !vflag) {
				char *s, *p;
				for (p = s = body; p < body+bodylen+1; p++) {
					if (*p == '\r' || *p == '\n') {
						*p = 0;
						if (p-s > 1)
							match(curfile, "/", s);
						s = p+1;
					}
				}
			} else if (match(curfile, "/", body)) {
				r = MIME_STOP;
			}
		} else {
			/* XXX decode here */
		}
		free(charset);
	}

	return r;
}

void
match_body(char *file)
{
	char *filename;
	filename = curfile = file;
	while (*filename == ' ' || *filename == '\t')
		filename++;

	struct message *msg = blaze822_file(filename);
	if (!msg)
		return;

	blaze822_walk_mime(msg, 0, match_part);
	blaze822_free(msg);
}

int
match_value(char *file, char *h, char *v)
{
	if (dflag) {
		char d[4096];
		blaze822_decode_rfc2047(d, v, sizeof d, "UTF-8");
		return match(file, h, d);
	} else if (aflag) {
		char *disp, *addr;
		while ((v = blaze822_addr(v, &disp, &addr))) {
			if (addr && match(file, h, addr))
				return 1;
		}
	} else {
		return match(file, h, v);
	}
	return 0;
}

void
magrep(char *file)
{
	if (!*header) {
		char *flags = strstr(file, ":2,");
		if (flags)
			match(file, "flags", flags+3);
		return;
	} else if (strcmp(header, "/") == 0) {
		match_body(file);
		return;
	}

	char *filename = file;
	while (*filename == ' ' || *filename == '\t')
		filename++;

	struct message *msg = blaze822(filename);
	if (!msg)
		return;

	if (strcmp(header, "*") == 0) {
		char *hdr = 0;
		while ((hdr = blaze822_next_header(msg, hdr))) {
			char *v = strchr(hdr, ':');
			if (v) {
				*v = 0;
				if (match_value(file, hdr, v + 1 + (v[1] == ' ')) && lflag)
					break;
				*v = ':';
			}
		}
	} else {
		char *v = blaze822_chdr(msg, header);
		if (v)
			(void)match_value(file, header, v);
	}

	blaze822_free(msg);
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "acdhilm:opqv")) != -1)
		switch (c) {
		case 'a': aflag = 1; break;
		case 'c': cflag = 1; break;
		case 'd': dflag = 1; break;
		case 'h': hflag = 1; break;
		case 'i': iflag = REG_ICASE; break;
		case 'l': lflag = 1; break;
		case 'm': mflag = atol(optarg); break;
		case 'o': oflag = 1; break;
		case 'p': pflag = 1; break;
		case 'q': qflag = 1; break;
		case 'v': vflag = 1; break;
		default:
usage:
			fprintf(stderr,
"Usage: magrep [-c|-h|-o|-p|-q|-m max] [-v] [-i] [-l] [-a|-d] header:regex [msgs...]\n");
			exit(2);
		}

	if (argc == optind)
		goto usage;
	header = argv[optind++];

	char *rx = strchr(header, ':');
	if (!rx)
		goto usage;

	xpledge("stdio rpath", "");

	*rx++ = 0;
	int r = regcomp(&pattern, rx, REG_EXTENDED | iflag);
	if (r != 0) {
		char buf[256];
		regerror(r, &pattern, buf, sizeof buf);
		fprintf(stderr, "magrep: regex error: %s\n", buf);
		exit(2);
	}

	if (argc == optind && isatty(0))
		blaze822_loop1(":", magrep);
	else
		blaze822_loop(argc-optind, argv+optind, magrep);

	if (cflag && !qflag && !mflag)
		printf("%ld\n", matches);

	return !matches;
}