2016-07-26 16:20:05 +00:00
|
|
|
#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>
|
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-19 14:24:47 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <locale.h>
|
2016-07-11 12:23:55 +00:00
|
|
|
|
|
|
|
#include "blaze822.h"
|
|
|
|
|
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;
|
2016-07-31 17:42:30 +00:00
|
|
|
static int nflag;
|
2016-07-27 16:06:55 +00:00
|
|
|
static int curyear;
|
|
|
|
static int curyday;
|
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
|
|
|
|
2016-07-11 12:23:55 +00:00
|
|
|
void
|
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
|
|
|
{
|
2016-07-26 14:53:36 +00:00
|
|
|
while (*s && l > 0) {
|
2016-07-20 13:02:48 +00:00
|
|
|
if (*s == '\t')
|
|
|
|
*s = ' ';
|
2016-07-19 14:24:47 +00:00
|
|
|
if (*s >= 32 && *s < 127) {
|
|
|
|
putc(*s, out);
|
|
|
|
s++;
|
|
|
|
l--;
|
|
|
|
} else {
|
|
|
|
wchar_t wc;
|
|
|
|
int r = mbtowc(&wc, s, 4);
|
|
|
|
if (r < 0) {
|
|
|
|
r = 1;
|
|
|
|
wc = replacement;
|
|
|
|
}
|
|
|
|
s += r;
|
|
|
|
l -= wcwidth(wc);
|
2016-07-26 14:53:36 +00:00
|
|
|
if (l >= 0)
|
|
|
|
fprintf(out, "%lc", wc);
|
2016-07-19 14:24:47 +00:00
|
|
|
}
|
2016-07-11 12:23:55 +00:00
|
|
|
}
|
|
|
|
if (pad)
|
|
|
|
while (l-- > 0)
|
|
|
|
putc(' ', out);
|
|
|
|
}
|
|
|
|
|
2016-07-25 19:19:41 +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;
|
|
|
|
}
|
|
|
|
|
2016-07-31 17:42:30 +00:00
|
|
|
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 *
|
|
|
|
fmt_date(struct message *msg, int w, int iso)
|
|
|
|
{
|
|
|
|
static char date[32];
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return "";
|
|
|
|
|
2016-08-04 19:41:27 +00:00
|
|
|
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 (tm->tm_yday != curyday)
|
|
|
|
strftime(date, sizeof date, "%d%b", tm);
|
|
|
|
else
|
|
|
|
strftime(date, sizeof date, "%H:%M", tm);
|
2016-08-04 16:59:46 +00:00
|
|
|
} else {
|
2016-08-04 19:41:27 +00:00
|
|
|
if (tm->tm_year != curyear)
|
|
|
|
strftime(date, sizeof date, "%Y-%m-%d", tm);
|
|
|
|
else if (tm->tm_yday != curyday)
|
|
|
|
strftime(date, sizeof date, "%a %b %e", tm);
|
|
|
|
else
|
|
|
|
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 *
|
|
|
|
fmt_subject(struct message *msg, char *file)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
|
|
|
|
return subjdec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
fmt_from(struct message *msg)
|
|
|
|
{
|
|
|
|
static char fromdec[64];
|
|
|
|
char *from = "(unknown)";
|
|
|
|
char to[256];
|
2016-08-04 20:32:21 +00:00
|
|
|
char *v, *w;
|
2016-08-04 17:37:22 +00:00
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if ((v = blaze822_hdr(msg, "from"))) {
|
2016-08-04 20:32:21 +00:00
|
|
|
if (itsme(v) && ((w = blaze822_hdr(msg, "to")))) {
|
|
|
|
snprintf(to, sizeof to, "TO:%s", w);
|
2016-08-04 17:37:22 +00:00
|
|
|
from = to;
|
|
|
|
} else {
|
|
|
|
char *disp, *addr;
|
|
|
|
blaze822_addr(v, &disp, &addr);
|
|
|
|
if (disp)
|
|
|
|
from = disp;
|
|
|
|
else if (addr)
|
|
|
|
from = addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blaze822_decode_rfc2047(fromdec, from, sizeof fromdec - 1, "UTF-8");
|
|
|
|
fromdec[sizeof fromdec - 1] = 0;
|
|
|
|
|
|
|
|
return fromdec;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04 16:59:46 +00:00
|
|
|
static void
|
|
|
|
print_human(intmax_t i)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
printf("%4.2f", d);
|
|
|
|
else if (!*u)
|
|
|
|
printf("%4.0f", d);
|
|
|
|
else if (d < 10.0)
|
|
|
|
printf("%3.1f%s", d, u);
|
|
|
|
else
|
|
|
|
printf("%3.0f%s", d, u);
|
|
|
|
}
|
|
|
|
|
2016-07-13 15:18:06 +00:00
|
|
|
void
|
2016-07-11 12:23:55 +00:00
|
|
|
oneline(char *file)
|
|
|
|
{
|
2016-07-20 21:22:59 +00:00
|
|
|
if (!init) {
|
2016-07-26 14:55:55 +00:00
|
|
|
// delay loading of the seq until we need to scan the first
|
2016-07-20 21:22:59 +00:00
|
|
|
// file, in case someone in the pipe updated the map before
|
2016-07-26 14:55:55 +00:00
|
|
|
char *seq = blaze822_seq_open(0);
|
|
|
|
blaze822_seq_load(seq);
|
2016-07-20 21:22:59 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct message *msg = blaze822(file);
|
2016-08-04 17:37:22 +00:00
|
|
|
char *flags = msg ? strstr(file, ":2,") : "";
|
2016-08-04 18:16:59 +00:00
|
|
|
if (*flags)
|
|
|
|
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++;
|
|
|
|
|
|
|
|
int w = 0;
|
|
|
|
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 (!strchr(flags, 'S'))
|
|
|
|
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':
|
2016-08-04 17:39:01 +00:00
|
|
|
{
|
|
|
|
long lineno = msg ? blaze822_seq_find(file) : 0;
|
|
|
|
|
|
|
|
if (lineno)
|
2016-08-04 19:41:27 +00:00
|
|
|
wleft -= printf("%*ld", w, lineno);
|
2016-08-04 17:39:01 +00:00
|
|
|
else
|
2016-08-04 19:41:27 +00:00
|
|
|
wleft -= printf("%*s", w, "");
|
2016-08-04 17:39:01 +00:00
|
|
|
}
|
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':
|
2016-08-04 17:37:22 +00:00
|
|
|
u8putstr(stdout, fmt_from(msg), w ? w : 16, 1);
|
2016-08-04 16:59:46 +00:00
|
|
|
wleft -= w > 0 ? w : -w;
|
|
|
|
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':
|
|
|
|
if (!w) w = wleft;
|
2016-08-04 17:37:22 +00:00
|
|
|
u8putstr(stdout, fmt_subject(msg, file), wleft, 0);
|
2016-08-04 16:59:46 +00:00
|
|
|
wleft -= w;
|
|
|
|
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;
|
|
|
|
print_human(st.st_size);
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
}
|
2016-08-04 16:59:46 +00:00
|
|
|
wleft -= 5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
{
|
|
|
|
if (!w) w = 10;
|
|
|
|
char *e = file + strlen(file);
|
|
|
|
while (file < e && *e != '/')
|
|
|
|
e--;
|
|
|
|
e--;
|
|
|
|
while (file < e && *e != '/')
|
|
|
|
e--;
|
|
|
|
char *b = e - 1;
|
|
|
|
while (file < b && *b != '/')
|
|
|
|
b--;
|
|
|
|
if (*b == '/')
|
|
|
|
b++;
|
|
|
|
if (*b == '.')
|
|
|
|
b++;
|
|
|
|
wleft -= printf("%*.*s", w, e-b, b);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
putchar('%');
|
|
|
|
putchar(*f);
|
|
|
|
wleft -=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-27 16:06:55 +00:00
|
|
|
int c;
|
2016-08-04 16:59:46 +00:00
|
|
|
while ((c = getopt(argc, argv, "If:n")) != -1)
|
2016-07-27 16:06:55 +00:00
|
|
|
switch(c) {
|
|
|
|
case 'I': Iflag++; break;
|
2016-08-04 16:59:46 +00:00
|
|
|
case 'f': fflag = optarg; break;
|
2016-07-31 17:42:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-31 17:42:30 +00:00
|
|
|
if (nflag) {
|
|
|
|
if (argc == optind && isatty(0))
|
|
|
|
blaze822_loop1(":", numline);
|
|
|
|
else
|
|
|
|
blaze822_loop(argc-optind, argv+optind, numline);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-27 16:06:55 +00:00
|
|
|
time_t now = time(0);
|
|
|
|
struct tm *tm = localtime(&now);
|
|
|
|
curyear = tm->tm_year;
|
|
|
|
curyday = tm->tm_yday;
|
|
|
|
|
2016-07-19 14:24:47 +00:00
|
|
|
setlocale(LC_ALL, ""); // for wcwidth later
|
|
|
|
if (wcwidth(0xFFFD) > 0)
|
|
|
|
replacement = 0xFFFD;
|
|
|
|
|
2016-07-20 13:06:31 +00:00
|
|
|
struct winsize w;
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &w) == 0)
|
|
|
|
cols = w.ws_col;
|
|
|
|
if (getenv("COLUMNS"))
|
|
|
|
cols = atoi(getenv("COLUMNS"));
|
|
|
|
if (cols <= 40)
|
|
|
|
cols = 80;
|
|
|
|
|
2016-08-01 17:36:23 +00:00
|
|
|
char *f = blaze822_home_file(".mblaze/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]) &&
|
|
|
|
(v = blaze822_addr(v, &d, &a)))
|
|
|
|
if (a)
|
|
|
|
aliases[alias_idx++] = strdup(a);
|
|
|
|
if ((v = blaze822_hdr(config, "alternate-mailboxes")))
|
|
|
|
while (alias_idx < (int)(sizeof aliases / sizeof aliases[0]) &&
|
|
|
|
(v = blaze822_addr(v, &d, &a)))
|
|
|
|
if (a)
|
|
|
|
aliases[alias_idx++] = strdup(a);
|
2016-08-04 19:43:51 +00:00
|
|
|
if ((v = blaze822_hdr(config, "scan-format")))
|
|
|
|
if (fflag == default_fflag) // NB. ==
|
|
|
|
fflag = v;
|
2016-07-25 19:01:34 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 13:38:00 +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);
|
2016-07-20 13:38:00 +00:00
|
|
|
fprintf(stderr, "%ld mails scanned\n", i);
|
2016-07-11 12:23:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|