Created
March 3, 2014 08:02
-
-
Save sordina/9320411 to your computer and use it in GitHub Desktop.
Small test bot using simpleIRC library from hackage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Network.SimpleIRC | |
import Data.String | |
import System.Exit | |
import qualified Data.ByteString as B | |
import qualified Data.ByteString.Char8 as C | |
channel :: C.ByteString | |
channel = "#sordina-haskellbot-test" | |
config :: IrcConfig | |
config = ( mkDefaultConfig "irc.freenode.net" "sordina-haskellbot" ) | |
{ cEvents = Privmsg triage : [] -- ( allEvents debugMessages ) | |
, cChannels = [ C.unpack channel ] } | |
main :: IO () | |
main = do | |
connection <- connect config {- runInNewThread -} False {- enableDebug -} True | |
run connection | |
return () | |
run :: Show a => Either a t -> IO () | |
run (Left errors ) = print ("errors" :: String) >> print errors | |
run (Right _server) = return () | |
triage :: MIrc -> IrcMessage -> IO () | |
triage server msg = flip mapM_ [respondToSordina, command] (\x -> x server msg (mNick msg)) | |
respondToSordina :: MIrc -> IrcMessage -> Maybe C.ByteString -> IO () | |
respondToSordina server msg (Just nick) | "sordina1" `B.isInfixOf` nick | |
= sendMsg server channel ( B.concat ["Hello ", nick, ". You said: '", mMsg msg, "'."]) | |
respondToSordina _ _ _ = return () | |
command :: MIrc -> IrcMessage -> t -> IO () | |
command server msg _ | "@" `B.isPrefixOf` mMsg msg = acknowledgeCommand server msg >> specificCommand (C.words (mMsg msg)) | |
command _ _ _ = return () | |
acknowledgeCommand :: MIrc -> t -> IO () | |
acknowledgeCommand server _ = sendMsg server channel "Command Received" | |
specificCommand :: (Eq t, Show t, Data.String.IsString t) => [t] -> IO () | |
specificCommand ["@die"] = exitFailure | |
specificCommand l = print l | |
debugMessages :: (Show a, Show a1) => a -> t -> a1 -> IO () | |
debugMessages typestr _ msg = print typestr >> print msg | |
-- TODO: Wtf is the point of this stupid data structure... | |
allEvents :: IsString a => (a -> EventFunc) -> [IrcEvent] | |
allEvents f = [ Privmsg (f "Privmsg") | |
, Numeric (f "Numeric") | |
, Ping (f "Ping ") | |
, Join (f "Join ") | |
, Part (f "Part ") | |
, Mode (f "Mode ") | |
, Topic (f "Topic ") | |
, Invite (f "Invite ") | |
, Kick (f "Kick ") | |
, Quit (f "Quit ") | |
, Nick (f "Nick ") | |
, Notice (f "Notice ") | |
, RawMsg (f "RawMsg ") | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment