about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-07-31 16:28:08 -0400
committerJune McEnroe <june@causal.agency>2022-07-31 18:17:08 -0400
commit93e841b29ea567f8ddc31ce7f104dce5396a71ba (patch)
tree9d844670e8e88af199410f66c7c2e90a91233bcb
parentOnly set cache color if not Default (diff)
downloadcatgirl-93e841b29ea567f8ddc31ce7f104dce5396a71ba.tar.gz
catgirl-93e841b29ea567f8ddc31ce7f104dce5396a71ba.zip
Move cache color to an Entry struct
So that more values can be added sensibly.
-rw-r--r--cache.c21
-rw-r--r--chat.h8
-rw-r--r--command.c4
-rw-r--r--handle.c39
-rw-r--r--window.c2
5 files changed, 37 insertions, 37 deletions
diff --git a/cache.c b/cache.c
index a125944..30ebef4 100644
--- a/cache.c
+++ b/cache.c
@@ -36,11 +36,13 @@
 struct Node {
 	uint id;
 	char *key;
-	enum Color color;
+	struct Entry entry;
 	struct Node *prev;
 	struct Node *next;
 };
 
+static const struct Entry DefaultEntry = { .color = Default };
+
 static uint gen;
 static struct Node *head;
 static struct Node *tail;
@@ -50,7 +52,7 @@ static struct Node *alloc(uint id, const char *key) {
 	if (!node) err(EX_OSERR, "calloc");
 	node->id = id;
 	node->key = strdup(key);
-	node->color = Default;
+	node->entry = DefaultEntry;
 	if (!node->key) err(EX_OSERR, "strdup");
 	return node;
 }
@@ -105,13 +107,13 @@ static struct Node *insert(bool touch, uint id, const char *key) {
 	}
 }
 
-void cacheInsert(bool touch, uint id, const char *key) {
-	insert(touch, id, key);
+struct Entry *cacheInsert(bool touch, uint id, const char *key) {
+	return &insert(touch, id, key)->entry;
 }
 
-void cacheInsertColor(bool touch, uint id, const char *key, enum Color color) {
-	struct Node *node = insert(touch, id, key);
-	if (color != Default) node->color = color;
+const struct Entry *cacheGet(uint id, const char *key) {
+	struct Node *node = find(id, key);
+	return (node ? &node->entry : &DefaultEntry);
 }
 
 void cacheReplace(bool touch, const char *old, const char *new) {
@@ -126,11 +128,6 @@ void cacheReplace(bool touch, const char *old, const char *new) {
 	}
 }
 
-enum Color cacheColor(uint id, const char *key) {
-	struct Node *node = find(id, key);
-	return (node ? node->color : Default);
-}
-
 const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) {
 	size_t len = strlen(prefix);
 	if (curs->gen != gen) curs->node = NULL;
diff --git a/chat.h b/chat.h
index 1bc5f8f..aa2e6ab 100644
--- a/chat.h
+++ b/chat.h
@@ -396,14 +396,16 @@ int bufferReflow(
 	struct Buffer *buffer, int cols, enum Heat thresh, size_t tail
 );
 
+struct Entry {
+	enum Color color;
+};
 struct Cursor {
 	uint gen;
 	struct Node *node;
 };
-void cacheInsert(bool touch, uint id, const char *key);
-void cacheInsertColor(bool touch, uint id, const char *key, enum Color color);
+struct Entry *cacheInsert(bool touch, uint id, const char *key);
+const struct Entry *cacheGet(uint id, const char *key);
 void cacheReplace(bool touch, const char *old, const char *new);
-enum Color cacheColor(uint id, const char *key);
 const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix);
 const char *cacheSearch(struct Cursor *curs, uint id, const char *substr);
 uint cacheID(struct Cursor *curs, const char *key);
diff --git a/command.c b/command.c
index 0926c33..3acbe76 100644
--- a/command.c
+++ b/command.c
@@ -139,7 +139,7 @@ static void commandMsg(uint id, char *params) {
 	char *nick = strsep(&params, " ");
 	uint msg = idFor(nick);
 	if (idColors[msg] == Default) {
-		idColors[msg] = cacheColor(id, nick);
+		idColors[msg] = cacheGet(id, nick)->color;
 	}
 	if (params) {
 		splitMessage("PRIVMSG", msg, params);
@@ -380,7 +380,7 @@ static void commandQuery(uint id, char *params) {
 	if (!params) return;
 	uint query = idFor(params);
 	if (idColors[query] == Default) {
-		idColors[query] = cacheColor(id, params);
+		idColors[query] = cacheGet(id, params)->color;
 	}
 	windowShow(windowFor(query));
 }
diff --git a/handle.c b/handle.c
index 2dfa3ae..4b5ff7f 100644
--- a/handle.c
+++ b/handle.c
@@ -372,13 +372,13 @@ static void handleJoin(struct Message *msg) {
 			set(&self.host, msg->host);
 		}
 		idColors[id] = hash(msg->params[0]);
-		cacheInsertColor(true, None, msg->params[0], idColors[id]);
+		cacheInsert(true, None, msg->params[0])->color = idColors[id];
 		if (replies[ReplyJoin]) {
 			windowShow(windowFor(id));
 			replies[ReplyJoin]--;
 		}
 	}
-	cacheInsertColor(true, id, msg->nick, hash(msg->user));
+	cacheInsert(true, id, msg->nick)->color = hash(msg->user);
 	if (msg->params[2] && !strcasecmp(msg->params[2], msg->nick)) {
 		msg->params[2] = NULL;
 	}
@@ -432,14 +432,14 @@ static void handleKick(struct Message *msg) {
 	require(msg, true, 2);
 	uint id = idFor(msg->params[0]);
 	bool kicked = !strcmp(msg->params[1], self.nick);
-	cacheInsertColor(true, id, msg->nick, hash(msg->user));
+	cacheInsert(true, id, msg->nick)->color = hash(msg->user);
 	urlScan(id, msg->nick, msg->params[2]);
 	uiFormat(
 		id, (kicked ? Hot : Cold), tagTime(msg),
 		"%s\3%02d%s\17\tkicks \3%02d%s\3 out of \3%02d%s\3%s%s",
 		(kicked ? "\26" : ""),
 		hash(msg->user), msg->nick,
-		cacheColor(id, msg->params[1]), msg->params[1],
+		cacheGet(id, msg->params[1])->color, msg->params[1],
 		hash(msg->params[0]), msg->params[0],
 		(msg->params[2] ? ": " : ""), (msg->params[2] ?: "")
 	);
@@ -555,7 +555,7 @@ static void handleErrorUserOnChannel(struct Message *msg) {
 	uiFormat(
 		id, Warm, tagTime(msg),
 		"\3%02d%s\3 is already in \3%02d%s\3",
-		cacheColor(id, msg->params[1]), msg->params[1],
+		cacheGet(id, msg->params[1])->color, msg->params[1],
 		hash(msg->params[2]), msg->params[2]
 	);
 }
@@ -571,7 +571,8 @@ static void handleReplyNames(struct Message *msg) {
 		char *nick = &prefixes[strspn(prefixes, network.prefixes)];
 		char *user = strsep(&name, "@");
 		enum Color color = (user ? hash(user) : Default);
-		cacheInsertColor(false, id, nick, color);
+		struct Entry *entry = cacheInsert(false, id, nick);
+		if (user) entry->color = color;
 		if (!replies[ReplyNames] && !replies[ReplyNamesAuto]) continue;
 		ptr = seprintf(
 			ptr, end, "%s\3%02d%s\3", (ptr > buf ? ", " : ""), color, prefixes
@@ -897,7 +898,7 @@ static void handleMode(struct Message *msg) {
 				id, Cold, tagTime(msg),
 				"\3%02d%s\3\t%s \3%02d%c%s\3 %s%s in \3%02d%s\3",
 				hash(msg->user), msg->nick, verb,
-				cacheColor(id, nick), prefix, nick,
+				cacheGet(id, nick)->color, prefix, nick,
 				mode, name, hash(msg->params[0]), msg->params[0]
 			);
 			logFormat(
@@ -1035,7 +1036,7 @@ static void handleReplyBanList(struct Message *msg) {
 			id, Warm, tagTime(msg),
 			"Banned from \3%02d%s\3 since %s by \3%02d%s\3: %s",
 			hash(msg->params[1]), msg->params[1],
-			since, cacheColor(id, msg->params[3]), msg->params[3],
+			since, cacheGet(id, msg->params[3])->color, msg->params[3],
 			msg->params[2]
 		);
 	} else {
@@ -1058,7 +1059,7 @@ static void onList(const char *list, struct Message *msg) {
 			id, Warm, tagTime(msg),
 			"On the \3%02d%s\3 %s list since %s by \3%02d%s\3: %s",
 			hash(msg->params[1]), msg->params[1], list,
-			since, cacheColor(id, msg->params[3]), msg->params[3],
+			since, cacheGet(id, msg->params[3])->color, msg->params[3],
 			msg->params[2]
 		);
 	} else {
@@ -1091,7 +1092,7 @@ static void handleReplyList(struct Message *msg) {
 
 static void handleReplyWhoisUser(struct Message *msg) {
 	require(msg, false, 6);
-	cacheInsertColor(true, Network, msg->params[1], hash(msg->params[2]));
+	cacheInsert(true, Network, msg->params[1])->color = hash(msg->params[2]);
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\tis %s!%s@%s (%s\17)",
@@ -1106,7 +1107,7 @@ static void handleReplyWhoisServer(struct Message *msg) {
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\t%s connected to %s (%s)",
-		cacheColor(Network, msg->params[1]), msg->params[1],
+		cacheGet(Network, msg->params[1])->color, msg->params[1],
 		(replies[ReplyWhowas] ? "was" : "is"), msg->params[2], msg->params[3]
 	);
 }
@@ -1130,7 +1131,7 @@ static void handleReplyWhoisIdle(struct Message *msg) {
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\tis idle for %lu %s%s%s%s",
-		cacheColor(Network, msg->params[1]), msg->params[1],
+		cacheGet(Network, msg->params[1])->color, msg->params[1],
 		idle, unit, (idle != 1 ? "s" : ""),
 		(msg->params[3] ? ", signed on " : ""), (msg->params[3] ? signon : "")
 	);
@@ -1152,7 +1153,7 @@ static void handleReplyWhoisChannels(struct Message *msg) {
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\tis in %s",
-		cacheColor(Network, msg->params[1]), msg->params[1], buf
+		cacheGet(Network, msg->params[1])->color, msg->params[1], buf
 	);
 }
 
@@ -1166,7 +1167,7 @@ static void handleReplyWhoisGeneric(struct Message *msg) {
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\t%s%s%s",
-		cacheColor(Network, msg->params[1]), msg->params[1],
+		cacheGet(Network, msg->params[1])->color, msg->params[1],
 		msg->params[2], (msg->params[3] ? " " : ""), (msg->params[3] ?: "")
 	);
 }
@@ -1180,7 +1181,7 @@ static void handleReplyEndOfWhois(struct Message *msg) {
 
 static void handleReplyWhowasUser(struct Message *msg) {
 	require(msg, false, 6);
-	cacheInsertColor(true, Network, msg->params[1], hash(msg->params[2]));
+	cacheInsert(true, Network, msg->params[1])->color = hash(msg->params[2]);
 	uiFormat(
 		Network, Warm, tagTime(msg),
 		"\3%02d%s\3\twas %s!%s@%s (%s)",
@@ -1200,7 +1201,7 @@ static void handleReplyAway(struct Message *msg) {
 	require(msg, false, 3);
 	// Might be part of a WHOIS response.
 	uint id;
-	if (cacheColor(Network, msg->params[1]) != Default) {
+	if (cacheGet(Network, msg->params[1])->color != Default) {
 		id = Network;
 	} else {
 		id = idFor(msg->params[1]);
@@ -1208,7 +1209,7 @@ static void handleReplyAway(struct Message *msg) {
 	uiFormat(
 		id, (id == Network ? Warm : Cold), tagTime(msg),
 		"\3%02d%s\3\tis away: %s",
-		cacheColor(id, msg->params[1]), msg->params[1], msg->params[2]
+		cacheGet(id, msg->params[1])->color, msg->params[1], msg->params[2]
 	);
 	logFormat(
 		id, tagTime(msg), "%s is away: %s",
@@ -1279,7 +1280,7 @@ static char *colorMentions(char *ptr, char *end, uint id, const char *msg) {
 
 		size_t len = strcspn(msg, ",:<> ");
 		char *p = seprintf(ptr, end, "%.*s", (int)len, msg);
-		enum Color color = cacheColor(id, ptr);
+		enum Color color = cacheGet(id, ptr)->color;
 		if (color != Default) {
 			ptr = seprintf(ptr, end, "\3%02d%.*s\3", color, (int)len, msg);
 		} else {
@@ -1319,7 +1320,7 @@ static void handlePrivmsg(struct Message *msg) {
 	heat = filterCheck(heat, id, msg);
 	if (heat > Warm && !mine && !query) highlight = true;
 	if (!notice && !mine && heat > Ice) {
-		cacheInsertColor(true, id, msg->nick, hash(msg->user));
+		cacheInsert(true, id, msg->nick)->color = hash(msg->user);
 	}
 	if (heat > Ice) urlScan(id, msg->nick, msg->params[1]);
 
diff --git a/window.c b/window.c
index 72a3571..0c675e9 100644
--- a/window.c
+++ b/window.c
@@ -118,7 +118,7 @@ uint windowFor(uint id) {
 		window->thresh = windowThreshold;
 	}
 	window->buffer = bufferAlloc();
-	cacheInsertColor(false, None, idNames[id], idColors[id]);
+	cacheInsert(false, None, idNames[id])->color = idColors[id];
 
 	return windowPush(window);
 }