summaryrefslogtreecommitdiff
path: root/Hsbot/Plugin/Ping.hs
blob: 192bab48415091988ba11f1cc65154af777bdc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Hsbot.Plugin.Ping
    ( pingId
    , ping
    ) where

import Control.Concurrent.Chan ()
import Control.Exception
import Control.Monad.State (execStateT, forever)
import qualified Network.IRC as IRC
import Prelude hiding (catch)

import Hsbot.Message
import Hsbot.Types

pingId :: PluginId
pingId = PluginId
    { pluginName = "ping"
    , pluginEp   = ping }

-- | The plugin's main entry point
ping :: PluginState -> IO ()
ping state = do
    _ <- (execStateT run state) `catch` (\(_ :: AsyncException) -> return state)
    return ()

-- | The IrcPlugin monad main function
run :: Plugin IO ()
run = forever $ do
    msg <- readMsg
    eval msg
  where
    eval :: Message -> Plugin IO ()
    eval (IncomingMsg msg)
        | (IRC.msg_command msg) == "PING" = writeMsg . OutgoingMsg . IRC.Message Nothing "PONG" $ IRC.msg_params msg
        | otherwise = return ()
    eval _ = return ()