about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--archive.c7
-rw-r--r--imap.c11
-rw-r--r--imap.h4
3 files changed, 13 insertions, 9 deletions
diff --git a/archive.c b/archive.c
index d505949..992b592 100644
--- a/archive.c
+++ b/archive.c
@@ -104,6 +104,9 @@ int main(int argc, char *argv[]) {
 		if (!pass) errx(EX_CONFIG, ENV_PASSWORD " unset");
 	}
 
+	FILE *imapRead, *imap;
+	imapOpen(&imapRead, &imap, host, port);
+
 	enum {
 		Ready,
 		Login,
@@ -123,8 +126,7 @@ int main(int argc, char *argv[]) {
 
 	uint32_t uidNext = 0;
 	struct List threads = {0};
-	FILE *imap = imapOpen(host, port);
-	for (struct Resp resp; resp = imapResp(imap), resp.resp != AtomBye;) {
+	for (struct Resp resp; resp = imapResp(imapRead), resp.resp != AtomBye;) {
 		if (resp.resp == AtomNo || resp.resp == AtomBad) {
 			errx(EX_CONFIG, "%s %s", Atoms[resp.resp], resp.text);
 		}
@@ -221,5 +223,6 @@ int main(int argc, char *argv[]) {
 		}
 		respFree(resp);
 	}
+	fclose(imapRead);
 	fclose(imap);
 }
diff --git a/imap.c b/imap.c
index 7d623a5..9743ac8 100644
--- a/imap.c
+++ b/imap.c
@@ -58,10 +58,11 @@ static int imapClose(void *_tls) {
 	struct tls *tls = _tls;
 	int error = tls_close(tls);
 	if (error) errx(EX_IOERR, "tls_close: %s", tls_error(tls));
+	tls_free(tls);
 	return error;
 }
 
-FILE *imapOpen(const char *host, const char *port) {
+void imapOpen(FILE **read, FILE **write, const char *host, const char *port) {
 	struct tls *client = tls_client();
 	if (!client) errx(EX_SOFTWARE, "tls_client");
 
@@ -75,11 +76,11 @@ FILE *imapOpen(const char *host, const char *port) {
 	error = tls_connect(client, host, port);
 	if (error) errx(EX_NOHOST, "tls_connect: %s", tls_error(client));
 
-	FILE *imap = funopen(client, imapRead, imapWrite, NULL, imapClose);
-	if (!imap) err(EX_SOFTWARE, "funopen");
+	*read = funopen(client, imapRead, NULL, NULL, NULL);
+	*write = funopen(client, NULL, imapWrite, NULL, imapClose);
+	if (!*read || !*write) err(EX_SOFTWARE, "funopen");
 
-	setlinebuf(imap);
-	return imap;
+	setlinebuf(*write);
 }
 
 static size_t cap;
diff --git a/imap.h b/imap.h
index a364111..5919b8f 100644
--- a/imap.h
+++ b/imap.h
@@ -165,7 +165,7 @@ static inline void respFree(struct Resp resp) {
 }
 
 extern bool imapVerbose;
-FILE *imapOpen(const char *host, const char *port);
-struct Resp imapResp(FILE *imap);
+void imapOpen(FILE **read, FILE **write, const char *host, const char *port);
+struct Resp imapResp(FILE *imapRead);
 
 #endif /* IMAP_H */