// 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" "fmt" "maunium.net/go/mautrix/id" "xessenger/bridgekit/internal/storeseal" ) // What an extension (Options.Extensions, such as migrate) reaches through the bridge's Matrix // connector, which it gets as br.Matrix: an extension is handed only the bridge and its router. // E2EEEnabled reports whether a user is switched on in this bridge: the rooms created for them are // encrypted. func (c *connector) E2EEEnabled(_ context.Context, userID id.UserID) bool { return c.kit.user(userID) != nil } // HomeserverPerson reports whether userID is a person's account on this homeserver: not the bridge // bot, nor a ghost of either shape. Such a user in a switched-on user's encrypted room who is neither // the user nor one of the login's members is put out by the member sync (members.go). func (c *connector) HomeserverPerson(userID id.UserID) bool { return c.kit.isHomeserverUser(userID) } // StoreSealer is the sealer of what the bridge database keeps about encrypted rooms (store.go), for // an extension to seal what it keeps there that touches content. Without a store key it has none // (Ready reports false). An extension names its own kind in the additional data of every value, so // no value of it opens as one of bridgekit's or another extension's. func (c *connector) StoreSealer() *storeseal.Sealer { return c.kit.box } // WhenEncrypting runs fn in the background once the bridge's encryption runs, at once when it runs // already, and again each time it starts running after a reset: an extension's pass that sends // encrypted state waits on it (migrate's names of moved rooms). fn may run more than once (given as // encryption starts running, or after a reset): a pass meant to run once a start keeps its own mark. func (c *connector) WhenEncrypting(fn func(ctx context.Context)) { k := c.kit k.whenEncryptingLock.Lock() k.whenEncrypting = append(k.whenEncrypting, fn) running := k.machine() != nil k.whenEncryptingLock.Unlock() if running { go fn(k.backgroundContext()) } } // runWhenEncrypting runs what extensions wait on encryption for, once it runs (cryptoReady). func (k *Kit) runWhenEncrypting() { k.whenEncryptingLock.Lock() defer k.whenEncryptingLock.Unlock() for _, fn := range k.whenEncrypting { go fn(k.backgroundContext()) } } // roomMetaHeld is why SendRoomMeta sent nothing into a room that cannot take metadata now. ownerAway // marks a room whose owner is out of it, as the homeserver has it too, or that has no owner switched // on: it stays so until the owner is back, if ever. type roomMetaHeld struct{ ownerAway bool } func (*roomMetaHeld) Error() string { return "the room's metadata cannot go now" } // Is makes every room held errRoomMetaHeld. func (*roomMetaHeld) Is(target error) bool { return target == errRoomMetaHeld } // RoomMetaOwnerAway reports whether the room is held as its owner is out of it, or it has none. An // extension sees bridgekit through interfaces only: migrate lets the old room of such a group go without // filling in its name, as holding it back would hold back the deletion of the user's later rooms. func (h *roomMetaHeld) RoomMetaOwnerAway() bool { return h.ownerAway } var ( // errRoomMetaHeld is what SendRoomMeta reports for a room it sent nothing into because the room // cannot take metadata now. errRoomMetaHeld error = &roomMetaHeld{} // errRoomOwnerAway is errRoomMetaHeld for a room whose owner is out of it (RoomMetaOwnerAway). errRoomOwnerAway error = &roomMetaHeld{ownerAway: true} ) // SendRoomMeta sends an encrypted room's metadata as its portal has it once the room's lock is held // (sendRoomMeta), where it changed since it last went, and reports whether it went (false: it went // before as it is now) and what failed: a send that read the portal before the extension changed it // does not go after it. A room that is not encrypted, has no portal, or whose owner is not in it gets // nothing, and that is reported as a failure (errRoomMetaHeld), not as metadata that went before: an // extension that saves what the metadata carries only once it went would otherwise save it unsent (a // room whose owner is away gets it as they come in, memberArrived, but only as the portal has it then). // An owner the state store has out of the room is looked for once more on the homeserver // (ownerJoinedOnHomeserver). func (c *connector) SendRoomMeta(ctx context.Context, roomID id.RoomID) (bool, error) { if !c.kit.isEncrypted(ctx, roomID) { return false, fmt.Errorf("%w: the room is not encrypted", errRoomMetaHeld) } else if !c.kit.ownerPresent(ctx, roomID) { if err := c.kit.ownerJoinedOnHomeserver(ctx, roomID); err != nil { return false, err } } return c.kit.sendRoomMetaWent(ctx, roomID) } // ownerJoinedOnHomeserver reads again from the homeserver who is joined to a room whose owner the state // store has out of it (JoinedMembers, which mautrix puts in the state store), and reports the owner out // of it still, or the members not read, as errRoomMetaHeld: a join the bridge missed would otherwise hold // the room's metadata at every start. Only an owner the homeserver has out of the room too is away // (errRoomOwnerAway). One it has joined whose join the state store did not take (mautrix logs a failed // write and goes on) is held to try again, as members not read are: the metadata does not go on the // homeserver's word alone, as the room's key goes to the members the state store has. A room of no owner // (none switched on) is not read. func (k *Kit) ownerJoinedOnHomeserver(ctx context.Context, roomID id.RoomID) error { owner := k.roomOwner(ctx, roomID) if owner == "" { return fmt.Errorf("%w: the room has no owner switched on", errRoomOwnerAway) } else if k.conn.Bot == nil || k.conn.Bot.Client == nil { return fmt.Errorf("%w: the room's owner is not in it, and its members cannot be read", errRoomMetaHeld) } // The client's own read: the intent's would join the room first. joined, err := k.conn.Bot.Client.JoinedMembers(ctx, roomID) if err != nil { return fmt.Errorf("%w: the room's owner is not in it, and its members could not be read again: %w", errRoomMetaHeld, err) } else if k.ownerPresent(ctx, roomID) { return nil } else if joined != nil { if _, in := joined.Joined[owner]; in { return fmt.Errorf("%w: the room's owner is joined on the homeserver, but the state store did not take it", errRoomMetaHeld) } } return fmt.Errorf("%w: the room's owner is not in it, nor joined on the homeserver", errRoomOwnerAway) }