summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-11 18:07:33 -0400
committerJune McEnroe <june@causal.agency>2020-07-11 18:07:33 -0400
commit5d857422687068526d6477073ef83630ffcf0c1e (patch)
tree0ccc0a277ca4166bdd0d007bf7bee1f6a4393bab
parentHandle KCGI_EXIT and call khttp_fcgi_free (diff)
downloadscooper-5d857422687068526d6477073ef83630ffcf0c1e.tar.gz
scooper-5d857422687068526d6477073ef83630ffcf0c1e.zip
Clean up page handlers
-rw-r--r--contexts.c22
-rw-r--r--events.c40
-rw-r--r--networks.c12
-rw-r--r--search.c42
-rw-r--r--server.h3
5 files changed, 55 insertions, 64 deletions
diff --git a/contexts.c b/contexts.c
index e17ccfe..e7f44e4 100644
--- a/contexts.c
+++ b/contexts.c
@@ -31,13 +31,13 @@ const char *ContextsQuery = SQL(
 		SELECT name, query
 		FROM contexts
 		JOIN recentEvents USING (context)
-		WHERE network = :network AND coalesce(query = :query, true)
+		WHERE network = :network AND query <= NOT :public
 		GROUP BY context
 		ORDER BY max(time) DESC
 	), allContexts AS (
 		SELECT name, query
 		FROM contexts
-		WHERE network = :network AND coalesce(query = :query, true)
+		WHERE network = :network AND query <= NOT :public
 		ORDER BY query, name
 	)
 	SELECT name, query, 1 FROM activeContexts
@@ -60,9 +60,10 @@ enum kcgi_err pageContexts(struct kreq *req) {
 		|| htmlNav(&html, scope);
 	if (error) return error;
 
-	dbBindInt(stmt.contexts, ":recent", pageRecent);
+	sqlite3_reset(stmt.contexts);
 	dbBindText(stmt.contexts, ":network", scope.network);
-	if (pagePublic) dbBindInt(stmt.contexts, ":query", false);
+	dbBindInt(stmt.contexts, ":recent", pageRecent);
+	dbBindInt(stmt.contexts, ":public", pagePublic);
 
 	enum State {
 		None,
@@ -74,9 +75,10 @@ enum kcgi_err pageContexts(struct kreq *req) {
 
 	int result;
 	while (SQLITE_ROW == (result = sqlite3_step(stmt.contexts))) {
-		const char *context = (const char *)sqlite3_column_text(stmt.contexts, 0);
-		bool query = sqlite3_column_int(stmt.contexts, 1);
-		bool active = sqlite3_column_int(stmt.contexts, 2);
+		int i = 0;
+		const char *context = sqlite3_column_text(stmt.contexts, i++);
+		bool query = sqlite3_column_int(stmt.contexts, i++);
+		bool active = sqlite3_column_int(stmt.contexts, i++);
 
 		enum State prev = state;
 		state = (active ? Active : (query ? Queries : Channels));
@@ -106,9 +108,5 @@ enum kcgi_err pageContexts(struct kreq *req) {
 		if (error) break;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
-	sqlite3_reset(stmt.contexts);
-
-	return error
-		|| htmlFooter(&html)
-		|| khtml_close(&html);
+	return error || htmlFooter(&html) || khtml_close(&html);
 }
diff --git a/events.c b/events.c
index 5e332a3..679a8af 100644
--- a/events.c
+++ b/events.c
@@ -37,9 +37,9 @@ const char *EventsAfterQuery = SQL(
 	JOIN names USING (name)
 	WHERE contexts.network = :network
 		AND contexts.name = :context
-		AND coalesce(contexts.query = :query, true)
+		AND contexts.query <= NOT :public
 		AND events.time >= strftime('%s', :time)
-	ORDER BY events.time
+	ORDER BY events.time, events.event
 	LIMIT :limit;
 );
 
@@ -59,14 +59,13 @@ const char *EventsBeforeQuery = SQL(
 		JOIN names USING (name)
 		WHERE contexts.network = :network
 			AND contexts.name = :context
-			AND coalesce(contexts.query = :query, true)
+			AND contexts.query <= NOT :public
 			AND events.time < strftime('%s', :time)
 		ORDER BY events.time DESC
 		LIMIT :limit
 	)
-	SELECT *
-	FROM before
-	ORDER BY time;
+	SELECT * FROM before
+	ORDER BY time, event;
 );
 
 enum kcgi_err pageEvents(struct kreq *req) {
@@ -111,31 +110,28 @@ enum kcgi_err pageEvents(struct kreq *req) {
 	sqlite3_stmt *events = stmt.eventsAfter;
 	if (req->fieldmap[Before]) events = stmt.eventsBefore;
 
+	sqlite3_reset(events);
 	dbBindText(events, ":network", scope.network);
 	dbBindText(events, ":context", scope.context);
-	if (pagePublic) dbBindInt(events, ":query", false);
 	dbBindText(events, ":time", time);
+	dbBindInt(events, ":public", pagePublic);
 	dbBindInt(events, ":limit", pageLimit);
 
 	int result;
 	while (SQLITE_ROW == (result = sqlite3_step(events))) {
-		struct Event event = {
-			.event = sqlite3_column_int64(events, 0),
-			.time = sqlite3_column_int64(events, 1),
-			.type = sqlite3_column_int(events, 2),
-			.nick = (const char *)sqlite3_column_text(events, 3),
-			.user = (const char *)sqlite3_column_text(events, 4),
-			.host = (const char *)sqlite3_column_text(events, 5),
-			.target = (const char *)sqlite3_column_text(events, 6),
-			.message = (const char *)sqlite3_column_text(events, 7),
-		};
+		int i = 0;
+		struct Event event = {0};
+		event.event = sqlite3_column_int64(events, i++);
+		event.time = sqlite3_column_int64(events, i++);
+		event.type = sqlite3_column_int(events, i++);
+		event.nick = sqlite3_column_text(events, i++);
+		event.user = sqlite3_column_text(events, i++);
+		event.host = sqlite3_column_text(events, i++);
+		event.target = sqlite3_column_text(events, i++);
+		event.message = sqlite3_column_text(events, i++);
 		error = htmlEvent(&html, scope, event);
 		if (error) break;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
-	sqlite3_reset(events);
-
-	return error
-		|| htmlFooter(&html)
-		|| khtml_close(&html);
+	return error || htmlFooter(&html) || khtml_close(&html);
 }
diff --git a/networks.c b/networks.c
index 74b9ea2..4fcd476 100644
--- a/networks.c
+++ b/networks.c
@@ -57,6 +57,7 @@ enum kcgi_err pageNetworks(struct kreq *req) {
 		|| htmlNav(&html, scope);
 	if (error) return error;
 
+	sqlite3_reset(stmt.networks);
 	dbBindInt(stmt.networks, ":recent", pageRecent);
 
 	enum State {
@@ -68,8 +69,9 @@ enum kcgi_err pageNetworks(struct kreq *req) {
 
 	int result;
 	while (SQLITE_ROW == (result = sqlite3_step(stmt.networks))) {
-		const char *network = (const char *)sqlite3_column_text(stmt.networks, 0);
-		bool active = sqlite3_column_int(stmt.networks, 1);
+		int i = 0;
+		const char *network = sqlite3_column_text(stmt.networks, i++);
+		bool active = sqlite3_column_int(stmt.networks, i++);
 
 		enum State prev = state;
 		state = (active ? Active : Networks);
@@ -98,9 +100,5 @@ enum kcgi_err pageNetworks(struct kreq *req) {
 		if (error) break;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
-	sqlite3_reset(stmt.networks);
-
-	return error
-		|| htmlFooter(&html)
-		|| khtml_close(&html);
+	return error || htmlFooter(&html) || khtml_close(&html);
 }
diff --git a/search.c b/search.c
index 6fba589..3026a3b 100644
--- a/search.c
+++ b/search.c
@@ -39,10 +39,9 @@ const char *SearchQuery = SQL(
 	JOIN search ON search.rowid = events.event
 	WHERE coalesce(contexts.network = :network, true)
 		AND coalesce(contexts.name = :context, true)
-		AND coalesce(contexts.query = :query, true)
-		AND search MATCH :search
-	LIMIT :limit
-	OFFSET :offset;
+		AND contexts.query <= NOT :public
+		AND search MATCH :query
+	LIMIT :limit, :offset;
 );
 
 enum kcgi_err pageSearch(struct kreq *req) {
@@ -65,35 +64,32 @@ enum kcgi_err pageSearch(struct kreq *req) {
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;
 
-	dbBindText(stmt.search, ":highlight", "\26");
+	sqlite3_reset(stmt.search);
 	dbBindText(stmt.search, ":network", scope.network);
 	dbBindText(stmt.search, ":context", scope.context);
-	if (pagePublic) dbBindInt(stmt.search, ":query", false);
 	dbBindText(stmt.search, ":search", scope.query);
+	dbBindInt(stmt.search, ":public", pagePublic);
 	dbBindInt(stmt.search, ":limit", pageLimit);
 	dbBindInt(stmt.search, ":offset", offset);
+	dbBindText(stmt.search, ":highlight", "\26");
 
 	int result;
 	while (SQLITE_ROW == (result = sqlite3_step(stmt.search))) {
-		struct Event event = {
-			.event = sqlite3_column_int64(stmt.search, 0),
-			.time = sqlite3_column_int64(stmt.search, 1),
-			.network = (const char *)sqlite3_column_text(stmt.search, 2),
-			.context = (const char *)sqlite3_column_text(stmt.search, 3),
-			.type = sqlite3_column_int(stmt.search, 4),
-			.nick = (const char *)sqlite3_column_text(stmt.search, 5),
-			.user = (const char *)sqlite3_column_text(stmt.search, 6),
-			.host = (const char *)sqlite3_column_text(stmt.search, 7),
-			.target = (const char *)sqlite3_column_text(stmt.search, 8),
-			.message = (const char *)sqlite3_column_text(stmt.search, 9),
-		};
+		int i = 0;
+		struct Event event = {0};
+		event.event = sqlite3_column_int64(stmt.search, i++);
+		event.time = sqlite3_column_int64(stmt.search, i++);
+		event.network = sqlite3_column_text(stmt.search, i++);
+		event.context = sqlite3_column_text(stmt.search, i++);
+		event.type = sqlite3_column_int(stmt.search, i++);
+		event.nick = sqlite3_column_text(stmt.search, i++);
+		event.user = sqlite3_column_text(stmt.search, i++);
+		event.host = sqlite3_column_text(stmt.search, i++);
+		event.target = sqlite3_column_text(stmt.search, i++);
+		event.message = sqlite3_column_text(stmt.search, i++);
 		error = htmlEvent(&html, scope, event);
 		if (error) break;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
-	sqlite3_reset(stmt.search);
-
-	return error
-		|| htmlFooter(&html)
-		|| khtml_close(&html);
+	return error || htmlFooter(&html) || khtml_close(&html);
 }
diff --git a/server.h b/server.h
index 437a041..c3e274e 100644
--- a/server.h
+++ b/server.h
@@ -31,6 +31,9 @@
 
 #define SQL(...) #__VA_ARGS__
 
+// Why does it return (const unsigned char *)?
+#define sqlite3_column_text(...) (const char *)sqlite3_column_text(__VA_ARGS__)
+
 enum { DatabaseVersion = 5 };
 
 #define ENUM_TYPE \