summary refs log tree commit diff
path: root/export.c
diff options
context:
space:
mode:
Diffstat (limited to 'export.c')
-rw-r--r--export.c51
1 files changed, 51 insertions, 0 deletions
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, &section, 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(