about summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-10 16:44:04 -0400
committerJune McEnroe <june@causal.agency>2020-04-10 16:44:04 -0400
commit4e0840f8485a6f52ecd316159e22429726b90725 (patch)
tree1b025cdcc2980f2b4fa26634a69ae6b06a50e84f /concat.c
parentParse envelope and find thread in concatData (diff)
downloadbubger-4e0840f8485a6f52ecd316159e22429726b90725.tar.gz
bubger-4e0840f8485a6f52ecd316159e22429726b90725.zip
Concatenate mbox threads
Diffstat (limited to 'concat.c')
-rw-r--r--concat.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/concat.c b/concat.c
index 26f24f6..2c58d8a 100644
--- a/concat.c
+++ b/concat.c
@@ -16,9 +16,11 @@
 
 #include <err.h>
 #include <inttypes.h>
+#include <limits.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #include <sysexits.h>
 
 #include "archive.h"
@@ -50,6 +52,43 @@ void concatFetch(FILE *imap, enum Atom tag, struct List threads) {
 	fprintf(imap, " (UID ENVELOPE)\r\n");
 }
 
+static const char *uidPath(uint32_t uid, const char *type) {
+	static char buf[PATH_MAX];
+	snprintf(buf, sizeof(buf), "UID/%" PRIu32 ".%s", uid, type);
+	return buf;
+}
+
+static const char *threadPath(const char *messageID, const char *type) {
+	static char buf[PATH_MAX];
+	snprintf(buf, sizeof(buf), "thread/%s.%s", messageID, type);
+	return buf;
+}
+
+static time_t uidNewest(struct List uids, const char *type) {
+	time_t newest = 0;
+	for (size_t i = 0; i < uids.len; ++i) {
+		uint32_t uid = dataCheck(uids.ptr[i], Number).number;
+		const char *path = uidPath(uid, type);
+		struct stat file;
+		int error = stat(path, &file);
+		if (error) err(EX_DATAERR, "%s", path);
+		if (file.st_mtime > newest) newest = file.st_mtime;
+	}
+	return newest;
+}
+
+static int concatFile(FILE *dst, const char *path) {
+	char buf[4096];
+	FILE *src = fopen(path, "r");
+	if (!src) err(EX_DATAERR, "%s", path);
+	for (size_t len; (len = fread(buf, 1, sizeof(buf), src));) {
+		size_t n = fwrite(buf, len, 1, dst);
+		if (!n) return -1;
+	}
+	fclose(src);
+	return 0;
+}
+
 void concatData(struct List threads, struct List items) {
 	uint32_t uid = 0;
 	struct Envelope envelope = {0};
@@ -68,4 +107,26 @@ void concatData(struct List threads, struct List items) {
 	if (!envelope.subject) errx(EX_PROTOCOL, "missing ENVELOPE data item");
 
 	struct List thread = threadFind(threads, uid);
+	struct List flat = {0};
+	listFlatten(&flat, thread);
+
+	int error;
+	const char *path;
+	struct stat file;
+
+	path = threadPath(envelope.messageID, "mbox");
+	error = stat(path, &file);
+	if (error || file.st_mtime < uidNewest(flat, "mbox")) {
+		FILE *mbox = fopen(path, "w");
+		if (!mbox) err(EX_CANTCREAT, "%s", path);
+		for (size_t i = 0; i < flat.len; ++i) {
+			uint32_t uid = dataCheck(flat.ptr[i], Number).number;
+			error = concatFile(mbox, uidPath(uid, "mbox"));
+			if (error) err(EX_IOERR, "%s", path);
+		}
+		error = fclose(mbox);
+		if (error) err(EX_IOERR, "%s", path);
+	}
+
+	listFree(flat);
 }