From 2d4e99d6101285a639aeb9579fceac91e6fd0b0d Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 22 Nov 2023 22:11:32 +0000 Subject: [PATCH] cli: set device name for remote control via CLI option (#3427) * cli: set device name for remote control via CLI option * fix * add property in tests --- apps/simplex-broadcast-bot/src/Broadcast/Options.hs | 1 + .../src/Directory/Options.hs | 1 + src/Simplex/Chat.hs | 7 ++++--- src/Simplex/Chat/Controller.hs | 3 ++- src/Simplex/Chat/Mobile.hs | 4 +++- src/Simplex/Chat/Options.hs | 11 +++++++++++ src/Simplex/Chat/Terminal.hs | 3 ++- tests/ChatClient.hs | 1 + 8 files changed, 25 insertions(+), 6 deletions(-) diff --git a/apps/simplex-broadcast-bot/src/Broadcast/Options.hs b/apps/simplex-broadcast-bot/src/Broadcast/Options.hs index 76b349a49..3758af2fc 100644 --- a/apps/simplex-broadcast-bot/src/Broadcast/Options.hs +++ b/apps/simplex-broadcast-bot/src/Broadcast/Options.hs @@ -74,6 +74,7 @@ mkChatOpts :: BroadcastBotOpts -> ChatOpts mkChatOpts BroadcastBotOpts {coreOptions} = ChatOpts { coreOptions, + deviceName = Nothing, chatCmd = "", chatCmdDelay = 3, chatServerPort = Nothing, diff --git a/apps/simplex-directory-service/src/Directory/Options.hs b/apps/simplex-directory-service/src/Directory/Options.hs index 1f06afe11..8f28c9013 100644 --- a/apps/simplex-directory-service/src/Directory/Options.hs +++ b/apps/simplex-directory-service/src/Directory/Options.hs @@ -72,6 +72,7 @@ mkChatOpts :: DirectoryOpts -> ChatOpts mkChatOpts DirectoryOpts {coreOptions} = ChatOpts { coreOptions, + deviceName = Nothing, chatCmd = "", chatCmdDelay = 3, chatServerPort = Nothing, diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 2696b5311..bbbb4a924 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -153,7 +153,8 @@ defaultChatConfig = cleanupManagerStepDelay = 3 * 1000000, -- 3 seconds ciExpirationInterval = 30 * 60 * 1000000, -- 30 minutes coreApi = False, - highlyAvailable = False + highlyAvailable = False, + deviceNameForRemote = "" } _defaultSMPServers :: NonEmpty SMPServerWithAuth @@ -197,7 +198,7 @@ createChatDatabase filePrefix key confirmMigrations = runExceptT $ do pure ChatDatabase {chatStore, agentStore} newChatController :: ChatDatabase -> Maybe User -> ChatConfig -> ChatOpts -> IO ChatController -newChatController ChatDatabase {chatStore, agentStore} user cfg@ChatConfig {agentConfig = aCfg, defaultServers, inlineFiles, tempDir} ChatOpts {coreOptions = CoreChatOpts {smpServers, xftpServers, networkConfig, logLevel, logConnections, logServerHosts, logFile, tbqSize, highlyAvailable}, optFilesFolder, showReactions, allowInstantFiles, autoAcceptFileSize} = do +newChatController ChatDatabase {chatStore, agentStore} user cfg@ChatConfig {agentConfig = aCfg, defaultServers, inlineFiles, tempDir, deviceNameForRemote} ChatOpts {coreOptions = CoreChatOpts {smpServers, xftpServers, networkConfig, logLevel, logConnections, logServerHosts, logFile, tbqSize, highlyAvailable}, deviceName, optFilesFolder, showReactions, allowInstantFiles, autoAcceptFileSize} = do let inlineFiles' = if allowInstantFiles || autoAcceptFileSize > 0 then inlineFiles else inlineFiles {sendChunks = 0, receiveInstant = False} config = cfg {logLevel, showReactions, tbqSize, subscriptionEvents = logConnections, hostEvents = logServerHosts, defaultServers = configServers, inlineFiles = inlineFiles', autoAcceptFileSize, highlyAvailable} firstTime = dbNew chatStore @@ -215,7 +216,7 @@ newChatController ChatDatabase {chatStore, agentStore} user cfg@ChatConfig {agen sndFiles <- newTVarIO M.empty rcvFiles <- newTVarIO M.empty currentCalls <- atomically TM.empty - localDeviceName <- newTVarIO "" -- TODO set in config + localDeviceName <- newTVarIO $ fromMaybe deviceNameForRemote deviceName multicastSubscribers <- newTMVarIO 0 remoteSessionSeq <- newTVarIO 0 remoteHostSessions <- atomically TM.empty diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index e470a6fd8..aa2358745 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -135,7 +135,8 @@ data ChatConfig = ChatConfig cleanupManagerStepDelay :: Int64, ciExpirationInterval :: Int64, -- microseconds coreApi :: Bool, - highlyAvailable :: Bool + highlyAvailable :: Bool, + deviceNameForRemote :: Text } data DefaultAgentServers = DefaultAgentServers diff --git a/src/Simplex/Chat/Mobile.hs b/src/Simplex/Chat/Mobile.hs index ffcf5a0ce..912710254 100644 --- a/src/Simplex/Chat/Mobile.hs +++ b/src/Simplex/Chat/Mobile.hs @@ -181,6 +181,7 @@ mobileChatOpts dbFilePrefix dbKey = tbqSize = 1024, highlyAvailable = False }, + deviceName = Nothing, chatCmd = "", chatCmdDelay = 3, chatServerPort = Nothing, @@ -197,7 +198,8 @@ defaultMobileConfig = defaultChatConfig { confirmMigrations = MCYesUp, logLevel = CLLError, - coreApi = True + coreApi = True, + deviceNameForRemote = "Mobile" } getActiveUser_ :: SQLiteStore -> IO (Maybe User) diff --git a/src/Simplex/Chat/Options.hs b/src/Simplex/Chat/Options.hs index 04aef29df..7ce6305d2 100644 --- a/src/Simplex/Chat/Options.hs +++ b/src/Simplex/Chat/Options.hs @@ -19,6 +19,7 @@ where import Control.Logger.Simple (LogLevel (..)) import qualified Data.Attoparsec.ByteString.Char8 as A import qualified Data.ByteString.Char8 as B +import Data.Text (Text) import Numeric.Natural (Natural) import Options.Applicative import Simplex.Chat.Controller (ChatLogLevel (..), updateStr, versionNumber, versionString) @@ -32,6 +33,7 @@ import System.FilePath (combine) data ChatOpts = ChatOpts { coreOptions :: CoreChatOpts, + deviceName :: Maybe Text, chatCmd :: String, chatCmdDelay :: Int, chatServerPort :: Maybe String, @@ -200,6 +202,14 @@ coreChatOptsP appDir defaultDbFileName = do chatOptsP :: FilePath -> FilePath -> Parser ChatOpts chatOptsP appDir defaultDbFileName = do coreOptions <- coreChatOptsP appDir defaultDbFileName + deviceName <- + optional $ + strOption + ( long "device-name" + <> short 'e' + <> metavar "DEVICE" + <> help "Device name to use in connections with remote hosts and controller" + ) chatCmd <- strOption ( long "execute" @@ -268,6 +278,7 @@ chatOptsP appDir defaultDbFileName = do pure ChatOpts { coreOptions, + deviceName, chatCmd, chatCmdDelay, chatServerPort, diff --git a/src/Simplex/Chat/Terminal.hs b/src/Simplex/Chat/Terminal.hs index 68aaa5131..89d234f94 100644 --- a/src/Simplex/Chat/Terminal.hs +++ b/src/Simplex/Chat/Terminal.hs @@ -35,7 +35,8 @@ terminalChatConfig = ntf = ["ntf://FB-Uop7RTaZZEG0ZLD2CIaTjsPh-Fw0zFAnb7QyA8Ks=@ntf2.simplex.im,ntg7jdjy2i3qbib3sykiho3enekwiaqg3icctliqhtqcg6jmoh6cxiad.onion"], xftp = defaultXFTPServers, netCfg = defaultNetworkConfig - } + }, + deviceNameForRemote = "SimpleX CLI" } simplexChatTerminal :: WithTerminal t => ChatConfig -> ChatOpts -> t -> IO () diff --git a/tests/ChatClient.hs b/tests/ChatClient.hs index c9f7c9dc8..b75fa3841 100644 --- a/tests/ChatClient.hs +++ b/tests/ChatClient.hs @@ -74,6 +74,7 @@ testOpts = tbqSize = 16, highlyAvailable = False }, + deviceName = Nothing, chatCmd = "", chatCmdDelay = 3, chatServerPort = Nothing,