summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-19 11:27:26 -0400
committerJune McEnroe <june@causal.agency>2020-04-20 11:17:56 -0400
commit4d35c86401b4a947689eac375ec4ba29b3c99d6d (patch)
tree70b71841a1114c88f4d4c0bea0e645ff3b301ca7 /concat.c
parentSkip leading whitespace in message IDs (diff)
downloadbubger-4d35c86401b4a947689eac375ec4ba29b3c99d6d.tar.gz
bubger-4d35c86401b4a947689eac375ec4ba29b3c99d6d.zip
Use template system for paths and URLs
This probably still needs a lot of cleaning up.
Diffstat (limited to '')
-rw-r--r--concat.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/concat.c b/concat.c
index 94f9bed..a4bf1aa 100644
--- a/concat.c
+++ b/concat.c
@@ -52,10 +52,23 @@ 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) {
+	char str[32];
+	snprintf(str, sizeof(str), "%" PRIu32, uid);
+	struct Variable vars[] = {
+		{ "uid", str },
+		{ "type", type },
+		{0},
+	};
+	static char buf[PATH_MAX + 1];
+	templateBuffer(buf, sizeof(buf), PATH_UID, vars, escapePath);
+	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) {
-		const char *path = pathUID(dataCheck(uids.ptr[i], Number).number, type);
+		const char *path = uidPath(dataCheck(uids.ptr[i], Number).number, type);
 		struct stat status;
 		int error = stat(path, &status);
 		if (error) err(EX_DATAERR, "%s", path);
@@ -86,13 +99,24 @@ static int concatHTML(FILE *file, struct List thread) {
 				|| htmlSubthreadClose(file);
 		} else {
 			uint32_t uid = dataCheck(thread.ptr[i], Number).number;
-			error = concatFile(file, pathUID(uid, "html"));
+			error = concatFile(file, uidPath(uid, "html"));
 		}
 		if (error) return error;
 	}
 	return 0;
 }
 
+static const char *threadPath(const char *messageID, const char *type) {
+	static char buf[PATH_MAX + 1];
+	struct Variable vars[] = {
+		{ "messageID", messageID },
+		{ "type", type },
+		{0},
+	};
+	templateBuffer(buf, sizeof(buf), PATH_THREAD, vars, escapePath);
+	return buf;
+}
+
 const char *concatHead;
 
 void concatData(struct List threads, struct List items) {
@@ -121,7 +145,7 @@ void concatData(struct List threads, struct List items) {
 	const char *path;
 	struct stat status;
 
-	path = pathThread(envelope.messageID, "mbox");
+	path = threadPath(envelope.messageID, "mbox");
 	error = stat(path, &status);
 	if (error || status.st_mtime < uidNewest(flat, "mbox")) {
 		file = fopen(path, "w");
@@ -129,7 +153,7 @@ void concatData(struct List threads, struct List items) {
 
 		for (size_t i = 0; i < flat.len; ++i) {
 			uint32_t uid = dataCheck(flat.ptr[i], Number).number;
-			error = concatFile(file, pathUID(uid, "mbox"));
+			error = concatFile(file, uidPath(uid, "mbox"));
 			if (error) err(EX_IOERR, "%s", path);
 		}
 
@@ -137,7 +161,7 @@ void concatData(struct List threads, struct List items) {
 		if (error) err(EX_IOERR, "%s", path);
 	}
 
-	path = pathThread(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");
@@ -148,7 +172,7 @@ void concatData(struct List threads, struct List items) {
 
 		for (size_t i = 0; i < flat.len; ++i) {
 			uint32_t uid = dataCheck(flat.ptr[i], Number).number;
-			error = concatFile(file, pathUID(uid, "atom"));
+			error = concatFile(file, uidPath(uid, "atom"));
 			if (error) err(EX_IOERR, "%s", path);
 		}
 
@@ -156,7 +180,7 @@ void concatData(struct List threads, struct List items) {
 		if (error) err(EX_IOERR, "%s", path);
 	}
 
-	path = pathThread(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");