hask-irc/Main.hs

56 lines
1.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings, OverlappingInstances, NoImplicitPrelude #-}
2014-05-04 08:54:12 +05:30
module Main (main) where
2014-05-04 02:57:43 +05:30
import qualified Data.Configurator as CF
2014-05-04 07:43:37 +05:30
2014-05-10 21:45:16 +05:30
import ClassyPrelude hiding (try, getArgs)
2014-05-11 19:08:43 +05:30
import Control.Concurrent.Lifted
import Control.Exception.Lifted
import Data.Configurator.Types
2014-05-04 02:57:43 +05:30
import System.Environment
import System.Exit
2014-05-11 19:08:43 +05:30
import System.Posix.Signals
2014-05-04 02:57:43 +05:30
2014-05-07 14:35:25 +05:30
import Network.IRC.Types (BotConfig(BotConfig))
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
2014-05-10 21:45:16 +05:30
putStrLn $ "Usage: " ++ pack prog ++ " <config file path>"
exitFailure
2014-05-11 19:08:43 +05:30
mainThreadId <- myThreadId
installHandler sigINT (Catch $ throwTo mainThreadId UserInterrupt) Nothing
installHandler sigTERM (Catch $ throwTo mainThreadId UserInterrupt) Nothing
2014-05-10 21:45:16 +05:30
let configFile = headEx args
loadBotConfig configFile >>= run
loadBotConfig :: String -> IO BotConfig
loadBotConfig configFile = do
eCfg <- try $ CF.load [Required configFile]
case eCfg of
Left (ParseError _ _) -> error "Error while loading config"
Right cfg -> do
eBotConfig <- try $ do
server <- CF.require cfg "server"
port <- CF.require cfg "port"
channel <- CF.require cfg "channel"
botNick <- CF.require cfg "nick"
timeout <- CF.require cfg "timeout"
msghandlers <- CF.require cfg "msghandlers"
return $ BotConfig server port channel botNick timeout msghandlers cfg
case eBotConfig of
2014-05-10 21:45:16 +05:30
Left (KeyError k) -> error $ "Error while reading key from config: " ++ unpack k
Right botConfig -> return botConfig