From d72dcfd4665f937eec2d325c10d35a644e9c06ad Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Wed, 15 Apr 2020 19:06:42 -0400 Subject: Rewrite HTML rendering --- export.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'export.c') diff --git a/export.c b/export.c index fe166f8..3c6a31f 100644 --- a/export.c +++ b/export.c @@ -111,6 +111,55 @@ static void exportAtom( if (error) err(EX_IOERR, "%s", path); } +static int exportHTMLBody( + FILE *file, struct List *section, + const struct BodyPart *structure, struct Data body +) { + int error; + if (structure->multipart) { + // TODO: Choose a part from multipart/alternative. + for (size_t i = 0; i < structure->parts.len; ++i) { + struct Data part = { .type = Number, .number = 1 + i }; + listPush(section, part); + error = exportHTMLBody( + file, section, + &structure->parts.ptr[i], dataCheck(body, List).list.ptr[i] + ); + if (error) return error; + section->len--; + } + } else if (structure->message.envelope) { + error = 0 + || htmlMessageOpen(file, structure->message.envelope) + || exportHTMLBody(file, section, structure->message.structure, body) + || htmlMessageClose(file); + } else { + // TODO: Content. + error = 0; + } + return error; +} + +static void exportHTML( + uint32_t uid, const struct Envelope *envelope, + const struct BodyPart *structure, struct Data body +) { + const char *path = pathUID(uid, "html"); + FILE *file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); + + int error = htmlMessageOpen(file, envelope); + if (error) err(EX_IOERR, "%s", path); + + struct List section = {0}; + error = exportHTMLBody(file, §ion, structure, body); + if (error) err(EX_IOERR, "%s", path); + listFree(section); + + error = htmlMessageClose(file) || fclose(file); + if (error) err(EX_IOERR, "%s", path); +} + static void fetchParts( FILE *imap, struct List *section, const struct BodyPart *structure ) { @@ -232,8 +281,10 @@ bool exportData(FILE *imap, enum Atom tag, struct List items) { bool fetch = false; if (!structure.multipart) { exportAtom(uid, &envelope, &structure, bodyText); + exportHTML(uid, &envelope, &structure, bodyText); } else if (bodyParts.type == List) { exportAtom(uid, &envelope, &structure, bodyParts); + exportHTML(uid, &envelope, &structure, bodyParts); } else { fetch = true; fprintf( -- cgit 1.4.1