about summary refs log tree commit diff
path: root/export.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-13 14:58:53 -0400
committerJune McEnroe <june@causal.agency>2020-04-13 14:58:53 -0400
commit9032566beb37bc907ed21cf0446ee427a82bdcb0 (patch)
treeea42d3f62f344f696551cee811028748422116b5 /export.c
parentDo not escape > in XML/HTML (diff)
downloadbubger-9032566beb37bc907ed21cf0446ee427a82bdcb0.tar.gz
bubger-9032566beb37bc907ed21cf0446ee427a82bdcb0.zip
Export content to Atom
Temporarily disabling HTML output...
Diffstat (limited to 'export.c')
-rw-r--r--export.c64
1 files changed, 28 insertions, 36 deletions
diff --git a/export.c b/export.c
index 6327d22..54178f6 100644
--- a/export.c
+++ b/export.c
@@ -53,31 +53,7 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
 	return true;
 }
 
-static void exportEnvelope(uint32_t uid, const struct Envelope *envelope) {
-	int error;
-	FILE *file;
-	const char *path;
-
-	path = pathUID(uid, "html");
-	file = fopen(path, "w");
-	if (!file) err(EX_CANTCREAT, "%s", path);
-	error = 0
-		|| htmlMessageHead(file, envelope)
-		|| htmlMessageTail(file)
-		|| fclose(file);
-	if (error) err(EX_IOERR, "%s", path);
-
-	path = pathUID(uid, "atom");
-	file = fopen(path, "w");
-	if (!file) err(EX_CANTCREAT, "%s", path);
-	error = 0
-		|| atomEntryOpen(file, envelope)
-		|| atomEntryClose(file)
-		|| fclose(file);
-	if (error) err(EX_IOERR, "%s", path);
-}
-
-static void exportRaw(
+static void exportMbox(
 	uint32_t uid, const struct Envelope *envelope,
 	const char *header, const char *body
 ) {
@@ -97,10 +73,32 @@ static void exportRaw(
 	if (error) err(EX_CANTCREAT, "%s", msg);
 }
 
-static void exportBodyPart(
-	uint32_t uid, const struct BodyPart *structure,
-	const struct BodyPart *part, const char *content
+static void exportAtom(
+	uint32_t uid, const struct Envelope *envelope,
+	const struct BodyPart *structure, const char *body
 ) {
+	const char *path = pathUID(uid, "atom");
+	FILE *file = fopen(path, "w");
+	if (!file) err(EX_CANTCREAT, "%s", path);
+
+	int error = atomEntryOpen(file, envelope);
+	if (error) err(EX_IOERR, "%s", path);
+
+	if (
+		!structure->multipart &&
+		!strcmp(structure->type, "TEXT") &&
+		!strcmp(structure->subtype, "PLAIN")
+	) {
+		// TODO: Decode content into file.
+		error = 0
+			|| atomContentOpen(file)
+			|| escapeXML(file, body)
+			|| atomContentClose(file);
+		if (error) err(EX_IOERR, "%s", path);
+	}
+
+	error = atomEntryClose(file) || fclose(file);
+	if (error) err(EX_IOERR, "%s", path);
 }
 
 bool exportData(FILE *imap, enum Atom tag, struct List items) {
@@ -147,14 +145,8 @@ bool exportData(FILE *imap, enum Atom tag, struct List items) {
 	if (!header) errx(EX_PROTOCOL, "missing BODY[HEADER.FIELDS] data item");
 	if (!body) errx(EX_PROTOCOL, "missing BODY[TEXT] data item");
 
-	exportRaw(uid, &envelope, header, body);
-	exportEnvelope(uid, &envelope);
-
-	if (structure.multipart) {
-		// TODO: FETCH each body part.
-	} else {
-		exportBodyPart(uid, &structure, &structure, body);
-	}
+	exportMbox(uid, &envelope, header, body);
+	exportAtom(uid, &envelope, &structure, body);
 
 	envelopeFree(envelope);
 	bodyPartFree(structure);