about summary refs log tree commit diff
path: root/export.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--export.c63
1 files changed, 16 insertions, 47 deletions
diff --git a/export.c b/export.c
index c8d742f..6e4c7fd 100644
--- a/export.c
+++ b/export.c
@@ -54,17 +54,12 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
 	struct List uids = {0};
 	flatten(&uids, threads);
 	for (size_t i = uids.len - 1; i < uids.len; --i) {
-		if (uids.ptr[i].type != Number) {
-			errx(EX_PROTOCOL, "invalid thread UID");
-		}
-		uint32_t uid = uids.ptr[i].number;
-		int error = 0;
-		if (!error) error = access(uidPath(uid, "atom"), F_OK);
-		if (!error) error = access(uidPath(uid, "html"), F_OK);
-		if (!error) error = access(uidPath(uid, "mbox"), F_OK);
-		if (!error) {
-			uids.ptr[i] = uids.ptr[--uids.len];
-		}
+		uint32_t uid = dataCheck(uids.ptr[i], Number).number;
+		int error = 0
+			|| access(uidPath(uid, "atom"), F_OK)
+			|| access(uidPath(uid, "html"), F_OK)
+			|| access(uidPath(uid, "mbox"), F_OK);
+		if (!error) uids.ptr[i] = uids.ptr[--uids.len];
 	}
 	if (!uids.len) {
 		listFree(uids);
@@ -77,10 +72,8 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
 	}
 	fprintf(
 		imap,
-		" ("
-		"UID ENVELOPE "
-		"BODY[HEADER.FIELDS (" MBOX_HEADERS ")] BODY[TEXT]"
-		")\r\n"
+		" (UID ENVELOPE"
+		" BODY[HEADER.FIELDS (" MBOX_HEADERS ")] BODY[TEXT])\r\n"
 	);
 	return true;
 }
@@ -104,10 +97,7 @@ static struct AddressList parseAddressList(struct List list) {
 	struct Address *addrs = calloc(list.len, sizeof(*addrs));
 	if (!addrs) err(EX_OSERR, "calloc");
 	for (size_t i = 0; i < list.len; ++i) {
-		if (list.ptr[i].type != List) {
-			errx(EX_PROTOCOL, "invalid address field");
-		}
-		addrs[i] = parseAddress(list.ptr[i].list);
+		addrs[i] = parseAddress(dataCheck(list.ptr[i], List).list);
 	}
 	return (struct AddressList) { list.len, addrs };
 }
@@ -132,21 +122,15 @@ static struct Envelope parseEnvelope(struct List list) {
 	}
 	struct Envelope envelope = {0};
 
-	if (list.ptr[Date].type != String) {
-		errx(EX_PROTOCOL, "invalid envelope date field");
-	}
-	const char *date = list.ptr[Date].string;
+	const char *date = dataCheck(list.ptr[Date], String).string;
 	date = strptime(date, "%a, %e %b %Y %H:%M:%S %z", &envelope.date);
 	if (!date) errx(EX_PROTOCOL, "invalid envelope date format");
 
 	envelope.date.tm_isdst = -1;
 	envelope.utc = mktime(&envelope.date);
 
-	if (list.ptr[Subject].type != String) {
-		errx(EX_PROTOCOL, "invalid envelope subject field");
-	}
 	// TODO: Decode UTF-8 in subject.
-	envelope.subject = strdup(list.ptr[Subject].string);
+	envelope.subject = strdup(dataCheck(list.ptr[Subject], String).string);
 	if (!envelope.subject) err(EX_OSERR, "strdup");
 
 	for (size_t i = From; i <= Bcc; ++i) {
@@ -173,10 +157,7 @@ static struct Envelope parseEnvelope(struct List list) {
 	if (list.ptr[InReplyTo].type == String) {
 		envelope.inReplyTo = parseID(list.ptr[InReplyTo].string);
 	}
-	if (list.ptr[MessageID].type != String) {
-		errx(EX_PROTOCOL, "invalid envelope message-id field");
-	}
-	envelope.messageID = parseID(list.ptr[MessageID].string);
+	envelope.messageID = parseID(dataCheck(list.ptr[MessageID], String).string);
 
 	return envelope;
 }
@@ -217,25 +198,13 @@ void exportData(struct List items) {
 			i--;
 			continue;
 		} else if (name == AtomUID) {
-			if (items.ptr[i + 1].type != Number) {
-				errx(EX_PROTOCOL, "invalid UID data item value");
-			}
-			uid = items.ptr[i + 1].number;
+			uid = dataCheck(items.ptr[i + 1], Number).number;
 		} else if (name == AtomEnvelope) {
-			if (items.ptr[i + 1].type != List) {
-				errx(EX_PROTOCOL, "invalid ENVELOPE data item value");
-			}
-			envelope = parseEnvelope(items.ptr[i + 1].list);
+			envelope = parseEnvelope(dataCheck(items.ptr[i + 1], List).list);
 		} else if (name == AtomHeaderFields) {
-			if (items.ptr[i + 1].type != String) {
-				errx(EX_PROTOCOL, "invalid BODY[HEADER.FIELDS] data item value");
-			}
-			header = items.ptr[i + 1].string;
+			header = dataCheck(items.ptr[i + 1], String).string;
 		} else if (name == AtomText) {
-			if (items.ptr[i + 1].type != String) {
-				errx(EX_PROTOCOL, "invalid BODY[TEXT] data item value");
-			}
-			body = items.ptr[i + 1].string;
+			body = dataCheck(items.ptr[i + 1], String).string;
 		}
 	}