summaryrefslogtreecommitdiff
path: root/src/Lib.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Lib.hs')
-rw-r--r--src/Lib.hs40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
index d36ff27..12d4dbc 100644
--- a/src/Lib.hs
+++ b/src/Lib.hs
@@ -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