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

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

#include "blaze822.h"

static int aflag;
static char defaulthflags[] = "from:sender:reply-to:to:cc:bcc:"
    "resent-from:resent-sender:resent-to:resent-cc:resent-bcc:";
static char *hflag = defaulthflags;

void
print_quoted(char *s)
{
	char *t;

	for (t = s; *t; t++)
                if ((unsigned char)*t < 32 || strchr("()<>[]:;@\\,.\"", *t))
			goto quote;

	printf("%s", s);
	return;

quote:
	putchar('"');
	for (t = s; *t; t++) {
		if (*t == '"' || *t == '\\')
			putchar('\\');
		putchar(*t);
	}
	putchar('"');

}

void
addr(char *file)
{
	while (*file == ' ' || *file == '\t')
		file++;

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

	char *h = hflag;
	char *v;
	while (*h) {
		char *n = strchr(h, ':');
		if (n)
			*n = 0;
		v = blaze822_chdr(msg, h);
		if (v) {
			char *disp, *addr;
			char vdec[16384];
			blaze822_decode_rfc2047(vdec, v, sizeof vdec - 1, "UTF-8");
			vdec[sizeof vdec - 1] = 0;
			v = vdec;

			while ((v = blaze822_addr(v, &disp, &addr))) {
				if (disp && addr && strcmp(disp, addr) == 0)
					disp = 0;
				if (disp && addr) {
					if (aflag) {
						printf("%s\n", addr);
					} else {
						print_quoted(disp);
						printf(" <%s>\n", addr);
					}
				} else if (addr) {
					printf("%s\n", addr);
				}
			}
		}
		if (n) {
			*n = ':';
			h = n + 1;
		} else {
			break;
		}
	}
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "ah:")) != -1)
		switch (c) {
		case 'a': aflag = 1; break;
		case 'h': hflag = optarg; break;
		default:
			fprintf(stderr,
			    "Usage: maddr [-a] [-h headers] [msgs...]\n");
			exit(1);
		}

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

	return 0;
}