Removed all the dynamic module loading stuff.
This commit is contained in:
parent
4c76d3d40b
commit
455b18bc10
7 changed files with 31 additions and 58 deletions
|
@ -9,7 +9,7 @@ import Hsbot.Types
|
||||||
|
|
||||||
-- | Imported plugins goes there
|
-- | Imported plugins goes there
|
||||||
defaultPlugins :: [String]
|
defaultPlugins :: [String]
|
||||||
defaultPlugins = [ "Ping", "Core" ]
|
defaultPlugins = []
|
||||||
|
|
||||||
-- | User server
|
-- | User server
|
||||||
localhost :: IrcServer
|
localhost :: IrcServer
|
||||||
|
|
6
Hsbot.hs
6
Hsbot.hs
|
@ -9,6 +9,9 @@ module Hsbot
|
||||||
, module Hsbot.Plugin
|
, module Hsbot.Plugin
|
||||||
, module Hsbot.Types
|
, module Hsbot.Types
|
||||||
, module Hsbot.Utils
|
, module Hsbot.Utils
|
||||||
|
, module Plugins.Core
|
||||||
|
, module Plugins.Ping
|
||||||
|
, module Plugins.Quote
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Config
|
import Config
|
||||||
|
@ -21,4 +24,7 @@ import Hsbot.Main
|
||||||
import Hsbot.Plugin
|
import Hsbot.Plugin
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
import Hsbot.Utils
|
import Hsbot.Utils
|
||||||
|
import Plugins.Core
|
||||||
|
import Plugins.Ping
|
||||||
|
import Plugins.Quote
|
||||||
|
|
||||||
|
|
|
@ -77,12 +77,9 @@ processCoreCommand :: IntCmd -> IrcBot ()
|
||||||
processCoreCommand intCmd = do
|
processCoreCommand intCmd = do
|
||||||
let command' = intCmdCmd intCmd
|
let command' = intCmdCmd intCmd
|
||||||
case command' of
|
case command' of
|
||||||
"LOAD" -> loadPlugin $ intCmdMsg intCmd
|
"LOAD" -> traceM $ inColor "hsbot has been compiled in static mode." [31]
|
||||||
"RELOAD" -> reloadPlugin $ intCmdMsg intCmd
|
|
||||||
"UNLOAD" -> unloadPlugin $ intCmdMsg intCmd
|
"UNLOAD" -> unloadPlugin $ intCmdMsg intCmd
|
||||||
"REGISTER" -> registerCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
|
"REGISTER" -> registerCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
|
||||||
"UNREGISTER" -> unregisterCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
|
"UNREGISTER" -> unregisterCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
|
||||||
_ -> traceM $ inColor ("Invalid command : " ++ (show intCmd)) [31]
|
_ -> traceM $ inColor ("Invalid command : " ++ (show intCmd)) [31]
|
||||||
bot' <- get
|
|
||||||
traceM $ show bot'
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,10 @@ import Hsbot.IRC
|
||||||
import Hsbot.Plugin
|
import Hsbot.Plugin
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
|
||||||
|
import Plugins.Core(mainCore)
|
||||||
|
import Plugins.Ping(mainPing)
|
||||||
|
import Plugins.Quote(mainQuote)
|
||||||
|
|
||||||
-- | Bot's main entry point
|
-- | Bot's main entry point
|
||||||
imain :: IO ()
|
imain :: IO ()
|
||||||
imain = do
|
imain = do
|
||||||
|
@ -24,6 +28,8 @@ imain = do
|
||||||
run :: IrcBot ()
|
run :: IrcBot ()
|
||||||
run = do
|
run = do
|
||||||
initServer
|
initServer
|
||||||
mapM_ loadPlugin defaultPlugins
|
loadPlugin "Ping" mainPing
|
||||||
|
loadPlugin "Core" mainCore
|
||||||
|
loadPlugin "Quote" mainQuote
|
||||||
runServer
|
runServer
|
||||||
|
|
||||||
|
|
|
@ -1,68 +1,35 @@
|
||||||
module Hsbot.Plugin
|
module Hsbot.Plugin
|
||||||
( loadPlugin
|
( loadPlugin
|
||||||
, sendToPlugin
|
, sendToPlugin
|
||||||
, reloadPlugin
|
|
||||||
, unloadPlugin
|
, unloadPlugin
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
import Control.Concurrent.Chan
|
import Control.Concurrent.Chan()
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Maybe
|
import Data.Maybe()
|
||||||
import System.IO
|
import System.IO()
|
||||||
import System.Plugins
|
|
||||||
|
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
import Hsbot.Utils
|
import Hsbot.Utils
|
||||||
|
|
||||||
-- TODO : reload plugin, list plugins, etc
|
|
||||||
|
|
||||||
-- | Loads a plugin into an ircBot
|
-- | Loads a plugin into an ircBot
|
||||||
loadPlugin :: String -> IrcBot ()
|
loadPlugin :: String -> (Chan BotMsg -> Chan BotMsg -> IO ()) -> IrcBot ()
|
||||||
loadPlugin name = do
|
loadPlugin name entryPoint = do
|
||||||
bot <- get
|
bot <- get
|
||||||
let oldPlugins = botPlugins bot
|
let oldPlugins = botPlugins bot
|
||||||
if name `M.member` oldPlugins
|
plugin <- liftIO $ effectivelyLoadPlugin name entryPoint (botChannel bot)
|
||||||
then traceM $ inColor ("Can't load plugin \"" ++ name ++ "\", this identifier has already been registered.") [31] -- or a wait, smthg like that?
|
put $ bot { botPlugins = M.insert name plugin oldPlugins}
|
||||||
else do
|
|
||||||
plugin <- liftIO $ effectivelyLoadPlugin name (botChannel bot)
|
|
||||||
case plugin of
|
|
||||||
Just plugin' -> do
|
|
||||||
put $ bot { botPlugins = M.insert name plugin' oldPlugins}
|
|
||||||
Nothing -> return ()
|
|
||||||
|
|
||||||
-- | Effectively try to load a plugin
|
-- | Effectively try to load a plugin
|
||||||
effectivelyLoadPlugin :: String -> Chan BotMsg -> IO (Maybe Plugin)
|
effectivelyLoadPlugin :: String -> (Chan BotMsg -> Chan BotMsg -> IO ()) -> Chan BotMsg -> IO (Plugin)
|
||||||
effectivelyLoadPlugin name serverChan = do
|
effectivelyLoadPlugin name entryPoint serverChan = do
|
||||||
-- TODO : test if Plugins/ ++ name ++ .hs exists
|
putStrLn $ inColor ("Loaded (static) plugin: " ++ name) [32]
|
||||||
m <- liftIO $ makeAll ("Plugins/" ++ name ++ ".hs") ["-XScopedTypeVariables"]
|
chan <- newChan :: IO (Chan BotMsg)
|
||||||
plugin <- case m of
|
threadId <- forkIO $ entryPoint serverChan chan
|
||||||
MakeSuccess _ _ -> do
|
return $ Plugin name threadId chan
|
||||||
ldstat <- load_ ("Plugins/" ++ name ++ ".o") [".","Hsbot","Hsbot/Plugins"] ("main" ++ name)
|
|
||||||
case ldstat of
|
|
||||||
LoadSuccess v entryPoint -> do
|
|
||||||
putStrLn $ inColor ("Loaded plugin: " ++ name) [32]
|
|
||||||
chan <- newChan :: IO (Chan BotMsg)
|
|
||||||
threadId <- forkIO $ entryPoint serverChan chan
|
|
||||||
return $ Just (Plugin name v threadId chan)
|
|
||||||
LoadFailure e -> do
|
|
||||||
putStrLn $ inColor ("Couldn't load plugin: " ++ name) [31]
|
|
||||||
mapM_ putStrLn e
|
|
||||||
return Nothing
|
|
||||||
MakeFailure e -> do
|
|
||||||
putStrLn $ inColor ("FATAL: Couldn't compile plugin: " ++ name) [31]
|
|
||||||
mapM_ putStrLn e
|
|
||||||
return Nothing
|
|
||||||
return plugin
|
|
||||||
|
|
||||||
-- | Reloads a plugin
|
|
||||||
-- TODO : make it a safe reload (compile before unloading)
|
|
||||||
reloadPlugin :: String -> IrcBot ()
|
|
||||||
reloadPlugin name = do
|
|
||||||
unloadPlugin name
|
|
||||||
loadPlugin name
|
|
||||||
|
|
||||||
-- | Unloads a plugin
|
-- | Unloads a plugin
|
||||||
unloadPlugin :: String -> IrcBot ()
|
unloadPlugin :: String -> IrcBot ()
|
||||||
|
@ -73,9 +40,8 @@ unloadPlugin name = do
|
||||||
Just plugin -> do
|
Just plugin -> do
|
||||||
let newPlugins = M.delete name oldPlugins
|
let newPlugins = M.delete name oldPlugins
|
||||||
liftIO $ throwTo (pluginThreadId plugin) UserInterrupt
|
liftIO $ throwTo (pluginThreadId plugin) UserInterrupt
|
||||||
liftIO $ unloadAll $ pluginModule plugin
|
|
||||||
put $ bot { botPlugins = newPlugins }
|
put $ bot { botPlugins = newPlugins }
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
|
|
||||||
-- | Sends a msg to a plugin
|
-- | Sends a msg to a plugin
|
||||||
sendToPlugin :: BotMsg -> Plugin -> IrcBot ()
|
sendToPlugin :: BotMsg -> Plugin -> IrcBot ()
|
||||||
|
|
|
@ -18,7 +18,6 @@ import Control.Monad.State
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Network
|
import Network
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Plugins
|
|
||||||
import System.Time (ClockTime)
|
import System.Time (ClockTime)
|
||||||
|
|
||||||
-- | TODO : a monad for a channel, and a monad for a server, all together driven by a Bot?
|
-- | TODO : a monad for a channel, and a monad for a server, all together driven by a Bot?
|
||||||
|
@ -102,13 +101,12 @@ data BotMsg = InputMsg IrcMsg | OutputMsg IrcMsg | InternalCmd IntCmd deriving (
|
||||||
-- | A plugin (core side)
|
-- | A plugin (core side)
|
||||||
data Plugin = Plugin
|
data Plugin = Plugin
|
||||||
{ pluginName :: String -- The plugin's name
|
{ pluginName :: String -- The plugin's name
|
||||||
, pluginModule :: Module -- The plugin himself
|
|
||||||
, pluginThreadId :: ThreadId -- The plugin thread
|
, pluginThreadId :: ThreadId -- The plugin thread
|
||||||
, pluginChannel :: Chan BotMsg -- The plugin channel
|
, pluginChannel :: Chan BotMsg -- The plugin channel
|
||||||
}
|
}
|
||||||
|
|
||||||
instance Show Plugin where
|
instance Show Plugin where
|
||||||
show (Plugin name _ _ _) = show name
|
show (Plugin name _ _) = show name
|
||||||
|
|
||||||
-- | A IrcPlugin ("user" side)
|
-- | A IrcPlugin ("user" side)
|
||||||
data PluginInstance = PluginInstance
|
data PluginInstance = PluginInstance
|
||||||
|
|
2
TODO
2
TODO
|
@ -1,8 +1,8 @@
|
||||||
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
|
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
|
||||||
|
|
||||||
|
* Add the IrcMsg as an optional parameter for an internal command
|
||||||
* Solve the catching that never happen in main
|
* Solve the catching that never happen in main
|
||||||
* throwto exception to main thread
|
* throwto exception to main thread
|
||||||
* list plugins
|
|
||||||
* write a safe reload : try reload before unloading
|
* write a safe reload : try reload before unloading
|
||||||
* discard all trace with a color param and replace those with functions info/warn/error/debug
|
* discard all trace with a color param and replace those with functions info/warn/error/debug
|
||||||
|
|
||||||
|
|
Reference in a new issue