From d548547d99c5cd72fa205ac2ba6347e672185db7 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Mon, 11 Jul 2016 21:50:04 +0200 Subject: [PATCH] add show --- Makefile | 3 ++- blaze822.c | 16 ++++++++++++ blaze822.h | 1 + show.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 show.c diff --git a/Makefile b/Makefile index 2d66f29..b80b616 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ CFLAGS=-g -O1 -Wall -Wno-switch -Wextra -Wwrite-strings -fstack-protector-strong -D_FORTIFY_SOURCE=2 -ALL = scan thread hdr +ALL = scan thread hdr show all: $(ALL) scan: blaze822.o scan.o fmt_rfc2047.o thread: blaze822.o thread.o hdr: blaze822.o hdr.o +show: blaze822.o show.o clean: FRC -rm -f $(ALL) *.o diff --git a/blaze822.c b/blaze822.c index f55c00d..3df50a8 100644 --- a/blaze822.c +++ b/blaze822.c @@ -376,3 +376,19 @@ blaze822_loop(int argc, char *argv[], void (*cb)(char *)) return i; } + +int +blaze822_body(struct message *mesg, char *file) +{ + int fd = open(file, O_RDONLY); + if (fd < 0) + return fd; + + if (lseek(fd, mesg->end - mesg->msg, SEEK_SET) < 0) { + perror("lseek"); + close(fd); + return -1; + } + + return fd; +} diff --git a/blaze822.h b/blaze822.h index 15de6f5..5c77de0 100644 --- a/blaze822.h +++ b/blaze822.h @@ -6,6 +6,7 @@ struct message *blaze822(char *file); void blaze822_free(struct message *mesg); char *blaze822_hdr_(struct message *mesg, const char *hdr, size_t len); #define blaze822_hdr(mesg, hdr) blaze822_hdr_(mesg, "\0" hdr ":", 2+strlen((hdr))) +int blaze822_body(struct message *mesg, char *file); int blaze822_loop(int, char **, void (*)(char *)); diff --git a/show.c b/show.c new file mode 100644 index 0000000..aa7a29c --- /dev/null +++ b/show.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "blaze822.h" + +void +printhdr(char *hdr) +{ + int uc = 1; + + while (*hdr) { + putc(uc ? toupper(*hdr) : *hdr, stdout); + uc = (*hdr == '-'); + hdr++; + } + putc(' ', stdout); +} + +void +show(char *file) +{ + struct message *msg; + + msg = blaze822(file); + if (!msg) + return; + + char fields[] = "\0from:\0subject:\0to:\0cc:\0date:\0"; + + // XXX custom field formatting + + char *f, *v; + for (f = fields; f < fields + sizeof fields; f += strlen(f+1)+1) { + v = blaze822_hdr_(msg, f, strlen(f+1)+1); + if (v) { + printhdr(f+1); + printf("%s\n", v); + } + } + + int fd = blaze822_body(msg, file); + if (fd < 0) + return; + + printf("\n"); + fflush(stdout); + + char buf[2048]; + ssize_t rd; + // XXX skip initial lf here, convert crlf + // XXX do mime... + while ((rd = read(fd, buf, sizeof buf))) + write(1, buf, rd); +} + +int +main(int argc, char *argv[]) +{ + int i = blaze822_loop(argc-1, argv+1, show); + + return 0; +}