Skip to content

Instantly share code, notes, and snippets.

@funrep
Last active December 18, 2015 22:49
Show Gist options
  • Save funrep/5857035 to your computer and use it in GitHub Desktop.
Save funrep/5857035 to your computer and use it in GitHub Desktop.
-- Extremely simple irc bot that logs all privmsg's sent to #lobby
import Network (connectTo, PortID (PortNumber))
import System.IO (hGetLine, hPutStrLn, hSetBuffering, BufferMode (NoBuffering), Handle)
import Data.List (isPrefixOf)
main = connect
connect :: IO ()
connect = do
h <- connectTo "irc.codetalk.io" $ PortNumber $ fromIntegral 6667
hSetBuffering h NoBuffering
send h "NICK" "logger"
send h "USER" "logger 0 * :IRC Logger"
logger h
logger :: Handle -> IO ()
logger h = do
s <- hGetLine h
let msg = init s
if ping msg then
pong msg
else check h msg
logger h
where
ping = isPrefixOf "PING :"
pong = send h "PONG" . (:) ':' . drop 6
check :: Handle -> String -> IO ()
check h s
| "End of /MOTD command." `isPrefixOf` clean s = join
| privmsg s = store s
| otherwise = return ()
where
privmsg = (== "PRIVMSG") . takeWhile (/= ' ') . drop 1 . dropWhile (/= ' ')
store = appendFile "log.txt" . flip (++) "\n"
clean = drop 1 . dropWhile (/= ':') . drop 1
join = send h "JOIN" "#lobby"
send :: Handle -> String -> String -> IO ()
send h x y = hPutStrLn h $ x ++ " " ++ y ++ "\r\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment