Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Created June 9, 2013 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterHaslem/5737352 to your computer and use it in GitHub Desktop.
Save DexterHaslem/5737352 to your computer and use it in GitHub Desktop.
import System.IO
import Network
import Text.Printf
import Data.List
import System.Exit
channel = "#warsow.na"
nick = "derpbot"
server = "us.quakenet.org"
port = 6667
main = do
handle <- connectTo server ( PortNumber ( fromIntegral port ) )
hSetBuffering handle NoBuffering
write handle "NICK" nick
write handle "USER" (nick ++ " derpbot * :derpbot")
write handle "JOIN" channel
output handle
write :: Handle
-> String -- command
-> String -- string
-> IO ()
write handle command text = do
hPrintf handle "%s %s\r\n" command text
hFlush handle -- flush here so our local print shows up
-- the socket is not buffered but the loop action messes something up
printf "-> %s %s\n" command text
output :: Handle -> IO ()
output handle = loop $ do
line <- hGetLine handle
let rawInput = init line
if ping rawInput then pong rawInput else eval handle ( trim rawInput )
putStrLn line
where
loop a = a >> loop a
trim = drop 1 . dropWhile ( /= ':' ) . drop 1
ping x = "PING :" `isPrefixOf` x
pong x = write handle "PONG" ( ':' : drop 6 x )
eval :: Handle
-> String -- bot command
-> IO ()
eval h "!quit" = write h "QUIT" ":l8r" >> exitWith ExitSuccess
eval _ _ = return ()
privmsg :: Handle -> String -> IO ()
privmsg handle message = write handle "PRIVMSG" ( channel ++ " :" ++ message )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment