From dcd4ca366cd1230275aa695f5ef5a17fa48b1b42 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 8 Aug 2023 15:11:30 +0300 Subject: [PATCH] Update whatsmeow and ignore events from @lid users --- database/user.go | 6 ++++-- formatting.go | 3 +++ go.mod | 2 +- go.sum | 4 ++-- portal.go | 30 ++++++++++++++++++++++++++---- puppet.go | 3 +++ user.go | 10 ++++++++++ 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/database/user.go b/database/user.go index 088e998..a4ebc35 100644 --- a/database/user.go +++ b/database/user.go @@ -122,14 +122,16 @@ func (user *User) usernamePtr() *string { func (user *User) agentPtr() *uint8 { if !user.JID.IsEmpty() { - return &user.JID.Agent + zero := uint8(0) + return &zero } return nil } func (user *User) devicePtr() *uint8 { if !user.JID.IsEmpty() { - return &user.JID.Device + device := uint8(user.JID.Device) + return &device } return nil } diff --git a/formatting.go b/formatting.go index 3a06575..a60eb99 100644 --- a/formatting.go +++ b/formatting.go @@ -141,6 +141,9 @@ func (formatter *Formatter) ParseWhatsApp(roomID id.RoomID, content *event.Messa continue } else if jid.Server == types.LegacyUserServer { jid.Server = types.DefaultUserServer + } else if jid.Server != types.DefaultUserServer { + // TODO lid support? + continue } mxid, displayname := formatter.getMatrixInfoByJID(roomID, jid) number := "@" + jid.User diff --git a/go.mod b/go.mod index 245154e..4d95023 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/tidwall/gjson v1.14.4 go.mau.fi/util v0.0.0-20230805161919-cf42c11d39c3 - go.mau.fi/whatsmeow v0.0.0-20230804210635-04e4e3a38f73 + go.mau.fi/whatsmeow v0.0.0-20230808115051-056c25e4b485 golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 golang.org/x/image v0.9.0 golang.org/x/net v0.12.0 diff --git a/go.sum b/go.sum index 368bc93..7970dda 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,8 @@ go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c= go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I= go.mau.fi/util v0.0.0-20230805161919-cf42c11d39c3 h1:r3Hrayw0CfmkrDhse7SbClYzq0e7/5P3iKpyV+gW16w= go.mau.fi/util v0.0.0-20230805161919-cf42c11d39c3/go.mod h1:tNxQ2KpD+QhP2MlMfJvFSGSJfDjg4OhIwP7bIK43X/I= -go.mau.fi/whatsmeow v0.0.0-20230804210635-04e4e3a38f73 h1:JK9z+AWKvWXINSqz6wqfF67L/uUMEMJ+Mfw4sVMFnQg= -go.mau.fi/whatsmeow v0.0.0-20230804210635-04e4e3a38f73/go.mod h1:+ObGpFE6cbbY4hKc1FmQH9MVfqaemmlXGXSnwDvCOyE= +go.mau.fi/whatsmeow v0.0.0-20230808115051-056c25e4b485 h1:AaWJS6eKFOQ606PAvrhFeNco4hZ0ullUdQGPJ+G6HCQ= +go.mau.fi/whatsmeow v0.0.0-20230808115051-056c25e4b485/go.mod h1:Iv3G4uv6+HWtqL7XSLRa2dSy077Bnji14IvqUbG+bRo= go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto= go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/portal.go b/portal.go index bd157cf..a0f1f8f 100644 --- a/portal.go +++ b/portal.go @@ -352,6 +352,10 @@ func (portal *Portal) handleMatrixMessageLoopItem(msg PortalMatrixMessage) { } func (portal *Portal) handleReceipt(receipt *events.Receipt, source *User) { + if receipt.Sender.Server != types.DefaultUserServer { + // TODO handle lids + return + } // The order of the message ID array depends on the sender's platform, so we just have to find // the last message based on timestamp. Also, timestamps only have second precision, so if // there are many messages at the same second just mark them all as read, because we don't @@ -703,6 +707,11 @@ func (portal *Portal) handleFakeMessage(msg fakeMessage) { portal.log.Debugfln("Not handling %s (fake): message is duplicate", msg.ID) return } + if msg.Sender.Server != types.DefaultUserServer { + portal.log.Debugfln("Not handling %s (fake): message is from a lid user (%s)", msg.ID, msg.Sender) + // TODO handle lids + return + } intent := portal.bridge.GetPuppetByJID(msg.Sender).IntentFor(portal) if !intent.IsCustomPuppet && portal.IsPrivateChat() && msg.Sender.User == portal.Key.Receiver.User && portal.Key.Receiver != portal.Key.JID { portal.log.Debugfln("Not handling %s (fake): user doesn't have double puppeting enabled", msg.ID) @@ -1046,6 +1055,10 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo) participantMap := make(map[types.JID]bool) userIDs := make([]id.UserID, 0, len(metadata.Participants)) for _, participant := range metadata.Participants { + if participant.JID.IsEmpty() || participant.JID.Server != types.DefaultUserServer { + // TODO handle lids + continue + } portal.log.Debugfln("Syncing participant %s (admin: %t)", participant.JID, participant.IsAdmin) participantMap[participant.JID] = true puppet := portal.bridge.GetPuppetByJID(participant.JID) @@ -1167,7 +1180,7 @@ func (portal *Portal) UpdateAvatar(user *User, setBy types.JID, updateInfo bool) if len(portal.MXID) > 0 { intent := portal.MainIntent() - if !setBy.IsEmpty() { + if !setBy.IsEmpty() && setBy.Server == types.DefaultUserServer { intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal) } _, err := intent.SetRoomAvatar(portal.MXID, portal.AvatarURL) @@ -1205,7 +1218,7 @@ func (portal *Portal) UpdateName(name string, setBy types.JID, updateInfo bool) portal.UpdateBridgeInfo() } else if len(portal.MXID) > 0 { intent := portal.MainIntent() - if !setBy.IsEmpty() { + if !setBy.IsEmpty() && setBy.Server == types.DefaultUserServer { intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal) } _, err := intent.SetRoomName(portal.MXID, name) @@ -1234,7 +1247,7 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.JID, updateInfo bool portal.TopicSet = false intent := portal.MainIntent() - if !setBy.IsEmpty() { + if !setBy.IsEmpty() && setBy.Server == types.DefaultUserServer { intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal) } _, err := intent.SetRoomTopic(portal.MXID, topic) @@ -1405,6 +1418,10 @@ func (portal *Portal) ChangeAdminStatus(jids []types.JID, setAdmin bool) id.Even } changed := portal.applyPowerLevelFixes(levels) for _, jid := range jids { + if jid.Server != types.DefaultUserServer { + // TODO handle lids + continue + } puppet := portal.bridge.GetPuppetByJID(jid) changed = levels.EnsureUserLevel(puppet.MXID, newLevel) || changed @@ -1936,7 +1953,8 @@ func (portal *Portal) addReplyMention(content *event.MessageEventContent, sender if content.Mentions == nil || (sender.IsEmpty() && senderMXID == "") { return } - if senderMXID == "" { + // TODO handle lids + if senderMXID == "" && sender.Server == types.DefaultUserServer { if user := portal.bridge.GetUserByJID(sender); user != nil { senderMXID = user.MXID } else { @@ -3244,6 +3262,10 @@ func (portal *Portal) handleMediaRetry(retry *events.MediaRetry, source *User) { } else { puppet = portal.bridge.GetPuppetByJID(retry.SenderID) } + if puppet == nil { + // TODO handle lids? + return + } intent := puppet.IntentFor(portal) retryData, err := whatsmeow.DecryptMediaRetryNotification(retry, meta.Media.Key) diff --git a/puppet.go b/puppet.go index e1e45d3..0c3f9ac 100644 --- a/puppet.go +++ b/puppet.go @@ -330,6 +330,9 @@ func (puppet *Puppet) updatePortalName() { } func (puppet *Puppet) SyncContact(source *User, onlyIfNoName, shouldHavePushName bool, reason string) { + if puppet == nil { + return + } if onlyIfNoName && len(puppet.Displayname) > 0 && (!shouldHavePushName || puppet.NameQuality > config.NameQualityPhone) { source.EnqueuePuppetResync(puppet) return diff --git a/user.go b/user.go index 5ccfbe5..9c276e3 100644 --- a/user.go +++ b/user.go @@ -896,6 +896,9 @@ func (user *User) HandleEvent(event interface{}) { user.handleCallStart(v.CallCreator, v.CallID, v.Type, v.Timestamp) case *events.IdentityChange: puppet := user.bridge.GetPuppetByJID(v.JID) + if puppet == nil { + return + } portal := user.GetPortalByJID(v.JID) if len(portal.MXID) > 0 && user.bridge.Config.Bridge.IdentityChangeNotices { text := fmt.Sprintf("Your security code with %s changed.", puppet.Displayname) @@ -1221,6 +1224,9 @@ const WATypingTimeout = 15 * time.Second func (user *User) handleChatPresence(presence *events.ChatPresence) { puppet := user.bridge.GetPuppetByJID(presence.Sender) + if puppet == nil { + return + } portal := user.GetPortalByJID(presence.Chat) if puppet == nil || portal == nil || len(portal.MXID) == 0 { return @@ -1341,6 +1347,10 @@ func (user *User) handleGroupUpdate(evt *events.GroupInfo) { log.Debug().Msg("Ignoring group info update in chat with no portal") return } + if evt.Sender != nil && evt.Sender.Server == types.HiddenUserServer { + log.Debug().Str("sender", evt.Sender.String()).Msg("Ignoring group info update from @lid user") + return + } switch { case evt.Announce != nil: log.Debug().Msg("Group announcement mode (message send permission) changed")