about summary refs log tree commit diff
path: root/litterbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'litterbox.c')
-rw-r--r--litterbox.c55
1 files changed, 30 insertions, 25 deletions
diff --git a/litterbox.c b/litterbox.c
index 48e5b60..f33ed22 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+/* Copyright (C) 2019  June McEnroe <june@causal.agency>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -432,6 +432,23 @@ static void insertEvent(
 	dbRun(stmt);
 }
 
+static enum Type messageType(struct Message *msg) {
+	if (msg->cmd[0] == 'N') return Notice;
+	if (strncmp(msg->params[1], "\1ACTION", 7)) return Privmsg;
+	if (msg->params[1][7] == ' ') {
+		msg->params[1] += 8;
+	} else if (msg->params[1][7] == '\1') {
+		msg->params[1] += 7;
+	} else {
+		return Privmsg;
+	}
+	size_t len = strlen(msg->params[1]);
+	if (msg->params[1][len - 1] == '\1') {
+		msg->params[1][len - 1] = '\0';
+	}
+	return Action;
+}
+
 static void handlePrivmsg(struct Message *msg) {
 	require(msg, true, 2);
 
@@ -440,14 +457,7 @@ static void handlePrivmsg(struct Message *msg) {
 	if (statusmsg) context += strspn(context, statusmsg);
 	if (strchr(chanTypes, context[0])) query = false;
 	if (!strcmp(context, self)) context = msg->nick;
-
-	enum Type type = (!strcmp(msg->cmd, "NOTICE") ? Notice : Privmsg);
-	char *message = msg->params[1];
-	if (!strncmp(message, "\1ACTION ", 8)) {
-		message += 8;
-		message[strcspn(message, "\1")] = '\0';
-		type = Action;
-	}
+	enum Type type = messageType(msg);
 
 	bool selfMessage = !strcmp(msg->nick, msg->params[0]);
 	if (query && searchQuery && type == Privmsg) {
@@ -460,7 +470,7 @@ static void handlePrivmsg(struct Message *msg) {
 
 	insertContext(context, query);
 	insertName(msg);
-	insertEvent(msg, type, context, NULL, message);
+	insertEvent(msg, type, context, NULL, msg->params[1]);
 }
 
 static void insertTopic(
@@ -742,8 +752,9 @@ static void handle(struct Message *msg) {
 }
 
 static void atExit(void) {
-	if (client) tls_close(client);
+	dbExec(SQL(PRAGMA optimize;));
 	dbClose();
+	if (client) tls_close(client);
 }
 
 static void quit(int sig) {
@@ -756,7 +767,7 @@ static void quit(int sig) {
 int main(int argc, char *argv[]) {
 	bool init = false;
 	bool migrate = false;
-	const char *path = NULL;
+	const char *dbPath = NULL;
 	const char *backup = NULL;
 
 	bool insecure = false;
@@ -806,7 +817,7 @@ int main(int argc, char *argv[]) {
 			break; case 'U': scooperURL = optarg;
 			break; case 'b': backup = optarg;
 			break; case 'c': cert = optarg;
-			break; case 'd': path = optarg;
+			break; case 'd': dbPath = optarg;
 			break; case 'h': host = optarg;
 			break; case 'i': init = true;
 			break; case 'j': join = optarg;
@@ -831,7 +842,7 @@ int main(int argc, char *argv[]) {
 
 	int flags = SQLITE_OPEN_READWRITE;
 	if (init) flags |= SQLITE_OPEN_CREATE;
-	dbFind(path, flags);
+	dbFind(dbPath, flags);
 	atexit(atExit);
 
 	if (init) {
@@ -859,22 +870,18 @@ int main(int argc, char *argv[]) {
 	client = tls_client();
 	if (!client) errx(EX_SOFTWARE, "tls_client");
 
+	int error;
+	char path[PATH_MAX];
 	struct tls_config *config = tls_config_new();
 	if (!config) errx(EX_SOFTWARE, "tls_config_new");
 
-	int error = tls_config_set_ciphers(config, "compat");
-	if (error) {
-		errx(EX_SOFTWARE, "tls_config_set_ciphers: %s", tls_config_error(config));
-	}
-
 	if (insecure) {
 		tls_config_insecure_noverifycert(config);
 		tls_config_insecure_noverifyname(config);
 	}
 	if (trust) {
 		tls_config_insecure_noverifyname(config);
-		const char *dirs = NULL;
-		while (NULL != (path = configPath(&dirs, trust))) {
+		for (int i = 0; configPath(path, sizeof(path), trust, i); ++i) {
 			error = tls_config_set_ca_file(config, path);
 			if (!error) break;
 		}
@@ -882,8 +889,7 @@ int main(int argc, char *argv[]) {
 	}
 
 	if (cert) {
-		const char *dirs = NULL;
-		while (NULL != (path = configPath(&dirs, cert))) {
+		for (int i = 0; configPath(path, sizeof(path), cert, i); ++i) {
 			if (priv) {
 				error = tls_config_set_cert_file(config, path);
 			} else {
@@ -894,8 +900,7 @@ int main(int argc, char *argv[]) {
 		if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config));
 	}
 	if (priv) {
-		const char *dirs = NULL;
-		while (NULL != (path = configPath(&dirs, priv))) {
+		for (int i = 0; configPath(path, sizeof(path), priv, i); ++i) {
 			error = tls_config_set_key_file(config, path);
 			if (!error) break;
 		}