From f63818efb8d2ed84f9171a052fb3aa4950a530a4 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Fri, 24 Nov 2023 10:03:08 +1100 Subject: [PATCH 1/3] Fix join and part/leave on replay Without this, on matterircd reconnect & relay, users in channels can be out of sync. This causes issues with trying to auto-complete IRC nicks for example. --- mm-go-irckit/channel.go | 27 ++++++++++++++++++--------- mm-go-irckit/userbridge.go | 28 ++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/mm-go-irckit/channel.go b/mm-go-irckit/channel.go index 7d4c7bd1..b5fc692a 100644 --- a/mm-go-irckit/channel.go +++ b/mm-go-irckit/channel.go @@ -162,14 +162,17 @@ func (ch *channel) Part(u *User, text string) { if _, ok := ch.usersIdx[u.ID()]; !ok { ch.mu.Unlock() - + for _, to := range ch.usersIdx { + if !to.Ghost { + to.Encode(msg) //nolint:errcheck + } + } u.Encode(&irc.Message{ Prefix: ch.Prefix(), Command: irc.ERR_NOTONCHANNEL, Params: []string{ch.name}, - Trailing: "You're not on that channel", + Trailing: "User not on that channel", }) - return } @@ -335,8 +338,20 @@ func (ch *channel) Join(u *User) error { return nil } + msg := &irc.Message{ + Prefix: u.Prefix(), + Command: irc.JOIN, + Params: []string{ch.name}, + } + if _, exists := ch.usersIdx[u.ID()]; exists { ch.mu.Unlock() + for _, to := range ch.usersIdx { + // only send join messages to real users + if !to.Ghost { + to.Encode(msg) //nolint:errcheck + } + } return nil } @@ -354,12 +369,6 @@ func (ch *channel) Join(u *User) error { return nil } - msg := &irc.Message{ - Prefix: u.Prefix(), - Command: irc.JOIN, - Params: []string{ch.name}, - } - // send regular users a notification of the join ch.mu.RLock() diff --git a/mm-go-irckit/userbridge.go b/mm-go-irckit/userbridge.go index e6511d54..5018d1f4 100644 --- a/mm-go-irckit/userbridge.go +++ b/mm-go-irckit/userbridge.go @@ -698,9 +698,6 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt // traverse the order in reverse for i := len(mmPostList.Order) - 1; i >= 0; i-- { p := mmPostList.Posts[mmPostList.Order[i]] - if p.Type == model.PostTypeJoinLeave { - continue - } if p.DeleteAt > p.CreateAt { continue @@ -725,8 +722,31 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt nick = botname } - if p.Type == model.PostTypeAddToTeam || p.Type == model.PostTypeRemoveFromTeam { + switch { + case p.Type == model.PostTypeAddToTeam: + nick = systemUser + ghost := u.createUserFromInfo(user) + u.Srv.Channel(brchannel.ID).Join(ghost) //nolint:errcheck + case p.Type == model.PostTypeRemoveFromTeam: nick = systemUser + ghost := u.createUserFromInfo(user) + u.Srv.Channel(brchannel.ID).Part(ghost, "") + case p.Type == model.PostTypeJoinChannel: + ghost := u.createUserFromInfo(user) + u.Srv.Channel(brchannel.ID).Join(ghost) //nolint:errcheck + case p.Type == model.PostTypeLeaveChannel: + ghost := u.createUserFromInfo(user) + u.Srv.Channel(brchannel.ID).Part(ghost, "") + case p.Type == model.PostTypeAddToChannel: + if addedUserID, ok := props["addedUserId"].(string); ok { + ghost := u.createUserFromInfo(u.br.GetUser(addedUserID)) + u.Srv.Channel(brchannel.ID).Join(ghost) //nolint:errcheck + } + case p.Type == model.PostTypeRemoveFromChannel: + if removedUserID, ok := props["removedUserId"].(string); ok { + ghost := u.createUserFromInfo(u.br.GetUser(removedUserID)) + u.Srv.Channel(brchannel.ID).Part(ghost, "") + } } for _, post := range strings.Split(p.Message, "\n") { From 757864a5d9e472841e682bdbbeebf39134a6c55f Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Wed, 13 Dec 2023 19:46:00 +1100 Subject: [PATCH 2/3] Fix self-join causing IRC to re-join fresh --- mm-go-irckit/channel.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm-go-irckit/channel.go b/mm-go-irckit/channel.go index b5fc692a..86ddac51 100644 --- a/mm-go-irckit/channel.go +++ b/mm-go-irckit/channel.go @@ -163,7 +163,7 @@ func (ch *channel) Part(u *User, text string) { if _, ok := ch.usersIdx[u.ID()]; !ok { ch.mu.Unlock() for _, to := range ch.usersIdx { - if !to.Ghost { + if !to.Ghost && to.Nick != u.Nick { to.Encode(msg) //nolint:errcheck } } @@ -348,7 +348,7 @@ func (ch *channel) Join(u *User) error { ch.mu.Unlock() for _, to := range ch.usersIdx { // only send join messages to real users - if !to.Ghost { + if !to.Ghost && to.Nick != u.Nick { to.Encode(msg) //nolint:errcheck } } From 2d3d4309fc79b5f4e66716010f0e57a0dbb11fde Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Fri, 7 Jun 2024 18:09:17 +1000 Subject: [PATCH 3/3] Fix duplicate JOIN/PART msgs causing crashes --- mm-go-irckit/channel.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/mm-go-irckit/channel.go b/mm-go-irckit/channel.go index 86ddac51..4cb57799 100644 --- a/mm-go-irckit/channel.go +++ b/mm-go-irckit/channel.go @@ -162,17 +162,14 @@ func (ch *channel) Part(u *User, text string) { if _, ok := ch.usersIdx[u.ID()]; !ok { ch.mu.Unlock() - for _, to := range ch.usersIdx { - if !to.Ghost && to.Nick != u.Nick { - to.Encode(msg) //nolint:errcheck - } - } + u.Encode(&irc.Message{ Prefix: ch.Prefix(), Command: irc.ERR_NOTONCHANNEL, Params: []string{ch.name}, Trailing: "User not on that channel", }) + return } @@ -338,20 +335,8 @@ func (ch *channel) Join(u *User) error { return nil } - msg := &irc.Message{ - Prefix: u.Prefix(), - Command: irc.JOIN, - Params: []string{ch.name}, - } - if _, exists := ch.usersIdx[u.ID()]; exists { ch.mu.Unlock() - for _, to := range ch.usersIdx { - // only send join messages to real users - if !to.Ghost && to.Nick != u.Nick { - to.Encode(msg) //nolint:errcheck - } - } return nil } @@ -369,6 +354,12 @@ func (ch *channel) Join(u *User) error { return nil } + msg := &irc.Message{ + Prefix: u.Prefix(), + Command: irc.JOIN, + Params: []string{ch.name}, + } + // send regular users a notification of the join ch.mu.RLock()