diff options
Diffstat (limited to 'main.hs')
-rw-r--r-- | main.hs | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Control.Monad (forever) +import Network (withSocketsDo, PortID(..), connectTo) +import System.IO (hSetBuffering, hSetNewlineMode, hPutStrLn, hClose, hGetLine, BufferMode(LineBuffering), universalNewlineMode, Handle) +import Control.Concurrent.Async (race) +import Control.Exception.Base (finally) +import Data.Text (isPrefixOf, pack, replace, unpack) + +main :: IO () +main = withSocketsDo $ do + h <- connectTo "ni.r" (PortNumber 6667) + talk h `finally` hClose h + +handshake :: Handle -> IO () +handshake h = do + hPutStrLn h "NICK test" + hPutStrLn h "USER test * 0 :test" + hPutStrLn h "JOIN #retiolum" + +talk :: Handle -> IO () +talk h = do + hSetNewlineMode h universalNewlineMode + hSetBuffering h LineBuffering + handshake h + _ <- race fromServer toServer + return () + where + fromServer = forever $ do + line <- hGetLine h + --case line of + if (isPrefixOf "PING" (pack line)) then + hPutStrLn h (unpack (replace "PING" "PONG" (pack line))) + else + print line + toServer = do + line <- getLine + case line of + ":quit" -> do hPutStrLn h "/quit"; return "Quit" + _ -> do hPutStrLn h ("PRIVMSG #retiolum :" ++ line); toServer |