gmessages/libgm/event_handler.go

83 lines
2.4 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
"crypto/sha256"
"encoding/base64"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
2023-06-30 09:54:08 +00:00
)
func (r *RPC) deduplicateHash(hash [32]byte) bool {
const recentUpdatesLen = len(r.recentUpdates)
for i := r.recentUpdatesPtr + recentUpdatesLen - 1; i >= r.recentUpdatesPtr; i-- {
if r.recentUpdates[i%recentUpdatesLen] == hash {
return true
}
}
r.recentUpdates[r.recentUpdatesPtr] = hash
r.recentUpdatesPtr = (r.recentUpdatesPtr + 1) % recentUpdatesLen
return false
}
2023-07-15 13:17:02 +00:00
func (r *RPC) logContent(res *pblite.Response) {
if r.client.Logger.Trace().Enabled() && res.Data.Decrypted != nil {
r.client.Logger.Trace().
Str("proto_name", string(res.Data.Decrypted.ProtoReflect().Descriptor().FullName())).
Str("data", base64.StdEncoding.EncodeToString(res.Data.RawDecrypted)).
Msg("Got event")
}
}
func (r *RPC) deduplicateUpdate(response *pblite.Response) bool {
if response.Data.RawDecrypted != nil {
contentHash := sha256.Sum256(response.Data.RawDecrypted)
if r.deduplicateHash(contentHash) {
r.client.Logger.Trace().Hex("data_hash", contentHash[:]).Msg("Ignoring duplicate update")
return true
}
2023-07-15 13:17:02 +00:00
r.logContent(response)
}
return false
}
2023-07-17 13:51:31 +00:00
func (r *RPC) HandleRPCMsg(msg *gmproto.InternalMessage) {
2023-07-16 12:55:30 +00:00
response, decodeErr := pblite.DecryptInternalMessage(msg, r.client.AuthData.RequestCrypto)
if decodeErr != nil {
r.client.Logger.Error().Err(decodeErr).Msg("rpc decrypt msg err")
return
2023-06-30 09:54:08 +00:00
}
if response == nil {
r.client.Logger.Error().Msg("nil response in rpc handler")
return
}
2023-07-15 22:45:57 +00:00
r.client.sessionHandler.queueMessageAck(response.ResponseID)
if r.client.sessionHandler.receiveResponse(response) {
return
}
switch response.BugleRoute {
2023-07-17 13:51:31 +00:00
case gmproto.BugleRoute_PairEvent:
2023-07-15 22:45:57 +00:00
go r.client.handlePairingEvent(response)
2023-07-17 13:51:31 +00:00
case gmproto.BugleRoute_DataEvent:
2023-07-15 22:45:57 +00:00
if r.skipCount > 0 {
r.skipCount--
r.client.Logger.Debug().
Any("action", response.Data.Action).
Int("remaining_skip_count", r.skipCount).
Msg("Skipped DataEvent")
if response.Data.Decrypted != nil {
r.client.Logger.Trace().
Str("proto_name", string(response.Data.Decrypted.ProtoReflect().Descriptor().FullName())).
Str("data", base64.StdEncoding.EncodeToString(response.Data.RawDecrypted)).
Msg("Skipped event data")
}
2023-07-15 22:45:57 +00:00
return
}
2023-07-15 22:45:57 +00:00
r.client.handleUpdatesEvent(response)
default:
2023-07-17 13:43:34 +00:00
r.client.Logger.Debug().Any("res", response).Msg("Got unknown bugle route")
2023-06-30 09:54:08 +00:00
}
}