diff options
author | Julien Dessaux | 2011-08-08 20:56:20 +0200 |
---|---|---|
committer | Julien Dessaux | 2011-08-08 20:58:34 +0200 |
commit | 3b914c1b7729f52ba96e51ad43424909acae681c (patch) | |
tree | bc4f81ee8af1abea9cd2aef1471f288721b8f088 /Hsbot/Utils.hs | |
parent | Added Administrative plugin that will permit dynamic reloading (diff) | |
download | hsbot-3b914c1b7729f52ba96e51ad43424909acae681c.tar.gz hsbot-3b914c1b7729f52ba96e51ad43424909acae681c.tar.bz2 hsbot-3b914c1b7729f52ba96e51ad43424909acae681c.zip |
Added exception handling, an autorestart when that happens and output in case of restart/reload
Diffstat (limited to 'Hsbot/Utils.hs')
-rw-r--r-- | Hsbot/Utils.hs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/Hsbot/Utils.hs b/Hsbot/Utils.hs index 2a8f58c..043037d 100644 --- a/Hsbot/Utils.hs +++ b/Hsbot/Utils.hs @@ -8,12 +8,14 @@ module Hsbot.Utils ) where import Control.Concurrent +import Control.Exception (IOException, catch) import Control.Monad.Reader import Control.Monad.State import qualified Data.ByteString.Lazy.UTF8 as L import qualified Data.List as L import qualified Network.IRC as IRC import Network.TLS +import Prelude hiding (catch) import System.IO import Hsbot.Types @@ -47,9 +49,16 @@ hasAccess (Just mask) right = do | otherwise = False -- Helpers -sendStr :: Handle -> Maybe TLSCtx -> String -> IO () -sendStr _ (Just ctx) msg = sendData ctx . L.fromString $ msg ++ "\r\n" -sendStr handle Nothing msg = hPutStrLn handle $ msg ++ "\r\n" +sendStr :: BotEnv -> Handle -> Maybe TLSCtx -> String -> IO () +sendStr env _ (Just ctx) msg = sendData ctx (L.fromString $ msg ++ "\r\n") `catch` handleIOException env ("sendStr " ++ msg) +sendStr env handle Nothing msg = hPutStrLn handle (msg ++ "\r\n") `catch` handleIOException env ("sendStr " ++ msg) + +handleIOException :: BotEnv -> String -> IOException -> IO () +handleIOException env msg ioException = do + runReaderT (setGlobalQuitMVar $ BotRestart (show ioException, Just msg)) env + myId <- myThreadId + killThread myId + return () -- TLS utils initTLSEnv :: TLSConfig -> IO TLSParams |