mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-05-03 22:46:51 +00:00
d42cab8e22
* core: preset servers and operators (WIP) * usageConditionsToAdd * simplify * WIP * database entity IDs * preset operators and servers (compiles) * update (most tests pass) * remove imports * fix * update * make preset servers lists potentially empty in some operators, as long as the combined list is not empty * CLI API in progress, validateUserServers * make servers of disabled operators "unknown", consider only enabled servers when switching profile links * exclude disabled operators when receiving files * fix TH in ghc 8.10.7 * add type for ghc 8.10.7 * pattern match for ghc 8.10.7 * ghc 8.10.7 fix attempt * remove additional pattern, update servers * do not strip title from conditions * remove space --------- Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
87 lines
3.6 KiB
Haskell
87 lines
3.6 KiB
Haskell
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Simplex.Chat.Terminal where
|
|
|
|
import Control.Exception (handle, throwIO)
|
|
import Control.Monad
|
|
import qualified Data.ByteArray as BA
|
|
import qualified Data.List.NonEmpty as L
|
|
import qualified Data.Text as T
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
import Database.SQLite.Simple (SQLError (..))
|
|
import qualified Database.SQLite.Simple as DB
|
|
import Simplex.Chat (_defaultNtfServers, defaultChatConfig, operatorSimpleXChat)
|
|
import Simplex.Chat.Controller
|
|
import Simplex.Chat.Core
|
|
import Simplex.Chat.Help (chatWelcome)
|
|
import Simplex.Chat.Operators
|
|
import Simplex.Chat.Options
|
|
import Simplex.Chat.Terminal.Input
|
|
import Simplex.Chat.Terminal.Output
|
|
import Simplex.FileTransfer.Client.Presets (defaultXFTPServers)
|
|
import Simplex.Messaging.Client (NetworkConfig (..), SMPProxyFallback (..), SMPProxyMode (..), defaultNetworkConfig)
|
|
import Simplex.Messaging.Util (raceAny_)
|
|
import System.IO (hFlush, hSetEcho, stdin, stdout)
|
|
|
|
terminalChatConfig :: ChatConfig
|
|
terminalChatConfig =
|
|
defaultChatConfig
|
|
{ presetServers =
|
|
PresetServers
|
|
{ operators =
|
|
[ PresetOperator
|
|
{ operator = Just operatorSimpleXChat,
|
|
smp =
|
|
map
|
|
(presetServer True)
|
|
[ "smp://u2dS9sG8nMNURyZwqASV4yROM28Er0luVTx5X1CsMrU=@smp4.simplex.im,o5vmywmrnaxalvz6wi3zicyftgio6psuvyniis6gco6bp6ekl4cqj4id.onion",
|
|
"smp://hpq7_4gGJiilmz5Rf-CswuU5kZGkm_zOIooSw6yALRg=@smp5.simplex.im,jjbyvoemxysm7qxap7m5d5m35jzv5qq6gnlv7s4rsn7tdwwmuqciwpid.onion",
|
|
"smp://PQUV2eL0t7OStZOoAsPEV2QYWt4-xilbakvGUGOItUo=@smp6.simplex.im,bylepyau3ty4czmn77q4fglvperknl4bi2eb2fdy2bh4jxtf32kf73yd.onion"
|
|
],
|
|
useSMP = 3,
|
|
xftp = map (presetServer True) $ L.toList defaultXFTPServers,
|
|
useXFTP = 3
|
|
}
|
|
],
|
|
ntf = _defaultNtfServers,
|
|
netCfg =
|
|
defaultNetworkConfig
|
|
{ smpProxyMode = SPMUnknown,
|
|
smpProxyFallback = SPFAllowProtected
|
|
}
|
|
},
|
|
deviceNameForRemote = "SimpleX CLI"
|
|
}
|
|
|
|
simplexChatTerminal :: WithTerminal t => ChatConfig -> ChatOpts -> t -> IO ()
|
|
simplexChatTerminal cfg options t = run options
|
|
where
|
|
run opts@ChatOpts {coreOptions = coreOptions@CoreChatOpts {dbKey}} =
|
|
handle checkDBKeyError . simplexChatCore cfg opts $ \u cc -> do
|
|
ct <- newChatTerminal t opts
|
|
when (firstTime cc) . printToTerminal ct $ chatWelcome u
|
|
runChatTerminal ct cc opts
|
|
where
|
|
checkDBKeyError :: SQLError -> IO ()
|
|
checkDBKeyError e = case sqlError e of
|
|
DB.ErrorNotADatabase -> do
|
|
putStrLn $ "Database file is invalid or " <> if BA.null dbKey then "encrypted." else "you passed an incorrect encryption key."
|
|
run =<< getKeyOpts
|
|
_ -> throwIO e
|
|
getKeyOpts :: IO ChatOpts
|
|
getKeyOpts = do
|
|
putStr "Enter database encryption key (Ctrl-C to exit):"
|
|
hFlush stdout
|
|
hSetEcho stdin False
|
|
key <- getLine
|
|
hSetEcho stdin True
|
|
putStrLn ""
|
|
pure opts {coreOptions = coreOptions {dbKey = BA.convert $ encodeUtf8 $ T.pack key}}
|
|
|
|
runChatTerminal :: ChatTerminal -> ChatController -> ChatOpts -> IO ()
|
|
runChatTerminal ct cc opts = raceAny_ [runTerminalInput ct cc, runTerminalOutput ct cc opts, runInputLoop ct cc]
|