aboutsummaryrefslogtreecommitdiff
path: root/mhdr.c
blob: 97b99e1ee5357d230cf149501c6dbbe037195c0a (plain)
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
#include <sys/stat.h>
#include <sys/types.h>

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

#include "blaze822.h"

static char *hflag;
static int Aflag;
static int Dflag;
static int Mflag;
static int dflag;

static void
printhdr(char *hdr)
{
	int uc = 1;

	while (*hdr && *hdr != ':') {
		putc(uc ? toupper(*hdr) : *hdr, stdout);
		uc = (*hdr == '-');
		hdr++;
	}
	fputs(hdr, stdout);
	fputc('\n', stdout);
}

void
headerall(struct message *msg)
{
	char *h = 0;
	while ((h = blaze822_next_header(msg, h))) {
		if (dflag) {
			char d[4096];
			blaze822_decode_rfc2047(d, h, sizeof d, "UTF-8");
			printhdr(d);
		} else {
			printhdr(h);
		}
	}

	blaze822_free(msg);
}

void
print_addresses(char *s)
{
	char *disp, *addr;
	while ((s = blaze822_addr(s, &disp, &addr))) {
		if (disp && addr) {
			if (dflag) {
				char d[4096];
				blaze822_decode_rfc2047(d, disp, sizeof d,
				    "UTF-8");
				printf("%s <%s>\n", d, addr);
			} else {
				printf("%s <%s>\n", disp, addr);
			}
		} else if (addr) {
			printf("%s\n", addr);
		}
	}
}

void
print_date(char *s)
{
	time_t t = blaze822_date(s);
	if (t == -1)
		return;
	printf("%ld\n", t);
}

void
print_decode_header(char *s)
{
	char d[4096];
	blaze822_decode_rfc2047(d, s, sizeof d, "UTF-8");
	printf("%s\n", d);
}

void
print_header(char *v)
{
	if (Aflag)
		print_addresses(v);
	else if (Dflag)
		print_date(v);
	else if (dflag)
		print_decode_header(v);
	else
		printf("%s\n", v);
}

void
headermany(struct message *msg)
{
	char *hdr = 0;
	while ((hdr = blaze822_next_header(msg, hdr))) {
		char *h = hflag;
		while (*h) {
			char *n = strchr(h, ':');
			if (n)
				*n = 0;

			size_t l = strlen(h);
			if (strncmp(hdr, h, l) == 0 && hdr[l] == ':') {
				hdr += l + 1;
				while (*hdr == ' ' || *hdr == '\t')
					hdr++;
				print_header(hdr);
			}

			if (n) {
				*n = ':';
				h = n + 1;
			} else {
				break;
			}
		}
	}

	blaze822_free(msg);
}

void
header(char *file)
{
	struct message *msg;

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

	msg = blaze822(file);
	if (!msg)
		return;

	if (!hflag)
		return headerall(msg);
	if (Mflag)
		return headermany(msg);

	char *h = hflag;
	while (*h) {
		char *n = strchr(h, ':');
		if (n)
			*n = 0;
		char *v = blaze822_chdr(msg, h);
		if (v)
			print_header(v);
		if (n) {
			*n = ':';
			h = n + 1;
		} else {
			break;
		}
	}

	blaze822_free(msg);
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "h:ADMd")) != -1)
		switch(c) {
		case 'h': hflag = optarg; break;
		case 'A': Aflag = 1; break;
		case 'D': Dflag = 1; break;
		case 'M': Mflag = 1; break;
		case 'd': dflag = 1; break;
		default:
			fprintf(stderr,
"Usage: mhdr [-h header] [-d] [-M] [-A|-D] [msgs...]\n");
			exit(1);
		}

	if (argc == optind && isatty(0))
		blaze822_loop1(".", header);
	else
		blaze822_loop(argc-optind, argv+optind, header);
	
	return 0;
}