2016-07-11 14:11:52 +00:00
|
|
|
#include <sys/stat.h>
|
2016-07-18 15:06:41 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2016-07-11 14:11:52 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2016-07-18 15:06:41 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2016-07-11 14:11:52 +00:00
|
|
|
|
|
|
|
#include "blaze822.h"
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
static char *hflag;
|
2017-09-06 15:46:20 +00:00
|
|
|
static char *pflag;
|
2016-07-28 11:08:04 +00:00
|
|
|
static int Aflag;
|
|
|
|
static int Dflag;
|
2016-08-17 13:45:09 +00:00
|
|
|
static int Hflag;
|
2016-07-28 11:08:04 +00:00
|
|
|
static int Mflag;
|
|
|
|
static int dflag;
|
2016-07-11 14:11:52 +00:00
|
|
|
|
2016-08-17 13:45:09 +00:00
|
|
|
static char *curfile;
|
2016-08-08 12:23:20 +00:00
|
|
|
static int status;
|
|
|
|
|
2016-07-14 13:40:57 +00:00
|
|
|
static void
|
|
|
|
printhdr(char *hdr)
|
|
|
|
{
|
|
|
|
int uc = 1;
|
|
|
|
|
2016-08-17 13:45:09 +00:00
|
|
|
if (Hflag)
|
|
|
|
printf("%s\t", curfile);
|
|
|
|
|
2016-07-14 13:40:57 +00:00
|
|
|
while (*hdr && *hdr != ':') {
|
|
|
|
putc(uc ? toupper(*hdr) : *hdr, stdout);
|
|
|
|
uc = (*hdr == '-');
|
|
|
|
hdr++;
|
|
|
|
}
|
|
|
|
fputs(hdr, stdout);
|
|
|
|
fputc('\n', stdout);
|
2016-08-08 12:23:20 +00:00
|
|
|
|
|
|
|
status = 0;
|
2016-07-14 13:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-28 11:08:04 +00:00
|
|
|
headerall(struct message *msg)
|
|
|
|
{
|
|
|
|
char *h = 0;
|
|
|
|
while ((h = blaze822_next_header(msg, h))) {
|
|
|
|
if (dflag) {
|
|
|
|
char d[4096];
|
|
|
|
blaze822_decode_rfc2047(d, h, sizeof d, "UTF-8");
|
|
|
|
printhdr(d);
|
|
|
|
} else {
|
|
|
|
printhdr(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blaze822_free(msg);
|
|
|
|
}
|
|
|
|
|
2018-05-23 14:24:48 +00:00
|
|
|
void
|
|
|
|
print_quoted(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
for (t = s; *t; t++)
|
|
|
|
if ((unsigned char)*t < 32 || strchr("()<>[]:;@\\,.\"", *t))
|
|
|
|
goto quote;
|
2018-05-23 14:42:41 +00:00
|
|
|
|
2018-05-23 14:24:48 +00:00
|
|
|
printf("%s", s);
|
|
|
|
return;
|
|
|
|
|
|
|
|
quote:
|
|
|
|
putchar('"');
|
|
|
|
for (t = s; *t; t++) {
|
|
|
|
if (*t == '"' || *t == '\\')
|
|
|
|
putchar('\\');
|
|
|
|
putchar(*t);
|
|
|
|
}
|
|
|
|
putchar('"');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
void
|
|
|
|
print_addresses(char *s)
|
|
|
|
{
|
|
|
|
char *disp, *addr;
|
2017-01-21 16:47:14 +00:00
|
|
|
char sdec[4096];
|
|
|
|
|
|
|
|
if (dflag) {
|
|
|
|
blaze822_decode_rfc2047(sdec, s, sizeof sdec, "UTF-8");
|
|
|
|
sdec[sizeof sdec - 1] = 0;
|
|
|
|
s = sdec;
|
|
|
|
}
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
while ((s = blaze822_addr(s, &disp, &addr))) {
|
2017-01-21 16:48:12 +00:00
|
|
|
if (Hflag && addr)
|
2016-08-17 13:45:09 +00:00
|
|
|
printf("%s\t", curfile);
|
2017-01-21 16:47:14 +00:00
|
|
|
|
2018-05-23 14:24:48 +00:00
|
|
|
if (disp && addr) {
|
|
|
|
print_quoted(disp);
|
|
|
|
printf(" <%s>\n", addr);
|
|
|
|
} else if (addr) {
|
2016-07-28 11:08:04 +00:00
|
|
|
printf("%s\n", addr);
|
2018-05-23 14:24:48 +00:00
|
|
|
}
|
2016-07-28 11:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_date(char *s)
|
|
|
|
{
|
|
|
|
time_t t = blaze822_date(s);
|
|
|
|
if (t == -1)
|
|
|
|
return;
|
2017-05-27 15:49:49 +00:00
|
|
|
printf("%ld\n", (long)t);
|
2016-07-28 11:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_decode_header(char *s)
|
|
|
|
{
|
|
|
|
char d[4096];
|
|
|
|
blaze822_decode_rfc2047(d, s, sizeof d, "UTF-8");
|
|
|
|
printf("%s\n", d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_header(char *v)
|
|
|
|
{
|
2017-09-06 15:46:20 +00:00
|
|
|
if (pflag) {
|
|
|
|
char *s, *se;
|
|
|
|
if (blaze822_mime_parameter(v, pflag, &s, &se)) {
|
|
|
|
*se = 0;
|
|
|
|
v = s;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 12:23:20 +00:00
|
|
|
status = 0;
|
|
|
|
|
2016-08-17 13:45:09 +00:00
|
|
|
if (Hflag && !Aflag)
|
|
|
|
printf("%s\t", curfile);
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
if (Aflag)
|
|
|
|
print_addresses(v);
|
|
|
|
else if (Dflag)
|
|
|
|
print_date(v);
|
|
|
|
else if (dflag)
|
|
|
|
print_decode_header(v);
|
|
|
|
else
|
|
|
|
printf("%s\n", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
headermany(struct message *msg)
|
|
|
|
{
|
|
|
|
char *hdr = 0;
|
|
|
|
while ((hdr = blaze822_next_header(msg, hdr))) {
|
|
|
|
char *h = hflag;
|
|
|
|
while (*h) {
|
|
|
|
char *n = strchr(h, ':');
|
|
|
|
if (n)
|
|
|
|
*n = 0;
|
|
|
|
|
|
|
|
size_t l = strlen(h);
|
2018-04-23 12:17:03 +00:00
|
|
|
if (strncasecmp(hdr, h, l) == 0 && hdr[l] == ':') {
|
2016-07-28 11:08:04 +00:00
|
|
|
hdr += l + 1;
|
|
|
|
while (*hdr == ' ' || *hdr == '\t')
|
|
|
|
hdr++;
|
|
|
|
print_header(hdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
*n = ':';
|
|
|
|
h = n + 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blaze822_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
header(char *file)
|
2016-07-14 13:40:57 +00:00
|
|
|
{
|
|
|
|
struct message *msg;
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
while (*file == ' ' || *file == '\t')
|
|
|
|
file++;
|
|
|
|
|
2016-08-17 13:45:09 +00:00
|
|
|
curfile = file;
|
|
|
|
|
2016-07-14 13:40:57 +00:00
|
|
|
msg = blaze822(file);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
2017-10-06 11:16:15 +00:00
|
|
|
if (!hflag) {
|
|
|
|
headerall(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Mflag) {
|
|
|
|
headermany(msg);
|
|
|
|
return;
|
|
|
|
}
|
2016-07-28 11:08:04 +00:00
|
|
|
|
|
|
|
char *h = hflag;
|
|
|
|
while (*h) {
|
|
|
|
char *n = strchr(h, ':');
|
|
|
|
if (n)
|
|
|
|
*n = 0;
|
|
|
|
char *v = blaze822_chdr(msg, h);
|
|
|
|
if (v)
|
|
|
|
print_header(v);
|
|
|
|
if (n) {
|
|
|
|
*n = ':';
|
|
|
|
h = n + 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-15 14:51:43 +00:00
|
|
|
}
|
2016-07-28 11:08:04 +00:00
|
|
|
|
|
|
|
blaze822_free(msg);
|
2016-07-14 13:40:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:11:52 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2016-07-28 11:08:04 +00:00
|
|
|
int c;
|
2017-09-06 15:46:20 +00:00
|
|
|
while ((c = getopt(argc, argv, "h:p:ADHMd")) != -1)
|
2017-08-31 15:30:17 +00:00
|
|
|
switch (c) {
|
2016-07-28 11:08:04 +00:00
|
|
|
case 'h': hflag = optarg; break;
|
2017-09-06 15:46:20 +00:00
|
|
|
case 'p': pflag = optarg; break;
|
2016-07-28 11:08:04 +00:00
|
|
|
case 'A': Aflag = 1; break;
|
|
|
|
case 'D': Dflag = 1; break;
|
2016-08-17 13:45:09 +00:00
|
|
|
case 'H': Hflag = 1; break;
|
2016-07-28 11:08:04 +00:00
|
|
|
case 'M': Mflag = 1; break;
|
|
|
|
case 'd': dflag = 1; break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
2017-09-06 15:46:20 +00:00
|
|
|
"Usage: mhdr [-h header [-p parameter]] [-d] [-H] [-M] [-A|-D] [msgs...]\n");
|
2016-08-08 12:23:20 +00:00
|
|
|
exit(2);
|
2016-07-28 11:08:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 12:23:20 +00:00
|
|
|
status = 1;
|
|
|
|
|
2016-07-28 11:08:04 +00:00
|
|
|
if (argc == optind && isatty(0))
|
|
|
|
blaze822_loop1(".", header);
|
2016-07-22 22:29:38 +00:00
|
|
|
else
|
2016-07-28 11:08:04 +00:00
|
|
|
blaze822_loop(argc-optind, argv+optind, header);
|
2017-08-31 15:30:17 +00:00
|
|
|
|
2016-08-08 12:23:20 +00:00
|
|
|
return status;
|
2016-07-11 14:11:52 +00:00
|
|
|
}
|