summaryrefslogtreecommitdiff
path: root/Hsbot/Plugin.hs
diff options
context:
space:
mode:
authorJulien Dessaux2010-02-04 20:38:06 +0100
committerJulien Dessaux2010-02-04 20:38:06 +0100
commitd2f40f64819ec0b19911c684ea919beebf16af6d (patch)
tree18c7393fcd62d894741bdaede8f13e2bb708a437 /Hsbot/Plugin.hs
parentReorganized code and types, changed slightly the architecture. (diff)
downloadhsbot-d2f40f64819ec0b19911c684ea919beebf16af6d.tar.gz
hsbot-d2f40f64819ec0b19911c684ea919beebf16af6d.tar.bz2
hsbot-d2f40f64819ec0b19911c684ea919beebf16af6d.zip
Finished changing plugin data structure to Maps.
Diffstat (limited to '')
-rw-r--r--Hsbot/Plugin.hs25
1 files changed, 19 insertions, 6 deletions
diff --git a/Hsbot/Plugin.hs b/Hsbot/Plugin.hs
index 662f8e9..2121c7d 100644
--- a/Hsbot/Plugin.hs
+++ b/Hsbot/Plugin.hs
@@ -1,27 +1,34 @@
module Hsbot.Plugin
( loadPlugin
+ , pluginExists
, sendToPlugin
) where
import Control.Concurrent
import Control.Concurrent.Chan
import Control.Monad.State
+import qualified Data.Map as M
import System.IO
import System.Plugins
import Hsbot.Types
import Hsbot.Utils
+-- TODO : unload plugin, reload plugin, list plugins, etc
+
-- | Loads a plugin into an ircBot
loadPlugin :: String -> IrcBot ()
loadPlugin name = do
bot <- get
- plugin <- liftIO $ effectivelyLoadPlugin name (botChannel bot)
- case plugin of
- Just plugin' -> do
- let oldPlugins = botPlugins bot
- put $ bot { botPlugins = plugin' : oldPlugins } -- TODO : clean with a correct append
- Nothing -> return ()
+ let oldPlugins = botPlugins bot
+ if name `M.member` oldPlugins
+ then traceM $ inColor ("Can't load plugin \"" ++ name ++ "\", this identifier has already been registered.") [31]
+ 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
effectivelyLoadPlugin :: String -> Chan BotMsg -> IO (Maybe Plugin)
@@ -54,3 +61,9 @@ sendToPlugin msg plugin = do
let chan = pluginChannel plugin
liftIO $ writeChan chan msg
+-- | Tells if a plugin is loaded or not
+pluginExists :: String -> IrcBot Bool
+pluginExists name = do
+ plugins <- gets botPlugins
+ return $ name `M.member` plugins
+