summaryrefslogtreecommitdiffstats
path: root/ircout.hs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-05-02 21:59:10 +0200
committertv <tv@krebsco.de>2017-05-02 22:06:38 +0200
commit7225d47e9c1f4c7032ad55fbe1d9f33ff205549c (patch)
tree42320805e2c485e1bb5de701a55227840864a52d /ircout.hs
parent6312ce9800708eaf619ad0e93af3a224e09ce4de (diff)
ircout: init
Diffstat (limited to 'ircout.hs')
-rw-r--r--ircout.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/ircout.hs b/ircout.hs
new file mode 100644
index 0000000..e3d0296
--- /dev/null
+++ b/ircout.hs
@@ -0,0 +1,50 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+
+import Control.Concurrent.Async (race)
+import Control.Exception.Base (finally)
+import Control.Monad (forever,unless)
+import Data.List (intercalate,null)
+import Data.Text (isPrefixOf,pack,replace,unpack)
+import Network (withSocketsDo,PortID(..),connectTo)
+import qualified Data.ByteString.Char8 as BS8
+import System.IO (hSetBuffering,hSetNewlineMode,hPutStrLn,hClose,hGetLine,BufferMode(LineBuffering),universalNewlineMode,Handle)
+
+import Config
+
+
+main :: IO ()
+main = do
+ c@Config{..} <- parseConfigFromArgs
+ withSocketsDo $ do
+ h <- connectTo server_hostname (PortNumber server_port)
+ (`finally` hClose h) $ do
+ hSetNewlineMode h universalNewlineMode
+ hSetBuffering h LineBuffering
+ handshake c h >> race (ircAgent c h) (stdinForwarder c h) >>= print
+
+
+handshake :: Config -> Handle -> IO ()
+handshake Config{..} h = do
+ hPutStrLn h ("NICK " ++ nick)
+ hPutStrLn h ("USER " ++ nick ++ " * 0 :" ++ nick)
+ unless (null channels) $ hPutStrLn h ("JOIN " ++ channels)
+ where
+ channels = intercalate "," $ filter ((=='#') . head) msgtarget
+
+
+ircAgent :: Config -> Handle -> IO ()
+ircAgent Config{..} h = forever $ do
+ line <- hGetLine h
+ if (isPrefixOf "PING" (pack line)) then
+ hPutStrLn h (unpack (replace "PING" "PONG" (pack line)))
+ else
+ print line
+
+
+stdinForwarder :: Config -> Handle -> IO ()
+stdinForwarder Config{..} h = forever $ do
+ line <- BS8.getLine
+ hPutStrLn h ("PRIVMSG " ++ msgtarget' ++ " :" ++ BS8.unpack line)
+ where
+ msgtarget' = intercalate "," msgtarget