mblaze/mscan.c

578 lines
11 KiB
C
Raw Normal View History

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
2016-07-20 13:06:31 +00:00
#include <sys/ioctl.h>
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>
2017-08-31 15:30:17 +00:00
#include <locale.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>
2016-08-10 15:48:33 +00:00
#include <strings.h>
2016-07-11 12:23:55 +00:00
#include <time.h>
2016-07-18 15:06:41 +00:00
#include <unistd.h>
2016-07-19 14:24:47 +00:00
#include <wchar.h>
2016-07-11 12:23:55 +00:00
#include "blaze822.h"
2017-11-22 22:48:00 +00:00
#include "u8decode.h"
2016-07-11 12:23:55 +00:00
2016-07-20 13:06:31 +00:00
static int cols;
static wchar_t replacement = L'?';
static char *cur;
2016-07-19 14:24:47 +00:00
2016-07-25 19:01:34 +00:00
static char *aliases[32];
static int alias_idx;
2016-07-27 16:06:55 +00:00
static int Iflag;
static int nflag;
2016-07-27 16:06:55 +00:00
static int curyear;
static time_t now;
2016-08-04 17:37:22 +00:00
static char default_fflag[] = "%c%u%r %-3n %10d %17f %t %2i%s";
2016-08-04 16:59:46 +00:00
static char *fflag = default_fflag;
2016-07-27 16:06:55 +00:00
ssize_t
2016-07-26 14:53:36 +00:00
u8putstr(FILE *out, char *s, ssize_t l, int pad)
2016-07-11 12:23:55 +00:00
{
ssize_t ol = l;
2016-07-26 14:53:36 +00:00
while (*s && l > 0) {
2016-07-20 13:02:48 +00:00
if (*s == '\t')
*s = ' ';
2017-11-22 22:48:00 +00:00
if ((unsigned)*s < 32 || *s == 127) { // C0
fprintf(out, "%lc", (wint_t)(*s == 127 ? 0x2421 : 0x2400+*s));
2017-03-13 14:49:30 +00:00
s++;
l--;
2016-07-19 14:24:47 +00:00
} else {
2017-11-22 22:48:00 +00:00
uint32_t c;
int r = u8decode(s, &c);
2016-07-19 14:24:47 +00:00
if (r < 0) {
r = 1;
2017-11-22 22:48:00 +00:00
fprintf(out, "%lc", (wint_t)replacement);
s++;
} else {
l -= wcwidth((wchar_t)c);
if (l >= 0)
fwrite(s, 1, r, out);
s += r;
2016-07-19 14:24:47 +00:00
}
}
2016-07-11 12:23:55 +00:00
}
if (pad)
while (l-- > 0)
putc(' ', out);
if (l < 0)
l = 0;
return ol - l;
2016-07-11 12:23:55 +00:00
}
int
itsme(char *v)
{
int i;
char *disp, *addr;
while ((v = blaze822_addr(v, &disp, &addr)))
for (i = 0; addr && i < alias_idx; i++)
if (strcmp(aliases[i], addr) == 0)
return 1;
return 0;
}
static int init;
void
numline(char *file)
{
if (!init) {
// delay loading of the seq until we need to scan the first
// file, in case someone in the pipe updated the map before
char *seq = blaze822_seq_open(0);
blaze822_seq_load(seq);
cur = blaze822_seq_cur();
init = 1;
}
while (*file == ' ' || *file == '\t')
file++;
long lineno = blaze822_seq_find(file);
if (lineno)
printf("%ld\n", lineno);
else
printf("%s\n", file);
}
2016-08-04 16:59:46 +00:00
static char *
2017-10-06 11:20:28 +00:00
fmt_date(struct message *msg, int w, int iso)
2016-08-04 16:59:46 +00:00
{
static char date[32];
char *v;
if (!msg)
return "";
v = blaze822_hdr(msg, "date");
if (!v)
return "(unknown)";
time_t t = blaze822_date(v);
if (t == -1)
return "(invalid)";
struct tm *tm;
tm = localtime(&t);
if (iso) {
if (w >= 19)
strftime(date, sizeof date, "%Y-%m-%d %H:%M:%S", tm);
else if (w >= 16)
strftime(date, sizeof date, "%Y-%m-%d %H:%M", tm);
else
strftime(date, sizeof date, "%Y-%m-%d", tm);
} else if (w < 10) {
if (tm->tm_year != curyear)
strftime(date, sizeof date, "%b%y", tm);
else if (t > now || now - t > 86400)
strftime(date, sizeof date, "%d%b", tm);
else
strftime(date, sizeof date, "%H:%M", tm);
2016-08-04 16:59:46 +00:00
} else {
if (tm->tm_year != curyear)
strftime(date, sizeof date, "%Y-%m-%d", tm);
else if (t > now || now - t > 86400)
strftime(date, sizeof date, "%a %b %e", tm);
else
2017-03-13 14:59:18 +00:00
strftime(date, sizeof date, "%a %H:%M", tm);
2016-08-04 16:59:46 +00:00
}
return date;
}
2016-08-04 17:37:22 +00:00
static char *
2016-08-10 15:48:33 +00:00
fmt_subject(struct message *msg, char *file, int strip)
2016-08-04 17:37:22 +00:00
{
static char subjdec[100];
char *subj = "(no subject)";
char *v;
if (!msg) {
snprintf(subjdec, sizeof subjdec, "\\_ %s", file);
return subjdec;
}
if ((v = blaze822_hdr(msg, "subject")))
subj = v;
blaze822_decode_rfc2047(subjdec, subj, sizeof subjdec - 1, "UTF-8");
2016-08-10 15:48:33 +00:00
if (strip) {
size_t i;
for (i = 0; subjdec[i]; ) {
if (subjdec[i] == ' ') {
i++;
continue;
} else if (strncasecmp("re:", subjdec+i, 3) == 0 ||
2017-01-26 19:27:26 +00:00
strncasecmp("aw:", subjdec+i, 3) == 0) {
2016-08-10 15:48:33 +00:00
i += 3;
continue;
} else if (strncasecmp("fwd:", subjdec+i, 4) == 0) {
i += 4;
continue;
}
break;
}
return subjdec + i;
}
2016-08-04 17:37:22 +00:00
return subjdec;
}
static char *
fmt_from(struct message *msg)
{
static char fromdec[256];
2016-08-04 17:37:22 +00:00
char *from = "(unknown)";
2016-08-04 20:32:21 +00:00
char *v, *w;
2016-08-04 17:37:22 +00:00
if (!msg)
return "";
2017-01-26 19:27:26 +00:00
if ((v = blaze822_hdr(msg, "from"))) {
blaze822_decode_rfc2047(fromdec, v, sizeof fromdec - 1, "UTF-8");
fromdec[sizeof fromdec - 1] = 0;
from = fromdec;
if (itsme(fromdec) && ((w = blaze822_hdr(msg, "to")))) {
2017-11-08 20:32:28 +00:00
memcpy(fromdec, "TO:", 4);
blaze822_decode_rfc2047(fromdec + 3, w,
sizeof fromdec - 1 - 3, "UTF-8");
from = fromdec;
2016-08-04 17:37:22 +00:00
} else {
char *disp, *addr;
blaze822_addr(fromdec, &disp, &addr);
2016-08-04 17:37:22 +00:00
if (disp)
from = disp;
else if (addr)
from = addr;
}
}
return from;
2016-08-04 17:37:22 +00:00
}
static char *
fmt_to_flag(struct message *msg)
{
char *v;
if (!msg || !alias_idx)
return " ";
if ((v = blaze822_hdr(msg, "to")) && itsme(v))
return ">";
else if ((v = blaze822_hdr(msg, "cc")) && itsme(v))
return "+";
else if ((v = blaze822_hdr(msg, "resent-to")) && itsme(v))
return ":";
else if ((v = blaze822_hdr(msg, "from")) && itsme(v))
return "<";
else
return " ";
}
2016-08-09 19:16:09 +00:00
static ssize_t
2017-10-06 11:20:28 +00:00
print_human(intmax_t i, int w)
2016-08-04 16:59:46 +00:00
{
double d = i / 1024.0;
const char *u = "\0\0M\0G\0T\0P\0E\0Z\0Y\0";
while (d >= 1024) {
u += 2;
d /= 1024.0;
}
if (d < 1.0)
2016-08-09 19:16:09 +00:00
return printf("%*.2f", w, d);
2016-08-04 16:59:46 +00:00
else if (!*u)
2016-08-09 19:16:09 +00:00
return printf("%*.0f", w, d);
2016-08-04 16:59:46 +00:00
else if (d < 10.0)
2016-08-09 19:16:09 +00:00
return printf("%*.1f%s", w-1, d, u);
2016-08-04 16:59:46 +00:00
else
2016-08-09 19:16:09 +00:00
return printf("%*.0f%s", w-1, d, u);
2016-08-04 16:59:46 +00:00
}
2016-07-13 15:18:06 +00:00
void
2016-07-11 12:23:55 +00:00
oneline(char *file)
{
if (!init) {
// delay loading of the seq until we need to scan the first
// file, in case someone in the pipe updated the map before
char *seq = blaze822_seq_open(0);
blaze822_seq_load(seq);
cur = blaze822_seq_cur();
init = 1;
}
2016-07-11 12:23:55 +00:00
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++;
}
2017-08-31 15:30:17 +00:00
2016-07-11 12:23:55 +00:00
struct message *msg = blaze822(file);
2016-08-08 14:13:51 +00:00
char *flags = msg ? strstr(file, ":2,") : 0;
if (!flags)
flags = "";
else
2016-08-04 18:16:59 +00:00
flags += 3;
2016-08-04 16:59:46 +00:00
int wleft = cols;
char *f;
for (f = fflag; *f; f++) {
2016-08-04 17:12:20 +00:00
if (*f == '\\') {
f++;
switch (*f) {
case 'n': putchar('\n'); wleft = cols; break;
case 't': putchar('\t'); wleft -= (8 - wleft % 8); break;
default:
putchar('\\'); wleft--;
putchar(*f); wleft--;
}
continue;
}
2016-08-04 16:59:46 +00:00
if (*f != '%') {
putchar(*f);
wleft--;
continue;
}
f++;
2017-10-06 11:20:28 +00:00
int w = 0;
2016-08-04 16:59:46 +00:00
if ((*f >= '0' && *f <= '9') || *f == '-') {
errno = 0;
char *e;
w = strtol(f, &e, 10);
if (errno != 0)
w = 0;
else
f = e;
}
if (!*f)
break;
2016-08-04 17:12:20 +00:00
switch (*f) {
2016-08-04 16:59:46 +00:00
case '%':
putchar('%');
wleft--;
break;
case 'c':
2016-08-04 17:37:22 +00:00
if (cur && strcmp(cur, file) == 0)
putchar('>');
else
putchar(' ');
wleft--;
break;
case 'u': // unseen
if (strchr(flags, 'F'))
putchar('*');
else if (msg && !strchr(flags, 'S'))
2016-08-04 17:37:22 +00:00
putchar('.');
else if (strchr(flags, 'T'))
putchar('x');
else
putchar(' ');
2016-08-04 16:59:46 +00:00
wleft--;
break;
2016-08-04 17:37:22 +00:00
case 'r': // replied
if (strchr(flags, 'R'))
putchar('-');
else if (strchr(flags, 'P'))
putchar(':');
else
putchar(' ');
2016-08-04 16:59:46 +00:00
wleft--;
break;
2016-08-04 17:37:22 +00:00
case 't': // to-flag
wleft -= printf("%s", fmt_to_flag(msg));
break;
case 'M': // raw Maildir flags
2016-08-04 16:59:46 +00:00
if (!w) w = -3;
2016-08-04 18:16:59 +00:00
wleft -= printf("%*s", w, flags);
2016-08-04 16:59:46 +00:00
break;
case 'n':
{
long lineno = msg ? blaze822_seq_find(file) : 0;
if (lineno)
wleft -= printf("%*ld", w, lineno);
else
wleft -= printf("%*s", w, "");
}
2016-08-04 16:59:46 +00:00
break;
case 'd':
case 'D':
if (!w) w = 10;
wleft -= printf("%*s", w,
fmt_date(msg, w, Iflag || *f == 'D'));
break;
case 'f':
if (w < 0)
w += wleft;
if (w)
wleft -= u8putstr(stdout,
fmt_from(msg), w, 1);
else
wleft -= u8putstr(stdout,
fmt_from(msg), wleft, 0);
2016-08-04 16:59:46 +00:00
break;
case 'i':
{
int z;
if (!w)
w = 1;
if (indent >= 10) {
wleft -= printf("..%2d..", indent);
indent = 10 - 6/w;
}
for (z = 0; z < w*indent; z++)
putchar(' ');
wleft -= w*indent;
}
break;
case 's':
2016-08-10 15:48:33 +00:00
case 'S':
if (w < 0)
w += wleft;
if (w)
wleft -= u8putstr(stdout,
2016-08-10 15:48:33 +00:00
fmt_subject(msg, file, *f == 'S'), w, 1);
else
wleft -= u8putstr(stdout,
2016-08-10 15:48:33 +00:00
fmt_subject(msg, file, *f == 'S'), wleft, 0);
2016-08-04 16:59:46 +00:00
break;
case 'b':
{
struct stat st;
2016-08-04 18:17:07 +00:00
if (msg) {
if (stat(file, &st) != 0)
st.st_size = 0;
2016-08-09 19:16:09 +00:00
wleft -= print_human(st.st_size, w);
2016-08-04 18:17:07 +00:00
} else {
2016-08-09 19:16:09 +00:00
wleft -= printf("%.*s", w, "");
2016-08-04 18:17:07 +00:00
}
2016-08-04 16:59:46 +00:00
}
break;
case 'F':
{
char *e = file + strlen(file);
while (file < e && *e != '/')
e--;
e--;
while (file < e && *e != '/')
e--;
while (file < e && *e == '/')
e--;
char *b = e;
e++;
2016-08-04 16:59:46 +00:00
while (file < b && *b != '/')
b--;
if (*b == '/')
b++;
if (*b == '.')
b++;
2016-08-09 19:27:22 +00:00
if (w) {
if (w < 0)
w = -w;
wleft -= printf("%*.*s",
-w, (int)(e-b < w ? e-b : w), b);
} else {
wleft -= printf("%.*s", (int)(e-b), b);
}
2016-08-04 16:59:46 +00:00
}
break;
2016-08-09 19:27:22 +00:00
case 'R':
if (w)
wleft -= printf("%*.*s", w, w, file);
else
wleft -= printf("%s", file);
break;
2016-08-11 18:32:54 +00:00
case 'I':
{
char *m = msg ? blaze822_hdr(msg, "message-id") : 0;
if (!m)
m = "(unknown)";
if (w)
wleft -= printf("%*.*s", w, w, m);
else
wleft -= printf("%s", m);
}
break;
2016-08-04 16:59:46 +00:00
default:
putchar('%');
putchar(*f);
2016-08-11 18:32:54 +00:00
wleft -= 2;
2016-08-04 16:59:46 +00:00
}
}
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[])
{
pid_t pid1 = -1;
2016-07-27 16:06:55 +00:00
int c;
2016-08-04 16:59:46 +00:00
while ((c = getopt(argc, argv, "If:n")) != -1)
2017-08-31 15:30:17 +00:00
switch (c) {
2016-07-27 16:06:55 +00:00
case 'I': Iflag++; break;
2016-08-04 16:59:46 +00:00
case 'f': fflag = optarg; break;
case 'n': nflag = 1; break;
2016-07-27 16:06:55 +00:00
default:
2016-08-04 16:59:46 +00:00
fprintf(stderr, "Usage: mscan [-n] [-f format] [-I] [msgs...]\n");
2016-07-27 16:06:55 +00:00
exit(1);
}
if (nflag) {
if (argc == optind && isatty(0))
blaze822_loop1(":", numline);
else
blaze822_loop(argc-optind, argv+optind, numline);
return 0;
}
now = time(0);
2016-07-27 16:06:55 +00:00
struct tm *tm = localtime(&now);
curyear = tm->tm_year;
2017-08-31 15:30:17 +00:00
setlocale(LC_ALL, ""); // for wcwidth later
2016-09-07 18:57:49 +00:00
if (wcwidth(0xfffd) > 0)
replacement = 0xfffd;
2016-07-19 14:24:47 +00:00
2016-07-20 13:06:31 +00:00
struct winsize w;
int ttyfd = open("/dev/tty", O_RDONLY | O_NOCTTY);
if (ttyfd >= 0 && ioctl(ttyfd, TIOCGWINSZ, &w) == 0) {
2016-07-20 13:06:31 +00:00
cols = w.ws_col;
char *pg;
pg = getenv("MBLAZE_PAGER");
if (!pg)
pg = getenv("PAGER");
if (pg && *pg && strcmp(pg, "cat") != 0) {
pid1 = pipeto(pg);
if (pid1 < 0)
fprintf(stderr,
"mscan: spawning pager '%s': %s\n",
pg, strerror(errno));
}
}
if (ttyfd >= 0)
close(ttyfd);
2016-07-20 13:06:31 +00:00
if (getenv("COLUMNS"))
cols = atoi(getenv("COLUMNS"));
if (cols <= 40)
cols = 80;
char *f = blaze822_home_file("profile");
2016-07-25 19:01:34 +00:00
struct message *config = blaze822(f);
if (config) {
char *v, *d, *a;
if ((v = blaze822_hdr(config, "local-mailbox")))
while (alias_idx < (int)(sizeof aliases / sizeof aliases[0]) &&
2017-08-31 15:30:17 +00:00
(v = blaze822_addr(v, &d, &a)))
2016-07-25 19:01:34 +00:00
if (a)
aliases[alias_idx++] = strdup(a);
if ((v = blaze822_hdr(config, "alternate-mailboxes")))
while (alias_idx < (int)(sizeof aliases / sizeof aliases[0]) &&
2017-08-31 15:30:17 +00:00
(v = blaze822_addr(v, &d, &a)))
2016-07-25 19:01:34 +00:00
if (a)
aliases[alias_idx++] = strdup(a);
if ((v = blaze822_hdr(config, "scan-format")))
if (fflag == default_fflag) // NB. ==
fflag = v;
2016-07-25 19:01:34 +00:00
}
long i;
2016-07-27 16:06:55 +00:00
if (argc == optind && isatty(0))
2016-07-22 22:26:55 +00:00
i = blaze822_loop1(":", oneline);
else
2016-07-27 16:06:55 +00:00
i = blaze822_loop(argc-optind, argv+optind, oneline);
fprintf(stderr, "%ld mails scanned\n", i);
2016-07-11 12:23:55 +00:00
if (pid1 > 0)
pipeclose(pid1);
2016-07-11 12:23:55 +00:00
return 0;
}