2016-07-11 12:23:55 +00:00
|
|
|
#include <sys/stat.h>
|
2016-07-18 15:06:41 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-07-11 12:23:55 +00:00
|
|
|
#include <fcntl.h>
|
2016-07-18 15:06:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-07-11 12:23:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2016-07-18 15:06:41 +00:00
|
|
|
#include <unistd.h>
|
2016-07-11 12:23:55 +00:00
|
|
|
|
|
|
|
#include "blaze822.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
u8putstr(FILE *out, char *s, size_t l, int pad)
|
|
|
|
{
|
2016-07-12 14:12:12 +00:00
|
|
|
while (*s && l) {
|
2016-07-11 12:23:55 +00:00
|
|
|
putc(*s, out);
|
2016-07-12 14:12:12 +00:00
|
|
|
// elongate by utf8 overhead
|
|
|
|
if ((*s & 0xf0) == 0xf0) l += 3;
|
|
|
|
else if ((*s & 0xe0) == 0xe0) l += 2;
|
|
|
|
else if ((*s & 0xc0) == 0xc0) l += 1;
|
|
|
|
l--;
|
|
|
|
s++;
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
|
|
|
if (pad)
|
|
|
|
while (l-- > 0)
|
|
|
|
putc(' ', out);
|
|
|
|
}
|
|
|
|
|
2016-07-18 16:31:33 +00:00
|
|
|
static char *cur;
|
|
|
|
|
2016-07-13 15:18:06 +00:00
|
|
|
void
|
2016-07-11 12:23:55 +00:00
|
|
|
oneline(char *file)
|
|
|
|
{
|
|
|
|
int indent = 0;
|
2016-07-17 19:49:34 +00:00
|
|
|
while (*file == ' ' || *file == '\t') {
|
2016-07-11 12:23:55 +00:00
|
|
|
indent++;
|
|
|
|
file++;
|
|
|
|
}
|
2016-07-12 18:17:21 +00:00
|
|
|
indent *= 2;
|
2016-07-17 19:49:34 +00:00
|
|
|
|
|
|
|
char *e = file + strlen(file) - 1;
|
|
|
|
while (file < e && (*e == ' ' || *e == '\t'))
|
|
|
|
*e-- = 0;
|
2016-07-11 12:23:55 +00:00
|
|
|
|
|
|
|
struct message *msg = blaze822(file);
|
|
|
|
if (!msg) {
|
2016-07-13 14:47:24 +00:00
|
|
|
int p = 80-38-3-indent;
|
|
|
|
printf("%*.*s\\_ %*.*s\n", -38 - indent, 38 + indent, "",
|
2016-07-12 18:14:38 +00:00
|
|
|
-p, p, file);
|
2016-07-13 15:18:06 +00:00
|
|
|
return;
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char flag1, flag2;
|
|
|
|
|
2016-07-18 16:26:20 +00:00
|
|
|
char *f = strstr(file, ":2,");
|
2016-07-11 12:23:55 +00:00
|
|
|
if (!f)
|
|
|
|
f = "";
|
|
|
|
|
2016-07-18 16:31:33 +00:00
|
|
|
if (strcmp(cur, file) == 0)
|
|
|
|
flag1 = '>';
|
|
|
|
else if (!strchr(f, 'S'))
|
2016-07-11 12:23:55 +00:00
|
|
|
flag1 = '.';
|
|
|
|
else if (strchr(f, 'T'))
|
|
|
|
flag1 = 'x';
|
|
|
|
else
|
|
|
|
flag1 = ' ';
|
|
|
|
|
|
|
|
if (strchr(f, 'F'))
|
2016-07-18 16:31:33 +00:00
|
|
|
flag2 = '*';
|
2016-07-11 12:23:55 +00:00
|
|
|
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);
|
2016-07-12 13:14:27 +00:00
|
|
|
} else {
|
|
|
|
strcpy(date, "(invalid)");
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-12 13:14:27 +00:00
|
|
|
strcpy(date, "(unknown)");
|
2016-07-11 12:23:55 +00:00
|
|
|
// 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];
|
2016-07-11 21:40:00 +00:00
|
|
|
blaze822_decode_rfc2047(fromdec, from, sizeof fromdec - 1, "UTF-8");
|
2016-07-11 12:23:55 +00:00
|
|
|
fromdec[sizeof fromdec - 1] = 0;
|
|
|
|
|
|
|
|
char *subj = "(no subject)";
|
2016-07-11 21:40:00 +00:00
|
|
|
char subjdec[100];
|
2016-07-11 12:23:55 +00:00
|
|
|
if ((v = blaze822_hdr(msg, "subject"))) {
|
2016-07-11 21:40:00 +00:00
|
|
|
subj = v;
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
2016-07-12 11:38:44 +00:00
|
|
|
blaze822_decode_rfc2047(subjdec, subj, sizeof subjdec - 1, "UTF-8");
|
2016-07-11 12:23:55 +00:00
|
|
|
|
2016-07-17 19:52:04 +00:00
|
|
|
long lineno = blaze822_seq_find(file);
|
|
|
|
if (lineno)
|
|
|
|
printf("%c%c %-3ld %-10s ", flag1, flag2, lineno, date);
|
|
|
|
else
|
|
|
|
printf("%c%c %-10s ", flag1, flag2, date);
|
2016-07-11 12:23:55 +00:00
|
|
|
u8putstr(stdout, fromdec, 17, 1);
|
|
|
|
printf(" ");
|
|
|
|
int z;
|
|
|
|
for (z = 0; z < indent; z++)
|
|
|
|
printf(" ");
|
2016-07-13 14:47:24 +00:00
|
|
|
u8putstr(stdout, subjdec, 80-38-indent, 0);
|
2016-07-11 12:23:55 +00:00
|
|
|
printf("\n");
|
2016-07-13 15:18:06 +00:00
|
|
|
|
|
|
|
blaze822_free(msg);
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2016-07-17 19:52:04 +00:00
|
|
|
char *seqmap = blaze822_seq_open(0);
|
|
|
|
blaze822_seq_load(seqmap);
|
2016-07-18 16:31:33 +00:00
|
|
|
cur = blaze822_seq_cur();
|
2016-07-17 19:52:04 +00:00
|
|
|
|
2016-07-11 14:28:22 +00:00
|
|
|
int i = blaze822_loop(argc-1, argv+1, oneline);
|
2016-07-11 12:23:55 +00:00
|
|
|
|
2016-07-18 16:31:33 +00:00
|
|
|
fprintf(stderr, "%d mails scanned\n", i);
|
2016-07-11 12:23:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|