hask-irc/Main.hs

52 lines
1.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings, OverlappingInstances #-}
2014-05-04 08:54:12 +05:30
module Main (main) where
2014-05-04 02:57:43 +05:30
2014-05-04 07:43:37 +05:30
import qualified Data.Text as T
import Control.Exception
import Control.Monad
import Data.Configurator
import Data.Configurator.Types
import Data.Maybe
2014-05-04 02:57:43 +05:30
import System.Environment
import System.Exit
import Network.IRC.Types
2014-05-04 04:28:44 +05:30
import Network.IRC.Client
2014-05-04 02:57:43 +05:30
instance Configured a => Configured [a] where
convert (List xs) = Just . mapMaybe convert $ xs
convert _ = Nothing
2014-05-04 04:28:44 +05:30
main :: IO ()
2014-05-04 02:57:43 +05:30
main = do
args <- getArgs
prog <- getProgName
2014-05-04 04:28:44 +05:30
when (length args < 1) $ do
putStrLn ("Usage: " ++ prog ++ " <config file path>")
exitFailure
let configFile = head args
loadBotConfig configFile >>= run
loadBotConfig :: FilePath -> IO BotConfig
loadBotConfig configFile = do
eCfg <- try $ load [Required configFile]
case eCfg of
Left (ParseError _ _) -> error "Error while loading config"
Right cfg -> do
eBotConfig <- try $ do
server <- require cfg "server"
port <- require cfg "port"
channel <- require cfg "channel"
botNick <- require cfg "nick"
timeout <- require cfg "timeout"
handlers <- require cfg "handlers"
return $ BotConfig server port channel botNick timeout handlers cfg
case eBotConfig of
Left (KeyError k) -> error $ "Error while reading key from config: " ++ T.unpack k
Right botConfig -> return botConfig