2016-08-02 13:50:30 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "blaze822.h"
|
|
|
|
|
|
|
|
static int aflag;
|
|
|
|
static int cflag;
|
|
|
|
static int dflag;
|
|
|
|
static int iflag;
|
|
|
|
static int qflag;
|
|
|
|
static int vflag;
|
2016-08-02 15:03:32 +00:00
|
|
|
static long mflag;
|
2016-08-02 13:50:30 +00:00
|
|
|
static long matches;
|
|
|
|
|
|
|
|
static regex_t pattern;
|
|
|
|
static char *header;
|
2016-08-02 14:28:35 +00:00
|
|
|
static char *curfile;
|
2016-08-02 13:50:30 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
match(char *file, char *s)
|
|
|
|
{
|
|
|
|
if (vflag ^ (regexec(&pattern, s, 0, 0, 0) == 0)) {
|
|
|
|
if (qflag)
|
|
|
|
exit(0);
|
2016-08-02 15:03:32 +00:00
|
|
|
matches++;
|
2016-08-02 13:50:30 +00:00
|
|
|
if (!cflag)
|
|
|
|
printf("%s\n", file);
|
2016-08-02 15:03:32 +00:00
|
|
|
if (mflag && matches >= mflag)
|
|
|
|
exit(0);
|
2016-08-02 13:50:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:28:35 +00:00
|
|
|
blaze822_mime_action
|
|
|
|
match_part(int depth, struct message *msg, char *body, size_t bodylen)
|
|
|
|
{
|
|
|
|
(void) depth;
|
|
|
|
|
|
|
|
char *ct = blaze822_hdr(msg, "content-type");
|
|
|
|
|
|
|
|
blaze822_mime_action r = MIME_CONTINUE;
|
|
|
|
|
|
|
|
if (!ct || strncmp(ct, "text/plain", 10) == 0) {
|
|
|
|
char *charset = 0, *cs, *cse;
|
|
|
|
if (blaze822_mime_parameter(ct, "charset", &cs, &cse))
|
|
|
|
charset = strndup(cs, cse-cs);
|
|
|
|
if (!charset ||
|
|
|
|
strcasecmp(charset, "utf-8") == 0 ||
|
|
|
|
strcasecmp(charset, "utf8") == 0 ||
|
|
|
|
strcasecmp(charset, "us-ascii") == 0) {
|
2016-08-02 15:03:32 +00:00
|
|
|
(void) bodylen; /* XXX */
|
2016-08-02 14:28:35 +00:00
|
|
|
if (match(curfile, body))
|
|
|
|
r = MIME_STOP;
|
|
|
|
} else {
|
|
|
|
/* XXX decode here */
|
|
|
|
}
|
|
|
|
free(charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
match_body(char *file)
|
|
|
|
{
|
|
|
|
curfile = file;
|
|
|
|
while (*curfile == ' ' || *curfile == '\t')
|
2016-08-02 15:03:32 +00:00
|
|
|
curfile++;
|
2016-08-02 14:28:35 +00:00
|
|
|
|
|
|
|
struct message *msg = blaze822_file(curfile);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
blaze822_walk_mime(msg, 0, match_part);
|
|
|
|
}
|
|
|
|
|
2016-08-02 13:50:30 +00:00
|
|
|
void
|
|
|
|
magrep(char *file)
|
|
|
|
{
|
2016-08-02 14:02:46 +00:00
|
|
|
if (!*header) {
|
|
|
|
char *flags = strstr(file, ":2,");
|
|
|
|
if (flags)
|
|
|
|
match(file, flags+3);
|
|
|
|
return;
|
2016-08-02 14:28:35 +00:00
|
|
|
} else if (strcmp(header, "/") == 0) {
|
|
|
|
match_body(file);
|
|
|
|
return;
|
2016-08-02 14:02:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 13:50:30 +00:00
|
|
|
char *filename = file;
|
|
|
|
while (*filename == ' ' || *filename == '\t')
|
2016-08-02 15:03:32 +00:00
|
|
|
filename++;
|
2016-08-02 13:50:30 +00:00
|
|
|
|
|
|
|
struct message *msg = blaze822(filename);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *v = blaze822_chdr(msg, header);
|
|
|
|
if (v) {
|
|
|
|
if (dflag) {
|
|
|
|
char d[4096];
|
|
|
|
blaze822_decode_rfc2047(d, v, sizeof d, "UTF-8");
|
|
|
|
match(file, d);
|
|
|
|
} else if (aflag) {
|
|
|
|
char *disp, *addr;
|
2016-08-02 15:03:32 +00:00
|
|
|
while ((v = blaze822_addr(v, &disp, &addr))) {
|
2016-08-02 13:50:30 +00:00
|
|
|
if (addr && match(file, addr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match(file, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blaze822_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
2016-08-02 15:03:32 +00:00
|
|
|
while ((c = getopt(argc, argv, "acdim:qv")) != -1)
|
2016-08-02 13:50:30 +00:00
|
|
|
switch(c) {
|
2016-08-02 15:03:32 +00:00
|
|
|
case 'a': aflag = 1; break;
|
|
|
|
case 'c': cflag = 1; break;
|
|
|
|
case 'd': dflag = 1; break;
|
|
|
|
case 'i': iflag = REG_ICASE; break;
|
|
|
|
case 'm': mflag = atol(optarg); break;
|
|
|
|
case 'q': qflag = 1; break;
|
|
|
|
case 'v': vflag = 1; break;
|
2016-08-02 13:50:30 +00:00
|
|
|
default:
|
|
|
|
usage:
|
|
|
|
fprintf(stderr,
|
2016-08-02 15:03:32 +00:00
|
|
|
"Usage: magrep [-c|-q|-m max] [-v] [-i] [-a|-d] header:regex [msgs...]\n");
|
2016-08-02 13:50:30 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind)
|
|
|
|
goto usage;
|
|
|
|
header = argv[optind++];
|
|
|
|
|
|
|
|
char *rx = strchr(header, ':');
|
|
|
|
if (!rx)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
*rx++ = 0;
|
|
|
|
int r = regcomp(&pattern, rx, REG_EXTENDED | iflag);
|
|
|
|
if (r != 0) {
|
|
|
|
char buf[256];
|
|
|
|
regerror(r, &pattern, buf, sizeof buf);
|
|
|
|
fprintf(stderr, "magrep: regex error: %s\n", buf);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind && isatty(0))
|
|
|
|
blaze822_loop1(":", magrep);
|
|
|
|
else
|
|
|
|
blaze822_loop(argc-optind, argv+optind, magrep);
|
|
|
|
|
2016-08-02 15:03:32 +00:00
|
|
|
if (cflag && !qflag && !mflag)
|
2016-08-02 13:50:30 +00:00
|
|
|
printf("%ld\n", matches);
|
|
|
|
|
|
|
|
return !matches;
|
|
|
|
}
|