5 import System.IO -- base
6 import qualified Network.Socket as N -- network
9 runBirch :: String -> N.PortNumber -> String -> [String] -> IO ()
10 runBirch myServer myPort myNick myChannels = do
11 h <- connectTo myServer myPort
13 write h "USER" (myNick ++ " 0 * :tutorial bot")
14 -- map (write h "JOIN") myChannels
15 write h "JOIN" (head myChannels)
18 -- Connect to a server given its name and port number
19 connectTo :: N.HostName -> N.PortNumber -> IO Handle
20 connectTo host port = do
21 addr : _ <- N.getAddrInfo Nothing (Just host) (Just (show port))
22 sock <- N.socket (N.addrFamily addr) (N.addrSocketType addr) (N.addrProtocol addr)
23 N.connect sock (N.addrAddress addr)
24 N.socketToHandle sock ReadWriteMode
26 -- Send a message to a handle
27 write :: Handle -> String -> String -> IO ()
29 let msg = cmd ++ " " ++ args ++ "\r\n"
30 hPutStr h msg -- Send message on the wire
31 putStr ("> " ++ msg) -- Show sent message on the command line
33 -- Process each line from the server
34 listen :: Handle -> IO ()
35 listen h = forever $ do
39 forever :: IO () -> IO ()
40 forever a = do a; forever a