summary refs log tree commit diff
path: root/html.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-12 19:22:04 -0400
committerJune McEnroe <june@causal.agency>2020-07-12 19:22:04 -0400
commitaa619626bcab0c4c13266702d1f53a72b24b22f7 (patch)
tree4e46eaf75efc1de8edd1bb4ff23e3d25fb9c52e7 /html.c
parentDon't use KHTML_PRETTY (diff)
downloadscooper-aa619626bcab0c4c13266702d1f53a72b24b22f7.tar.gz
scooper-aa619626bcab0c4c13266702d1f53a72b24b22f7.zip
Render all types of events
Diffstat (limited to 'html.c')
-rw-r--r--html.c101
1 files changed, 93 insertions, 8 deletions
diff --git a/html.c b/html.c
index 0433d29..2d987f8 100644
--- a/html.c
+++ b/html.c
@@ -256,10 +256,13 @@ static int hash(const char *str) {
 	return 2 + hash % 74;
 }
 
-static enum kcgi_err eventNick(struct khtmlreq *html, struct Event event) {
-	char color[sizeof("fg99")];
-	snprintf(color, sizeof(color), "fg%02d", hash(event.user));
+static const char *colorClass(int color) {
+	static char class[sizeof("fg99")];
+	snprintf(class, sizeof(class), "fg%02d", color);
+	return class;
+}
 
+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");
@@ -268,7 +271,7 @@ static enum kcgi_err eventNick(struct khtmlreq *html, struct Event event) {
 		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "nick", KATTR__MAX)
 		|| khtml_attr(
 			html, KELEM_SPAN,
-			KATTR_CLASS, color,
+			KATTR_CLASS, colorClass(hash(event.user)),
 			KATTR_TITLE, mask,
 			KATTR__MAX
 		)
@@ -279,14 +282,89 @@ static enum kcgi_err eventNick(struct khtmlreq *html, struct Event event) {
 	return error;
 }
 
-static enum kcgi_err eventMessage(struct khtmlreq *html, struct Event event) {
-	if (!event.message) return KCGI_OK;
+static enum kcgi_err typeJoin(struct khtmlreq *html, struct Event event) {
+	(void)event;
+	return khtml_puts(html, "joined");
+}
+
+static enum kcgi_err typePart(struct khtmlreq *html, struct Event event) {
+	if (!event.message) return khtml_puts(html, "left");
+	return 0
+		|| khtml_puts(html, "left (")
+		|| htmlIRC(html, event.message)
+		|| khtml_puts(html, ")");
+}
+
+static enum kcgi_err typeQuit(struct khtmlreq *html, struct Event event) {
+	if (!event.message) return khtml_puts(html, "quit");
 	return 0
-		|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "message", KATTR__MAX)
-		|| khtml_puts(html, event.message)
+		|| khtml_puts(html, "quit (")
+		|| htmlIRC(html, event.message)
+		|| khtml_puts(html, ")");
+}
+
+static enum kcgi_err typeKick(struct khtmlreq *html, struct Event event) {
+	if (!event.target) return KCGI_OK;
+	if (!event.message) return khtml_printf(html, "kicked %s", event.target);
+	return 0
+		|| khtml_printf(html, "kicked %s (", event.target)
+		|| htmlIRC(html, event.message)
+		|| khtml_puts(html, ")");
+}
+
+static enum kcgi_err typeNick(struct khtmlreq *html, struct Event event) {
+	if (!event.target) return KCGI_OK;
+	return 0
+		|| khtml_puts(html, "changed nick to ")
+		|| khtml_attr(
+			html, KELEM_SPAN,
+			KATTR_CLASS, colorClass(hash(event.user)),
+			KATTR__MAX
+		)
+		|| khtml_puts(html, event.target)
 		|| khtml_closeelem(html, 1);
 }
 
+static enum kcgi_err typeTopic(struct khtmlreq *html, struct Event event) {
+	if (!event.message) return KCGI_OK;
+	return 0
+		|| khtml_puts(html, "set the topic: ")
+		|| htmlIRC(html, event.message);
+}
+
+static enum kcgi_err typeBan(struct khtmlreq *html, struct Event event) {
+	if (!event.target) return KCGI_OK;
+	return khtml_printf(html, "banned %s", event.target);
+}
+
+static enum kcgi_err typeUnban(struct khtmlreq *html, struct Event event) {
+	if (!event.target) return KCGI_OK;
+	return khtml_printf(html, "unbanned %s", event.target);
+}
+
+static enum kcgi_err eventMessage(struct khtmlreq *html, struct Event event) {
+	enum kcgi_err error = khtml_attr(
+		html, KELEM_TD,
+		KATTR_CLASS, "message",
+		KATTR__MAX
+	);
+	if (error) return error;
+	switch (event.type) {
+		break; case Join: error = typeJoin(html, event);
+		break; case Part: error = typePart(html, event);
+		break; case Quit: error = typeQuit(html, event);
+		break; case Kick: error = typeKick(html, event);
+		break; case Nick: error = typeNick(html, event);
+		break; case Topic: error = typeTopic(html, event);
+		break; case Ban: error = typeBan(html, event);
+		break; case Unban: error = typeUnban(html, event);
+		break; default: {
+			if (event.message) error = htmlIRC(html, event.message);
+		}
+	}
+	return error || khtml_closeelem(html, 1);
+}
+
 static const char *Types[TypesLen] = {
 #define X(id, name) [id] = name,
 	ENUM_TYPE
@@ -310,3 +388,10 @@ htmlEvent(struct khtmlreq *html, struct Scope scope, struct Event event) {
 		|| eventMessage(html, event)
 		|| khtml_closeelem(html, 1);
 }
+
+enum kcgi_err htmlIRC(struct khtmlreq *html, const char *str) {
+	return 0
+		|| khtml_attr(html, KELEM_SPAN, KATTR_CLASS, "irc", KATTR__MAX)
+		|| khtml_puts(html, str)
+		|| khtml_closeelem(html, 1);
+}