summaryrefslogtreecommitdiffstats
path: root/main.hs
blob: b178370982fff430b48479a03c76d83008eaa74f (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
{-# 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