aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-04-28 00:58:26 +0300
committerTulir Asokan <tulir@maunium.net>2020-04-28 00:58:38 +0300
commit7d0d701c5014be43f993d34a52e122f41eedc18d (patch)
treeaeae98e87e4971af21b446d3393547a4562899f7 /matrix
parent0e8e81a6078a98803f1012882f8c176fd3b5204e (diff)
Add initial support for encrypting messages
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go22
-rw-r--r--matrix/rooms/room.go11
-rw-r--r--matrix/sync.go9
3 files changed, 37 insertions, 5 deletions
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{