From 9a9cd33fd83c77a0af6c18b11cef805e105dbe01 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 20 Apr 2019 21:42:56 +0200 Subject: [PATCH] Added KICK handling and cleaned up part handling, fixed test incorrectness. --- channel.go | 4 +++ client.go | 24 ++++++++++++----- client_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 13 deletions(-) diff --git a/channel.go b/channel.go index 89f5ca9..bba988d 100644 --- a/channel.go +++ b/channel.go @@ -55,6 +55,10 @@ func (channel *Channel) Handle(event *Event, client *Client) { { channel.userlist.Remove(event.Nick) } + case "packet.kick": + { + channel.userlist.Remove(event.Arg(1)) + } case "packet.nick": { channel.userlist.Rename(event.Nick, event.Arg(0)) diff --git a/client.go b/client.go index 654da4c..c668bd4 100644 --- a/client.go +++ b/client.go @@ -992,10 +992,6 @@ func (client *Client) handleEvent(event *Event) { } client.handleInTarget(channel, event) - - if channel != nil { - channel.Handle(event, client) - } } case "packet.part": @@ -1005,8 +1001,6 @@ func (client *Client) handleEvent(event *Event) { break } - channel.Handle(event, client) - if event.Nick == client.nick { client.RemoveTarget(channel) } else { @@ -1014,6 +1008,20 @@ func (client *Client) handleEvent(event *Event) { } } + case "packet.kick": + { + channel := client.Channel(event.Arg(0)) + if channel == nil { + break + } + + if event.Arg(1) == client.nick { + client.RemoveTarget(channel) + } else { + client.handleInTarget(channel, event) + } + } + case "packet.quit": { client.handleInTargets(event.Nick, event) @@ -1216,6 +1224,10 @@ func (client *Client) handleInTargets(nick string, event *Event) { } func (client *Client) handleInTarget(target Target, event *Event) { + if target == nil { + return + } + client.mutex.RLock() target.Handle(event, client) diff --git a/client_test.go b/client_test.go index 649f1a3..f58dff2 100644 --- a/client_test.go +++ b/client_test.go @@ -105,9 +105,18 @@ func TestClient(t *testing.T) { return nil }}, {Kind: 'C', Data: "JOIN #Test"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 JOIN #Test *"}, - {Kind: 'S', Data: ":testserver.example.com 353 Test768 = #Test :Test768!~test@127.0.0.1 @+Gisle!gisle@gisle.me"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 JOIN #Test *"}, + {Kind: 'S', Data: ":testserver.example.com 353 Test768 = #Test :Test768!~Tester@127.0.0.1 @+Gisle!irce@10.32.0.1"}, {Kind: 'S', Data: ":testserver.example.com 366 Test768 #Test :End of /NAMES list."}, + {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync. + {Kind: 'C', Data: "PONG :testserver.example.com"}, + {Callback: func() error { + if client.Channel("#Test") == nil { + return errors.New("Channel #Test not found") + } + + return nil + }}, {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +osv Test768 Test768"}, {Kind: 'S', Data: ":Gisle!~irce@10.32.0.1 MODE #Test +N-s "}, {Kind: 'S', Data: ":Test1234!~test2@172.17.37.1 JOIN #Test Test1234"}, @@ -228,10 +237,10 @@ func TestClient(t *testing.T) { {Kind: 'C', Data: "PRIVMSG #Test :\x01ACTION describes stuff\x01"}, {Kind: 'C', Data: "PRIVMSG #Test :Hello, World"}, {Kind: 'C', Data: "PRIVMSG #Test :Hello again"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :\x01ACTION does stuff\x01"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :\x01ACTION describes stuff\x01"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :Hello, World"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 PRIVMSG #Test :Hello again"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :\x01ACTION does stuff\x01"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :\x01ACTION describes stuff\x01"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :Hello, World"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 PRIVMSG #Test :Hello again"}, {Callback: func() error { channel := client.Channel("#Test") if channel == nil { @@ -244,7 +253,7 @@ func TestClient(t *testing.T) { }}, {Kind: 'C', Data: "MODE #Test +N"}, {Kind: 'C', Data: "NPCA #Test Test_NPC :stuffs things"}, - {Kind: 'S', Data: ":Test768!~test@127.0.0.1 MODE #Test +N"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 MODE #Test +N"}, {Kind: 'S', Data: ":\x1FTest_NPC\x1F!Test768@npc.fakeuser.invalid PRIVMSG #Test :\x01ACTION stuffs things\x01"}, {Callback: func() error { channel := client.Channel("#Test") @@ -263,6 +272,55 @@ func TestClient(t *testing.T) { return nil }}, {Kind: 'C', Data: "PART #Test"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 PART #Test"}, + {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync. + {Kind: 'C', Data: "PONG :testserver.example.com"}, + {Callback: func() error { + if client.Channel("#Test") != nil { + return errors.New("#Test is still there.") + } + + return nil + }}, + {Callback: func() error { + client.Join("#Test2") + return nil + }}, + {Kind: 'C', Data: "JOIN #Test2"}, + {Kind: 'S', Data: ":Test768!~Tester@127.0.0.1 JOIN #Test2 *"}, + {Kind: 'S', Data: ":testserver.example.com 353 Test768 = #Test2 :Test768!~Tester@127.0.0.1 +DoomedUser!doom@example.com @+ZealousMod!zeal@example.com"}, + {Kind: 'S', Data: ":testserver.example.com 366 Test768 #Test2 :End of /NAMES list."}, + {Kind: 'S', Data: "PING :testserver.example.com"}, // Ping/Pong to sync. + {Kind: 'C', Data: "PONG :testserver.example.com"}, + {Callback: func() error { + channel := client.Channel("#Test2") + if channel == nil { + return errors.New("Channel #Test2 not found") + } + + return irctest.AssertUserlist(t, channel, "@ZealousMod", "+DoomedUser", "Test768") + }}, + {Kind: 'S', Data: ":ZealousMod!zeal@example.com KICK #Test2 DoomedUser :Kickety kick"}, + {Kind: 'S', Data: "PING :testserver.example.com sync"}, // Ping/Pong to sync. + {Kind: 'C', Data: "PONG :testserver.example.com sync"}, + {Callback: func() error { + channel := client.Channel("#Test2") + if channel == nil { + return errors.New("Channel #Test2 not found") + } + + return irctest.AssertUserlist(t, channel, "@ZealousMod", "Test768") + }}, + {Kind: 'S', Data: ":ZealousMod!zeal@example.com KICK #Test2 Test768 :Kickety kick"}, + {Kind: 'S', Data: "PING :testserver.example.com sync"}, // Ping/Pong to sync. + {Kind: 'C', Data: "PONG :testserver.example.com sync"}, + {Callback: func() error { + if client.Channel("#Test2") != nil { + return errors.New("#Test2 is still there.") + } + + return nil + }}, }, }