From 22681875f32fa97f65c9a52e2ee666932706ce95 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 5 May 2020 18:39:28 +0300 Subject: Update mautrix-go and give crypto module access to state store --- matrix/matrix.go | 18 +++++++++++++++--- matrix/rooms/roomcache.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index 27ed053..fe1aaa5 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -139,7 +139,7 @@ func (c *Container) InitClient() error { if err != nil { return err } - c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore) + c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore, c.config.Rooms) err = c.crypto.Load() if err != nil { return err @@ -301,7 +301,7 @@ func (c *Container) Stop() { } c.history = nil debug.Print("Flushing crypto store") - err = c.crypto.Store.Flush() + err = c.crypto.CryptoStore.Flush() if err != nil { debug.Print("Error flushing crypto store:", err) } @@ -356,6 +356,9 @@ func (c *Container) OnLogin() { debug.Print("Initializing syncer") c.syncer = NewGomuksSyncer(c.config.Rooms) c.syncer.OnSync(c.crypto.ProcessSyncResponse) + c.syncer.OnEventType(event.StateMember, func(source EventSource, evt *event.Event) { + c.crypto.HandleMemberEvent(evt) + }) c.syncer.OnEventType(event.EventMessage, c.HandleMessage) c.syncer.OnEventType(event.EventEncrypted, c.HandleEncrypted) c.syncer.OnEventType(event.EventSticker, c.HandleMessage) @@ -993,11 +996,20 @@ func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*muksevt.Event, e return nil, err } debug.Printf("Loaded %d events for %s from server from %s to %s", len(resp.Chunk), room.ID, resp.Start, resp.End) - for _, evt := range resp.Chunk { + for i, evt := range resp.Chunk { err := evt.Content.ParseRaw(evt.Type) if err != nil { debug.Printf("Failed to unmarshal content of event %s (type %s) by %s in %s: %v\n%s", evt.ID, evt.Type.Repr(), evt.Sender, evt.RoomID, err, string(evt.Content.VeryRaw)) } + + if evt.Type == event.EventEncrypted { + decrypted, err := c.crypto.DecryptMegolmEvent(evt) + if err != nil { + debug.Print("Failed to decrypt event:", err) + } else { + resp.Chunk[i] = decrypted + } + } } for _, evt := range resp.State { room.UpdateState(evt) diff --git a/matrix/rooms/roomcache.go b/matrix/rooms/roomcache.go index ffdcad1..d66078c 100644 --- a/matrix/rooms/roomcache.go +++ b/matrix/rooms/roomcache.go @@ -27,6 +27,7 @@ import ( sync "github.com/sasha-s/go-deadlock" "maunium.net/go/gomuks/debug" + "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) @@ -67,6 +68,26 @@ func (cache *RoomCache) EnableUnloading() { cache.noUnload = false } +func (cache *RoomCache) IsEncrypted(roomID id.RoomID) bool { + room := cache.Get(roomID) + return room != nil && room.Encrypted +} + +func (cache *RoomCache) FindSharedRooms(userID id.UserID) (shared []id.RoomID) { + cache.Lock() + for _, room := range cache.Map { + if !room.Encrypted { + continue + } + member, ok := room.GetMembers()[userID] + if ok && member.Membership == event.MembershipJoin { + shared = append(shared, room.ID) + } + } + cache.Unlock() + return +} + func (cache *RoomCache) LoadList() error { cache.Lock() defer cache.Unlock() -- cgit v1.2.3