diff options
author | Hendrik Jaeger <git-commit@henk.geekmail.org> | 2019-10-21 18:34:41 +0200 |
---|---|---|
committer | Hendrik Jaeger <git-commit@henk.geekmail.org> | 2019-10-21 18:34:41 +0200 |
commit | 5530bd9742bc73c2c6631c20c942d3ecd4b81505 (patch) | |
tree | 15f5234c63410e27683e1b071d7263cbd233e9a8 /src | |
parent | 853539c80ebe2124ff9191b48f717a4386d5faee (diff) |
Completed first steps
Diffstat (limited to 'src')
-rw-r--r-- | src/Lib.hs | 40 |
1 files changed, 37 insertions, 3 deletions
@@ -1,6 +1,40 @@ module Lib - ( someFunc + ( runBirch ) where -someFunc :: IO () -someFunc = putStrLn "someFunc" +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 |