aboutsummaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Internal.hs
blob: d3ac9cf0e008869525bfffb1582242e49eeb9d5f (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE OverloadedStrings #-}
module Reaktor.Internal (module Reaktor.Internal, 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.Extended as BS
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