From ce276eee82ec0b8c4106beb4c51d6f9eb77335c4 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 13 Jan 2019 23:52:05 +0100 Subject: src: init --- src/Reaktor/Plugins/Register.hs | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Reaktor/Plugins/Register.hs (limited to 'src/Reaktor/Plugins/Register.hs') diff --git a/src/Reaktor/Plugins/Register.hs b/src/Reaktor/Plugins/Register.hs new file mode 100644 index 0000000..fd17f48 --- /dev/null +++ b/src/Reaktor/Plugins/Register.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiWayIf #-} +{-# LANGUAGE OverloadedStrings #-} +module Reaktor.Plugins.Register (plugin) where + +import Control.Monad (when) +import Data.Aeson +import qualified Data.ByteString.Char8 as BS +import GHC.Generics +import Reaktor.Types +import Reaktor.Utils (nextNick,randomNick) + + +data RegisterConfig = RegisterConfig { + channels :: [BS.ByteString] + } + deriving (FromJSON,Generic) + + +plugin :: Value -> IO Plugin +plugin = simplePlugin run + + +run :: RegisterConfig -> PluginFunc +run cfg msg = do + nick_ <- getNick + case msg of + + Message _ "" _ -> do + sendMsg (Message Nothing "NICK" [nick_]) + sendMsg (Message Nothing "USER" [nick_, "*", "0", nick_]) + + Message (Just _self) "NICK" (newnick:[]) -> do + when (newnick == nick_) $ do + -- TODO JOIN only if not already joined + -- i.e. not during subsequent nick changes + sendMsg (Message Nothing "JOIN" [ BS.intercalate "," (channels cfg) ]) + + -- RFC1459 ERR_NICKNAMEINUSE + Message (Just _servername) "433" (_msgtarget:nickinuse:_reason:[]) -> do + if nickinuse == nick_ then do + let nick' = nextNick nickinuse + sendMsg (Message Nothing "NICK" [nick']) + -- TODO change state on "NICK" + setNick nick' + + -- TODO is this just for NickServ? (also check that module if it has + -- stuff only for "Register") + else do + nick' <- lift randomNick + sendMsg (Message Nothing "NICK" [nick']) + -- TODO set nick on "NICK" message + setNick nick' + + -- RFC2812 ERR_UNAVAILRESOURCE + --Message (Just _servername) "437" (_msgtarget:nickunavail:_reason:[]) -> do + + -- RFC2812 RPL_WELCOME + Message _ "001" [_nick,_s] -> do + --logStrLn $ SGR [32,1] (Plain s) + sendMsg (Message Nothing "JOIN" [ BS.intercalate "," (channels cfg) ]) + + + _ -> return () -- cgit v1.2.3