summary refs log tree commit diff
path: root/scan.c
blob: 74e0b143df43b01494e41e0281a9be0d54c552fd (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
#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>
#include <wchar.h>

#include "blaze822.h"

void
u8putstr(FILE *out, char *s, size_t l, int pad)
{
	while (*s && l > 0) {
		putc(*s, out);
		if ((*s++ & 0xc0) != 0x80)
			l--;
	}
	if (pad)
		while (l-- > 0)
			putc(' ', out);
}

int
oneline(char *file)
{
	int indent = 0;
	while (*file == ' ') {
		indent++;
		file++;
	}
	
	struct message *msg = blaze822(file);

	if (!msg) {
		printf("%*.*s \\ %33.33s\n", -33 - indent, 33 + indent, "",
		    file);
		return 0;
	}

	char flag1, flag2;

	char *f = strstr(file, "2:,");
        if (!f)
		f = "";

	if (!strchr(f, 'S'))
		flag1 = '.';
	else if (strchr(f, 'T'))
		flag1 = 'x';
	else
		flag1 = ' ';

	if (strchr(f, 'F'))
		flag2 = '#';
	else if (strchr(f, 'R'))
		flag2 = '-';
	else
		flag2 = ' ';

	char date[16];
        char *v;

        if ((v = blaze822_hdr(msg, "date"))) {
		time_t t = blaze822_date(v);
		if (t != -1) {
			struct tm *tm;
			tm = localtime(&t);
			strftime(date, sizeof date, "%Y-%m-%d", tm);
		}
	} else {
		strcpy(date, "(invalid)");
		// mtime perhaps?
	}


	char *from = "(unknown)";
        if ((v = blaze822_hdr(msg, "from"))) {
		char *disp, *addr;
		blaze822_addr(v, &disp, &addr);
		if (*disp)
			from = disp;
		else if (*addr)
			from = addr;
		else
			from = "(unknown)";
	}

	char fromdec[17];
	if (!decode_rfc2047(from, fromdec, sizeof fromdec))
		memcpy(fromdec, from, sizeof fromdec);
	fromdec[sizeof fromdec - 1] = 0;


	char *subj = "(no subject)";
	char subjdec[1000];   // XXX rewrite decode_rfc2047, it overflows!
        if ((v = blaze822_hdr(msg, "subject"))) {
		if (decode_rfc2047(v, subjdec, sizeof subjdec - 1))
			subj = subjdec;
		else
			subj = v;
		
	}

	printf("%c%c%9s  ", flag1, flag2, date);
	u8putstr(stdout, fromdec, 17, 1);
	printf("  ");
	int z;
	for (z = 0; z < indent; z++)
		printf(" ");
	u8putstr(stdout, subj, 80-33-indent, 0);
	printf("\n");
}

int
main(int argc, char *argv[])
{
	int i = blaze822_loop(argc-1, argv+1, oneline);

	printf("%d mails scanned\n", i);

	return 0;
}