@@ -6,29 +6,16 @@ module TzBot.BotMain where
66
77import Universum
88
9- import Control.Monad.Managed (managed , runManaged )
109import Data.ByteString qualified as BS
11- import Network.HTTP.Client (newManager )
12- import Network.HTTP.Client.TLS (tlsManagerSettings )
1310import Options.Applicative (execParser )
14- import Slacker
15- (defaultSlackConfig , handleThreadExceptionSensibly , runSocketMode , setApiToken , setAppToken ,
16- setGracefulShutdownHandler , setOnException )
1711import System.Directory (doesFileExist )
1812import Text.Interpolation.Nyan (int , rmode' )
19- import Time (hour )
2013
21- import TzBot.Cache
22- (TzCacheSettings (tcsExpiryRandomAmplitudeFraction ), defaultTzCacheSettings , withTzCache ,
23- withTzCacheDefault )
24- import TzBot.Config
14+ import TzBot.BotMain.Server (runServer )
15+ import TzBot.BotMain.Server.Verification (runVerificationServer )
16+ import TzBot.BotMain.SocketMode (runSocketMode )
2517import TzBot.Config.Default (defaultConfigText )
26- import TzBot.Config.Types (BotConfig )
27- import TzBot.Logger
2818import TzBot.Options
29- import TzBot.ProcessEvents (handler )
30- import TzBot.RunMonad
31- import TzBot.Util (withMaybe )
3219
3320{- |
3421Usage:
@@ -43,7 +30,11 @@ main = do
4330 cliOptions <- execParser totalParser
4431 case cliOptions of
4532 DumpConfig dumpOpts -> dumpConfig dumpOpts
46- DefaultCommand op -> run op
33+ RunSocketMode opts -> runSocketMode opts
34+ RunServer opts ->
35+ if rsoVerification opts
36+ then runVerificationServer opts
37+ else runServer opts
4738
4839dumpConfig :: DumpOptions -> IO ()
4940dumpConfig = \ case
@@ -57,51 +48,3 @@ dumpConfig = \case
5748 (hPutStrLn @ Text stderr [int ||File #{path} already exists, \
5849 use --force to overwrite|] >> exitFailure)
5950 writeAction
60-
61- run :: Options -> IO ()
62- run opts = do
63- let mbConfigFilePath = oConfigFile opts
64- bsConfig@ Config {.. } <- readConfig mbConfigFilePath
65- runManaged $ do
66-
67- let fifteenPercentAmplitudeSettings = defaultTzCacheSettings
68- { tcsExpiryRandomAmplitudeFraction = Just 0.15
69- }
70-
71- gracefulShutdownContainer <- liftIO $ newIORef $ (pure () :: IO () )
72- let extractShutdownFunction :: IO () -> IO ()
73- extractShutdownFunction = writeIORef gracefulShutdownContainer
74- let sCfg = defaultSlackConfig
75- & setApiToken (unBotToken cBotToken)
76- & setAppToken (unAppLevelToken cAppToken)
77- & setOnException handleThreadExceptionSensibly -- auto-handle disconnects
78- & setGracefulShutdownHandler extractShutdownFunction
79-
80- bsManager <- liftIO $ newManager tlsManagerSettings
81- bsFeedbackConfig <-
82- managed $ withFeedbackConfig bsConfig
83- bsUserInfoCache <-
84- managed $ withTzCache fifteenPercentAmplitudeSettings cCacheUsersInfo
85- bsConversationMembersCache <-
86- managed $ withTzCache fifteenPercentAmplitudeSettings cCacheConversationMembers
87- let defaultMessageInfoCachingTime = hour 1
88- bsMessageCache <-
89- managed $ withTzCacheDefault defaultMessageInfoCachingTime
90- bsMessageLinkCache <-
91- managed $ withTzCacheDefault defaultMessageInfoCachingTime
92- bsReportEntries <-
93- managed $ withTzCacheDefault cCacheReportDialog
94- -- auto-acknowledge received messages
95- (bsLogNamespace, bsLogContext, bsLogEnv) <- managed $ withLogger cLogLevel
96- liftIO $ runSocketMode sCfg $ handler gracefulShutdownContainer BotState {.. }
97-
98- withFeedbackConfig :: BotConfig -> (FeedbackConfig -> IO a ) -> IO a
99- withFeedbackConfig Config {.. } action = do
100- let fcFeedbackChannel = cFeedbackChannel
101- withFeedbackFile cFeedbackFile $ \ fcFeedbackFile ->
102- action FeedbackConfig {.. }
103- where
104- withFeedbackFile :: Maybe FilePath -> (Maybe Handle -> IO a ) -> IO a
105- withFeedbackFile mbPath action =
106- withMaybe mbPath (action Nothing ) $ \ path ->
107- withFile path AppendMode (action . Just )
0 commit comments