Add support for decrypting messages
This commit is contained in:
parent
96bb87e8ac
commit
0e8e81a607
@ -55,6 +55,7 @@ type UserPreferences struct {
|
||||
// Config contains the main config of gomuks.
|
||||
type Config struct {
|
||||
UserID id.UserID `yaml:"mxid"`
|
||||
DeviceID id.DeviceID `yaml:"device_id"`
|
||||
AccessToken string `yaml:"access_token"`
|
||||
HS string `yaml:"homeserver"`
|
||||
|
||||
|
2
go.mod
2
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
|
||||
maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b
|
||||
maunium.net/go/mauview v0.1.0
|
||||
maunium.net/go/tcell v0.1.0
|
||||
)
|
||||
|
15
go.sum
15
go.sum
@ -47,6 +47,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
|
||||
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
|
||||
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tidwall/pretty v1.0.1 h1:WE4RBSZ1x6McVVC8S/Md+Qse8YUv6HRObAx6ke00NY8=
|
||||
github.com/tidwall/pretty v1.0.1/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tidwall/sjson v1.1.1 h1:7h1vk049Jnd5EH9NyzNiEuwYW4b5qgreBbqRC19AS3U=
|
||||
github.com/tidwall/sjson v1.1.1/go.mod h1:yvVuSnpEQv5cYIrO+AT6kw4QVfd5SDZoGIS7/5+fZFs=
|
||||
github.com/zyedidia/clipboard v0.0.0-20190823154308-241f98e9b197 h1:gYTNnAW6azuB3BbA6QYWO/H4F2ABSOjjw3Z03tlXd2c=
|
||||
github.com/zyedidia/clipboard v0.0.0-20190823154308-241f98e9b197/go.mod h1:WDk3p8GiZV9+xFWlSo8qreeoLhW6Ik692rqXk+cNeRY=
|
||||
github.com/zyedidia/poller v1.0.1/go.mod h1:vZXJOHGDcuK08GXhF6IAY0ZFd2WcgOR5DOTp84Uk5eE=
|
||||
@ -73,10 +82,8 @@ gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2/go.mod h1:s1Sn2yZos05Qfs7NK
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
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.3 h1:pm5MQszvzYaLyCEGjyUau4DvzrOw5dT45UAvDLz5zBo=
|
||||
maunium.net/go/mautrix v0.2.0-beta.3/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
|
||||
maunium.net/go/mautrix v0.2.0-beta.4 h1:L7Jpc+8GGc+Qo0DdamACEeU1Ci9G1mergJpsTTgDOUA=
|
||||
maunium.net/go/mautrix v0.2.0-beta.4/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
|
||||
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/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=
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
@ -97,6 +99,7 @@ type GomuksSyncer struct {
|
||||
func NewGomuksSyncer(rooms *rooms.RoomCache) *GomuksSyncer {
|
||||
return &GomuksSyncer{
|
||||
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.Progress.SetSteps(steps + 2 + len(s.globalListeners))
|
||||
|
||||
wait := &sync.WaitGroup{}
|
||||
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 := &sync.WaitGroup{}
|
||||
|
||||
wait.Add(steps)
|
||||
callback := func() {
|
||||
wait.Done()
|
||||
s.Progress.Step()
|
||||
}
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user