summary refs log tree commit diff
path: root/events.c
diff options
context:
space:
mode:
Diffstat (limited to 'events.c')
-rw-r--r--events.c136
1 files changed, 58 insertions, 78 deletions
diff --git a/events.c b/events.c
index e5b0777..932f535 100644
--- a/events.c
+++ b/events.c
@@ -34,23 +34,10 @@ static const char *timestamp(time_t time) {
 	return stamp;
 }
 
-static enum kcgi_err tidyField(struct kreq *req, struct khtmlreq *html) {
-	if (!req->fieldmap[Tidy]) return KCGI_OK;
-	return khtml_attr(
-		html, KELEM_INPUT,
-		KATTR_TYPE, "hidden",
-		KATTR_NAME, Keys[Tidy].name,
-		KATTR_VALUE, req->fieldmap[Tidy]->parsed.s,
-		KATTR__MAX
-	);
-}
-
 static enum kcgi_err
-dateForm(struct kreq *req, struct khtmlreq *html, struct Scope scope) {
-	struct kpair *field = req->fieldmap[After];
-	if (!field) field = req->fieldmap[Before];
+dateForm(struct khtmlreq *html, struct kreq *req, const char *current) {
 	struct tm tm = {0};
-	if (!strptime(field->parsed.s, "%F", &tm)) {
+	if (!strptime(current, "%F", &tm)) {
 		tm = *gmtime(&(time_t) { time(NULL) });
 	}
 	char date[sizeof("0000-00-00")];
@@ -62,7 +49,8 @@ dateForm(struct kreq *req, struct khtmlreq *html, struct Scope scope) {
 			KATTR_ACTION, Pages[Events],
 			KATTR__MAX
 		)
-		|| htmlScopeFields(html, scope)
+		|| htmlHidden(html, req, Network)
+		|| htmlHidden(html, req, Context)
 		|| khtml_attr(
 			html, KELEM_INPUT,
 			KATTR_TYPE, "date",
@@ -70,20 +58,14 @@ dateForm(struct kreq *req, struct khtmlreq *html, struct Scope scope) {
 			KATTR_VALUE, date,
 			KATTR__MAX
 		)
-		|| tidyField(req, html)
-		|| khtml_attr(
-			html, KELEM_INPUT,
-			KATTR_TYPE, "submit",
-			KATTR_VALUE, "Jump",
-			KATTR__MAX
-		)
-		|| khtml_closeelem(html, 1);
+		|| htmlHidden(html, req, Tidy)
+		|| khtml_elem(html, KELEM_BUTTON)
+		|| khtml_puts(html, "Jump")
+		|| khtml_closeelem(html, 2);
 }
 
 static enum kcgi_err
-displayForm(struct kreq *req, struct khtmlreq *html, struct Scope scope) {
-	struct kpair *time = req->fieldmap[After];
-	if (!time) time = req->fieldmap[Before];
+displayForm(struct khtmlreq *html, struct kreq *req, bool tidy) {
 	return 0
 		|| khtml_attr(
 			html, KELEM_FORM,
@@ -91,32 +73,40 @@ displayForm(struct kreq *req, struct khtmlreq *html, struct Scope scope) {
 			KATTR_ACTION, Pages[Events],
 			KATTR__MAX
 		)
-		|| htmlScopeFields(html, scope)
-		|| khtml_attr(
-			html, KELEM_INPUT,
-			KATTR_TYPE, "hidden",
-			KATTR_NAME, time->key,
-			KATTR_VALUE, time->parsed.s,
-			KATTR__MAX
-		)
+		|| htmlHidden(html, req, Network)
+		|| htmlHidden(html, req, Context)
+		|| htmlHidden(html, req, After)
+		|| htmlHidden(html, req, Before)
 		|| khtml_elem(html, KELEM_LABEL)
 		|| khtml_attr(
 			html, KELEM_INPUT,
 			KATTR_TYPE, "checkbox",
 			KATTR_NAME, Keys[Tidy].name,
 			KATTR_VALUE, "1",
-			(req->fieldmap[Tidy] ? KATTR_CHECKED : KATTR__MAX), "checked",
+			(tidy ? KATTR_CHECKED : KATTR__MAX), "checked",
 			KATTR__MAX
 		)
 		|| khtml_puts(html, "Hide general events")
 		|| khtml_closeelem(html, 1)
-		|| khtml_attr(
-			html, KELEM_INPUT,
-			KATTR_TYPE, "submit",
-			KATTR_VALUE, "Apply",
-			KATTR__MAX
-		)
-		|| khtml_closeelem(html, 1);
+		|| khtml_elem(html, KELEM_BUTTON)
+		|| khtml_puts(html, "Apply")
+		|| khtml_closeelem(html, 2);
+}
+
+static char *pageURL(
+	const char *network, const char *context,
+	enum Key key, time_t time, bool tidy
+) {
+	char *url = khttp_urlpart(
+		NULL, NULL, Pages[Events],
+		Keys[Network].name, network,
+		Keys[Context].name, context,
+		Keys[key].name, timestamp(time),
+		(tidy ? Keys[Tidy].name : NULL), "1",
+		NULL
+	);
+	if (!url) err(EX_OSERR, "khttp_urlpart");
+	return url;
 }
 
 const char *EventsTopicQuery = SQL(
@@ -180,14 +170,17 @@ const char *EventsBeforeQuery = SQL(
 );
 
 enum kcgi_err eventsPage(struct kreq *req) {
-	struct Scope scope = pageScope(req);
-	if (!scope.network || !scope.context) return httpFail(req, KHTTP_400);
+	if (!req->fieldmap[Network] || !req->fieldmap[Context]) {
+		return httpFail(req, KHTTP_400);
+	}
+	const char *network = req->fieldmap[Network]->parsed.s;
+	const char *context = req->fieldmap[Context]->parsed.s;
 
 	if (!req->fieldmap[After] && !req->fieldmap[Before]) {
 		char *url = khttp_urlpart(
 			NULL, NULL, Pages[Events],
-			Keys[Network].name, scope.network,
-			Keys[Context].name, scope.context,
+			Keys[Network].name, network,
+			Keys[Context].name, context,
 			Keys[Before].name, timestamp(time(NULL)),
 			NULL
 		);
@@ -200,6 +193,7 @@ enum kcgi_err eventsPage(struct kreq *req) {
 	const char *time = req->fieldmap[Before]
 		? req->fieldmap[Before]->parsed.s
 		: req->fieldmap[After]->parsed.s;
+	bool tidy = (req->fieldmap[Tidy] ? req->fieldmap[Tidy]->parsed.i : false);
 
 	enum kcgi_err error = 0
 		|| httpHead(req, KHTTP_200, KMIME_TEXT_HTML)
@@ -209,18 +203,18 @@ enum kcgi_err eventsPage(struct kreq *req) {
 	struct khtmlreq html;
 	error = error
 		|| khtml_open(&html, req, 0)
-		|| htmlHead(&html, scope.context)
-		|| htmlNav(&html, scope)
+		|| htmlHead(&html, context)
+		|| htmlNav(&html, req)
 		|| khtml_elem(&html, KELEM_DIV)
-		|| dateForm(req, &html, scope)
-		|| displayForm(req, &html, scope)
+		|| dateForm(&html, req, time)
+		|| displayForm(&html, req, tidy)
 		|| khtml_closeelem(&html, 1)
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;
 
 	sqlite3_reset(stmt.topic);
-	dbBindText(stmt.topic, ":network", scope.network);
-	dbBindText(stmt.topic, ":context", scope.context);
+	dbBindText(stmt.topic, ":network", network);
+	dbBindText(stmt.topic, ":context", context);
 	dbBindText(stmt.topic, ":time", time);
 	dbBindInt(stmt.topic, ":public", contextsPublic);
 
@@ -241,10 +235,10 @@ enum kcgi_err eventsPage(struct kreq *req) {
 	if (req->fieldmap[Before]) events = stmt.eventsBefore;
 
 	sqlite3_reset(events);
-	dbBindText(events, ":network", scope.network);
-	dbBindText(events, ":context", scope.context);
+	dbBindText(events, ":network", network);
+	dbBindText(events, ":context", context);
 	dbBindText(events, ":time", time);
-	dbBindInt(events, ":tidy", (req->fieldmap[Tidy] ? Join : TypesLen));
+	dbBindInt(events, ":tidy", (tidy ? Join : TypesLen));
 	dbBindInt(events, ":public", contextsPublic);
 	dbBindInt(events, ":limit", eventsLimit);
 
@@ -264,21 +258,12 @@ enum kcgi_err eventsPage(struct kreq *req) {
 		event.message = sqlite3_column_text(events, i++);
 
 		if (!rows) {
-			char *base = khttp_urlpart(
-				NULL, NULL, Pages[Events],
-				Keys[Network].name, scope.network,
-				Keys[Context].name, scope.context,
-				Keys[Before].name, timestamp(event.time + eventsOverlap),
-				(req->fieldmap[Tidy] ? Keys[Tidy].name : NULL), "1",
-				NULL
-			);
-			if (!base) err(EX_OSERR, "khttp_urlpart");
-
 			char *href = NULL;
-			asprintf(&href, "%s#%" PRId64, base, event.event);
+			char *page = pageURL(
+				network, context, Before, event.time + eventsOverlap, tidy
+			);
+			asprintf(&href, "%s#%" PRId64, page, event.event);
 			if (!href) err(EX_OSERR, "asprintf");
-			free(base);
-
 			error = 0
 				|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 				|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "3", KATTR__MAX)
@@ -286,6 +271,7 @@ enum kcgi_err eventsPage(struct kreq *req) {
 				|| khtml_puts(&html, "Earlier messages")
 				|| khtml_closeelem(&html, 3);
 			free(href);
+			free(page);
 			if (error) return error;
 		}
 
@@ -300,21 +286,15 @@ enum kcgi_err eventsPage(struct kreq *req) {
 		prevEvent = event.event;
 		prevTime = event.time;
 
-		error = htmlEvent(&html, scope, event);
+		error = htmlEvent(&html, req, &event);
 		if (error) return error;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
 
 	if (rows && (rows == eventsLimit || req->fieldmap[Before])) {
-		char *href = khttp_urlpart(
-			NULL, NULL, Pages[Events],
-			Keys[Network].name, scope.network,
-			Keys[Context].name, scope.context,
-			Keys[After].name, timestamp(prevTime - eventsOverlap),
-			(req->fieldmap[Tidy] ? Keys[Tidy].name : NULL), "1",
-			NULL
+		char *href = pageURL(
+			network, context, After, prevTime - eventsOverlap, tidy
 		);
-		if (!href) err(EX_OSERR, "khttp_urlpart");
 		error = 0
 			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "3", KATTR__MAX)