summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--concat.c67
1 files changed, 50 insertions, 17 deletions
diff --git a/concat.c b/concat.c
index a2b767a..6d0e507 100644
--- a/concat.c
+++ b/concat.c
@@ -77,6 +77,27 @@ static int concatFile(FILE *dst, const char *path) {
 	return 0;
 }
 
+static int concatHTML(FILE *file, struct List thread) {
+	static char path[PATH_MAX];
+	int error;
+	for (size_t i = 0; i < thread.len; ++i) {
+		error = htmlThreadOpen(file);
+		if (error) return error;
+		if (thread.ptr[i].type == List) {
+			error = concatHTML(file, thread.ptr[i].list);
+		} else {
+			uint32_t uid = dataCheck(thread.ptr[i], Number).number;
+			error = concatFile(file, pathUID(path, uid, "html"));
+		}
+		if (error) return error;
+	}
+	for (size_t i = 0; i < thread.len; ++i) {
+		error = htmlThreadClose(file);
+		if (error) return error;
+	}
+	return 0;
+}
+
 void concatData(struct List threads, struct List items) {
 	uint32_t uid = 0;
 	struct Envelope envelope = {0};
@@ -99,43 +120,55 @@ void concatData(struct List threads, struct List items) {
 	listFlatten(&flat, thread);
 
 	int error;
-	struct stat file;
+	FILE *file;
+	struct stat status;
 	char dst[PATH_MAX];
 	char src[PATH_MAX];
 
-	pathThread(dst, envelope.messageID, "mbox");
-	error = stat(dst, &file);
-	if (error || file.st_mtime < uidNewest(flat, "mbox")) {
-		FILE *mbox = fopen(dst, "w");
-		if (!mbox) err(EX_CANTCREAT, "%s", dst);
+	error = stat(pathThread(dst, envelope.messageID, "mbox"), &status);
+	if (error || status.st_mtime < uidNewest(flat, "mbox")) {
+		file = fopen(dst, "w");
+		if (!file) err(EX_CANTCREAT, "%s", dst);
 
 		for (size_t i = 0; i < flat.len; ++i) {
 			uint32_t uid = dataCheck(flat.ptr[i], Number).number;
-			error = concatFile(mbox, pathUID(src, uid, "mbox"));
+			error = concatFile(file, pathUID(src, uid, "mbox"));
 			if (error) err(EX_IOERR, "%s", dst);
 		}
 
-		error = fclose(mbox);
+		error = fclose(file);
 		if (error) err(EX_IOERR, "%s", dst);
 	}
 
-	pathThread(dst, envelope.messageID, "atom");
-	error = stat(dst, &file);
-	if (error || file.st_mtime < uidNewest(flat, "atom")) {
-		FILE *atom = fopen(dst, "w");
-		if (!atom) err(EX_CANTCREAT, "%s", dst);
+	error = stat(pathThread(dst, envelope.messageID, "atom"), &status);
+	if (error || status.st_mtime < uidNewest(flat, "atom")) {
+		FILE *file = fopen(dst, "w");
+		if (!file) err(EX_CANTCREAT, "%s", dst);
 
-		error = atomFeedHead(atom, &envelope);
+		error = atomFeedHead(file, &envelope);
 		if (error) err(EX_IOERR, "%s", dst);
 
 		for (size_t i = 0; i < flat.len; ++i) {
 			uint32_t uid = dataCheck(flat.ptr[i], Number).number;
-			error = concatFile(atom, pathUID(src, uid, "atom"));
+			error = concatFile(file, pathUID(src, uid, "atom"));
 			if (error) err(EX_IOERR, "%s", dst);
 		}
 
-		error = atomFeedTail(atom)
-			|| fclose(atom);
+		error = atomFeedTail(file) || fclose(file);
+		if (error) err(EX_IOERR, "%s", dst);
+	}
+
+	error = stat(pathThread(dst, envelope.messageID, "html"), &status);
+	if (error || status.st_mtime < uidNewest(flat, "html")) {
+		FILE *file = fopen(dst, "w");
+		if (!file) err(EX_CANTCREAT, "%s", dst);
+
+		error = 0
+			|| htmlThreadHead(file, &envelope) // TODO: Include -h file.
+			|| htmlThreadHeader(file, &envelope)
+			|| concatHTML(file, thread)
+			|| htmlThreadTail(file)
+			|| fclose(file);
 		if (error) err(EX_IOERR, "%s", dst);
 	}