2024-01-20 14:59:13 +00:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2021-06-25 18:34:29 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2021-06-25 18:18:24 +01:00
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
2024-01-20 14:59:13 +00:00
|
|
|
import Control.Concurrent (forkIO, threadDelay)
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Monad
|
2022-11-14 08:42:54 +00:00
|
|
|
import Data.Time.Clock (getCurrentTime)
|
2023-05-08 20:07:51 +04:00
|
|
|
import Data.Time.LocalTime (getCurrentTimeZone)
|
2022-05-13 19:44:03 +01:00
|
|
|
import Server
|
2024-01-20 14:59:13 +00:00
|
|
|
import Simplex.Chat.Controller (ChatController (..), ChatResponse (..), currentRemoteHost, versionNumber, versionString)
|
2022-04-10 17:13:06 +01:00
|
|
|
import Simplex.Chat.Core
|
2021-07-05 20:05:07 +01:00
|
|
|
import Simplex.Chat.Options
|
2022-01-21 11:09:33 +00:00
|
|
|
import Simplex.Chat.Terminal
|
2022-07-26 07:29:28 +01:00
|
|
|
import Simplex.Chat.View (serializeChatResponse)
|
2022-08-02 15:36:12 +01:00
|
|
|
import Simplex.Messaging.Client (NetworkConfig (..))
|
2021-06-25 18:34:29 +01:00
|
|
|
import System.Directory (getAppUserDataDirectory)
|
2021-07-07 22:46:38 +01:00
|
|
|
import System.Terminal (withTerminal)
|
2021-06-25 18:34:29 +01:00
|
|
|
|
2021-06-25 18:18:24 +01:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2021-06-25 18:34:29 +01:00
|
|
|
appDir <- getAppUserDataDirectory "simplex"
|
2022-05-13 19:44:03 +01:00
|
|
|
opts@ChatOpts {chatCmd, chatServerPort} <- getChatOpts appDir "simplex_v1"
|
2022-04-10 16:30:54 +01:00
|
|
|
if null chatCmd
|
2022-05-13 19:44:03 +01:00
|
|
|
then case chatServerPort of
|
2024-01-20 14:59:13 +00:00
|
|
|
Just chatPort -> simplexChatServer defaultChatServerConfig {chatPort} terminalChatConfig opts
|
|
|
|
_ -> runCLI opts
|
|
|
|
else simplexChatCore terminalChatConfig opts $ runCommand opts
|
|
|
|
where
|
|
|
|
runCLI opts = do
|
|
|
|
welcome opts
|
|
|
|
t <- withTerminal pure
|
|
|
|
simplexChatTerminal terminalChatConfig opts t
|
|
|
|
runCommand ChatOpts {chatCmd, chatCmdLog, chatCmdDelay} user cc = do
|
|
|
|
when (chatCmdLog /= CCLNone) . void . forkIO . forever $ do
|
|
|
|
(_, _, r') <- atomically . readTBQueue $ outputQ cc
|
|
|
|
case r' of
|
|
|
|
CRNewChatItem {} -> printResponse r'
|
|
|
|
_ -> when (chatCmdLog == CCLAll) $ printResponse r'
|
|
|
|
sendChatCmdStr cc chatCmd >>= printResponse
|
|
|
|
threadDelay $ chatCmdDelay * 1000000
|
|
|
|
where
|
|
|
|
printResponse r = do
|
|
|
|
ts <- getCurrentTime
|
|
|
|
tz <- getCurrentTimeZone
|
|
|
|
rh <- readTVarIO $ currentRemoteHost cc
|
|
|
|
putStrLn $ serializeChatResponse (rh, Just user) ts tz rh r
|
2022-04-10 16:30:54 +01:00
|
|
|
|
|
|
|
welcome :: ChatOpts -> IO ()
|
2023-02-18 17:39:16 +00:00
|
|
|
welcome ChatOpts {coreOptions = CoreChatOpts {dbFilePrefix, networkConfig}} =
|
2022-07-25 14:04:27 +01:00
|
|
|
mapM_
|
|
|
|
putStrLn
|
2023-01-22 15:16:45 +00:00
|
|
|
[ versionString versionNumber,
|
2022-07-25 14:04:27 +01:00
|
|
|
"db: " <> dbFilePrefix <> "_chat.db, " <> dbFilePrefix <> "_agent.db",
|
2022-07-26 07:29:28 +01:00
|
|
|
maybe
|
|
|
|
"direct network connection - use `/network` command or `-x` CLI option to connect via SOCKS5 at :9050"
|
|
|
|
(("using SOCKS5 proxy " <>) . show)
|
2022-08-02 15:36:12 +01:00
|
|
|
(socksProxy networkConfig),
|
2022-07-25 14:04:27 +01:00
|
|
|
"type \"/help\" or \"/h\" for usage info"
|
|
|
|
]
|