Kick from MUCs instead of unsubscribing in commands

This commit is contained in:
Bohdan Horbeshko 2025-07-03 13:47:22 -04:00
parent 9721e60b0a
commit 65cc9b1551
3 changed files with 52 additions and 40 deletions

View file

@ -275,11 +275,6 @@ func keyValueString(key, value string) string {
return fmt.Sprintf("%s: %s", key, value)
}
func (c *Client) unsubscribe(chatID int64) error {
args := gateway.SimplePresence(chatID, "unsubscribed")
return c.sendPresence(args...)
}
func (c *Client) usernameOrIDToID(username string) (int64, error) {
userID, err := strconv.ParseInt(username, 10, 64)
// couldn't parse the id, try to lookup as a username
@ -361,7 +356,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
}
for _, id := range c.cache.ChatsKeys() {
c.unsubscribe(id)
c.leaveChat(id)
}
c.Session.Login = ""
@ -974,7 +969,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return err.Error(), true, false
}
err = c.unsubscribe(chatID)
err = c.leaveChat(chatID)
if err != nil {
return err.Error(), true, false
}
@ -985,7 +980,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return err.Error(), true, false
}
err = c.unsubscribe(chatID)
err = c.leaveChat(chatID)
if err != nil {
return err.Error(), true, false
}
@ -1027,7 +1022,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return err.Error(), true, false
}
err = c.unsubscribe(chatID)
err = c.leaveChat(chatID)
if err != nil {
return err.Error(), true, false
}
@ -1043,7 +1038,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return err.Error(), true, false
}
err = c.unsubscribe(chatID)
err = c.leaveChat(chatID)
if err != nil {
return err.Error(), true, false
}

View file

@ -236,20 +236,8 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
if c.Session.MUC {
c.locks.mucCacheLock.Lock()
var myJid string
if c.me != nil {
myJid = gateway.CHATJID(c.me.Id, true)
}
for chatID := range c.mucCache {
c.sendPresence(
gateway.SPFrom(gateway.MUCNODE(chatID)),
gateway.SPResource(c.GetMUCNickname(0)),
gateway.SPType("unavailable"),
gateway.SPMUCAffiliation("none"),
gateway.SPMUCRole("none"),
gateway.SPMUCJid(myJid),
gateway.SPMUCStatusCodes([]uint16{110, 332}),
)
c.kickMeFromMUC(chatID, []uint16{110, 332}, false, nil)
}
c.locks.mucCacheLock.Unlock()
}

View file

@ -654,23 +654,7 @@ func (c *Client) DestroyMUC(chatId int64) error {
return nil
}
var toResources []string
for resource := range mucState.Resources {
toResources = append(toResources, resource)
}
var myJid string
if c.me != nil {
myJid = gateway.CHATJID(c.me.Id, true)
}
c.sendPresence(
gateway.SPFrom(gateway.MUCNODE(chatId)),
gateway.SPResource(c.GetMUCNickname(0)),
gateway.SPToResources(toResources),
gateway.SPMUCAffiliation("none"),
gateway.SPMUCRole("none"),
gateway.SPMUCJid(myJid),
gateway.SPMUCDestroy(""),
)
c.kickMeFromMUC(chatId, nil, true, mucState)
delete(c.mucCache, chatId)
@ -2767,6 +2751,51 @@ func (c *Client) GetChatMembers(chatID int64, limited bool, query string, member
return members, nil
}
func (c *Client) unsubscribe(chatID int64) error {
args := gateway.SimplePresence(chatID, "unsubscribed")
return c.sendPresence(args...)
}
func (c *Client) leaveChat(chatID int64) error {
chat, err := c.GetChatByID(chatID, nil)
if err == nil && c.Session.MUC && c.IsGroup(chat) {
return c.kickMeFromMUC(chatID, []uint16{110, 307}, false, nil)
}
return c.unsubscribe(chatID)
}
func (c *Client) kickMeFromMUC(chatID int64, statusCodes []uint16, destroy bool, mucState *MUCState) error {
var myJid string
if c.me != nil {
myJid = gateway.CHATJID(c.me.Id, true)
}
args := []args.V{
gateway.SPFrom(gateway.MUCNODE(chatID)),
gateway.SPResource(c.GetMUCNickname(0)),
gateway.SPMUCAffiliation("none"),
gateway.SPMUCRole("none"),
gateway.SPMUCJid(myJid),
gateway.SPMUCStatusCodes(statusCodes),
}
if destroy {
var toResources []string
if mucState != nil {
for resource := range mucState.Resources {
toResources = append(toResources, resource)
}
}
args = append(
args,
gateway.SPMUCDestroy(""),
gateway.SPToResources(toResources),
)
} else {
args = append(args, gateway.SPType("unavailable"))
}
return c.sendPresence(args...)
}
// MigrateToMUCs unsubscribes from legacy group chats and invites to MUCs
func (c *Client) MigrateToMUCs() {
var chatIDs []int64