From d952869ba2f54f7ec737c14c49b8e9a10282371b Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Sun, 11 May 2014 19:08:43 +0530 Subject: [PATCH] Added proper signal handling support --- Main.hs | 8 +++++- Network/IRC/Client.hs | 8 +++--- Network/IRC/Handlers/SongSearch.hs | 2 +- hask-irc.cabal | 42 +++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/Main.hs b/Main.hs index 8ff872f..2958b19 100644 --- a/Main.hs +++ b/Main.hs @@ -5,10 +5,12 @@ module Main (main) where import qualified Data.Configurator as CF import ClassyPrelude hiding (try, getArgs) -import Control.Exception +import Control.Concurrent.Lifted +import Control.Exception.Lifted import Data.Configurator.Types import System.Environment import System.Exit +import System.Posix.Signals import Network.IRC.Types (BotConfig(BotConfig)) import Network.IRC.Client @@ -26,6 +28,10 @@ main = do putStrLn $ "Usage: " ++ pack prog ++ " " exitFailure + mainThreadId <- myThreadId + installHandler sigINT (Catch $ throwTo mainThreadId UserInterrupt) Nothing + installHandler sigTERM (Catch $ throwTo mainThreadId UserInterrupt) Nothing + let configFile = headEx args loadBotConfig configFile >>= run diff --git a/Network/IRC/Client.hs b/Network/IRC/Client.hs index aaa5a37..cf5ff78 100644 --- a/Network/IRC/Client.hs +++ b/Network/IRC/Client.hs @@ -33,8 +33,8 @@ sendCommand Bot { .. } reply = do TF.hprint socket "{}\r\n" $ TF.Only line TF.print "[{}] > {}\n" $ TF.buildParams (formatTime defaultTimeLocale "%F %T" time, line) -listen :: IRC () -listen = do +listenerLoop :: IRC () +listenerLoop = do status <- get bot@Bot { .. } <- ask let nick = botNick botConfig @@ -73,7 +73,7 @@ listen = do return nStatus put nStatus - when (nStatus /= Disconnected) listen + when (nStatus /= Disconnected) listenerLoop loadMsgHandlers :: BotConfig -> IO MsgHandlerStates loadMsgHandlers botConfig@BotConfig { .. } = @@ -142,4 +142,4 @@ run botConfig' = withSocketsDo $ do go bot = do sendCommand bot NickCmd sendCommand bot UserCmd - runIRC bot Connected listen + runIRC bot Connected listenerLoop diff --git a/Network/IRC/Handlers/SongSearch.hs b/Network/IRC/Handlers/SongSearch.hs index 0aae887..6b8cab1 100644 --- a/Network/IRC/Handlers/SongSearch.hs +++ b/Network/IRC/Handlers/SongSearch.hs @@ -5,7 +5,7 @@ module Network.IRC.Handlers.SongSearch (getMsgHandler) where import qualified Data.Configurator as CF import ClassyPrelude hiding (try) -import Control.Exception +import Control.Exception.Lifted import Control.Monad.Reader import Data.Aeson import Data.Aeson.Types (emptyArray) diff --git a/hask-irc.cabal b/hask-irc.cabal index d48f5bf..d82033d 100644 --- a/hask-irc.cabal +++ b/hask-irc.cabal @@ -49,11 +49,22 @@ build-type: Simple cabal-version: >=1.10 library - build-depends: base >=4.5 && <4.7, text >=0.11 && <0.12, mtl >=2.1 && <2.2, - network >=2.3 && <2.5, configurator >= 0.2, time >=1.4.0, - curl-aeson ==0.0.3, aeson >=0.6.0.0, HTTP, transformers >=0.3, - classy-prelude ==0.9.1, text-format >= 0.3.1, filepath >=1.3, directory >=1.2, - lifted-base >=0.2 + build-depends: base >=4.5 && <4.7, + text >=0.11 && <0.12, + mtl >=2.1 && <2.2, + network >=2.3 && <2.5, + configurator >= 0.2, + time >=1.4.0, + curl-aeson ==0.0.3, + aeson >=0.6.0.0, + HTTP >=4000, + transformers >=0.3, + classy-prelude ==0.9.1, + text-format >= 0.3.1, + filepath >=1.3, + directory >=1.2, + lifted-base >=0.2, + unix >=2.7 exposed-modules: Network.IRC.Types, Network.IRC.Protocol, Network.IRC.Handlers, Network.IRC.Client @@ -74,11 +85,22 @@ executable hask-irc other-extensions: RecordWildCards, OverloadedStrings, ScopedTypeVariables, OverlappingInstances -- Other library packages from which modules are imported. - build-depends: base >=4.5 && <4.7, text >=0.11 && <0.12, mtl >=2.1 && <2.2, - network >=2.3 && <2.5, configurator >= 0.2, time >=1.4.0, - curl-aeson ==0.0.3, aeson >=0.6.0.0, HTTP, transformers >=0.3, - classy-prelude ==0.9.1, text-format >= 0.3.1, filepath >=1.3, directory >=1.2, - lifted-base >=0.2 + build-depends: base >=4.5 && <4.7, + text >=0.11 && <0.12, + mtl >=2.1 && <2.2, + network >=2.3 && <2.5, + configurator >= 0.2, + time >=1.4.0, + curl-aeson ==0.0.3, + aeson >=0.6.0.0, + HTTP >=4000, + transformers >=0.3, + classy-prelude ==0.9.1, + text-format >= 0.3.1, + filepath >=1.3, + directory >=1.2, + lifted-base >=0.2, + unix >=2.7 -- Directories containing source files. -- hs-source-dirs: