tAdd function to easily retrieve headers - scribo - Email-based phlog generator
 (HTM) git clone git://git.z3bra.org/scribo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a100614d250ffdfd0c48a92ee2ca35ccb1f1b3dc
 (DIR) parent 384fc09310f57cddf07cdeaa7759963847d0c9bb
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon,  7 Sep 2020 18:37:16 +0200
       
       Add function to easily retrieve headers
       
       Diffstat:
         M config.def.h                        |       1 +
         M rfc5322.c                           |      14 --------------
         M scribo.c                            |      28 ++++++++++++++++++++++++++++
       
       3 files changed, 29 insertions(+), 14 deletions(-)
       ---
 (DIR) diff --git a/config.def.h b/config.def.h
       t@@ -1 +1,2 @@
        char *basedir = ".";
       +char *datefmt = "%c";
 (DIR) diff --git a/rfc5322.c b/rfc5322.c
       t@@ -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);
       -}
 (DIR) diff --git a/scribo.c b/scribo.c
       t@@ -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)
        {
       t@@ -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;
        }