From dfd0b3dcd72a8efa84612a4ae925204f3e4526d8 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 13 Aug 2009 00:07:08 +0200 Subject: Added the IRCParser (thx galdor), and PrivMsg handling (simply repeat) --- Hsbot/IRC.hs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'Hsbot/IRC.hs') diff --git a/Hsbot/IRC.hs b/Hsbot/IRC.hs index 7922a37..76853e2 100644 --- a/Hsbot/IRC.hs +++ b/Hsbot/IRC.hs @@ -5,15 +5,18 @@ module Hsbot.IRC , parseIrcMsg , ping , pong + , sendPrivmsg )where import Control.Monad import Data.List(isPrefixOf) +import Data.Maybe import Network import qualified Network.IRC as IRC import System.IO import Hsbot.Core +import Hsbot.IRCParser type User = String type Channel = String @@ -21,7 +24,7 @@ type Command = String type Args = [String] -- | An IRC line -data IrcLine = Str String -- a regular string +data IrcLine = Privmsg (String, [String]) -- statement (chan, sentence...) | Quit (IrcServer, Handle) -- a quit message from a server | Join (IrcServer, Channel) -- joined a channel | Part (IrcServer, Channel) -- parted the channel @@ -31,8 +34,21 @@ data IrcLine = Str String -- a regular string deriving (Eq,Show) -- | Parses an IrcInput -parseIrcMsg :: String -> IrcInput -parseIrcMsg str = (Cmd "user" "channel" (str, Just "args")) +parseIrcMsg :: String -> IrcLine +parseIrcMsg str = + case (ircParser str) of + Left err -> Nil + Right x -> eval x + where + eval :: IrcMsg -> IrcLine + eval x@(IrcMsg statement cmd stuff) + | cmd == "PING" = Ping $ head stuff + | cmd == "PRIVMSG" = + case statement of + Nothing -> Nil + Just statement' -> if stuff!!1 == "reboot" then Reboot + else Privmsg $ (statement', stuff) + | otherwise = Nil -- | Connects to a server connectServer :: IrcServer -> IO (IrcServer, Handle) @@ -61,3 +77,6 @@ ping = isPrefixOf "PING :" pong :: Handle -> String -> IO () pong handle str = sendstr handle $ "PONG " ++ (drop 5 str) +sendPrivmsg :: (IrcServer, Handle) -> [String] -> IO () +sendPrivmsg (server, handle) stuff' = sendstr handle (IRC.encode $ IRC.privmsg (head stuff') (unwords . tail $ stuff')) + -- cgit v1.2.3