Added plugin loading, and the most basic hook capability.
This commit is contained in:
parent
7e89b50bfe
commit
c497b24700
6 changed files with 68 additions and 35 deletions
38
Hsbot/Plugin.hs
Normal file
38
Hsbot/Plugin.hs
Normal file
|
@ -0,0 +1,38 @@
|
|||
module Hsbot.Plugin
|
||||
( loadPlugin
|
||||
) where
|
||||
|
||||
import Control.Concurrent
|
||||
import qualified Data.Map as M
|
||||
import Control.Monad.Reader
|
||||
import Control.Monad.State
|
||||
import System.Log.Logger
|
||||
|
||||
import Hsbot.Types
|
||||
|
||||
loadPlugin :: PluginId -> Bot (Env IO) ()
|
||||
loadPlugin pId = do
|
||||
bot <- get
|
||||
chan <- liftIO (newChan :: IO (Chan Message))
|
||||
master <- lift $ asks envChan
|
||||
let name = pluginName pId
|
||||
loop = pluginEp pId
|
||||
oldPlugins = botPlugins bot
|
||||
pState = PluginState { pluginId = pId
|
||||
, pluginChan = chan
|
||||
, pluginMaster = master }
|
||||
-- We check for unicity
|
||||
case M.lookup name oldPlugins of
|
||||
Just _ -> liftIO . warningM "Hsbot.Core.LoadPlugin" $ "Not loading already loaded plugin : " ++ name
|
||||
Nothing -> do
|
||||
liftIO . infoM "Hsbot.Core.LoadPlugin" $ "Loading plugin : " ++ name
|
||||
env <- lift ask
|
||||
finalStateMVar <- liftIO newEmptyMVar
|
||||
threadId <- liftIO . forkIO $ runReaderT (execStateT loop pState >>= storeFinalState finalStateMVar) env
|
||||
let newPlugins = M.insert name (pState, finalStateMVar, threadId) oldPlugins
|
||||
put $ bot { botPlugins = newPlugins
|
||||
, botHooks = chan : botHooks bot }
|
||||
where
|
||||
storeFinalState :: MVar PluginState -> PluginState -> Env IO ()
|
||||
storeFinalState finalStateMVar finalState = liftIO $ putMVar finalStateMVar finalState
|
||||
|
Reference in a new issue