From 14f87adb8b3cfc019b7de7f082e3851a91b6a364 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 7 Aug 2009 00:19:14 +0200 Subject: Wrote the ping pong stuff --- Hsbot/IRC.hs | 11 +++++++++++ Hsbot/Main.hs | 12 ++++++++++++ 2 files changed, 23 insertions(+) (limited to 'Hsbot') diff --git a/Hsbot/IRC.hs b/Hsbot/IRC.hs index d397456..cd6b7b8 100644 --- a/Hsbot/IRC.hs +++ b/Hsbot/IRC.hs @@ -4,9 +4,12 @@ module Hsbot.IRC , connectServer , initServer , parseIrcMsg + , ping + , pong )where import Control.Monad +import Data.List(isPrefixOf) import Network import qualified Network.IRC as IRC import System.IO @@ -56,3 +59,11 @@ initServer (server, handle) = do mapM_ (sendstr handle . IRC.encode . IRC.joinChan) (channels server) return () +-- | Check if a message is a PING +ping :: String -> Bool +ping = isPrefixOf "PING :" + +-- | Send a pong message given a ping message +pong :: Handle -> String -> IO () +pong handle str = sendstr handle $ "PONG " ++ (drop 5 str) + diff --git a/Hsbot/Main.hs b/Hsbot/Main.hs index 23b3991..dad568a 100644 --- a/Hsbot/Main.hs +++ b/Hsbot/Main.hs @@ -2,6 +2,9 @@ module Hsbot.Main ( imain ) where +import Control.Concurrent +import Control.Concurrent.Chan +import Control.Monad import System.IO import System.Plugins @@ -18,9 +21,18 @@ imain modul' reboot = imain' modul' reboot newbot -- | Bot's main entry point imain' :: Module -> Reboot -> Bot -> IO () imain' modul' reboot bot = do + putStrLn "yeah" putStrLn "Connecting servers..." servers' <- mapM connectServer (ircServers C.config) putStrLn "Joining channels..." mapM_ initServer servers' return () +-- | Thread entry point for socket listeners +listener :: (Chan IrcOutput) -> (IrcServer, Handle) -> IO () +listener chan (server, handle) = forever $ do + str <- hGetLine handle + writeChan chan (Str str) + if ping str then pong handle str + else return () + -- cgit v1.2.3