blob: 12d4dbcb5e36ca56d1c77f0194b248cabac60c1e (
plain)
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
|
module Lib
( runBirch
) where
import System.IO -- base
import qualified Network.Socket as N -- network
-- Toplevel program
runBirch :: String -> N.PortNumber -> String -> [String] -> IO ()
runBirch myServer myPort myNick myChannels = do
h <- connectTo myServer myPort
write h "NICK" myNick
write h "USER" (myNick ++ " 0 * :tutorial bot")
-- map (write h "JOIN") myChannels
write h "JOIN" (head myChannels)
listen h
-- Connect to a server given its name and port number
connectTo :: N.HostName -> N.PortNumber -> IO Handle
connectTo host port = do
addr : _ <- N.getAddrInfo Nothing (Just host) (Just (show port))
sock <- N.socket (N.addrFamily addr) (N.addrSocketType addr) (N.addrProtocol addr)
N.connect sock (N.addrAddress addr)
N.socketToHandle sock ReadWriteMode
-- Send a message to a handle
write :: Handle -> String -> String -> IO ()
write h cmd args = do
let msg = cmd ++ " " ++ args ++ "\r\n"
hPutStr h msg -- Send message on the wire
putStr ("> " ++ msg) -- Show sent message on the command line
-- Process each line from the server
listen :: Handle -> IO ()
listen h = forever $ do
line <- hGetLine h
putStrLn line
where
forever :: IO () -> IO ()
forever a = do a; forever a
|