Handle leaving and kicking from Matrix. Fixes #47
This commit is contained in:
parent
3a571b2c7a
commit
fed6756ae5
@ -142,6 +142,7 @@ func (mx *MatrixHandler) HandleMembership(evt *event.Event) {
|
|||||||
content := evt.Content.AsMember()
|
content := evt.Content.AsMember()
|
||||||
if content.Membership == event.MembershipInvite && id.UserID(evt.GetStateKey()) == mx.as.BotMXID() {
|
if content.Membership == event.MembershipInvite && id.UserID(evt.GetStateKey()) == mx.as.BotMXID() {
|
||||||
mx.HandleBotInvite(evt)
|
mx.HandleBotInvite(evt)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
portal := mx.bridge.GetPortalByMXID(evt.RoomID)
|
portal := mx.bridge.GetPortalByMXID(evt.RoomID)
|
||||||
|
47
portal.go
47
portal.go
@ -1929,6 +1929,35 @@ func (portal *Portal) Delete() {
|
|||||||
portal.bridge.portalsLock.Unlock()
|
portal.bridge.portalsLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) GetMatrixUsers() ([]id.UserID, error) {
|
||||||
|
members, err := portal.MainIntent().JoinedMembers(portal.MXID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to get member list")
|
||||||
|
}
|
||||||
|
var users []id.UserID
|
||||||
|
for userID := range members.Joined {
|
||||||
|
_, isPuppet := portal.bridge.ParsePuppetMXID(userID)
|
||||||
|
if !isPuppet && userID != portal.bridge.Bot.UserID {
|
||||||
|
users = append(users, userID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) CleanupIfEmpty() {
|
||||||
|
users, err := portal.GetMatrixUsers()
|
||||||
|
if err != nil {
|
||||||
|
portal.log.Errorfln("Failed to get Matrix user list to determine if portal needs to be cleaned up: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(users) == 0 {
|
||||||
|
portal.log.Infoln("Room seems to be empty, cleaning up...")
|
||||||
|
portal.Delete()
|
||||||
|
portal.Cleanup(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (portal *Portal) Cleanup(puppetsOnly bool) {
|
func (portal *Portal) Cleanup(puppetsOnly bool) {
|
||||||
if len(portal.MXID) == 0 {
|
if len(portal.MXID) == 0 {
|
||||||
return
|
return
|
||||||
@ -1975,9 +2004,25 @@ func (portal *Portal) HandleMatrixLeave(sender *User) {
|
|||||||
portal.Delete()
|
portal.Delete()
|
||||||
portal.Cleanup(false)
|
portal.Cleanup(false)
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
resp, err := sender.Conn.LeaveGroup(portal.Key.JID)
|
||||||
|
if err != nil {
|
||||||
|
portal.log.Errorfln("Failed to leave group as %s: %v", sender.MXID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
portal.log.Infoln("Leave response:", <-resp)
|
||||||
|
portal.CleanupIfEmpty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) HandleMatrixKick(sender *User, event *event.Event) {
|
func (portal *Portal) HandleMatrixKick(sender *User, event *event.Event) {
|
||||||
// TODO
|
puppet := portal.bridge.GetPuppetByMXID(id.UserID(event.GetStateKey()))
|
||||||
|
if puppet != nil {
|
||||||
|
resp, err := sender.Conn.RemoveMember(portal.Key.JID, []string{puppet.JID})
|
||||||
|
if err != nil {
|
||||||
|
portal.log.Errorfln("Failed to kick %s from group as %s: %v", puppet.JID, sender.MXID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
portal.log.Infoln("Kick %s response: %s", puppet.JID, <-resp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
13
puppet.go
13
puppet.go
@ -23,7 +23,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Rhymen/go-whatsapp"
|
"github.com/Rhymen/go-whatsapp"
|
||||||
|
|
||||||
log "maunium.net/go/maulogger/v2"
|
log "maunium.net/go/maulogger/v2"
|
||||||
|
|
||||||
"maunium.net/go/mautrix/appservice"
|
"maunium.net/go/mautrix/appservice"
|
||||||
@ -34,13 +33,13 @@ import (
|
|||||||
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var userIDRegex *regexp.Regexp
|
||||||
|
|
||||||
func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.WhatsAppID, bool) {
|
func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.WhatsAppID, bool) {
|
||||||
userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$",
|
if userIDRegex == nil {
|
||||||
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
|
userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
|
||||||
bridge.Config.Homeserver.Domain))
|
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
|
||||||
if err != nil {
|
bridge.Config.Homeserver.Domain))
|
||||||
bridge.Log.Warnln("Failed to compile puppet user ID regex:", err)
|
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
match := userIDRegex.FindStringSubmatch(string(mxid))
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
||||||
if match == nil || len(match) != 2 {
|
if match == nil || len(match) != 2 {
|
||||||
|
Loading…
Reference in New Issue
Block a user