2014-05-04 16:50:19 +05:30
|
|
|
{-# 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
|
|
|
|
|
2014-05-06 02:50:40 +05:30
|
|
|
import Control.Exception
|
|
|
|
import Control.Monad
|
|
|
|
import Data.Configurator
|
2014-05-04 16:50:19 +05:30
|
|
|
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
|
|
|
|
2014-05-04 16:50:19 +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
|
|
|
|
2014-05-06 02:50:40 +05:30
|
|
|
when (length args < 1) $ do
|
|
|
|
putStrLn ("Usage: " ++ prog ++ " <config file path>")
|
|
|
|
exitFailure
|
|
|
|
|
2014-05-04 16:50:19 +05:30
|
|
|
let configFile = head args
|
2014-05-06 02:50:40 +05:30
|
|
|
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
|