2016-09-05 12:03:46 +00:00
|
|
|
// FNM_CASEFOLD
|
2016-07-22 01:10:17 +00:00
|
|
|
#define _GNU_SOURCE
|
2016-09-05 12:03:46 +00:00
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
|
|
// strptime
|
2016-07-28 15:53:40 +00:00
|
|
|
#ifndef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#endif
|
|
|
|
|
2017-02-21 18:11:46 +00:00
|
|
|
#ifdef __has_include
|
|
|
|
#if __has_include(<stdnoreturn.h>)
|
|
|
|
#include <stdnoreturn.h>
|
|
|
|
#else
|
|
|
|
#define noreturn /**/
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define noreturn /**/
|
|
|
|
#endif
|
|
|
|
|
2016-07-28 15:53:40 +00:00
|
|
|
/* For Solaris. */
|
|
|
|
#if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
|
|
|
|
#define FNM_CASEFOLD FNM_IGNORECASE
|
|
|
|
#endif
|
2016-07-22 01:10:17 +00:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-31 15:30:17 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdarg.h>
|
2016-07-22 01:10:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-07-28 15:53:40 +00:00
|
|
|
#include <strings.h>
|
2016-07-22 01:10:17 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include "blaze822.h"
|
|
|
|
|
|
|
|
enum op {
|
|
|
|
EXPR_OR = 1,
|
|
|
|
EXPR_AND,
|
|
|
|
EXPR_NOT,
|
|
|
|
EXPR_LT,
|
|
|
|
EXPR_LE,
|
|
|
|
EXPR_EQ,
|
|
|
|
EXPR_NEQ,
|
|
|
|
EXPR_GE,
|
|
|
|
EXPR_GT,
|
|
|
|
EXPR_STREQ,
|
|
|
|
EXPR_STREQI,
|
|
|
|
EXPR_GLOB,
|
|
|
|
EXPR_GLOBI,
|
|
|
|
EXPR_REGEX,
|
|
|
|
EXPR_REGEXI,
|
2016-07-23 15:40:29 +00:00
|
|
|
EXPR_PRUNE,
|
2016-07-22 01:10:17 +00:00
|
|
|
EXPR_PRINT,
|
|
|
|
EXPR_TYPE,
|
|
|
|
EXPR_ALLSET,
|
|
|
|
EXPR_ANYSET,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum prop {
|
|
|
|
PROP_ATIME = 1,
|
|
|
|
PROP_CTIME,
|
|
|
|
PROP_DEPTH,
|
2016-07-31 15:05:20 +00:00
|
|
|
PROP_KEPT,
|
2016-07-22 01:10:17 +00:00
|
|
|
PROP_MTIME,
|
|
|
|
PROP_PATH,
|
2016-07-24 15:59:11 +00:00
|
|
|
PROP_REPLIES,
|
2016-07-22 01:10:17 +00:00
|
|
|
PROP_SIZE,
|
|
|
|
PROP_TOTAL,
|
|
|
|
PROP_FROM,
|
|
|
|
PROP_TO,
|
|
|
|
PROP_INDEX,
|
|
|
|
PROP_DATE,
|
|
|
|
PROP_FLAG,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum flags {
|
|
|
|
FLAG_PASSED = 1,
|
|
|
|
FLAG_REPLIED = 2,
|
|
|
|
FLAG_SEEN = 4,
|
|
|
|
FLAG_TRASHED = 8,
|
|
|
|
FLAG_DRAFT = 16,
|
|
|
|
FLAG_FLAGGED = 32,
|
|
|
|
/* custom */
|
|
|
|
FLAG_NEW = 64,
|
|
|
|
FLAG_CUR = 128,
|
2016-07-23 15:40:29 +00:00
|
|
|
|
|
|
|
FLAG_PARENT = 256,
|
|
|
|
FLAG_CHILD = 512,
|
2016-07-28 16:47:17 +00:00
|
|
|
|
|
|
|
FLAG_INFO = 1024,
|
2016-07-22 01:10:17 +00:00
|
|
|
};
|
|
|
|
|
2016-07-28 23:49:28 +00:00
|
|
|
enum var {
|
|
|
|
VAR_CUR = 1,
|
|
|
|
};
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
struct expr {
|
|
|
|
enum op op;
|
|
|
|
union {
|
|
|
|
enum prop prop;
|
|
|
|
struct expr *expr;
|
|
|
|
char *string;
|
|
|
|
int64_t num;
|
|
|
|
regex_t *regex;
|
2016-07-28 23:49:28 +00:00
|
|
|
enum var var;
|
2016-07-22 01:10:17 +00:00
|
|
|
} a, b;
|
2016-07-28 23:49:28 +00:00
|
|
|
int extra;
|
2016-07-22 01:10:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mailinfo {
|
|
|
|
char *fpath;
|
|
|
|
struct stat *sb;
|
|
|
|
struct message *msg;
|
|
|
|
time_t date;
|
|
|
|
int depth;
|
|
|
|
int index;
|
2016-07-23 15:40:29 +00:00
|
|
|
int replies;
|
|
|
|
int matched;
|
|
|
|
int prune;
|
2017-03-30 16:32:52 +00:00
|
|
|
int flags;
|
2016-07-22 01:10:17 +00:00
|
|
|
off_t total;
|
|
|
|
};
|
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
struct mlist {
|
|
|
|
struct mailinfo *m;
|
2016-07-24 15:59:11 +00:00
|
|
|
struct mlist *parent;
|
2016-07-23 15:40:29 +00:00
|
|
|
struct mlist *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thread {
|
|
|
|
int matched;
|
|
|
|
struct mlist childs[100];
|
|
|
|
struct mlist *cur;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct thread *thr;
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
static char *argv0;
|
2016-07-23 15:40:29 +00:00
|
|
|
static int Tflag;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-23 16:45:23 +00:00
|
|
|
static int need_thr;
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
static long kept;
|
2016-07-22 23:16:24 +00:00
|
|
|
static long num;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
|
|
|
static struct expr *expr;
|
2016-07-28 23:49:28 +00:00
|
|
|
static long cur_idx;
|
2016-07-22 01:10:17 +00:00
|
|
|
static char *cur;
|
|
|
|
static char *pos;
|
|
|
|
static time_t now;
|
2016-07-23 15:40:29 +00:00
|
|
|
static int prune;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ws()
|
|
|
|
{
|
2017-08-31 15:30:17 +00:00
|
|
|
while (isspace((unsigned char)*pos))
|
2016-07-22 01:10:17 +00:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-07-28 16:09:10 +00:00
|
|
|
token(char *token)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
|
|
|
if (strncmp(pos, token, strlen(token)) == 0) {
|
|
|
|
pos += strlen(token);
|
|
|
|
ws();
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:11:46 +00:00
|
|
|
noreturn static void
|
2016-07-28 16:09:10 +00:00
|
|
|
parse_error(char *msg, ...)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, msg);
|
|
|
|
fprintf(stderr, "%s: parse error: ", argv0);
|
|
|
|
vfprintf(stderr, msg, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
mkexpr(enum op op)
|
|
|
|
{
|
|
|
|
struct expr *e = malloc(sizeof (struct expr));
|
|
|
|
if (!e)
|
|
|
|
parse_error("out of memory");
|
|
|
|
e->op = op;
|
2017-03-30 16:32:52 +00:00
|
|
|
e->extra = 0;
|
2016-07-22 01:10:17 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
chain(struct expr *e1, enum op op, struct expr *e2)
|
|
|
|
{
|
|
|
|
struct expr *i, *j, *e;
|
|
|
|
if (!e1)
|
|
|
|
return e2;
|
|
|
|
if (!e2)
|
|
|
|
return e1;
|
|
|
|
for (j = 0, i = e1; i->op == op; j = i, i = i->b.expr)
|
|
|
|
;
|
|
|
|
if (!j) {
|
|
|
|
e = mkexpr(op);
|
|
|
|
e->a.expr = e1;
|
|
|
|
e->b.expr = e2;
|
|
|
|
return e;
|
|
|
|
} else {
|
|
|
|
e = mkexpr(op);
|
|
|
|
e->a.expr = i;
|
|
|
|
e->b.expr = e2;
|
|
|
|
j->b.expr = e;
|
|
|
|
return e1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum op
|
|
|
|
parse_op()
|
|
|
|
{
|
|
|
|
if (token("<="))
|
|
|
|
return EXPR_LE;
|
|
|
|
else if (token("<"))
|
|
|
|
return EXPR_LT;
|
|
|
|
else if (token(">="))
|
|
|
|
return EXPR_GE;
|
|
|
|
else if (token(">"))
|
|
|
|
return EXPR_GT;
|
|
|
|
else if (token("==") || token("="))
|
|
|
|
return EXPR_EQ;
|
|
|
|
else if (token("!="))
|
|
|
|
return EXPR_NEQ;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *parse_cmp();
|
|
|
|
static struct expr *parse_or();
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_inner()
|
|
|
|
{
|
2016-07-23 15:40:29 +00:00
|
|
|
if (token("prune")) {
|
|
|
|
struct expr *e = mkexpr(EXPR_PRUNE);
|
|
|
|
return e;
|
|
|
|
} else if (token("print")) {
|
2016-07-22 01:10:17 +00:00
|
|
|
struct expr *e = mkexpr(EXPR_PRINT);
|
|
|
|
return e;
|
|
|
|
} else if (token("!")) {
|
|
|
|
struct expr *e = parse_cmp();
|
|
|
|
struct expr *not = mkexpr(EXPR_NOT);
|
|
|
|
not->a.expr = e;
|
|
|
|
return not;
|
|
|
|
} else if (token("(")) {
|
|
|
|
struct expr *e = parse_or();
|
|
|
|
if (token(")"))
|
|
|
|
return e;
|
|
|
|
parse_error("missing ) at '%.15s'", pos);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
parse_error("unknown expression at '%.15s'", pos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_string(char **s)
|
|
|
|
{
|
|
|
|
char *buf = 0;
|
|
|
|
size_t bufsiz = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (*pos == '"') {
|
|
|
|
pos++;
|
|
|
|
while (*pos &&
|
|
|
|
(*pos != '"' || (*pos == '"' && *(pos+1) == '"'))) {
|
|
|
|
if (len >= bufsiz) {
|
|
|
|
bufsiz = 2*bufsiz + 16;
|
|
|
|
buf = realloc(buf, bufsiz);
|
|
|
|
if (!buf)
|
|
|
|
parse_error("string too long");
|
|
|
|
}
|
|
|
|
if (*pos == '"')
|
|
|
|
pos++;
|
|
|
|
buf[len++] = *pos++;
|
|
|
|
}
|
|
|
|
if (!*pos)
|
|
|
|
parse_error("unterminated string");
|
|
|
|
if (buf)
|
|
|
|
buf[len] = 0;
|
|
|
|
pos++;
|
|
|
|
ws();
|
2016-07-28 16:09:10 +00:00
|
|
|
*s = buf ? buf : "";
|
2016-07-22 01:10:17 +00:00
|
|
|
return 1;
|
|
|
|
} else if (*pos == '$') {
|
|
|
|
char t;
|
|
|
|
char *e = ++pos;
|
|
|
|
|
2017-08-31 15:30:17 +00:00
|
|
|
while (isalnum((unsigned char)*pos) || *pos == '_')
|
2016-07-22 01:10:17 +00:00
|
|
|
pos++;
|
|
|
|
if (e == pos)
|
|
|
|
parse_error("invalid environment variable name");
|
|
|
|
|
|
|
|
t = *pos;
|
|
|
|
*pos = 0;
|
|
|
|
*s = getenv(e);
|
|
|
|
if (!*s)
|
2016-07-28 16:09:10 +00:00
|
|
|
*s = "";
|
2016-07-22 01:10:17 +00:00
|
|
|
*pos = t;
|
|
|
|
ws();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_strcmp()
|
|
|
|
{
|
|
|
|
enum prop prop;
|
|
|
|
enum op op;
|
2017-10-26 19:50:29 +00:00
|
|
|
int negate;
|
2017-04-17 00:54:22 +00:00
|
|
|
char *h;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2017-04-17 00:54:22 +00:00
|
|
|
h = 0;
|
|
|
|
prop = 0;
|
2017-10-26 19:50:29 +00:00
|
|
|
negate = 0;
|
2017-04-17 00:54:22 +00:00
|
|
|
|
|
|
|
if (token("from"))
|
2016-07-22 01:10:17 +00:00
|
|
|
prop = PROP_FROM;
|
|
|
|
else if (token("to"))
|
|
|
|
prop = PROP_TO;
|
2017-04-17 00:54:22 +00:00
|
|
|
else if (token("subject"))
|
|
|
|
h = "subject";
|
|
|
|
else if (!parse_string(&h))
|
2016-07-22 01:10:17 +00:00
|
|
|
return parse_inner();
|
|
|
|
|
|
|
|
if (token("~~~"))
|
|
|
|
op = EXPR_GLOBI;
|
|
|
|
else if (token("~~"))
|
|
|
|
op = EXPR_GLOB;
|
|
|
|
else if (token("=~~"))
|
|
|
|
op = EXPR_REGEXI;
|
|
|
|
else if (token("=~"))
|
|
|
|
op = EXPR_REGEX;
|
|
|
|
else if (token("==="))
|
|
|
|
op = EXPR_STREQI;
|
|
|
|
else if (token("=="))
|
|
|
|
op = EXPR_STREQ;
|
|
|
|
else if (token("="))
|
|
|
|
op = EXPR_STREQ;
|
2017-10-26 19:50:29 +00:00
|
|
|
else if (token("!~~~"))
|
|
|
|
negate = 1, op = EXPR_GLOBI;
|
|
|
|
else if (token("!~~"))
|
|
|
|
negate = 1, op = EXPR_GLOB;
|
|
|
|
else if (token("!=~~"))
|
|
|
|
negate = 1, op = EXPR_REGEXI;
|
|
|
|
else if (token("!=~"))
|
|
|
|
negate = 1, op = EXPR_REGEX;
|
|
|
|
else if (token("!==="))
|
|
|
|
negate = 1, op = EXPR_STREQI;
|
|
|
|
else if (token("!==") || token("!="))
|
|
|
|
negate = 1, op = EXPR_STREQ;
|
2016-07-22 01:10:17 +00:00
|
|
|
else
|
|
|
|
parse_error("invalid string operator at '%.15s'", pos);
|
|
|
|
|
|
|
|
char *s;
|
2016-07-28 17:31:56 +00:00
|
|
|
if (!parse_string(&s)) {
|
|
|
|
parse_error("invalid string at '%.15s'", pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
int r = 0;
|
|
|
|
struct expr *e = mkexpr(op);
|
2017-04-17 00:54:22 +00:00
|
|
|
|
|
|
|
if (prop)
|
|
|
|
e->a.prop = prop;
|
|
|
|
else
|
|
|
|
e->a.string = h;
|
2016-07-28 17:31:56 +00:00
|
|
|
|
|
|
|
if (prop == PROP_FROM || prop == PROP_TO) {
|
|
|
|
char *disp, *addr;
|
2017-02-21 18:11:46 +00:00
|
|
|
blaze822_addr(s, &disp, &addr);
|
2016-07-28 17:31:56 +00:00
|
|
|
if (!disp && !addr)
|
|
|
|
parse_error("invalid address at '%.15s'", pos);
|
|
|
|
s = strdup((disp) ? disp : addr);
|
2016-07-28 23:49:28 +00:00
|
|
|
e->extra = (disp) ? 0 : 1;
|
2016-07-28 17:31:56 +00:00
|
|
|
}
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
if (op == EXPR_REGEX) {
|
|
|
|
e->b.regex = malloc(sizeof (regex_t));
|
|
|
|
r = regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB);
|
|
|
|
} else if (op == EXPR_REGEXI) {
|
|
|
|
e->b.regex = malloc(sizeof (regex_t));
|
|
|
|
r = regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB | REG_ICASE);
|
|
|
|
} else {
|
|
|
|
e->b.string = s;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
if (r != 0) {
|
|
|
|
char msg[256];
|
|
|
|
regerror(r, e->b.regex, msg, sizeof msg);
|
|
|
|
parse_error("invalid regex '%s': %s", s, msg);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:50:29 +00:00
|
|
|
if (negate) {
|
|
|
|
struct expr *not = mkexpr(EXPR_NOT);
|
|
|
|
not->a.expr = e;
|
|
|
|
return not;
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
return e;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
|
|
|
parse_num(int64_t *r)
|
|
|
|
{
|
|
|
|
char *s = pos;
|
2017-08-31 15:30:17 +00:00
|
|
|
if (isdigit((unsigned char)*pos)) {
|
2016-07-22 01:10:17 +00:00
|
|
|
int64_t n;
|
|
|
|
|
2017-08-31 15:30:17 +00:00
|
|
|
for (n = 0; isdigit((unsigned char)*pos) && n <= INT64_MAX / 10 - 10; pos++)
|
2016-07-22 01:10:17 +00:00
|
|
|
n = 10 * n + (*pos - '0');
|
2017-08-31 15:30:17 +00:00
|
|
|
if (isdigit((unsigned char)*pos))
|
2016-07-22 01:10:17 +00:00
|
|
|
parse_error("number too big: %s", s);
|
|
|
|
if (token("c")) ;
|
|
|
|
else if (token("b")) n *= 512LL;
|
|
|
|
else if (token("k")) n *= 1024LL;
|
|
|
|
else if (token("M")) n *= 1024LL * 1024;
|
|
|
|
else if (token("G")) n *= 1024LL * 1024 * 1024;
|
|
|
|
else if (token("T")) n *= 1024LL * 1024 * 1024 * 1024;
|
|
|
|
ws();
|
|
|
|
*r = n;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_flag()
|
|
|
|
{
|
|
|
|
enum flags flag;
|
|
|
|
|
|
|
|
if (token("passed")) {
|
|
|
|
flag = FLAG_PASSED;
|
|
|
|
} else if (token("replied")) {
|
|
|
|
flag = FLAG_REPLIED;
|
|
|
|
} else if (token("seen")) {
|
|
|
|
flag = FLAG_SEEN;
|
|
|
|
} else if (token("trashed")) {
|
|
|
|
flag = FLAG_TRASHED;
|
|
|
|
} else if (token("draft")) {
|
|
|
|
flag = FLAG_DRAFT;
|
|
|
|
} else if (token("flagged")) {
|
|
|
|
flag = FLAG_FLAGGED;
|
|
|
|
} else if (token("new")) {
|
|
|
|
flag = FLAG_NEW;
|
|
|
|
} else if (token("cur")) {
|
|
|
|
flag = FLAG_CUR;
|
2016-07-28 16:47:17 +00:00
|
|
|
} else if (token("info")) {
|
|
|
|
flag = FLAG_INFO;
|
2016-07-23 16:45:23 +00:00
|
|
|
} else if (token("parent")) {
|
|
|
|
flag = FLAG_PARENT;
|
|
|
|
need_thr = 1;
|
|
|
|
} else if (token("child")) {
|
|
|
|
flag = FLAG_CHILD;
|
|
|
|
need_thr = 1;
|
2016-07-22 01:10:17 +00:00
|
|
|
} else
|
|
|
|
return parse_strcmp();
|
|
|
|
|
|
|
|
struct expr *e = mkexpr(EXPR_ANYSET);
|
|
|
|
e->a.prop = PROP_FLAG;
|
|
|
|
e->b.num = flag;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_cmp()
|
|
|
|
{
|
|
|
|
enum prop prop;
|
|
|
|
enum op op;
|
|
|
|
|
|
|
|
if (token("depth"))
|
|
|
|
prop = PROP_DEPTH;
|
2016-07-31 15:05:20 +00:00
|
|
|
else if (token("kept"))
|
|
|
|
prop = PROP_KEPT;
|
2016-07-22 01:10:17 +00:00
|
|
|
else if (token("index"))
|
|
|
|
prop = PROP_INDEX;
|
2016-07-24 15:59:11 +00:00
|
|
|
else if (token("replies")) {
|
|
|
|
prop = PROP_REPLIES;
|
|
|
|
need_thr = 1;
|
|
|
|
} else if (token("size"))
|
2016-07-22 01:10:17 +00:00
|
|
|
prop = PROP_SIZE;
|
|
|
|
else if (token("total"))
|
|
|
|
prop = PROP_TOTAL;
|
|
|
|
else
|
|
|
|
return parse_flag();
|
|
|
|
|
|
|
|
if (!(op = parse_op()))
|
|
|
|
parse_error("invalid comparison at '%.15s'", pos);
|
|
|
|
|
|
|
|
int64_t n;
|
|
|
|
if (parse_num(&n)) {
|
|
|
|
struct expr *e = mkexpr(op);
|
|
|
|
e->a.prop = prop;
|
|
|
|
e->b.num = n;
|
|
|
|
return e;
|
2017-08-31 15:30:17 +00:00
|
|
|
} else if (token("cur")) {
|
2016-07-28 23:49:28 +00:00
|
|
|
struct expr *e = mkexpr(op);
|
|
|
|
e->a.prop = prop;
|
|
|
|
e->b.var = VAR_CUR;
|
|
|
|
e->extra = 1;
|
|
|
|
return e;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_dur(int64_t *n)
|
|
|
|
{
|
|
|
|
char *s, *r;
|
|
|
|
if (!parse_string(&s))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (*s == '/' || *s == '.') {
|
|
|
|
struct stat st;
|
|
|
|
if (stat(s, &st) < 0)
|
|
|
|
parse_error("can't stat file '%s': %s",
|
|
|
|
s, strerror(errno));
|
|
|
|
*n = st.st_mtime;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm tm = { 0 };
|
|
|
|
r = strptime(s, "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
if (r && !*r) {
|
|
|
|
*n = mktime(&tm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = strptime(s, "%Y-%m-%d", &tm);
|
|
|
|
if (r && !*r) {
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
|
|
|
|
*n = mktime(&tm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = strptime(s, "%H:%M:%S", &tm);
|
|
|
|
if (r && !*r) {
|
|
|
|
struct tm *tmnow = localtime(&now);
|
|
|
|
tm.tm_year = tmnow->tm_year;
|
|
|
|
tm.tm_mon = tmnow->tm_mon;
|
|
|
|
tm.tm_mday = tmnow->tm_mday;
|
|
|
|
*n = mktime(&tm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = strptime(s, "%H:%M", &tm);
|
|
|
|
if (r && !*r) {
|
|
|
|
struct tm *tmnow = localtime(&now);
|
|
|
|
tm.tm_year = tmnow->tm_year;
|
|
|
|
tm.tm_mon = tmnow->tm_mon;
|
|
|
|
tm.tm_mday = tmnow->tm_mday;
|
|
|
|
tm.tm_sec = 0;
|
|
|
|
*n = mktime(&tm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == '-') {
|
|
|
|
s++;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
int64_t d;
|
|
|
|
d = strtol(s, &r, 10);
|
|
|
|
if (errno == 0 && r[0] == 'd' && !r[1]) {
|
|
|
|
struct tm *tmnow = localtime(&now);
|
|
|
|
tmnow->tm_mday -= d;
|
|
|
|
tmnow->tm_hour = tmnow->tm_min = tmnow->tm_sec = 0;
|
|
|
|
*n = mktime(tmnow);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (errno == 0 && r[0] == 'h' && !r[1]) {
|
|
|
|
*n = now - (d*60*60);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (errno == 0 && r[0] == 'm' && !r[1]) {
|
|
|
|
*n = now - (d*60);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (errno == 0 && r[0] == 's' && !r[1]) {
|
|
|
|
*n = now - d;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
parse_error("invalid relative time format '%s'", s-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_error("invalid time format '%s'", s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_timecmp()
|
|
|
|
{
|
|
|
|
enum prop prop;
|
|
|
|
enum op op;
|
|
|
|
|
|
|
|
if (token("atime"))
|
|
|
|
prop = PROP_ATIME;
|
|
|
|
else if (token("ctime"))
|
|
|
|
prop = PROP_CTIME;
|
|
|
|
else if (token("mtime"))
|
|
|
|
prop = PROP_MTIME;
|
|
|
|
else if (token("date"))
|
|
|
|
prop = PROP_DATE;
|
|
|
|
else
|
|
|
|
return parse_cmp();
|
|
|
|
|
|
|
|
op = parse_op();
|
|
|
|
if (!op)
|
|
|
|
parse_error("invalid comparison at '%.15s'", pos);
|
|
|
|
|
|
|
|
int64_t n;
|
|
|
|
if (parse_num(&n) || parse_dur(&n)) {
|
|
|
|
struct expr *e = mkexpr(op);
|
|
|
|
e->a.prop = prop;
|
|
|
|
e->b.num = n;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_and()
|
|
|
|
{
|
|
|
|
struct expr *e1 = parse_timecmp();
|
|
|
|
struct expr *r = e1;
|
|
|
|
|
|
|
|
while (token("&&")) {
|
|
|
|
struct expr *e2 = parse_timecmp();
|
|
|
|
r = chain(r, EXPR_AND, e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
|
|
|
parse_or()
|
|
|
|
{
|
|
|
|
struct expr *e1 = parse_and();
|
|
|
|
struct expr *r = e1;
|
|
|
|
|
|
|
|
while (token("||")) {
|
|
|
|
struct expr *e2 = parse_and();
|
|
|
|
r = chain(r, EXPR_OR, e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
2016-07-28 16:09:10 +00:00
|
|
|
parse_expr(char *s)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
2016-07-28 16:09:10 +00:00
|
|
|
pos = s;
|
2016-07-22 01:10:17 +00:00
|
|
|
struct expr *e = parse_or();
|
|
|
|
if (*pos)
|
|
|
|
parse_error("trailing garbage at '%.15s'", pos);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct expr *
|
2016-07-28 16:09:10 +00:00
|
|
|
parse_msglist(char *s)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
|
|
|
int64_t n, m;
|
|
|
|
int r;
|
|
|
|
struct expr *e1, *e2;
|
|
|
|
char *d;
|
|
|
|
|
|
|
|
switch (*s) {
|
|
|
|
case '/':
|
|
|
|
s++;
|
|
|
|
e1 = mkexpr(EXPR_REGEXI);
|
2017-04-17 00:54:22 +00:00
|
|
|
e1->a.string = "subject";
|
2016-07-22 01:10:17 +00:00
|
|
|
e1->b.regex = malloc(sizeof (regex_t));
|
|
|
|
r = regcomp(e1->b.regex, s, REG_EXTENDED | REG_NOSUB | REG_ICASE);
|
|
|
|
if (r != 0) {
|
|
|
|
char msg[256];
|
|
|
|
regerror(r, e1->b.regex, msg, sizeof msg);
|
|
|
|
parse_error("invalid regex '%s': %s", s, msg);
|
|
|
|
}
|
|
|
|
return e1;
|
|
|
|
case ':':
|
|
|
|
if (strlen(s) <= 1)
|
|
|
|
parse_error("missing type at '%.15s'", s);
|
|
|
|
|
|
|
|
enum flags flag;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
switch (*++s) {
|
|
|
|
case 'P': flag = FLAG_PASSED; break;
|
|
|
|
case 'F': flag = FLAG_FLAGGED; break;
|
|
|
|
case 'D': flag = FLAG_DRAFT; break;
|
2017-03-29 16:04:55 +00:00
|
|
|
case 'd': /* FALL THROUGH */
|
2016-07-22 01:10:17 +00:00
|
|
|
case 'T': flag = FLAG_TRASHED; break;
|
2017-03-29 16:04:55 +00:00
|
|
|
case 'u': n = 1; /* FALL THROUGH */
|
|
|
|
case 'r': /* FALL THROUGH */
|
2016-07-22 01:10:17 +00:00
|
|
|
case 'S': flag = FLAG_SEEN; break;
|
2017-03-29 16:04:55 +00:00
|
|
|
case 'o': n = 1; /* FALL THROUGH */
|
2016-07-22 01:10:17 +00:00
|
|
|
case 'n': flag = FLAG_NEW; break;
|
2017-03-29 18:21:31 +00:00
|
|
|
case 'R': flag = FLAG_REPLIED; break;
|
2017-02-21 18:11:46 +00:00
|
|
|
default: parse_error("unknown type at '%.15s'", s);
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e1 = mkexpr(EXPR_ANYSET);
|
|
|
|
e1->a.prop = PROP_FLAG;
|
|
|
|
e1->b.num = flag;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return e1;
|
|
|
|
|
|
|
|
e2 = mkexpr(EXPR_NOT);
|
|
|
|
e2->a.expr = e1;
|
|
|
|
return e2;
|
|
|
|
default:
|
2016-07-28 16:09:10 +00:00
|
|
|
pos = s;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-28 16:00:52 +00:00
|
|
|
if (((d = strchr(s, ':')) || (d = strchr(s, '-')))
|
2016-07-28 16:09:10 +00:00
|
|
|
&& parse_num(&n) && (pos = d + 1) && parse_num(&m)) {
|
2016-07-22 01:10:17 +00:00
|
|
|
/* index >= n */
|
|
|
|
e1 = mkexpr(EXPR_GE);
|
|
|
|
e1->a.prop = PROP_INDEX;
|
|
|
|
e1->b.num = n;
|
|
|
|
|
|
|
|
/* index <= m */
|
|
|
|
e2 = mkexpr(EXPR_LE);
|
|
|
|
e2->a.prop = PROP_INDEX;
|
|
|
|
e2->b.num = m;
|
|
|
|
|
|
|
|
/* e1 && e2 */
|
|
|
|
return chain(e1, EXPR_AND, e2);
|
|
|
|
} else if (parse_num(&n)) {
|
|
|
|
e1 = mkexpr(EXPR_EQ);
|
|
|
|
e1->a.prop = PROP_INDEX;
|
|
|
|
e1->b.num = n;
|
|
|
|
|
|
|
|
return e1;
|
|
|
|
} else {
|
2017-01-20 22:24:39 +00:00
|
|
|
char *disp, *addr;
|
|
|
|
|
2017-02-21 18:11:46 +00:00
|
|
|
blaze822_addr(s, &disp, &addr);
|
2017-01-20 22:24:39 +00:00
|
|
|
if (!disp && !addr)
|
|
|
|
parse_error("invalid address at '%.15s'", pos);
|
|
|
|
|
|
|
|
d = strdup((disp) ? disp : addr);
|
|
|
|
|
|
|
|
e1 = mkexpr(EXPR_REGEXI);
|
|
|
|
e1->a.prop = PROP_FROM;
|
|
|
|
e1->b.regex = malloc(sizeof (regex_t));
|
|
|
|
e1->extra = (disp) ? 0 : 1;
|
|
|
|
|
|
|
|
r = regcomp(e1->b.regex, d, REG_EXTENDED | REG_NOSUB | REG_ICASE);
|
|
|
|
if (r != 0) {
|
|
|
|
char msg[256];
|
|
|
|
regerror(r, e1->b.regex, msg, sizeof msg);
|
|
|
|
parse_error("invalid regex '%s': %s", d, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e1;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-28 15:53:51 +00:00
|
|
|
return 0;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
msg_date(struct mailinfo *m)
|
|
|
|
{
|
|
|
|
if (m->date)
|
|
|
|
return m->date;
|
|
|
|
|
2017-03-29 18:20:44 +00:00
|
|
|
if (!m->msg)
|
|
|
|
m->msg = blaze822(m->fpath);
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
char *b;
|
2016-07-24 15:59:11 +00:00
|
|
|
if (m->msg && (b = blaze822_hdr(m->msg, "date")))
|
2016-07-22 01:10:17 +00:00
|
|
|
return (m->date = blaze822_date(b));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2017-04-17 00:54:22 +00:00
|
|
|
msg_hdr(struct mailinfo *m, const char *h)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
2017-03-29 18:20:44 +00:00
|
|
|
if (!m->msg)
|
|
|
|
m->msg = blaze822(m->fpath);
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
char *b;
|
2017-04-17 00:54:22 +00:00
|
|
|
if (!m->msg || !(b = blaze822_chdr(m->msg, h)))
|
2017-03-30 17:36:56 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-08-30 10:56:34 +00:00
|
|
|
char buf[4096];
|
2017-03-30 17:36:56 +00:00
|
|
|
blaze822_decode_rfc2047(buf, b, sizeof buf - 1, "UTF-8");
|
|
|
|
if (!*buf)
|
|
|
|
goto err;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2017-04-17 00:54:22 +00:00
|
|
|
return strdup(buf);
|
2017-03-30 17:36:56 +00:00
|
|
|
err:
|
2017-04-17 00:54:22 +00:00
|
|
|
return "";
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
char *
|
|
|
|
msg_addr(struct mailinfo *m, char *h, int t)
|
|
|
|
{
|
2017-03-29 18:20:44 +00:00
|
|
|
if (!m->msg)
|
|
|
|
m->msg = blaze822(m->fpath);
|
|
|
|
|
2016-07-28 17:31:56 +00:00
|
|
|
char *b;
|
|
|
|
if (m->msg == 0 || (b = blaze822_chdr(m->msg, h)) == 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
char *disp, *addr;
|
2017-02-21 18:11:46 +00:00
|
|
|
blaze822_addr(b, &disp, &addr);
|
2016-07-28 17:31:56 +00:00
|
|
|
|
|
|
|
if (t) {
|
|
|
|
if (!addr)
|
|
|
|
return "";
|
|
|
|
return addr;
|
|
|
|
} else {
|
|
|
|
if (!disp)
|
|
|
|
return "";
|
|
|
|
return disp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
int
|
|
|
|
eval(struct expr *e, struct mailinfo *m)
|
|
|
|
{
|
|
|
|
switch (e->op) {
|
|
|
|
case EXPR_OR:
|
|
|
|
return eval(e->a.expr, m) || eval(e->b.expr, m);
|
|
|
|
case EXPR_AND:
|
|
|
|
return eval(e->a.expr, m) && eval(e->b.expr, m);
|
|
|
|
case EXPR_NOT:
|
|
|
|
return !eval(e->a.expr, m);
|
|
|
|
return 1;
|
2016-07-23 15:40:29 +00:00
|
|
|
case EXPR_PRUNE:
|
|
|
|
prune = 1;
|
|
|
|
return 1;
|
2016-07-22 01:10:17 +00:00
|
|
|
case EXPR_PRINT:
|
|
|
|
return 1;
|
|
|
|
case EXPR_LT:
|
|
|
|
case EXPR_LE:
|
|
|
|
case EXPR_EQ:
|
|
|
|
case EXPR_NEQ:
|
|
|
|
case EXPR_GE:
|
|
|
|
case EXPR_GT:
|
|
|
|
case EXPR_ALLSET:
|
|
|
|
case EXPR_ANYSET: {
|
2016-07-31 14:51:49 +00:00
|
|
|
long v = 0, n;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-28 15:53:51 +00:00
|
|
|
if (!m->sb && (
|
2016-07-22 01:10:17 +00:00
|
|
|
e->a.prop == PROP_ATIME ||
|
|
|
|
e->a.prop == PROP_CTIME ||
|
|
|
|
e->a.prop == PROP_MTIME ||
|
|
|
|
e->a.prop == PROP_SIZE) &&
|
2016-07-28 15:53:51 +00:00
|
|
|
(m->sb = calloc(1, sizeof *m->sb)) &&
|
2016-07-22 01:10:17 +00:00
|
|
|
stat(m->fpath, m->sb) != 0) {
|
|
|
|
fprintf(stderr, "stat");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:32:52 +00:00
|
|
|
n = e->b.num;
|
|
|
|
|
|
|
|
if (e->extra)
|
2016-07-28 23:49:28 +00:00
|
|
|
switch (e->b.var) {
|
|
|
|
case VAR_CUR:
|
|
|
|
if (!cur_idx)
|
2016-07-31 14:51:49 +00:00
|
|
|
n = (e->op == EXPR_LT || e->op == EXPR_LE) ? LONG_MAX : -1;
|
2016-07-28 23:49:28 +00:00
|
|
|
else
|
2016-07-31 14:51:49 +00:00
|
|
|
n = cur_idx;
|
2016-07-28 23:49:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-22 01:10:17 +00:00
|
|
|
switch (e->a.prop) {
|
2017-02-21 18:11:46 +00:00
|
|
|
case PROP_ATIME: if (m->sb) v = m->sb->st_atime; break;
|
|
|
|
case PROP_CTIME: if (m->sb) v = m->sb->st_ctime; break;
|
|
|
|
case PROP_MTIME: if (m->sb) v = m->sb->st_mtime; break;
|
2016-07-31 15:05:20 +00:00
|
|
|
case PROP_KEPT: v = kept; break;
|
2016-07-24 15:59:11 +00:00
|
|
|
case PROP_REPLIES: v = m->replies; break;
|
2017-02-21 18:11:46 +00:00
|
|
|
case PROP_SIZE: if (m->sb) v = m->sb->st_size; break;
|
2016-07-29 00:20:24 +00:00
|
|
|
case PROP_DATE: v = msg_date(m); break;
|
2016-07-22 01:10:17 +00:00
|
|
|
case PROP_FLAG: v = m->flags; break;
|
|
|
|
case PROP_INDEX: v = m->index; break;
|
|
|
|
case PROP_DEPTH: v = m->depth; break;
|
2018-02-05 14:36:13 +00:00
|
|
|
default: parse_error("unknown property");
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (e->op) {
|
2016-07-31 14:51:49 +00:00
|
|
|
case EXPR_LT: return v < n;
|
|
|
|
case EXPR_LE: return v <= n;
|
|
|
|
case EXPR_EQ: return v == n;
|
|
|
|
case EXPR_NEQ: return v != n;
|
|
|
|
case EXPR_GE: return v >= n;
|
|
|
|
case EXPR_GT: return v > n;
|
|
|
|
case EXPR_ALLSET: return (v & n) == n;
|
|
|
|
case EXPR_ANYSET: return (v & n) > 0;
|
2018-02-05 14:36:13 +00:00
|
|
|
default: parse_error("invalid operator");
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case EXPR_STREQ:
|
|
|
|
case EXPR_STREQI:
|
|
|
|
case EXPR_GLOB:
|
|
|
|
case EXPR_GLOBI:
|
|
|
|
case EXPR_REGEX:
|
|
|
|
case EXPR_REGEXI: {
|
|
|
|
const char *s = "";
|
2017-08-31 15:30:17 +00:00
|
|
|
switch (e->a.prop) {
|
2016-07-22 01:10:17 +00:00
|
|
|
case PROP_PATH: s = m->fpath; break;
|
2016-07-28 23:49:28 +00:00
|
|
|
case PROP_FROM: s = msg_addr(m, "from", e->extra); break;
|
|
|
|
case PROP_TO: s = msg_addr(m, "to", e->extra); break;
|
2017-04-17 00:54:22 +00:00
|
|
|
default: s = msg_hdr(m, e->a.string); break;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
switch (e->op) {
|
|
|
|
case EXPR_STREQ: return strcmp(e->b.string, s) == 0;
|
|
|
|
case EXPR_STREQI: return strcasecmp(e->b.string, s) == 0;
|
|
|
|
case EXPR_GLOB: return fnmatch(e->b.string, s, 0) == 0;
|
|
|
|
case EXPR_GLOBI: return fnmatch(e->b.string, s, FNM_CASEFOLD) == 0;
|
|
|
|
case EXPR_REGEX:
|
|
|
|
case EXPR_REGEXI: return regexec(e->b.regex, s, 0, 0, 0) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
struct mailinfo *
|
|
|
|
mailfile(char *file)
|
2016-07-22 01:10:17 +00:00
|
|
|
{
|
|
|
|
static int init;
|
|
|
|
if (!init) {
|
|
|
|
// delay loading of the seqmap until we need to scan the first
|
|
|
|
// file, in case someone in the pipe updated the map before
|
|
|
|
char *seqmap = blaze822_seq_open(0);
|
|
|
|
blaze822_seq_load(seqmap);
|
|
|
|
cur = blaze822_seq_cur();
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
struct mailinfo *m;
|
|
|
|
m = calloc(1, sizeof *m);
|
|
|
|
if (!m) {
|
|
|
|
fprintf(stderr, "calloc");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
m->fpath = file;
|
|
|
|
m->index = num++;
|
|
|
|
m->flags = 0;
|
2016-07-24 15:59:11 +00:00
|
|
|
m->replies = 0;
|
2016-07-23 15:40:29 +00:00
|
|
|
m->depth = 0;
|
|
|
|
m->sb = 0;
|
|
|
|
m->msg = 0;
|
|
|
|
|
2017-08-31 15:30:17 +00:00
|
|
|
while (*m->fpath == ' ' || *m->fpath == '\t') {
|
2016-07-23 15:40:29 +00:00
|
|
|
m->depth++;
|
|
|
|
m->fpath++;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
char *e = m->fpath + strlen(m->fpath) - 1;
|
|
|
|
while (m->fpath < e && (*e == ' ' || *e == '\t'))
|
2016-07-22 01:10:17 +00:00
|
|
|
*e-- = 0;
|
|
|
|
|
2016-07-28 16:47:17 +00:00
|
|
|
if (m->fpath[0] == '<') {
|
|
|
|
m->flags |= FLAG_SEEN | FLAG_INFO;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:34:54 +00:00
|
|
|
if ((e = strrchr(m->fpath, '/') - 1) && (e - m->fpath) >= 2 &&
|
|
|
|
*e-- == 'w' && *e-- == 'e' && *e-- == 'n')
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_NEW;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-28 23:49:28 +00:00
|
|
|
if (cur && strcmp(cur, m->fpath) == 0) {
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_CUR;
|
2016-07-28 23:49:28 +00:00
|
|
|
cur_idx = m->index;
|
|
|
|
}
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
char *f = strstr(m->fpath, ":2,");
|
2016-07-22 01:10:17 +00:00
|
|
|
if (f) {
|
|
|
|
if (strchr(f, 'P'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_PASSED;
|
2016-07-22 01:10:17 +00:00
|
|
|
if (strchr(f, 'R'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_REPLIED;
|
2016-07-22 01:10:17 +00:00
|
|
|
if (strchr(f, 'S'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_SEEN;
|
2016-07-22 01:10:17 +00:00
|
|
|
if (strchr(f, 'T'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_TRASHED;
|
2016-07-22 01:10:17 +00:00
|
|
|
if (strchr(f, 'D'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_DRAFT;
|
2016-07-22 01:10:17 +00:00
|
|
|
if (strchr(f, 'F'))
|
2016-07-23 15:40:29 +00:00
|
|
|
m->flags |= FLAG_FLAGGED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
do_thr()
|
|
|
|
{
|
|
|
|
struct mlist *ml;
|
|
|
|
|
|
|
|
if (!thr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (ml = thr->childs; ml; ml = ml->next) {
|
|
|
|
if (!ml->m)
|
|
|
|
continue;
|
|
|
|
if ((ml->m->prune = prune) || (Tflag && thr->matched))
|
|
|
|
continue;
|
|
|
|
if (expr && eval(expr, ml->m)) {
|
|
|
|
ml->m->matched = 1;
|
|
|
|
thr->matched++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prune = 0;
|
|
|
|
|
|
|
|
for (ml = thr->childs; ml; ml = ml->next) {
|
|
|
|
if (!ml->m)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (((Tflag && thr->matched) || ml->m->matched) && !ml->m->prune) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ml->m->depth; i++)
|
|
|
|
putchar(' ');
|
|
|
|
|
|
|
|
fputs(ml->m->fpath, stdout);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
kept++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free collected mails */
|
|
|
|
if (ml->m->msg)
|
|
|
|
blaze822_free(ml->m->msg);
|
|
|
|
|
|
|
|
if (ml->m->sb)
|
|
|
|
free(ml->m->sb);
|
|
|
|
|
|
|
|
free(ml->m->fpath);
|
|
|
|
free(ml->m);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(thr);
|
2016-07-24 15:59:11 +00:00
|
|
|
thr = 0;
|
2016-07-23 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
collect(char *file)
|
|
|
|
{
|
|
|
|
struct mailinfo *m;
|
|
|
|
struct mlist *ml;
|
|
|
|
|
|
|
|
if ((m = mailfile(file)) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m->depth == 0) {
|
|
|
|
if (thr)
|
|
|
|
do_thr();
|
|
|
|
|
|
|
|
/* new thread */
|
|
|
|
thr = calloc(1, sizeof *thr);
|
|
|
|
if (!thr) {
|
|
|
|
fprintf(stderr, "calloc");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
thr->matched = 0;
|
2016-07-23 16:45:23 +00:00
|
|
|
ml = thr->cur = thr->childs;
|
2016-07-23 15:40:29 +00:00
|
|
|
thr->cur->m = m;
|
|
|
|
} else {
|
2016-07-24 15:59:11 +00:00
|
|
|
ml = thr->cur + 1;
|
|
|
|
|
|
|
|
if (thr->cur->m->depth < m->depth) {
|
2017-03-30 15:41:11 +00:00
|
|
|
/* previous mail is a parent */
|
2016-07-23 16:45:23 +00:00
|
|
|
thr->cur->m->flags |= FLAG_PARENT;
|
2016-07-24 15:59:11 +00:00
|
|
|
ml->parent = thr->cur;
|
|
|
|
} else if (thr->cur->m->depth == m->depth) {
|
|
|
|
/* same depth == same parent */
|
|
|
|
ml->parent = thr->cur->parent;
|
|
|
|
} else if (thr->cur->m->depth > m->depth) {
|
|
|
|
/* find parent mail */
|
|
|
|
struct mlist *pl;
|
2017-08-31 15:30:17 +00:00
|
|
|
for (pl = thr->cur; pl->m->depth >= m->depth; pl--) ;
|
2016-07-24 15:59:11 +00:00
|
|
|
ml->parent = pl;
|
|
|
|
}
|
|
|
|
|
2016-07-23 16:45:23 +00:00
|
|
|
m->flags |= FLAG_CHILD;
|
|
|
|
|
2016-07-23 15:40:29 +00:00
|
|
|
thr->cur->next = ml;
|
|
|
|
thr->cur = ml;
|
|
|
|
ml->m = m;
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 15:59:11 +00:00
|
|
|
for (ml = ml->parent; ml; ml = ml->parent)
|
|
|
|
ml->m->replies++;
|
2016-07-23 15:40:29 +00:00
|
|
|
|
2016-07-24 15:59:11 +00:00
|
|
|
m->fpath = strdup(m->fpath);
|
2016-07-23 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
oneline(char *file)
|
|
|
|
{
|
|
|
|
struct mailinfo *m;
|
|
|
|
|
2016-07-24 15:59:11 +00:00
|
|
|
m = mailfile(file);
|
2016-07-23 15:40:29 +00:00
|
|
|
if (expr && !eval(expr, m))
|
2016-07-22 01:10:17 +00:00
|
|
|
goto out;
|
|
|
|
|
2016-07-24 15:59:11 +00:00
|
|
|
fputs(file, stdout);
|
|
|
|
putchar('\n');
|
2016-07-22 01:10:17 +00:00
|
|
|
kept++;
|
|
|
|
|
|
|
|
out:
|
2016-07-24 15:59:11 +00:00
|
|
|
if (m->msg)
|
|
|
|
blaze822_free(m->msg);
|
|
|
|
if (m->sb)
|
|
|
|
free(m->sb);
|
2016-07-23 15:40:29 +00:00
|
|
|
free(m);
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
int c;
|
2018-09-25 13:14:43 +00:00
|
|
|
int vflag;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
|
|
|
argv0 = argv[0];
|
2016-07-29 00:20:24 +00:00
|
|
|
now = time(0);
|
2017-04-27 21:48:49 +00:00
|
|
|
num = 1;
|
2018-09-25 13:14:43 +00:00
|
|
|
vflag = 0;
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2018-09-25 13:14:43 +00:00
|
|
|
while ((c = getopt(argc, argv, "Tt:v")) != -1)
|
2016-07-22 01:10:17 +00:00
|
|
|
switch (c) {
|
2016-07-23 16:45:23 +00:00
|
|
|
case 'T': Tflag = need_thr = 1; break;
|
2016-07-22 01:10:17 +00:00
|
|
|
case 't': expr = chain(expr, EXPR_AND, parse_expr(optarg)); break;
|
2018-09-25 13:14:43 +00:00
|
|
|
case 'v': vflag = 1; break;
|
2016-07-27 16:33:46 +00:00
|
|
|
default:
|
2018-09-25 14:37:07 +00:00
|
|
|
fprintf(stderr, "Usage: %s [-Tv] [-t test] [msglist ...]\n", argv0);
|
2016-07-27 16:33:46 +00:00
|
|
|
exit(1);
|
2016-07-22 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc)
|
|
|
|
for (c = optind; c < argc; c++)
|
|
|
|
expr = chain(expr, EXPR_AND, parse_msglist(argv[c]));
|
|
|
|
|
2016-07-28 15:53:51 +00:00
|
|
|
if (isatty(0))
|
2016-07-23 16:45:23 +00:00
|
|
|
i = blaze822_loop1(":", need_thr ? collect : oneline);
|
2016-07-28 15:53:51 +00:00
|
|
|
else
|
|
|
|
i = blaze822_loop(0, 0, need_thr ? collect : oneline);
|
2016-07-23 15:40:29 +00:00
|
|
|
|
|
|
|
/* print and free last thread */
|
|
|
|
if (Tflag && thr)
|
|
|
|
do_thr();
|
2016-07-22 01:10:17 +00:00
|
|
|
|
2018-09-25 13:14:43 +00:00
|
|
|
if (vflag)
|
|
|
|
fprintf(stderr, "%ld mails tested, %ld picked.\n", i, kept);
|
2016-07-22 01:10:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|