about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--concat.c6
-rw-r--r--export.c63
2 files changed, 18 insertions, 51 deletions
diff --git a/concat.c b/concat.c
index 3417e8a..9ad8537 100644
--- a/concat.c
+++ b/concat.c
@@ -30,15 +30,13 @@ static uint32_t threadRoot(struct List thread) {
 		thread = thread.ptr[0].list;
 		if (!thread.len) errx(EX_PROTOCOL, "empty subthread");
 	}
-	if (thread.ptr[0].type != Number) errx(EX_PROTOCOL, "invalid thread root");
-	return thread.ptr[0].number;
+	return dataCheck(thread.ptr[0], Number).number;
 }
 
 void concatFetch(FILE *imap, enum Atom tag, struct List threads) {
 	fprintf(imap, "%s UID FETCH ", Atoms[tag]);
 	for (size_t i = 0; i < threads.len; ++i) {
-		if (threads.ptr[i].type != List) errx(EX_PROTOCOL, "invalid thread");
-		uint32_t root = threadRoot(threads.ptr[i].list);
+		uint32_t root = threadRoot(dataCheck(threads.ptr[i], List).list);
 		fprintf(imap, "%s%" PRIu32, (i ? "," : ""), root);
 	}
 	fprintf(imap, " (UID ENVELOPE)\r\n");
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;
 		}
 	}