2014-06-02 00:26:41 +05:30
|
|
|
{-# LANGUAGE OverlappingInstances #-}
|
|
|
|
|
|
|
|
module Network.IRC.Config (loadBotConfig) where
|
|
|
|
|
|
|
|
import qualified Data.Configurator as CF
|
|
|
|
|
|
|
|
import ClassyPrelude
|
|
|
|
import Data.Configurator.Types (Configured (..), Value (List), ConfigError (..), KeyError (..))
|
|
|
|
|
|
|
|
import Network.IRC.Handlers
|
|
|
|
import Network.IRC.Types
|
|
|
|
|
|
|
|
instance Configured a => Configured [a] where
|
|
|
|
convert (List xs) = Just . mapMaybe convert $ xs
|
|
|
|
convert _ = Nothing
|
|
|
|
|
|
|
|
loadBotConfig :: String -> IO BotConfig
|
|
|
|
loadBotConfig configFile = do
|
2014-06-08 07:12:33 +05:30
|
|
|
eConfig <- try $ CF.load [CF.Required configFile]
|
|
|
|
case eConfig of
|
2014-06-02 00:26:41 +05:30
|
|
|
Left (ParseError _ _) -> error "Error while loading config"
|
2014-06-08 07:12:33 +05:30
|
|
|
Right config -> do
|
2014-06-02 00:26:41 +05:30
|
|
|
eBotConfig <- try $ do
|
2014-06-08 07:12:33 +05:30
|
|
|
handlers :: [Text] <- CF.require config "msghandlers"
|
2014-06-02 00:26:41 +05:30
|
|
|
let handlerInfo = foldl' (\m h -> insertMap h mempty m) mempty handlers
|
2014-06-08 07:12:33 +05:30
|
|
|
botConfig <- newBotConfig <$>
|
|
|
|
CF.require config "server" <*>
|
|
|
|
CF.require config "port" <*>
|
|
|
|
CF.require config "channel" <*>
|
|
|
|
(Nick <$> CF.require config "nick") <*>
|
|
|
|
CF.require config "timeout"
|
|
|
|
return botConfig { msgHandlerInfo = handlerInfo
|
|
|
|
, msgHandlerMakers = allMsgHandlerMakers
|
|
|
|
, config = config
|
|
|
|
}
|
2014-06-02 00:26:41 +05:30
|
|
|
|
|
|
|
case eBotConfig of
|
|
|
|
Left (KeyError k) -> error $ "Error while reading key from config: " ++ unpack k
|
|
|
|
Right botConf -> return botConf
|