Created
June 9, 2013 02:15
-
-
Save DexterHaslem/5737352 to your computer and use it in GitHub Desktop.
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
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