mblaze/scan.c

137 lines
2.3 KiB
C
Raw Normal View History

2016-07-11 12:23:55 +00:00
#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) {
2016-07-11 12:23:55 +00:00
putc(*s, out);
// 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-13 14:47:24 +00:00
long lineno;
2016-07-13 15:18:06 +00:00
void
2016-07-11 12:23:55 +00:00
oneline(char *file)
{
2016-07-13 14:47:24 +00:00
lineno++;
2016-07-11 12:23:55 +00:00
int indent = 0;
while (*file == ' ') {
indent++;
file++;
}
2016-07-12 18:17:21 +00:00
indent *= 2;
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;
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);
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
}
blaze822_decode_rfc2047(subjdec, subj, sizeof subjdec - 1, "UTF-8");
2016-07-11 12:23:55 +00:00
2016-07-14 16:21:07 +00:00
printf("%c%c %-3ld %-10s ", flag1, flag2, lineno, 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-11 14:28:22 +00:00
int i = blaze822_loop(argc-1, argv+1, oneline);
2016-07-11 12:23:55 +00:00
printf("%d mails scanned\n", i);
return 0;
}