summary refs log tree commit diff
path: root/html.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-10 14:54:45 -0400
committerJune McEnroe <june@causal.agency>2020-07-10 14:54:45 -0400
commit3926094bc216277ff6c58e6e9efba64fc7af91da (patch)
tree55362740094489c3e59fbc125ed181dd18cf0077 /html.c
parentAdd -l and -r options (diff)
downloadscooper-3926094bc216277ff6c58e6e9efba64fc7af91da.tar.gz
scooper-3926094bc216277ff6c58e6e9efba64fc7af91da.zip
Implement partial table output of events
This should work for both events and search pages.
Diffstat (limited to 'html.c')
-rw-r--r--html.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/html.c b/html.c
index 6141514..9f929e6 100644
--- a/html.c
+++ b/html.c
@@ -15,9 +15,11 @@
  */
 
 #include <err.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sysexits.h>
+#include <time.h>
 
 #include "server.h"
 
@@ -168,3 +170,104 @@ enum kcgi_err htmlFooter(struct khtmlreq *html) {
 		|| khtml_puts(html, "Columns")
 		|| khtml_closeto(html, 0);
 }
+
+static enum kcgi_err eventTime(struct khtmlreq *html, struct Event event) {
+	char time[sizeof("0000-00-00 00:00:00")];
+	strftime(time, sizeof(time), "%F %T", gmtime(&event.time));
+
+	char *base = NULL;
+	if (event.network && event.context) {
+		base = khttp_urlpart(
+			NULL, NULL, Pages[Events],
+			Keys[Network].name, event.network,
+			Keys[Context].name, event.context,
+			Keys[After].name, time,
+			NULL
+		);
+		if (!base) err(EX_OSERR, "khttp_urlpart");
+	}
+	char *href = NULL;
+	asprintf(&href, "%s#%" PRId64, (base ? base : ""), event.event);
+	if (!href) err(EX_OSERR, "asprintf");
+	if (base) free(base);
+
+	enum kcgi_err error = 0
+		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "time", KATTR__MAX)
+		|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+		|| khtml_attr(html, KELEM_TIME, KATTR_DATETIME, time, KATTR__MAX)
+		|| khtml_puts(html, time)
+		|| khtml_closeelem(html, 3);
+
+	free(href);
+	return error;
+}
+
+static enum kcgi_err eventNetwork(struct khtmlreq *html, struct Event event) {
+	if (!event.network) return KCGI_OK;
+	char *href = khttp_urlpart(
+		NULL, NULL, Pages[Contexts],
+		Keys[Network].name, event.network,
+		NULL
+	);
+	if (!href) err(EX_OSERR, "khttp_urlpart");
+	enum kcgi_err error = 0
+		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "network", KATTR__MAX)
+		|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+		|| khtml_puts(html, event.network)
+		|| khtml_closeelem(html, 2);
+	free(href);
+	return error;
+}
+
+static enum kcgi_err eventContext(struct khtmlreq *html, struct Event event) {
+	if (!event.network || !event.context) return KCGI_OK;
+	char *href = khttp_urlpart(
+		NULL, NULL, Pages[Events],
+		Keys[Network].name, event.network,
+		Keys[Context].name, event.context,
+		NULL
+	);
+	if (!href) err(EX_OSERR, "khttp_urlpart");
+	enum kcgi_err error = 0
+		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "context", KATTR__MAX)
+		|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+		|| khtml_puts(html, event.context)
+		|| khtml_closeelem(html, 2);
+	free(href);
+	return error;
+}
+
+static enum kcgi_err eventNick(struct khtmlreq *html, struct Event event) {
+	char *mask = NULL;
+	asprintf(&mask, "%s!%s@%s", event.nick, event.user, event.host);
+	if (!mask) err(EX_OSERR, "asprintf");
+	enum kcgi_err error = 0
+		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "nick", KATTR__MAX)
+		|| khtml_attr(html, KELEM_SPAN, KATTR_TITLE, mask, KATTR__MAX)
+		|| khtml_puts(html, event.nick)
+		|| khtml_closeelem(html, 2);
+	free(mask);
+	return error;
+}
+
+static const char *Types[TypesLen] = {
+#define X(id, name) [id] = name,
+	ENUM_TYPE
+#undef X
+};
+
+enum kcgi_err htmlEvent(struct khtmlreq *html, struct Event event) {
+	const char *type = (event.type < TypesLen ? Types[event.type] : "unknown");
+	return 0
+		|| khtml_attrx(
+			html, KELEM_TR,
+			KATTR_ID, KATTRX_INT, event.event,
+			KATTR_CLASS, KATTRX_STRING, type,
+			KATTR__MAX
+		)
+		|| eventTime(html, event)
+		|| eventNetwork(html, event)
+		|| eventContext(html, event)
+		|| eventNick(html, event)
+		|| khtml_closeelem(html, 1);
+}