// Copyright (c) 2026 Xessenger // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package bridgekit import ( "context" "time" "github.com/rs/zerolog" "maunium.net/go/mautrix" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/matrix" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // botIntent is the bridge bot as bridgev2 sees it. The bot creates the rooms, invites everybody and // sends room state, so this is where a switched-on user's room is made encrypted and its plaintext // state is kept off the server. type botIntent struct { kit *Kit *matrix.ASIntent } var _ bridgev2.MatrixAPI = (*botIntent)(nil) var _ bridgev2.MarkAsDMMatrixAPI = (*botIntent)(nil) var _ bridgev2.MatrixAPIWithArbitraryRoomState = (*botIntent)(nil) func (b *botIntent) SendMessage(ctx context.Context, roomID id.RoomID, eventType event.Type, content *event.Content, extra *bridgev2.MatrixSendExtra) (*mautrix.RespSendEvent, error) { if b.kit.isEncrypted(ctx, roomID) { b.kit.rewriteContent(ctx, content) } files := b.kit.beforeSend(eventType, content) resp, err := b.ASIntent.SendMessage(ctx, roomID, eventType, content, extra) if err == nil { b.kit.afterSend(ctx, roomID, files, resp, extra) } return resp, err } func (b *botIntent) SendState(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string, content *event.Content, ts time.Time) (*mautrix.RespSendEvent, error) { return b.kit.sendState(ctx, b.ASIntent, roomID, eventType, stateKey, content, ts) } // UploadMedia without a room is a room picture when bridgev2 names it so (it uploads group pictures // before it knows which room shows them). While anybody is switched on a picture is uploaded // encrypted; a room that is not encrypted gets a plaintext copy when its m.room.avatar is sent. // Anything else without a room goes up as it is, to be read without a key (pictureUpload). func (b *botIntent) UploadMedia(ctx context.Context, roomID id.RoomID, data []byte, fileName, mimeType string) (id.ContentURIString, *event.EncryptedFileInfo, error) { if roomID == "" && pictureUpload(fileName) && b.kit.active() && b.kit.ready() { return b.kit.uploadEncryptedPicture(ctx, data, mimeType) } return b.ASIntent.UploadMedia(ctx, roomID, data, fileName, mimeType) } func (b *botIntent) CreateRoom(ctx context.Context, req *mautrix.ReqCreateRoom) (id.RoomID, error) { return b.kit.createRoom(ctx, b.ASIntent, req) } func (b *botIntent) EnsureInvited(ctx context.Context, roomID id.RoomID, userID id.UserID) error { defer Step(ctx, "bot_invite")() encrypted := b.kit.isEncrypted(ctx, roomID) if encrypted { userID = b.kit.wireUser(ctx, userID) } if err := b.ASIntent.EnsureInvited(ctx, roomID, userID); err != nil { return err } if encrypted { b.kit.memberArrived(ctx, roomID, userID) } return nil } func (b *botIntent) MarkAsDM(ctx context.Context, roomID id.RoomID, otherUser id.UserID) error { defer Step(ctx, "mark_dm")() if b.kit.isEncrypted(ctx, roomID) { otherUser = b.kit.wireUser(ctx, otherUser) } return b.ASIntent.MarkAsDM(ctx, roomID, otherUser) } func (b *botIntent) GetStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string) (*event.Event, error) { return b.kit.getState(ctx, b.ASIntent, roomID, eventType, stateKey) } // userIntent is a user's double puppet: what the user did on their phone, sent into Matrix as // them. In an encrypted room it names ghosts by their hashed MXIDs, like the bot. type userIntent struct { kit *Kit *matrix.ASIntent } var _ bridgev2.MatrixAPI = (*userIntent)(nil) var _ bridgev2.MarkAsDMMatrixAPI = (*userIntent)(nil) var _ bridgev2.MatrixAPIWithArbitraryRoomState = (*userIntent)(nil) func (u *userIntent) SendMessage(ctx context.Context, roomID id.RoomID, eventType event.Type, content *event.Content, extra *bridgev2.MatrixSendExtra) (*mautrix.RespSendEvent, error) { if u.kit.isEncrypted(ctx, roomID) { u.kit.rewriteContent(ctx, content) } files := u.kit.beforeSend(eventType, content) resp, err := u.ASIntent.SendMessage(ctx, roomID, eventType, content, extra) if err == nil { u.kit.afterSend(ctx, roomID, files, resp, extra) } return resp, err } func (u *userIntent) SendState(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string, content *event.Content, ts time.Time) (*mautrix.RespSendEvent, error) { return u.kit.sendState(ctx, u.ASIntent, roomID, eventType, stateKey, content, ts) } func (u *userIntent) CreateRoom(ctx context.Context, req *mautrix.ReqCreateRoom) (id.RoomID, error) { return u.kit.createRoom(ctx, u.ASIntent, req) } func (u *userIntent) EnsureJoined(ctx context.Context, roomID id.RoomID, params ...bridgev2.EnsureJoinedParams) error { defer Step(ctx, "user_join")() if err := u.ASIntent.EnsureJoined(ctx, roomID, params...); err != nil { return err } if u.kit.isEncrypted(ctx, roomID) { u.kit.memberArrived(ctx, roomID, u.GetMXID()) } return nil } func (u *userIntent) EnsureInvited(ctx context.Context, roomID id.RoomID, userID id.UserID) error { if u.kit.isEncrypted(ctx, roomID) { userID = u.kit.wireUser(ctx, userID) } return u.ASIntent.EnsureInvited(ctx, roomID, userID) } func (u *userIntent) MarkAsDM(ctx context.Context, roomID id.RoomID, otherUser id.UserID) error { defer Step(ctx, "mark_dm")() if u.kit.isEncrypted(ctx, roomID) { otherUser = u.kit.wireUser(ctx, otherUser) } return u.ASIntent.MarkAsDM(ctx, roomID, otherUser) } func (u *userIntent) GetStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string) (*event.Event, error) { return u.kit.getState(ctx, u.ASIntent, roomID, eventType, stateKey) } // sendState sends room state as the bot or a double puppet: encrypted rooms through // sendEncryptedRoomState; elsewhere a picture uploaded encrypted is replaced by its plaintext copy. func (k *Kit) sendState(ctx context.Context, intent *matrix.ASIntent, roomID id.RoomID, eventType event.Type, stateKey string, content *event.Content, ts time.Time) (*mautrix.RespSendEvent, error) { if k.isEncrypted(ctx, roomID) { return k.sendEncryptedRoomState(ctx, intent, roomID, eventType, stateKey, content, ts) } k.plaintextStatePictures(ctx, roomID, intent, eventType, stateKey, content) return intent.SendState(ctx, roomID, eventType, stateKey, content, ts) } func (k *Kit) getState(ctx context.Context, intent *matrix.ASIntent, roomID id.RoomID, eventType event.Type, stateKey string) (*event.Event, error) { encrypted := k.isEncrypted(ctx, roomID) if encrypted && eventType == event.StateMember { stateKey = k.wireUser(ctx, id.UserID(stateKey)).String() } evt, err := intent.GetStateEvent(ctx, roomID, eventType, stateKey) if encrypted && evt != nil { k.readState(ctx, evt) } return evt, err } // plaintextStatePictures swaps pictures uploaded encrypted for plaintext copies in state bound for a // room that is not encrypted, which shows them as they are. A member event without a name and picture // gets them here the way ASIntent.SendState would fill them in, so that the picture can be swapped. // Pictures are uploaded encrypted only while somebody is switched on, but they stay so in bridgev2's // database, so the swap does not depend on anybody being switched on now. func (k *Kit) plaintextStatePictures(ctx context.Context, roomID id.RoomID, intent *matrix.ASIntent, eventType event.Type, stateKey string, content *event.Content) { if !k.box.Ready() || content == nil { return } if member, ok := content.Parsed.(*event.MemberEventContent); ok && eventType == event.StateMember && member.Displayname == "" && member.AvatarURL == "" { k.fillMember(ctx, roomID, id.UserID(stateKey), member) } k.plaintextPictures(ctx, intent, content) } // plaintextPictures swaps the pictures in a state event's content (a room picture, bridge info or a // member event) for their plaintext copies. A copy that cannot be made leaves the picture out rather // than show a picture nobody can open. func (k *Kit) plaintextPictures(ctx context.Context, intent *matrix.ASIntent, content *event.Content) { if !k.box.Ready() || content == nil { return } plain := func(mxc id.ContentURIString) id.ContentURIString { if mxc == "" { return mxc } plaintext, err := k.plaintextPicture(ctx, intent, mxc) if err != nil { zerolog.Ctx(ctx).Err(err).Str("mxc", string(mxc)).Msg("Failed to make a plaintext copy of an encrypted picture") return "" } return plaintext } switch parsed := content.Parsed.(type) { case *event.RoomAvatarEventContent: parsed.URL = plain(parsed.URL) case *event.BridgeEventContent: parsed.Channel.AvatarURL = plain(parsed.Channel.AvatarURL) if parsed.Network != nil { parsed.Network.AvatarURL = plain(parsed.Network.AvatarURL) } case *event.MemberEventContent: parsed.AvatarURL = plain(parsed.AvatarURL) } } func (k *Kit) fillMember(ctx context.Context, roomID id.RoomID, userID id.UserID, member *event.MemberEventContent) { if previous, err := k.conn.StateStore.TryGetMember(ctx, roomID, userID); err == nil && previous != nil { member.Displayname, member.AvatarURL = previous.Displayname, previous.AvatarURL } else if ghost, err := k.br.GetGhostByMXID(ctx, userID); err == nil && ghost != nil { member.Displayname, member.AvatarURL = ghost.Name, ghost.AvatarMXC } }