changeset 1097:617c88eb9da9

irccd: fix channel duplicates because of case sensitivity
author David Demelier <markand@malikania.fr>
date Mon, 20 Sep 2021 10:06:54 +0200
parents c3191ab50cee
children 6c15d37b7518
files lib/irccd/channel.c lib/irccd/server.c tests/test-channel.c
diffstat 3 files changed, 12 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/channel.c	Fri Aug 13 14:27:48 2021 +0200
+++ b/lib/irccd/channel.c	Mon Sep 20 10:06:54 2021 +0200
@@ -18,7 +18,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
-#include <string.h>
+#include <strings.h>
 
 #include <utlist.h>
 
@@ -65,7 +65,7 @@
 	struct irc_channel_user *u;
 
 	LL_FOREACH(ch->users, u)
-		if (strcmp(u->nickname, nickname) == 0)
+		if (strcasecmp(u->nickname, nickname) == 0)
 			return u;
 
 	return NULL;
--- a/lib/irccd/server.c	Fri Aug 13 14:27:48 2021 +0200
+++ b/lib/irccd/server.c	Mon Sep 20 10:06:54 2021 +0200
@@ -17,12 +17,14 @@
  */
 
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <poll.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 #include <utlist.h>
 
@@ -76,7 +78,7 @@
 static inline int
 is_self(const struct irc_server *s, const char *nick)
 {
-	return strncmp(s->ident.nickname, nick, strlen(s->ident.nickname)) == 0;
+	return strncasecmp(s->ident.nickname, nick, strlen(s->ident.nickname)) == 0;
 }
 
 static struct irc_channel *
@@ -93,6 +95,10 @@
 	ch->joined = joined;
 	irc_util_strlcpy(ch->name, name, sizeof (ch->name));
 
+	/* Convert to lowercase */
+	for (char *c = ch->name; *c; ++c)
+		*c = tolower(*c);
+
 	if (password)
 		irc_util_strlcpy(ch->password, password, sizeof (ch->password));
 
@@ -862,7 +868,7 @@
 	struct irc_channel *ch;
 
 	LL_FOREACH(s->channels, ch)
-		if (strcmp(ch->name, name) == 0)
+		if (strcasecmp(ch->name, name) == 0)
 			return ch;
 
 	return NULL;
--- a/tests/test-channel.c	Fri Aug 13 14:27:48 2021 +0200
+++ b/tests/test-channel.c	Mon Sep 20 10:06:54 2021 +0200
@@ -91,7 +91,8 @@
 	GREATEST_ASSERT_EQ(1, user->modes);
 	GREATEST_ASSERT_STR_EQ("markand", user->nickname);
 
-	irc_channel_remove(ch, "markand");
+	/* Also test case sensitivity. */
+	irc_channel_remove(ch, "MaRKaND");
 	GREATEST_ASSERT(!ch->users);
 
 	irc_channel_finish(ch);