scribo

Email-based phlog generator
git clone git://git.z3bra.org/scribo.git
Log | Files | Refs

commit a100614d250ffdfd0c48a92ee2ca35ccb1f1b3dc
parent 384fc09310f57cddf07cdeaa7759963847d0c9bb
Author: Willy Goiffon <dev@z3bra.org>
Date:   Mon,  7 Sep 2020 18:37:16 +0200

Add function to easily retrieve headers

Diffstat:
Mconfig.def.h | 1+
Mrfc5322.c | 14--------------
Mscribo.c | 28++++++++++++++++++++++++++++
3 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1 +1,2 @@ char *basedir = "."; +char *datefmt = "%c"; diff --git a/rfc5322.c b/rfc5322.c @@ -51,17 +51,3 @@ rfc5322_unfold(char *body, char *line, size_t n) return strlcat(body, strsep(&line, "\r\n"), n); } - - -time_t -rfc5322_date(const char *s) -{ - char *p; - struct tm tm = {.tm_isdst = -1}; - - p = strptime(s, "%a, %d %b %Y %T %z", &tm); - if (!p) - return -1; - - return mktime(&tm); -} diff --git a/scribo.c b/scribo.c @@ -31,6 +31,18 @@ usage(char *pgm) fprintf(stderr, "usage: %s [-b basedir] [-io file]", pgm); } +char * +header(struct headers *head, char *key) +{ + struct hdr *h; + SLIST_FOREACH(h, head, entries) { + if (!strncmp(h->name, key, 997)) + return h->body; + } + + return NULL; +} + struct hdr * saveheader(struct headers *head, char *line) { @@ -79,11 +91,27 @@ parseheaders(FILE *fp, struct headers *head) int writelog(FILE *in, FILE *out) { + struct tm tm = {.tm_isdst = -1}; + char stamp[BUFSIZ]; + char *subject, *date, *from; struct headers headers; if (parseheaders(in, &headers) < 0) return -1; + subject = header(&headers, "Subject"); + date = header(&headers, "Date"); + from = header(&headers, "From"); + + /* convert date to an appropriate format */ + strptime(date, "%a, %d %b %Y %T %z", &tm); + strftime(stamp, sizeof(stamp), datefmt, &tm); + + fprintf(out, "Title: %s\n", subject); + fprintf(out, "Date: %s\n", stamp); + fprintf(out, "Author: %s\n", from); + fprintf(out, "\n"); + return 0; }