summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--archive.h7
-rw-r--r--decode.c39
-rw-r--r--export.c3
-rw-r--r--parse.c10
-rw-r--r--template.c2
6 files changed, 53 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 8e63376..935f25e 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@ LDLIBS = -ltls
 OBJS += archive.o
 OBJS += atom.o
 OBJS += concat.o
+OBJS += decode.o
 OBJS += export.o
 OBJS += html.o
 OBJS += imap.o
diff --git a/archive.h b/archive.h
index f0572b7..911b7e4 100644
--- a/archive.h
+++ b/archive.h
@@ -134,6 +134,7 @@ struct Variable {
 
 typedef int EscapeFn(FILE *file, const char *str);
 
+int escapeNull(FILE *file, const char *str);
 int escapeURL(FILE *file, const char *str);
 int escapeXML(FILE *file, const char *str);
 
@@ -143,6 +144,12 @@ int templateRender(
 );
 char *templateURL(const char *template, const struct Variable vars[]);
 
+char *decodeHeader(const char *header);
+int decodeContent(
+	FILE *file, EscapeFn *escape,
+	const struct BodyPart *part, const char *content
+);
+
 static inline const char *pathUID(uint32_t uid, const char *type) {
 	static char buf[PATH_MAX];
 	snprintf(buf, sizeof(buf), "UID/%" PRIu32 ".%s", uid, type);
diff --git a/decode.c b/decode.c
new file mode 100644
index 0000000..2e289fc
--- /dev/null
+++ b/decode.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+
+#include "archive.h"
+
+char *decodeHeader(const char *header) {
+	// TODO
+	char *dup = strdup(header);
+	if (!dup) err(EX_OSERR, "strdup");
+	return dup;
+}
+
+int decodeContent(
+	FILE *file, EscapeFn *escape,
+	const struct BodyPart *part, const char *content
+) {
+	if (!escape) escape = escapeNull;
+	// TODO
+	return escape(file, content);
+}
diff --git a/export.c b/export.c
index f703e20..b34111f 100644
--- a/export.c
+++ b/export.c
@@ -89,10 +89,9 @@ static void exportAtom(
 		!strcmp(structure->type, "TEXT") &&
 		!strcmp(structure->subtype, "PLAIN")
 	) {
-		// TODO: Decode content into file.
 		error = 0
 			|| atomContentOpen(file)
-			|| escapeXML(file, body)
+			|| decodeContent(file, escapeXML, structure, body)
 			|| atomContentClose(file);
 		if (error) err(EX_IOERR, "%s", path);
 	}
diff --git a/parse.c b/parse.c
index ff97789..d491e53 100644
--- a/parse.c
+++ b/parse.c
@@ -29,9 +29,7 @@ static struct Address parseAddress(struct List list) {
 	}
 	struct Address addr = {0};
 	if (list.ptr[0].type == String) {
-		// TODO: Decode UTF-8 in name.
-		addr.name = strdup(list.ptr[0].string);
-		if (!addr.name) err(EX_OSERR, "strdup");
+		addr.name = decodeHeader(list.ptr[0].string);
 	}
 	if (list.ptr[2].type == String) addr.mailbox = list.ptr[2].string;
 	if (list.ptr[3].type == String) addr.host = list.ptr[3].string;
@@ -83,9 +81,9 @@ void parseEnvelope(struct Envelope *envelope, struct List list) {
 	if (!date) errx(EX_PROTOCOL, "invalid envelope date format");
 	envelope->time = mktime(&time);
 
-	// TODO: Decode UTF-8 in subject.
-	envelope->subject = strdup(dataCheck(list.ptr[Subject], String).string);
-	if (!envelope->subject) err(EX_OSERR, "strdup");
+	envelope->subject = decodeHeader(
+		dataCheck(list.ptr[Subject], String).string
+	);
 
 	for (size_t i = From; i <= Bcc; ++i) {
 		if (list.ptr[i].type == List) continue;
diff --git a/template.c b/template.c
index 8653769..e88b50f 100644
--- a/template.c
+++ b/template.c
@@ -24,7 +24,7 @@
 
 #include "archive.h"
 
-static int escapeNull(FILE *file, const char *str) {
+int escapeNull(FILE *file, const char *str) {
 	size_t n = fwrite(str, strlen(str), 1, file);
 	return (n ? 0 : -1);
 }