summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
Diffstat (limited to 'concat.c')
-rw-r--r--concat.c76
1 files changed, 43 insertions, 33 deletions
diff --git a/concat.c b/concat.c
index 22645e4..e75c857 100644
--- a/concat.c
+++ b/concat.c
@@ -35,14 +35,6 @@ static uint32_t threadRoot(struct List thread) {
 	return dataCheck(thread.ptr[0], Number).number;
 }
 
-static struct List threadFind(struct List threads, uint32_t root) {
-	for (size_t i = 0; i < threads.len; ++i) {
-		struct List thread = dataCheck(threads.ptr[i], List).list;
-		if (threadRoot(thread) == root) return thread;
-	}
-	errx(EX_TEMPFAIL, "no thread with root UID %" PRIu32, root);
-}
-
 void concatFetch(FILE *imap, enum Atom tag, struct List threads) {
 	fprintf(imap, "%s UID FETCH ", Atoms[tag]);
 	for (size_t i = 0; i < threads.len; ++i) {
@@ -52,6 +44,33 @@ void concatFetch(FILE *imap, enum Atom tag, struct List threads) {
 	fprintf(imap, " (UID ENVELOPE)\r\n");
 }
 
+void concatData(
+	struct List threads, struct Envelope *envelopes, struct List items
+) {
+	uint32_t uid = 0;
+	struct List envelope = {0};
+	for (size_t i = 0; i + 1 < items.len; i += 2) {
+		enum Atom name = dataCheck(items.ptr[i], Atom).atom;
+		struct Data data = items.ptr[i + 1];
+		switch (name) {
+			break; case AtomUID: uid = dataCheck(data, Number).number;
+			break; case AtomEnvelope: envelope = dataCheck(data, List).list;
+			break; default:;
+		}
+	}
+	if (!uid) errx(EX_PROTOCOL, "missing UID data item");
+	if (!envelope.len) errx(EX_PROTOCOL, "missing ENVELOPE data item");
+
+	for (size_t i = 0; i < threads.len; ++i) {
+		struct List thread = dataCheck(threads.ptr[i], List).list;
+		if (threadRoot(thread) == uid) {
+			parseEnvelope(&envelopes[i], envelope);
+			return;
+		}
+	}
+	errx(EX_TEMPFAIL, "no thread with root UID %" PRIu32, uid);
+}
+
 static const char *uidPath(uint32_t uid, const char *type) {
 	char str[32];
 	snprintf(str, sizeof(str), "%" PRIu32, uid);
@@ -119,24 +138,7 @@ static const char *threadPath(const char *messageID, const char *type) {
 
 const char *concatHead;
 
-void concatData(struct List threads, struct List items) {
-	uint32_t uid = 0;
-	struct Envelope envelope = {0};
-	for (size_t i = 0; i + 1 < items.len; i += 2) {
-		enum Atom name = dataCheck(items.ptr[i], Atom).atom;
-		struct Data data = items.ptr[i + 1];
-		switch (name) {
-			break; case AtomUID:
-				uid = dataCheck(data, Number).number;
-			break; case AtomEnvelope:
-				parseEnvelope(&envelope, dataCheck(data, List).list);
-			break; default:;
-		}
-	}
-	if (!uid) errx(EX_PROTOCOL, "missing UID data item");
-	if (!envelope.subject) errx(EX_PROTOCOL, "missing ENVELOPE data item");
-
-	struct List thread = threadFind(threads, uid);
+static void concatThread(struct List thread, const struct Envelope *envelope) {
 	struct List flat = {0};
 	listFlatten(&flat, thread);
 
@@ -145,7 +147,7 @@ void concatData(struct List threads, struct List items) {
 	const char *path;
 	struct stat status;
 
-	path = threadPath(envelope.messageID, "mbox");
+	path = threadPath(envelope->messageID, "mbox");
 	error = stat(path, &status);
 	if (error || status.st_mtime < uidNewest(flat, "mbox")) {
 		file = fopen(path, "w");
@@ -161,13 +163,13 @@ void concatData(struct List threads, struct List items) {
 		if (error) err(EX_IOERR, "%s", path);
 	}
 
-	path = threadPath(envelope.messageID, "atom");
+	path = threadPath(envelope->messageID, "atom");
 	error = stat(path, &status);
 	if (error || status.st_mtime < uidNewest(flat, "atom")) {
 		FILE *file = fopen(path, "w");
 		if (!file) err(EX_CANTCREAT, "%s", path);
 
-		error = atomFeedOpen(file, &envelope);
+		error = atomFeedOpen(file, envelope);
 		if (error) err(EX_IOERR, "%s", path);
 
 		for (size_t i = 0; i < flat.len; ++i) {
@@ -180,13 +182,13 @@ void concatData(struct List threads, struct List items) {
 		if (error) err(EX_IOERR, "%s", path);
 	}
 
-	path = threadPath(envelope.messageID, "html");
+	path = threadPath(envelope->messageID, "html");
 	error = stat(path, &status);
 	if (error || status.st_mtime < uidNewest(flat, "html")) {
 		FILE *file = fopen(path, "w");
 		if (!file) err(EX_CANTCREAT, "%s", path);
 
-		error = htmlThreadHead(file, &envelope);
+		error = htmlThreadHead(file, envelope);
 		if (error) err(EX_IOERR, "%s", path);
 
 		if (concatHead) {
@@ -195,7 +197,7 @@ void concatData(struct List threads, struct List items) {
 		}
 
 		error = 0
-			|| htmlThreadOpen(file, &envelope)
+			|| htmlThreadOpen(file, envelope)
 			|| concatHTML(file, thread)
 			|| htmlThreadClose(file)
 			|| fclose(file);
@@ -203,5 +205,13 @@ void concatData(struct List threads, struct List items) {
 	}
 
 	listFree(flat);
-	envelopeFree(envelope);
+}
+
+void concatThreads(struct List threads, const struct Envelope *envelopes) {
+	for (size_t i = 0; i < threads.len; ++i) {
+		concatThread(dataCheck(threads.ptr[i], List).list, &envelopes[i]);
+	}
+}
+
+void concatIndex(struct List threads, const struct Envelope *envelopes) {
 }