polly wanna cracker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.8 KiB

6 months ago
{-# LANGUAGE OverloadedStrings #-}
module Main where
import System.IO
import System.Exit (exitSuccess)
6 months ago
import Control.Exception (bracket, bracket_)
import Control.Monad.Reader
import Control.Monad.IO.Class (liftIO)
6 months ago
import Data.List (intercalate, isPrefixOf)
import qualified Network.Socket as N
6 months ago
-- internal imports
import Types
6 months ago
-- configuration options (TODO: Move to yaml and read from there)
server = "irc.freenode.org"
port = 6667
chan = "#parrotbot-testing"
nick = "parrotbot"
6 months ago
-- | Entry point
6 months ago
main :: IO ()
6 months ago
main =
bracket connect disconnect loop
where
disconnect = hClose . botSocket
loop st = runReaderT run st
-- | Process bot comands inside of its monad
run
:: Net ()
run = do
write "NICK" nick
write "USER" (nick <> " 0 * :parrot bot")
write "JOIN" chan
listen
-- | Connect to the server und return the initial bot state
6 months ago
connect
6 months ago
:: IO Bot
connect = notify $ do
handle <- connectTo server port
return (Bot handle)
where
notify a = bracket_
(do
putStrLn ("Connecting to " <> server <> "")
hFlush stdout
)
(putStrLn "done.")
a
-- | Connect to an IRC server given domain name and port (Helper for 'connect'
connectTo
6 months ago
:: N.HostName -- ^ Domain to connect to
-> N.PortNumber -- ^ Port to connect to
-> IO Handle -- ^ Handle for the connection
6 months ago
connectTo chost cport = do
6 months ago
(addr:_) <- N.getAddrInfo Nothing (Just chost) (Just (show cport))
sock <- N.socket
(N.addrFamily addr)
(N.addrSocketType addr)
(N.addrProtocol addr)
N.connect sock (N.addrAddress addr)
N.socketToHandle sock ReadWriteMode
-- | Send messages to IRC
write
6 months ago
:: String -- ^ Command to issue
6 months ago
-> String -- ^ Command argument(s)
6 months ago
-> Net ()
write cmd args = do
6 months ago
let msg = intercalate " " [cmd, args, "\r\n"]
6 months ago
handle <- asks botSocket
liftIO $ do
hPutStr handle msg
putStr ("> " <> msg)
6 months ago
-- | Send Messages via PRIVMSG to the connected Channel
privmsg
6 months ago
:: String -- ^ Message Content
-> Net ()
privmsg msg = write "PRIVMSG" (chan <> " :" <> msg)
6 months ago
-- | Listen to and process messages from IRC
listen
6 months ago
:: Net ()
listen = forever $ do
handle <- asks botSocket
line <- liftIO $ hGetLine handle
liftIO $ putStrLn line
6 months ago
let s = init line
if isPing s
then
pong s
else
6 months ago
evaluate (clean s)
6 months ago
where
6 months ago
forever :: Net () -> Net ()
6 months ago
forever a = do
a
forever a
clean :: String -> String
clean = drop 1 . dropWhile (/= ':') . drop 1
isPing :: String -> Bool
isPing s = "PING" `isPrefixOf` s
6 months ago
pong :: String -> Net ()
pong x = write "PONG" (':' : drop 6 x)
6 months ago
-- | Evaluate input from IRC
evaluate
6 months ago
:: String -- ^ Input to be processed
-> Net ()
evaluate "!quit" = do
write "QUIT" ":I have been orderd to go"
liftIO exitSuccess
evaluate text | "!say " `isPrefixOf` text =
privmsg (drop 5 text)
evaluate _ =
6 months ago
return ()