Implicitly enable private_chat_portal_meta in encrypted rooms

This commit is contained in:
Tulir Asokan 2022-10-07 21:01:04 +03:00
parent b826aa1bb7
commit 859355a3db
4 changed files with 22 additions and 8 deletions

View file

@ -231,6 +231,7 @@ bridge:
login_shared_secret_map: login_shared_secret_map:
example.com: foobar example.com: foobar
# Should the bridge explicitly set the avatar and room name for private chat portal rooms? # Should the bridge explicitly set the avatar and room name for private chat portal rooms?
# This is implicitly enabled in encrypted rooms.
private_chat_portal_meta: false private_chat_portal_meta: false
# Should group members be synced in parallel? This makes member sync faster # Should group members be synced in parallel? This makes member sync faster
parallel_member_sync: false parallel_member_sync: false
@ -320,7 +321,6 @@ bridge:
allow: false allow: false
# Default to encryption, force-enable encryption in all portals the bridge creates # Default to encryption, force-enable encryption in all portals the bridge creates
# This will cause the bridge bot to be in private chats for the encryption to work properly. # This will cause the bridge bot to be in private chats for the encryption to work properly.
# It is recommended to also set private_chat_portal_meta to true when using this.
default: false default: false
# Whether to use MSC2409/MSC3202 instead of /sync long polling for receiving encryption-related data. # Whether to use MSC2409/MSC3202 instead of /sync long polling for receiving encryption-related data.
appservice: false appservice: false

View file

@ -56,10 +56,19 @@ func (br *WABridge) CreatePrivatePortal(roomID id.RoomID, brInviter bridge.User,
} }
func (br *WABridge) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { func (br *WABridge) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) {
// TODO check if room is already encrypted
var existingEncryption event.EncryptionEventContent
var encryptionEnabled bool
err := portal.MainIntent().StateEvent(roomID, event.StateEncryption, "", &existingEncryption)
if err != nil {
portal.log.Warnfln("Failed to check if encryption is enabled in private chat room %s", roomID)
} else {
encryptionEnabled = existingEncryption.Algorithm == id.AlgorithmMegolmV1
}
portal.MXID = roomID portal.MXID = roomID
portal.Topic = PrivateChatTopic portal.Topic = PrivateChatTopic
_, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic) _, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic)
if portal.bridge.Config.Bridge.PrivateChatPortalMeta { if portal.bridge.Config.Bridge.PrivateChatPortalMeta || br.Config.Bridge.Encryption.Default || encryptionEnabled {
portal.Name = puppet.Displayname portal.Name = puppet.Displayname
portal.AvatarURL = puppet.AvatarURL portal.AvatarURL = puppet.AvatarURL
portal.Avatar = puppet.Avatar portal.Avatar = puppet.Avatar
@ -71,7 +80,7 @@ func (br *WABridge) createPrivatePortalFromInvite(roomID id.RoomID, inviter *Use
portal.log.Infofln("Created private chat portal in %s after invite from %s", roomID, inviter.MXID) portal.log.Infofln("Created private chat portal in %s after invite from %s", roomID, inviter.MXID)
intent := puppet.DefaultIntent() intent := puppet.DefaultIntent()
if br.Config.Bridge.Encryption.Default { if br.Config.Bridge.Encryption.Default || encryptionEnabled {
_, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: br.Bot.UserID}) _, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: br.Bot.UserID})
if err != nil { if err != nil {
portal.log.Warnln("Failed to invite bridge bot to enable e2be:", err) portal.log.Warnln("Failed to invite bridge bot to enable e2be:", err)
@ -80,10 +89,12 @@ func (br *WABridge) createPrivatePortalFromInvite(roomID id.RoomID, inviter *Use
if err != nil { if err != nil {
portal.log.Warnln("Failed to join as bridge bot to enable e2be:", err) portal.log.Warnln("Failed to join as bridge bot to enable e2be:", err)
} }
if !encryptionEnabled {
_, err = intent.SendStateEvent(roomID, event.StateEncryption, "", portal.GetEncryptionEventContent()) _, err = intent.SendStateEvent(roomID, event.StateEncryption, "", portal.GetEncryptionEventContent())
if err != nil { if err != nil {
portal.log.Warnln("Failed to enable e2be:", err) portal.log.Warnln("Failed to enable e2be:", err)
} }
}
br.AS.StateStore.SetMembership(roomID, inviter.MXID, event.MembershipJoin) br.AS.StateStore.SetMembership(roomID, inviter.MXID, event.MembershipJoin)
br.AS.StateStore.SetMembership(roomID, puppet.MXID, event.MembershipJoin) br.AS.StateStore.SetMembership(roomID, puppet.MXID, event.MembershipJoin)
br.AS.StateStore.SetMembership(roomID, br.Bot.UserID, event.MembershipJoin) br.AS.StateStore.SetMembership(roomID, br.Bot.UserID, event.MembershipJoin)

View file

@ -1388,7 +1388,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) puppet := portal.bridge.GetPuppetByJID(portal.Key.JID)
puppet.SyncContact(user, true, false, "creating private chat portal") puppet.SyncContact(user, true, false, "creating private chat portal")
if portal.bridge.Config.Bridge.PrivateChatPortalMeta { if portal.bridge.Config.Bridge.PrivateChatPortalMeta || portal.bridge.Config.Bridge.Encryption.Default {
portal.Name = puppet.Displayname portal.Name = puppet.Displayname
portal.AvatarURL = puppet.AvatarURL portal.AvatarURL = puppet.AvatarURL
portal.Avatar = puppet.Avatar portal.Avatar = puppet.Avatar

View file

@ -265,8 +265,11 @@ func (puppet *Puppet) UpdateName(contact types.ContactInfo, forcePortalSync bool
} }
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) { func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
if puppet.bridge.Config.Bridge.PrivateChatPortalMeta { if puppet.bridge.Config.Bridge.PrivateChatPortalMeta || puppet.bridge.Config.Bridge.Encryption.Allow {
for _, portal := range puppet.bridge.GetAllPortalsByJID(puppet.JID) { for _, portal := range puppet.bridge.GetAllPortalsByJID(puppet.JID) {
if !puppet.bridge.Config.Bridge.PrivateChatPortalMeta && !portal.Encrypted {
continue
}
// Get room create lock to prevent races between receiving contact info and room creation. // Get room create lock to prevent races between receiving contact info and room creation.
portal.roomCreateLock.Lock() portal.roomCreateLock.Lock()
meta(portal) meta(portal)