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/Types.hs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/Reaktor/Types.hs (limited to 'src/Reaktor/Types.hs') diff --git a/src/Reaktor/Types.hs b/src/Reaktor/Types.hs new file mode 100644 index 0000000..f2115be --- /dev/null +++ b/src/Reaktor/Types.hs @@ -0,0 +1,68 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} +module Reaktor.Types (module Reaktor.Types, module X) where + +import Blessings (Blessings) +import Control.Monad.Trans.Class as X (lift) +import Control.Monad.Trans.State as X (gets,modify) +import Control.Monad.Trans.State (StateT) +import Data.Aeson +import Data.Aeson.Types +import qualified Data.ByteString.Char8 as BS +import qualified Data.Text.Encoding as T +import Network.Socket as X (HostName,ServiceName) + + +type Prefix = BS.ByteString + +type Nickname = BS.ByteString +type Password = BS.ByteString +type MsgTarget = BS.ByteString +type Channel = MsgTarget + +data PluginState = PluginState { + s_putLog :: Blessings BS.ByteString -> IO (), + s_nick :: BS.ByteString, + s_sendMsg :: Message -> IO (), + s_sendMsg' :: Message -> Message -> IO () + } + +setNick :: Nickname -> PluginIO () +setNick newnick = modify (\q -> q { s_nick = newnick }) + +getNick :: PluginIO Nickname +getNick = gets s_nick + +sendMsg :: Message -> PluginIO () +sendMsg msg = gets s_sendMsg >>= \f -> lift $ f msg + +sendMsg' :: Message -> Message -> PluginIO () +sendMsg' msg logMsg = gets s_sendMsg' >>= \f -> lift $ f msg logMsg + + +type PluginIO = StateT PluginState IO + +type PluginFunc = Message -> PluginIO () + +data Plugin = Plugin { + pluginFunc :: PluginFunc, + requireTLS :: Bool + } + +simplePlugin :: FromJSON a => (a -> PluginFunc) -> Value -> IO Plugin +simplePlugin f v = + either error (\x -> return $ Plugin (f x) False) (parseEither parseJSON v) + + +type Param = BS.ByteString +type Command = BS.ByteString +data Message = Message (Maybe Prefix) Command [Param] + deriving Show + + +instance FromJSON BS.ByteString where + parseJSON (String t) = pure (T.encodeUtf8 t) + parseJSON _ = pure undefined + +instance FromJSONKey BS.ByteString where + fromJSONKey = FromJSONKeyText T.encodeUtf8 -- cgit v1.2.3