From 40a9c2c6c817144504ba4fec38145b71f2e78382 Mon Sep 17 00:00:00 2001
From: tazjin <tazjin@tvl.su>
Date: Tue, 28 May 2024 22:06:25 +0300
Subject: [PATCH 05/10] Check if IRC messages come from the expected channel
 (#422)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Explicitly check that the channel from which a message is
received matches the channel that is configured in teleirc.

Signed-off-by: Alexander Miroshnichenko <alex@millerson.name>
---
 internal/handlers/irc/handlers.go      |  9 +++--
 internal/handlers/irc/handlers_test.go | 50 ++++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/internal/handlers/irc/handlers.go b/internal/handlers/irc/handlers.go
index 4c3332568379..5f0b16dad2ec 100644
--- a/internal/handlers/irc/handlers.go
+++ b/internal/handlers/irc/handlers.go
@@ -113,13 +113,16 @@ and channel messages. However, it only cares about channel messages
 */
 func messageHandler(c ClientInterface) func(*girc.Client, girc.Event) {
 	var colorStripper = regexp.MustCompile(`[\x02\x1F\x0F\x16]|\x03(\d\d?(,\d\d?)?)?`)
+	var ircChannel = c.IRCSettings().Channel
 
 	return func(gc *girc.Client, e girc.Event) {
 		c.Logger().LogDebug("messageHandler triggered")
-		// Only send if user is not in blacklist
-		if !(checkBlacklist(c, e.Source.Name)) {
 
-			if e.IsFromChannel() {
+		// Only send if user is not in blacklist ...
+		if !(checkBlacklist(c, e.Source.Name)) {
+			// ... and if the channel matches. Array index is safe because IsFromChannel
+			// itself does it this way.
+			if e.IsFromChannel() && e.Params[0] == ircChannel {
 				formatted := ""
 				if e.IsAction() {
 					msg := e.Last()
diff --git a/internal/handlers/irc/handlers_test.go b/internal/handlers/irc/handlers_test.go
index afab7cf2e460..0a2460753377 100644
--- a/internal/handlers/irc/handlers_test.go
+++ b/internal/handlers/irc/handlers_test.go
@@ -937,7 +937,8 @@ func TestMessageHandlerInBlacklist(t *testing.T) {
 	mockClient.
 		EXPECT().
 		IRCSettings().
-		Return(&ircSettings)
+		Return(&ircSettings).
+		MaxTimes(2)
 	mockClient.
 		EXPECT().
 		SendToTg(gomock.Any()).
@@ -972,7 +973,8 @@ func TestMessageHandlerNotChannel(t *testing.T) {
 	mockClient.
 		EXPECT().
 		IRCSettings().
-		Return(&ircSettings)
+		Return(&ircSettings).
+		MaxTimes(2)
 	mockClient.
 		EXPECT().
 		SendToTg(gomock.Any()).
@@ -1043,6 +1045,7 @@ func TestMessageHandlerFull(t *testing.T) {
 		IRCBlacklist: []string{},
 		Prefix:       "<<",
 		Suffix:       ">>",
+		Channel:      "#testchannel",
 	}
 
 	mockClient := NewMockClientInterface(ctrl)
@@ -1076,3 +1079,46 @@ func TestMessageHandlerFull(t *testing.T) {
 		},
 	})
 }
+
+func TestMessageHandlerWrongChannel(t *testing.T) {
+	ctrl := gomock.NewController(t)
+
+	defer ctrl.Finish()
+
+	ircSettings := internal.IRCSettings{
+		IRCBlacklist: []string{},
+		Channel:      "#testchannel",
+	}
+
+	mockClient := NewMockClientInterface(ctrl)
+	mockLogger := internal.NewMockDebugLogger(ctrl)
+	mockClient.
+		EXPECT().
+		Logger().
+		Return(mockLogger)
+	mockLogger.
+		EXPECT().
+		LogDebug(gomock.Eq("messageHandler triggered"))
+	mockClient.
+		EXPECT().
+		IRCSettings().
+		Return(&ircSettings).
+		MaxTimes(2)
+	mockClient.
+		EXPECT().
+		SendToTg(gomock.Any()).
+		MaxTimes(0)
+
+	myHandler := messageHandler(mockClient)
+	myHandler(&girc.Client{}, girc.Event{
+		Source: &girc.Source{
+			Name: "SomeUser",
+		},
+		// Need to be PRIVMSG
+		Command: girc.PRIVMSG,
+		Params: []string{
+			"#otherchannel",
+			"a message",
+		},
+	})
+}
-- 
2.41.0

