// 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" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/matrix" "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // connector is what bridgev2 sees as its Matrix connector. It embeds bridgev2's own, so every // optional interface the original implements stays implemented (bridgev2 and the network connectors // check them with type assertions on Bridge.Matrix), and changes only what names ghosts and reads // room members: in an encrypted room a ghost is its hashed MXID on the wire and its plaintext MXID // for bridgev2. The one bridgev2 command that asserts Bridge.Matrix is bridgev2's own type is // replaced (replaceCommands). type connector struct { *matrix.Connector kit *Kit } var _ bridgev2.MatrixConnectorWithArbitraryRoomState = (*connector)(nil) var _ bridgev2.MatrixConnectorWithNameDisambiguation = (*connector)(nil) // Unwrap returns bridgev2's own Matrix connector, for code that needs its appservice. func (c *connector) Unwrap() *matrix.Connector { return c.Connector } // GhostIntent gives each contact's ghost its two identities (ghost.go). bridgev2's legacy database // migration (mxmain/legacymigrate.go) asserts ghost intents are bridgev2's own type, but it runs only // for bridges that started on the old Python bridges, which no Xessenger bridge did. func (c *connector) GhostIntent(userID networkid.UserID) bridgev2.MatrixAPI { inner := c.Connector.GhostIntent(userID) legacy, ok := inner.(*matrix.ASIntent) if !ok { return inner } return c.kit.newGhostIntent(userID, legacy) } func (c *connector) BotIntent() bridgev2.MatrixAPI { return c.kit.bot } func (c *connector) NewUserIntent(ctx context.Context, userID id.UserID, accessToken string) (bridgev2.MatrixAPI, string, error) { intent, token, err := c.Connector.NewUserIntent(ctx, userID, accessToken) if err != nil || intent == nil { return intent, token, err } as, ok := intent.(*matrix.ASIntent) if !ok { return intent, token, nil } return &userIntent{kit: c.kit, ASIntent: as}, token, nil } // ParseGhostMXID also knows hashed ghosts, by looking them up: their MXID cannot be turned back into // a network ID. One the bridge has never named is not one of its ghosts. func (c *connector) ParseGhostMXID(userID id.UserID) (networkid.UserID, bool) { if c.kit.ghosts.isHashedShape(userID) { return c.kit.hashedGhostID(context.Background(), userID) } return c.Connector.ParseGhostMXID(userID) } // GenerateDeterministicRoomID remembers which portal a room is being created for: bridgev2 passes // the ID to CreateRoom, where it tells whose room it is. func (c *connector) GenerateDeterministicRoomID(key networkid.PortalKey) id.RoomID { roomID := c.Connector.GenerateDeterministicRoomID(key) c.kit.rememberPortalKey(roomID, key) return roomID } func (c *connector) GetMembers(ctx context.Context, roomID id.RoomID) (map[id.UserID]*event.MemberEventContent, error) { members, err := c.Connector.GetMembers(ctx, roomID) if err != nil || !c.kit.isEncrypted(ctx, roomID) { return members, err } translated := make(map[id.UserID]*event.MemberEventContent, len(members)) for userID, member := range members { bridgeID, hide := c.kit.bridgeUser(ctx, userID) if !hide { translated[bridgeID] = member } } return translated, nil } func (c *connector) GetMemberInfo(ctx context.Context, roomID id.RoomID, userID id.UserID) (*event.MemberEventContent, error) { if c.kit.isEncrypted(ctx, roomID) { userID = c.kit.wireUser(ctx, userID) } return c.Connector.GetMemberInfo(ctx, roomID, userID) } func (c *connector) GetPowerLevels(ctx context.Context, roomID id.RoomID) (*event.PowerLevelsEventContent, error) { levels, err := c.Connector.GetPowerLevels(ctx, roomID) if err != nil || !c.kit.isEncrypted(ctx, roomID) { return levels, err } return c.kit.bridgePowerLevels(ctx, levels), nil } func (c *connector) GetStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string) (*event.Event, error) { encrypted := c.kit.isEncrypted(ctx, roomID) if encrypted && eventType == event.StateMember { stateKey = c.kit.wireUser(ctx, id.UserID(stateKey)).String() } evt, err := c.Connector.GetStateEvent(ctx, roomID, eventType, stateKey) if encrypted && evt != nil { c.kit.readState(ctx, evt) } return evt, err } // IsConfusableName compares names in member events, which an encrypted room does not have. func (c *connector) IsConfusableName(ctx context.Context, roomID id.RoomID, userID id.UserID, name string) ([]id.UserID, error) { if c.kit.isEncrypted(ctx, roomID) { return nil, nil } return c.Connector.IsConfusableName(ctx, roomID, userID, name) } // pendingPortal is a portal whose room is about to be created. type pendingPortal struct { key networkid.PortalKey at time.Time } const pendingPortalLifetime = 10 * time.Minute func (k *Kit) rememberPortalKey(roomID id.RoomID, key networkid.PortalKey) { now := time.Now() k.portalKeys.Store(roomID, pendingPortal{key: key, at: now}) k.portalKeys.Range(func(roomID, value any) bool { if now.Sub(value.(pendingPortal).at) > pendingPortalLifetime { k.portalKeys.Delete(roomID) } return true }) } func (k *Kit) takePortalKey(roomID id.RoomID) (networkid.PortalKey, bool) { value, ok := k.portalKeys.LoadAndDelete(roomID) if !ok { return networkid.PortalKey{}, false } return value.(pendingPortal).key, true }