Created
June 2, 2013 09:54
-
-
Save funrep/5693194 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module IRC where | |
import Network (connectTo, PortNumber) | |
import System.IO (hGetLine) | |
import Text.Printf (hPrintf, printf) | |
import Config | |
type Net = ReaderT Bot IO | |
data Bot = Bot { socket :: Handle } | |
connect :: IO Bot | |
connect = | |
connectTo server $ PortNumber $ fromIntegral port | |
>>= return . Bot | |
listen :: (String -> Net ()) -> Handle -> Net () | |
listen cmd h = do | |
s <- fmap last $ liftIO $ hGetLine h | |
liftIO $ putStrLn s | |
if ping s then pong s else liftIO $ cmd $ clean s | |
listen cmd h | |
where | |
clean = drop 1 . dropWhile (/= ':' . drop 1 | |
ping x = "PING :" `isPrefixOf` x | |
pong x = write "PONG" $ ':' : drop 6 x | |
privmsg :: String -> Net () | |
privmsg s = write "PRIVMSG" $ chan ++ " :" ++ s | |
write :: String -> String -> Net () | |
write s t = do | |
h <- asks socket | |
liftIO $ hPrintf h "%s %s\n" s t | |
liftIO $ printf "> %s %s\n" s t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Building tob-0.0...
Preprocessing executable 'tob' for tob-0.0...
[2 of 4] Compiling IRC ( src/IRC.hs, dist/build/tob/tob-tmp/IRC.o )
src/IRC.hs:25:5: parse error (possibly incorrect indentation)