From 689f112d8f32cbe06bf95b3df008b27230a4986a Mon Sep 17 00:00:00 2001 From: Nikos Filippakis Date: Mon, 29 Jun 2020 15:54:24 +0200 Subject: [PATCH] Consider case where device ID is empty and log a warning Signed-off-by: Nikos Filippakis --- clients/bot_client.go | 32 +++++++++++++++++++++++++++++++- clients/clients.go | 27 ++++----------------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/clients/bot_client.go b/clients/bot_client.go index 4c01cc6..bcae08d 100644 --- a/clients/bot_client.go +++ b/clients/bot_client.go @@ -1,6 +1,8 @@ package clients import ( + "time" + "github.com/matrix-org/go-neb/api" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" @@ -38,7 +40,11 @@ func (botClient *BotClient) InitOlmMachine(client *mautrix.Client, nebStore *mat cryptoStore = sqlCryptoStore cryptoLogger.Debug("Using SQL backend as the crypto store") } else { - cryptoStore, err = crypto.NewGobStore(client.DeviceID.String() + ".gob") + deviceID := client.DeviceID.String() + if deviceID == "" { + deviceID = "_empty_device_id" + } + cryptoStore, err = crypto.NewGobStore(deviceID + ".gob") if err != nil { return } @@ -111,3 +117,27 @@ func (botClient *BotClient) SendMessageEvent(roomID id.RoomID, evtType mevt.Type } return botClient.Client.SendMessageEvent(roomID, evtType, content, extra...) } + +// Sync loops to keep syncing the client with the homeserver by calling the /sync endpoint. +func (botClient *BotClient) Sync() { + // Get the state store up to date + resp, err := botClient.SyncRequest(30000, "", "", true, mevt.PresenceOnline) + if err != nil { + log.WithError(err).Error("Error performing initial sync") + return + } + botClient.stateStore.UpdateStateStore(resp) + + for { + if e := botClient.Client.Sync(); e != nil { + log.WithFields(log.Fields{ + log.ErrorKey: e, + "user_id": botClient.config.UserID, + }).Error("Fatal Sync() error") + time.Sleep(10 * time.Second) + } else { + log.WithField("user_id", botClient.config.UserID).Info("Stopping Sync()") + return + } + } +} diff --git a/clients/clients.go b/clients/clients.go index 52e7fa8..8167cda 100644 --- a/clients/clients.go +++ b/clients/clients.go @@ -6,7 +6,6 @@ import ( "net/http" "strings" "sync" - "time" "github.com/matrix-org/go-neb/api" "github.com/matrix-org/go-neb/database" @@ -344,6 +343,9 @@ func (c *Clients) initClient(botClient *BotClient) error { client.Client = c.httpClient client.DeviceID = config.DeviceID + if client.DeviceID == "" { + log.Warn("Device ID is not set which will result in E2E encryption/decryption not working") + } botClient.Client = client syncer := client.Syncer.(*mautrix.DefaultSyncer) @@ -426,28 +428,7 @@ func (c *Clients) initClient(botClient *BotClient) error { }).Info("Created new client") if config.Sync { - go func() { - // Get the state store up to date - resp, err := botClient.SyncRequest(30000, "", "", true, mevt.PresenceOnline) - if err != nil { - log.WithError(err).Error("Error performing initial sync") - return - } - botClient.stateStore.UpdateStateStore(resp) - - for { - if e := client.Sync(); e != nil { - log.WithFields(log.Fields{ - log.ErrorKey: e, - "user_id": config.UserID, - }).Error("Fatal Sync() error") - time.Sleep(10 * time.Second) - } else { - log.WithField("user_id", config.UserID).Info("Stopping Sync()") - return - } - } - }() + go botClient.Sync() } return nil