-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcbot.hs
84 lines (70 loc) · 2.08 KB
/
rcbot.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Control.Concurrent
import Control.Monad
import qualified Data.ByteString.Char8 as C8
import Network
import qualified Network.Socket as S
import Network.IRC
import System.IO
import Text.Printf
server :: String
server = "irc.freenode.net"
port :: Int
port = 6667
nickname :: String
nickname = "noexc-rc"
channels :: [String]
channels = ["#qsolog", "#noexc"]
bot :: String -> IO Handle
bot nickname' = withSocketsDo $ do
h <- connectTo server (PortNumber (fromIntegral port))
hSetBuffering h NoBuffering
write h "NICK" nickname'
write h "USER" (nickname' ++ " 0 * :Recent Changes Bot")
mapM_ (write h "JOIN") channels
return h
write :: Handle -> String -> String -> IO ()
write h s t = do
hPrintf h "%s %s\r\n" s t
printf "> %s %s\n" s t
botPrivmsg :: Handle -> String -> IO ()
botPrivmsg h msg = mapM_ (\c -> write h "PRIVMSG" $ c ++ " :" ++ msg) channels
botPrivmsgChannel :: Handle -> String -> String -> IO ()
botPrivmsgChannel h ch msg = write h "PRIVMSG" $ ch ++ " :" ++ msg
botListen :: Handle -> IO ()
botListen h = forever $ do
s <- C8.hGetLine h
handleLine h $ decode s
C8.putStrLn s
handleLine :: Handle -> Maybe Message -> IO ()
handleLine _ Nothing = return ()
handleLine h (Just m) = reply $ msg_command m
where
params = C8.unpack <$> msg_params m
reply "PRIVMSG" =
case response of
Just s -> botPrivmsgChannel h (head params) s >> return ()
_ -> return ()
where
response =
case params !! 1 of
"!hello" -> Just "hey there"
_ -> Nothing
reply "PING" = write h "PONG " $ ":" ++ params !! 0
reply _ = return ()
udpServer :: Handle -> IO ()
udpServer h = withSocketsDo $ do
sock <- S.socket S.AF_INET S.Datagram 0
S.bindSocket sock (S.SockAddrInet 2000 S.iNADDR_ANY)
forever $ do
(mesg, _, client) <- S.recvFrom sock 1024
_ <- S.sendTo sock mesg client
botPrivmsg h mesg
putStrLn mesg
main :: IO ()
main = do
h <- bot nickname
_ <- forkIO $ botListen h
udpServer h