From 7d0d701c5014be43f993d34a52e122f41eedc18d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 28 Apr 2020 00:58:26 +0300 Subject: [PATCH] Add initial support for encrypting messages --- config/config.go | 1 + go.mod | 2 +- go.sum | 2 ++ matrix/matrix.go | 22 +++++++++++++++++++++- matrix/rooms/room.go | 11 +++++++++++ matrix/sync.go | 9 +++++---- 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 8234edc..e9e1864 100644 --- a/config/config.go +++ b/config/config.go @@ -118,6 +118,7 @@ func (config *Config) DeleteSession() { config.AuthCache.NextBatch = "" config.AuthCache.InitialSyncDone = false config.AccessToken = "" + config.DeviceID = "" config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID) config.PushRules = nil diff --git a/go.mod b/go.mod index 8564081..95879d3 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 gopkg.in/yaml.v2 v2.2.8 - maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b + maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8 maunium.net/go/mauview v0.1.0 maunium.net/go/tcell v0.1.0 ) diff --git a/go.sum b/go.sum index 9d0874b..f3ca71b 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b h1:s3+wlMmmtpPUoOGVyS5nyR62htnwD/TEfA0NVbDk7zc= maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y= +maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8 h1:xflYDdpEonVTaw1Diq4z3ZK72Y8/TutiQgKOrHgfOCA= +maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y= maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg= maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U= maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM= diff --git a/matrix/matrix.go b/matrix/matrix.go index 8804103..3d1b279 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -870,8 +870,28 @@ func (c *Container) Redact(roomID id.RoomID, eventID id.EventID, reason string) func (c *Container) SendEvent(evt *muksevt.Event) (id.EventID, error) { defer debug.Recover() - c.client.UserTyping(evt.RoomID, false, 0) + _, _ = c.client.UserTyping(evt.RoomID, false, 0) c.typing = 0 + room := c.GetRoom(evt.RoomID) + if room != nil && room.Encrypted { + encrypted, err := c.crypto.EncryptMegolmEvent(evt.RoomID, evt.Type, evt.Content) + if err != nil { + if err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession { + return "", err + } + debug.Print("Got", err, "while trying to encrypt message, sharing group session and trying again...") + err = c.crypto.ShareGroupSession(room.ID, room.GetMemberList()) + if err != nil { + return "", err + } + encrypted, err = c.crypto.EncryptMegolmEvent(evt.RoomID, evt.Type, evt.Content) + if err != nil { + return "", err + } + } + evt.Type = event.EventEncrypted + evt.Content = event.Content{Parsed: encrypted} + } resp, err := c.client.SendMessageEvent(evt.RoomID, evt.Type, &evt.Content, mautrix.ReqSendEvent{TransactionID: evt.Unsigned.TransactionID}) if err != nil { return "", err diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go index 45222e1..d5d1d8f 100644 --- a/matrix/rooms/room.go +++ b/matrix/rooms/room.go @@ -650,6 +650,17 @@ func (room *Room) GetMembers() map[id.UserID]*Member { return room.memberCache } +func (room *Room) GetMemberList() []id.UserID { + members := room.GetMembers() + memberList := make([]id.UserID, len(members)) + index := 0 + for userID, _ := range members { + memberList[index] = userID + index++ + } + return memberList +} + // GetMember returns the member with the given MXID. // If the member doesn't exist, nil is returned. func (room *Room) GetMember(userID id.UserID) *Member { diff --git a/matrix/sync.go b/matrix/sync.go index 415748e..2136088 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -83,7 +83,7 @@ func (es EventSource) String() string { } type EventHandler func(source EventSource, event *event.Event) -type SyncHandler func(resp *mautrix.RespSync) +type SyncHandler func(resp *mautrix.RespSync, since string) type GomuksSyncer struct { rooms *rooms.RoomCache @@ -122,7 +122,7 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err s.Progress.Step() } wait.Add(len(s.globalListeners)) - s.notifyGlobalListeners(res, callback) + s.notifyGlobalListeners(res, since, callback) wait.Wait() s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence) @@ -158,10 +158,10 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err return } -func (s *GomuksSyncer) notifyGlobalListeners(res *mautrix.RespSync, callback func()) { +func (s *GomuksSyncer) notifyGlobalListeners(res *mautrix.RespSync, since string, callback func()) { for _, listener := range s.globalListeners { go func(listener SyncHandler) { - listener(res) + listener(res, since) callback() }(listener) } @@ -288,6 +288,7 @@ func (s *GomuksSyncer) GetFilterJSON(_ id.UserID) *mautrix.Filter { event.StateCanonicalAlias, event.StatePowerLevels, event.StateTombstone, + event.StateEncryption, }, }, Timeline: mautrix.FilterPart{