summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--export.c56
1 files changed, 29 insertions, 27 deletions
diff --git a/export.c b/export.c
index 9cc4a37..411b70c 100644
--- a/export.c
+++ b/export.c
@@ -55,31 +55,12 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
 	return true;
 }
 
-static void exportEnvelope(
-	uint32_t uid, struct Envelope *envelope, char *header, char *body
-) {
+static void exportEnvelope(uint32_t uid, const struct Envelope *envelope) {
 	int error;
 	FILE *file;
 	char path[PATH_MAX];
 
-	pathUID(path, uid, "mbox");
-	file = fopen(path, "w");
-	if (!file) err(EX_CANTCREAT, "%s", path);
-	error = 0
-		|| mboxFrom(file)
-		|| mboxHeader(file, header)
-		|| mboxBody(file, body)
-		|| fclose(file);
-	if (error) err(EX_IOERR, "%s", path);
-
-	char dest[PATH_MAX];
-	pathMessage(dest, envelope->messageID, "mbox");
-	unlink(dest);
-	error = link(path, dest);
-	if (error) err(EX_CANTCREAT, "%s", dest);
-
-	pathUID(path, uid, "html");
-	file = fopen(path, "w");
+	file = fopen(pathUID(path, uid, "html"), "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 	error = 0
 		|| htmlMessageHead(file, envelope)
@@ -87,8 +68,7 @@ static void exportEnvelope(
 		|| fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
 
-	pathUID(path, uid, "atom");
-	file = fopen(path, "w");
+	file = fopen(pathUID(path, uid, "atom"), "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 	error = 0
 		|| atomEntryHead(file, envelope)
@@ -97,12 +77,33 @@ static void exportEnvelope(
 	if (error) err(EX_IOERR, "%s", path);
 }
 
+static void exportRaw(
+	uint32_t uid, const struct Envelope *envelope,
+	const char *header, const char *body
+) {
+	char src[PATH_MAX];
+	FILE *file = fopen(pathUID(src, uid, "mbox"), "w");
+	if (!file) err(EX_CANTCREAT, "%s", src);
+	int error = 0
+		|| mboxFrom(file)
+		|| mboxHeader(file, header)
+		|| mboxBody(file, body)
+		|| fclose(file);
+	if (error) err(EX_IOERR, "%s", src);
+
+	char dst[PATH_MAX];
+	pathMessage(dst, envelope->messageID, "mbox");
+	unlink(dst);
+	error = link(src, dst);
+	if (error) err(EX_CANTCREAT, "%s", dst);
+}
+
 bool exportData(FILE *imap, enum Atom tag, struct List items) {
 	uint32_t uid = 0;
 	struct Envelope envelope = {0};
 	struct BodyPart structure = {0};
-	char *header = NULL;
-	char *body = NULL;
+	const char *header = NULL;
+	const char *body = NULL;
 
 	for (size_t i = 0; i + 1 < items.len; i += 2) {
 		enum Atom name;
@@ -141,9 +142,10 @@ 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");
 
-	exportEnvelope(uid, &envelope, header, body);
+	exportRaw(uid, &envelope, header, body);
+	exportEnvelope(uid, &envelope);
+
 	envelopeFree(envelope);
 	bodyPartFree(structure);
-
 	return false;
 }