scribo

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

commit f7d22671f042f57bfe07d2ee31686d2efce8b2fa
parent 912d1da6b656d8907c7ff2478d3c1f5aab25d1ec
Author: Willy Goiffon <dev@z3bra.org>
Date:   Tue,  8 Sep 2020 11:55:17 +0200

Write phlog index from directory listing

Diffstat:
Mconfig.def.h | 4++++
Mscribo.c | 73++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,2 +1,6 @@ char *basedir = "."; char *datefmt = "%c"; + +char *phlogname = "z3bra's lair"; +char *host = "z3bra.org"; +int port = 70; diff --git a/scribo.c b/scribo.c @@ -1,4 +1,5 @@ #include <ctype.h> +#include <dirent.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> @@ -7,6 +8,7 @@ #include <unistd.h> #include <sys/queue.h> +#include <sys/types.h> #include "arg.h" #include "config.h" @@ -117,11 +119,13 @@ parseheaders(FILE *fp, struct headers *head) } int -writelog(FILE *in, FILE *out, struct headers *head) +writeentry(FILE *in, char *dir, struct headers *head) { + FILE *out; struct tm tm = {.tm_isdst = -1}; char stamp[BUFSIZ]; char *subject, *date, *from; + char entry[PATH_MAX]; ssize_t len; char buf[BUFSIZ]; @@ -130,6 +134,13 @@ writelog(FILE *in, FILE *out, struct headers *head) date = header(head, "Date"); from = header(head, "From"); + snprintf(entry, sizeof(entry), "%s/%s.txt", dir, sanitize(subject)); + out = fopen(entry, "w"); + if (!out) { + perror(entry); + return -1; + } + /* convert date to an appropriate format */ strptime(date, "%a, %d %b %Y %T %z", &tm); strftime(stamp, sizeof(stamp), datefmt, &tm); @@ -146,15 +157,41 @@ writelog(FILE *in, FILE *out, struct headers *head) } int +writeindex(FILE *out, char *dir) +{ + DIR *dirp; + struct dirent *d; + + dirp = opendir(dir); + if (!dirp) + return -1; + + fprintf(out, "[i|%69s|Err||]\n", phlogname); + fprintf(out, "[i|──────────────────────────────────────────────────────────────────────|Err||]\n"); + fprintf(out, "\n"); + fprintf(out, "\nlog entries:\n\n"); + + while ((d = readdir(dirp))) { + if (d->d_type != DT_REG || !strstr(d->d_name, ".txt")) + continue; + + fprintf(out, "[0|%s|%s|%s|%d]\n", d->d_name, d->d_name, host, port); + } + + closedir(dirp); + + return 0; +} + +int main(int argc, char *argv[]) { FILE *in = stdin, *out = stdout; char *argv0, *infile, *outfile; - char fn[PATH_MAX]; struct headers headers; infile = NULL; - outfile = NULL; + outfile = "index.gph"; ARGBEGIN { case 'b': @@ -174,25 +211,35 @@ main(int argc, char *argv[]) if (infile) in = fopen(infile, "r"); - if (!in) + if (!in) { + perror(infile); return -1; + } - if (chdir(basedir) < 0) + if (chdir(basedir) < 0) { + perror(basedir); return -1; + } + + if (outfile) + out = fopen(outfile, "w"); - if (parseheaders(in, &headers) < 0) + if (!out) { + perror(outfile); return -1; + } - if (!outfile) { - strlcpy(fn, sanitize(header(&headers, "Subject")), sizeof(fn)); - strlcat(fn, ".txt", sizeof(fn)); - outfile = fn; + if (parseheaders(in, &headers) < 0) { + perror("header section"); + return -1; } - if (outfile && strncmp(outfile, "-", PATH_MAX)) - out = fopen(outfile, "w"); + if (writeentry(in, basedir, &headers) < 0) { + perror("header section"); + return -1; + } - if (writelog(in, out, &headers) < 0) + if (writeindex(out, basedir) < 0) return -1; fclose(in);