summary refs log tree commit diff
path: root/litterbox.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-25 13:22:33 -0500
committerJune McEnroe <june@causal.agency>2019-12-25 13:23:50 -0500
commite465dbe2a9e789b1ddf06510c8fe4d568634c2ee (patch)
tree3c9975c0c6a4371a99031d95f7f4e2376508ab99 /litterbox.c
parentOnly send JOIN if there are channels to join (diff)
downloadlitterbox-e465dbe2a9e789b1ddf06510c8fe4d568634c2ee.tar.gz
litterbox-e465dbe2a9e789b1ddf06510c8fe4d568634c2ee.zip
Handle SIGINT and SIGTERM
Diffstat (limited to 'litterbox.c')
-rw-r--r--litterbox.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/litterbox.c b/litterbox.c
index 5c18021..181227a 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -16,6 +16,7 @@
 
 #include <assert.h>
 #include <err.h>
+#include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -438,6 +439,14 @@ static void handle(struct Message msg) {
 	}
 }
 
+static void quit(int sig) {
+	(void)sig;
+	format("QUIT\r\n");
+	tls_close(client);
+	dbClose();
+	_exit(EX_OK);
+}
+
 int main(int argc, char *argv[]) {
 	char *path = NULL;
 	bool init = false;
@@ -520,13 +529,16 @@ int main(int argc, char *argv[]) {
 	format("CAP REQ :server-time\r\n");
 	format("NICK :%s\r\nUSER %s 0 * :Litterbox\r\n", nick, user);
 
+	signal(SIGINT, quit);
+	signal(SIGTERM, quit);
+
 	char buf[8191 + 512];
 	size_t len = 0;
 	for (;;) {
 		ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len);
 		if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue;
 		if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client));
-		if (!ret) break;
+		if (!ret) errx(EX_PROTOCOL, "server closed connection");
 		len += ret;
 
 		char *line = buf;
@@ -540,6 +552,4 @@ int main(int argc, char *argv[]) {
 		len -= line - buf;
 		memmove(buf, line, len);
 	}
-
-	dbClose();
 }