Add initial support for encrypting messages

This commit is contained in:
Tulir Asokan 2020-04-28 00:58:26 +03:00
parent 0e8e81a607
commit 7d0d701c50
6 changed files with 41 additions and 6 deletions

View File

@ -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

2
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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

View File

@ -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 {

View File

@ -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{