summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--imbox.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/imbox.c b/imbox.c
index 5ab23c1..0328c34 100644
--- a/imbox.c
+++ b/imbox.c
@@ -26,8 +26,6 @@
 #include <tls.h>
 #include <unistd.h>
 
-#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
-
 static void compile(regex_t *regex, const char *pattern) {
 	if (regex->re_nsub) return;
 	int error = regcomp(regex, pattern, REG_EXTENDED | REG_NEWLINE);
@@ -132,9 +130,10 @@ enum Atom {
 #define X(id, _) id,
 	ENUM_ATOM
 #undef X
+	AtomsLen,
 };
 
-static const char *Atoms[] = {
+static const char *Atoms[AtomsLen] = {
 #define X(id, str) [id] = str,
 	ENUM_ATOM
 #undef X
@@ -142,7 +141,7 @@ static const char *Atoms[] = {
 
 static enum Atom atom(const char *str) {
 	if (!str) return Unknown;
-	for (size_t i = 0; i < ARRAY_LEN(Atoms); ++i) {
+	for (enum Atom i = 0; i < AtomsLen; ++i) {
 		if (!strcmp(str, Atoms[i])) return i;
 	}
 	return Unknown;
@@ -224,20 +223,21 @@ int main(int argc, char *argv[]) {
 	char *line = NULL;
 	size_t cap = 0;
 	while (0 < getline(&line, &cap, imap)) {
-		if (strchr(line, '\r')) *strchr(line, '\r') = '\0';
+		char *cr = strchr(line, '\r');
+		if (cr) *cr = '\0';
 
 		char *rest = line;
 		enum Atom tag = atom(strsep(&rest, " "));
-		if (rest && isdigit(rest[0])) strsep(&rest, " ");
+		if (rest && isdigit(rest[0])) {
+			strsep(&rest, " ");
+		}
 		enum Atom resp = atom(strsep(&rest, " "));
 
-		if (resp == No || resp == Bad) {
+		if (resp == No || resp == Bad || resp == Bye) {
 			errx(
 				EX_CONFIG, "%s: %s %s",
 				Atoms[tag], Atoms[resp], (rest ? rest : "")
 			);
-		} else if (resp == Bye) {
-			errx(EX_UNAVAILABLE, "unexpected BYE: %s", (rest ? rest : ""));
 		}
 
 		switch (tag) {
@@ -262,15 +262,15 @@ int main(int argc, char *argv[]) {
 				for (char *ch = uids; *ch; ++ch) {
 					if (*ch == ' ') *ch = ',';
 				}
-				// FIXME: Grab Content-Encoding as well?
 				fprintf(
 					imap,
-					"%s UID FETCH %s ("
-					"BODY[HEADER.FIELDS (Date From Subject Message-Id)] "
-					"BODY[TEXT]"
-					")\r\n",
+					"%s UID FETCH %s (BODY[HEADER.FIELDS ("
+					"Date From Subject Message-Id Content-Transfer-Encoding"
+					")] BODY[TEXT])\r\n",
 					Atoms[Fetch], uids
 				);
+				free(uids);
+				uids = NULL;
 			}
 			break; case Fetch: {
 				fprintf(imap, "ayy LOGOUT\r\n");
@@ -282,7 +282,7 @@ int main(int argc, char *argv[]) {
 
 		switch (resp) {
 			break; case Search: {
-				if (!rest) errx(EX_TEMPFAIL, "no messages match");
+				if (!rest) errx(EX_TEMPFAIL, "no matching messages");
 				uids = strdup(rest);
 				if (!uids) err(EX_OSERR, "strdup");
 			}
@@ -290,14 +290,14 @@ int main(int argc, char *argv[]) {
 				char *headers = readLiteral(imap, rest);
 
 				ssize_t len = getline(&line, &cap, imap);
-				if (len <= 0) errx(EX_PROTOCOL, "unexpected eof");
+				if (len <= 0) errx(EX_PROTOCOL, "unexpected eof after headers");
 
 				char *body = readLiteral(imap, line);
 
 				len = getline(&line, &cap, imap);
-				if (len <= 0) errx(EX_PROTOCOL, "unexpected eof");
+				if (len <= 0) errx(EX_PROTOCOL, "unexpected eof after body");
 				if (strcmp(line, ")\r\n")) {
-					errx(EX_PROTOCOL, "trailing fetch data");
+					errx(EX_PROTOCOL, "trailing data after headers and body");
 				}
 
 				mboxrd(headers, body);
@@ -307,4 +307,5 @@ int main(int argc, char *argv[]) {
 			break; default:;
 		}
 	}
+	errx(EX_PROTOCOL, "unexpected eof");
 }