summaryrefslogtreecommitdiff
path: root/Hsbot/Types.hs
blob: 49e8e6bb871f50fdf5e95c8b8843dd69fced4230 (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
59
60
61
62
63
64
65
module Hsbot.Types
    ( Bot
    , BotMsg (..)
    , BotResumeData
    , BotState (..)
    , BotStatus (..)
    , Msg (..)
    , Plugin
    , PluginState (..)
    , ResumeData
    , ResumeMsg (..)
    ) where

import Control.Concurrent
import Control.Monad.State
import qualified Data.Map as M
import Data.Time
import System.IO

import Hsbot.Config

-- | The Bot monad
type Bot = StateT BotState IO

-- | An Hsbot state
data BotState = BotState
    { botStartTime  :: UTCTime      -- the bot's uptime
    , botPlugins    :: M.Map String (PluginState, ThreadId) -- Loaded plugins
    , botChan       :: Chan BotMsg  -- the bot's communication channel
    , botConfig     :: Config       -- the bot's starting config
    , botResumeData :: MVar BotResumeData   -- the necessary data to resume the bot's operations on reboot
    }

-- | how we exit from the botLoop
data BotStatus = BotExit | BotReboot | BotContinue deriving (Eq)

-- | Types to factorise resume data
type ResumeData    = M.Map String String
type BotResumeData = M.Map String ResumeData

-- | The Plugin monad
type Plugin = StateT PluginState IO

-- | A plugin state
data PluginState = PluginState
    { pluginName       :: String              -- The plugin's name
    , pluginChan       :: Chan BotMsg         -- The plugin chan
    , pluginHandles    :: M.Map String Handle -- the plugins's handles
    }

-- | A hsbot message
data Msg = Msg
    { msgType  :: String -- the message type
    , msgFrom  :: String -- who issues it
    , msgTo    :: String -- who it is destinated to
    , msgStuff :: String -- the message to be transfered
    } deriving (Show)

data ResumeMsg = ResMsg
    { resMsgFrom :: String
    , resMsgData :: ResumeData
    } deriving (Show)

data BotMsg = InMsg Msg | OutMsg Msg | IntMsg Msg | UpdMsg ResumeMsg deriving (Show)