diff options
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/matrix.go | 48 | ||||
-rw-r--r-- | matrix/rooms/room.go | 2 | ||||
-rw-r--r-- | matrix/sync.go | 45 |
3 files changed, 80 insertions, 15 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go index d0fd2f4..8804103 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -37,6 +37,7 @@ import ( "github.com/pkg/errors" "maunium.net/go/mautrix" + "maunium.net/go/mautrix/crypto" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" @@ -55,6 +56,7 @@ import ( // It is used for all Matrix calls from the UI and Matrix event handlers. type Container struct { client *mautrix.Client + crypto *crypto.OlmMachine syncer *GomuksSyncer gmx ifc.Gomuks ui ifc.GomuksUI @@ -88,6 +90,24 @@ func (log mxLogger) Debugfln(message string, args ...interface{}) { debug.Printf("[Matrix] "+message, args...) } +type cryptoLogger struct{} + +func (c cryptoLogger) Error(message string, args ...interface{}) { + debug.Printf("[Crypto/Error] "+message, args...) +} + +func (c cryptoLogger) Warn(message string, args ...interface{}) { + debug.Printf("[Crypto/Warn] "+message, args...) +} + +func (c cryptoLogger) Debug(message string, args ...interface{}) { + debug.Printf("[Crypto/Debug] "+message, args...) +} + +func (c cryptoLogger) Trace(message string, args ...interface{}) { + debug.Printf("[Crypto/Trace] "+message, args...) +} + // InitClient initializes the mautrix client and connects to the homeserver specified in the config. func (c *Container) InitClient() error { if len(c.config.HS) == 0 { @@ -97,6 +117,7 @@ func (c *Container) InitClient() error { if c.client != nil { c.Stop() c.client = nil + c.crypto = nil } var mxid id.UserID @@ -112,6 +133,17 @@ func (c *Container) InitClient() error { return err } c.client.Logger = mxLogger{} + c.client.DeviceID = c.config.DeviceID + + cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.CacheDir, "crypto.gob")) + if err != nil { + return err + } + c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore) + err = c.crypto.Load() + if err != nil { + return err + } if c.history == nil { c.history, err = NewHistoryManager(c.config.HistoryPath) @@ -159,7 +191,9 @@ func (c *Container) PasswordLogin(user, password string) error { func (c *Container) finishLogin(resp *mautrix.RespLogin) { c.client.SetCredentials(resp.UserID, resp.AccessToken) + c.client.DeviceID = resp.DeviceID c.config.UserID = resp.UserID + c.config.DeviceID = resp.DeviceID c.config.AccessToken = resp.AccessToken c.config.Save() @@ -250,6 +284,7 @@ func (c *Container) Logout() { c.config.DeleteSession() c.Stop() c.client = nil + c.crypto = nil c.ui.OnLogout() } @@ -315,8 +350,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.EventMessage, c.HandleMessage) - c.syncer.OnEventType(event.EventEncrypted, c.HandleMessage) + c.syncer.OnEventType(event.EventEncrypted, c.HandleEncrypted) c.syncer.OnEventType(event.EventSticker, c.HandleMessage) c.syncer.OnEventType(event.EventReaction, c.HandleMessage) c.syncer.OnEventType(event.EventRedaction, c.HandleRedaction) @@ -516,6 +552,16 @@ func (c *Container) HandleReaction(room *rooms.Room, reactsTo id.EventID, reactE } } +func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) { + evt, err := c.crypto.DecryptMegolmEvent(mxEvent) + if err != nil { + debug.Print("Failed to decrypt event:", err) + return + } + debug.Print("!!!!!", evt) + c.HandleMessage(source, evt) +} + // HandleMessage is the event handler for the m.room.message timeline event. func (c *Container) HandleMessage(source EventSource, mxEvent *event.Event) { room := c.GetOrCreateRoom(mxEvent.RoomID) diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go index 0238cfb..45222e1 100644 --- a/matrix/rooms/room.go +++ b/matrix/rooms/room.go @@ -412,7 +412,7 @@ func (room *Room) UpdateState(evt *event.Event) { case *event.TopicEventContent: room.topicCache = content.Topic case *event.EncryptionEventContent: - if content.Algorithm == event.AlgorithmMegolmV1 { + if content.Algorithm == id.AlgorithmMegolmV1 { room.Encrypted = true } } diff --git a/matrix/sync.go b/matrix/sync.go index 85de68c..415748e 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -83,9 +83,11 @@ func (es EventSource) String() string { } type EventHandler func(source EventSource, event *event.Event) +type SyncHandler func(resp *mautrix.RespSync) type GomuksSyncer struct { rooms *rooms.RoomCache + globalListeners []SyncHandler listeners map[event.Type][]EventHandler // event type to listeners array FirstSyncDone bool InitDoneCallback func() @@ -96,10 +98,11 @@ type GomuksSyncer struct { // NewGomuksSyncer returns an instantiated GomuksSyncer func NewGomuksSyncer(rooms *rooms.RoomCache) *GomuksSyncer { return &GomuksSyncer{ - rooms: rooms, - listeners: make(map[event.Type][]EventHandler), - FirstSyncDone: false, - Progress: StubSyncingModal{}, + rooms: rooms, + globalListeners: []SyncHandler{}, + listeners: make(map[event.Type][]EventHandler), + FirstSyncDone: false, + Progress: StubSyncingModal{}, } } @@ -109,23 +112,26 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err s.rooms.DisableUnloading() } debug.Print("Received sync response") + s.Progress.SetMessage("Processing sync response") steps := len(res.Rooms.Join) + len(res.Rooms.Invite) + len(res.Rooms.Leave) - s.Progress.SetSteps(steps + 2) - s.Progress.SetMessage("Processing global events") - s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence) - s.Progress.Step() - s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData) - s.Progress.Step() + s.Progress.SetSteps(steps + 2 + len(s.globalListeners)) wait := &sync.WaitGroup{} - - wait.Add(steps) callback := func() { wait.Done() s.Progress.Step() } + wait.Add(len(s.globalListeners)) + s.notifyGlobalListeners(res, callback) + wait.Wait() + + s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence) + s.Progress.Step() + s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData) + s.Progress.Step() + + wait.Add(steps) - s.Progress.SetMessage("Processing room events") for roomID, roomData := range res.Rooms.Join { go s.processJoinedRoom(roomID, roomData, callback) } @@ -152,6 +158,15 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err return } +func (s *GomuksSyncer) notifyGlobalListeners(res *mautrix.RespSync, callback func()) { + for _, listener := range s.globalListeners { + go func(listener SyncHandler) { + listener(res) + callback() + }(listener) + } +} + func (s *GomuksSyncer) processJoinedRoom(roomID id.RoomID, roomData mautrix.SyncJoinedRoom, callback func()) { defer debug.Recover() room := s.rooms.GetOrCreate(roomID) @@ -239,6 +254,10 @@ func (s *GomuksSyncer) OnEventType(eventType event.Type, callback EventHandler) s.listeners[eventType] = append(s.listeners[eventType], callback) } +func (s *GomuksSyncer) OnSync(callback SyncHandler) { + s.globalListeners = append(s.globalListeners, callback) +} + func (s *GomuksSyncer) notifyListeners(source EventSource, evt *event.Event) { listeners, exists := s.listeners[evt.Type] if !exists { |