From 0ac9785e4b459528ea47c740a69f9f2a4f7c3d59 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 10 Apr 2022 16:30:54 +0100 Subject: [PATCH] terminal: option to execute a single chat command via command line (#515) --- apps/simplex-chat/Main.hs | 29 ++++++++++++++++++--------- src/Simplex/Chat/Mobile.hs | 4 +++- src/Simplex/Chat/Options.hs | 40 ++++++++++++++++++++++++++++++------- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/apps/simplex-chat/Main.hs b/apps/simplex-chat/Main.hs index 0465354994..b78d241aa1 100644 --- a/apps/simplex-chat/Main.hs +++ b/apps/simplex-chat/Main.hs @@ -2,24 +2,35 @@ module Main where +import Control.Concurrent (threadDelay) import Simplex.Chat -import Simplex.Chat.Controller (versionNumber) +import Simplex.Chat.Bot +import Simplex.Chat.Controller (ChatConfig, versionNumber) import Simplex.Chat.Options import Simplex.Chat.Terminal +import Simplex.Chat.View (serializeChatResponse) import System.Directory (getAppUserDataDirectory) import System.Terminal (withTerminal) +cfg :: ChatConfig +cfg = defaultChatConfig + main :: IO () main = do - opts <- welcomeGetOpts - t <- withTerminal pure - simplexChat defaultChatConfig opts t - -welcomeGetOpts :: IO ChatOpts -welcomeGetOpts = do appDir <- getAppUserDataDirectory "simplex" - opts@ChatOpts {dbFilePrefix} <- getChatOpts appDir "simplex_v1" + opts@ChatOpts {chatCmd} <- getChatOpts appDir "simplex_v1" + if null chatCmd + then do + welcome opts + t <- withTerminal pure + simplexChat cfg opts t + else simplexChatBot cfg opts $ \_ cc -> do + r <- sendCmd cc chatCmd + putStrLn $ serializeChatResponse r + threadDelay $ chatCmdDelay opts * 1000000 + +welcome :: ChatOpts -> IO () +welcome ChatOpts {dbFilePrefix} = do putStrLn $ "SimpleX Chat v" ++ versionNumber putStrLn $ "db: " <> dbFilePrefix <> "_chat.db, " <> dbFilePrefix <> "_agent.db" putStrLn "type \"/help\" or \"/h\" for usage info" - pure opts diff --git a/src/Simplex/Chat/Mobile.hs b/src/Simplex/Chat/Mobile.hs index e4f15dd0d1..0902c58bde 100644 --- a/src/Simplex/Chat/Mobile.hs +++ b/src/Simplex/Chat/Mobile.hs @@ -51,7 +51,9 @@ mobileChatOpts = { dbFilePrefix = "simplex_v1", -- two database files will be created: simplex_v1_chat.db and simplex_v1_agent.db smpServers = [], logConnections = False, - logAgent = False + logAgent = False, + chatCmd = "", + chatCmdDelay = 3 } defaultMobileConfig :: ChatConfig diff --git a/src/Simplex/Chat/Options.hs b/src/Simplex/Chat/Options.hs index 68b9394db1..09cc1e9f85 100644 --- a/src/Simplex/Chat/Options.hs +++ b/src/Simplex/Chat/Options.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE ApplicativeDo #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} module Simplex.Chat.Options @@ -20,13 +22,15 @@ data ChatOpts = ChatOpts { dbFilePrefix :: String, smpServers :: [SMPServer], logConnections :: Bool, - logAgent :: Bool + logAgent :: Bool, + chatCmd :: String, + chatCmdDelay :: Int } chatOpts :: FilePath -> FilePath -> Parser ChatOpts -chatOpts appDir defaultDbFileName = - ChatOpts - <$> strOption +chatOpts appDir defaultDbFileName = do + dbFilePrefix <- + strOption ( long "database" <> short 'd' <> metavar "DB_FILE" @@ -34,7 +38,8 @@ chatOpts appDir defaultDbFileName = <> value defaultDbFilePath <> showDefault ) - <*> option + smpServers <- + option parseSMPServers ( long "server" <> short 's' @@ -43,16 +48,37 @@ chatOpts appDir defaultDbFileName = "Comma separated list of SMP server(s) to use" <> value [] ) - <*> switch + logConnections <- + switch ( long "connections" <> short 'c' <> help "Log every contact and group connection on start" ) - <*> switch + logAgent <- + switch ( long "log-agent" <> short 'l' <> help "Enable logs from SMP agent" ) + chatCmd <- + strOption + ( long "execute" + <> short 'e' + <> metavar "COMMAND" + <> help "Execute chat command (received messages won't be logged) and exit" + <> value "" + ) + chatCmdDelay <- + option + auto + ( long "time" + <> short 't' + <> metavar "TIME" + <> help "Time to wait after sending chat command before exiting, seconds" + <> value 3 + <> showDefault + ) + pure ChatOpts {dbFilePrefix, smpServers, logConnections, logAgent, chatCmd, chatCmdDelay} where defaultDbFilePath = combine appDir defaultDbFileName