Added command registering and dispatching for plugins.
This commit is contained in:
parent
d2f40f6481
commit
cf68de02be
6 changed files with 79 additions and 16 deletions
|
@ -26,7 +26,7 @@ kro = IrcServer
|
||||||
-- | User configuration
|
-- | User configuration
|
||||||
config :: Config
|
config :: Config
|
||||||
config = Config
|
config = Config
|
||||||
{ commandPrefixes = ['@']
|
{ commandPrefix = '@'
|
||||||
, ircServer = kro
|
, ircServer = kro
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
Hsbot.hs
2
Hsbot.hs
|
@ -1,5 +1,6 @@
|
||||||
module Hsbot
|
module Hsbot
|
||||||
( module Config
|
( module Config
|
||||||
|
, module Hsbot.Command
|
||||||
, module Hsbot.Core
|
, module Hsbot.Core
|
||||||
, module Hsbot.IRC
|
, module Hsbot.IRC
|
||||||
, module Hsbot.IRCParser
|
, module Hsbot.IRCParser
|
||||||
|
@ -10,6 +11,7 @@ module Hsbot
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Config
|
import Config
|
||||||
|
import Hsbot.Command
|
||||||
import Hsbot.Core
|
import Hsbot.Core
|
||||||
import Hsbot.IRC
|
import Hsbot.IRC
|
||||||
import Hsbot.IRCParser
|
import Hsbot.IRCParser
|
||||||
|
|
52
Hsbot/Command.hs
Normal file
52
Hsbot/Command.hs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
module Hsbot.Command
|
||||||
|
( dispatchCommand
|
||||||
|
, registerCommand
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Monad.State
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Data.Maybe
|
||||||
|
|
||||||
|
import Config
|
||||||
|
import Hsbot.Plugin
|
||||||
|
import Hsbot.Types
|
||||||
|
import Hsbot.Utils
|
||||||
|
|
||||||
|
-- TODO : unregister command
|
||||||
|
|
||||||
|
dispatchCommand :: BotMsg -> IrcBot ()
|
||||||
|
dispatchCommand (InputMsg inputMsg) = do
|
||||||
|
plugins <- gets botPlugins
|
||||||
|
cmds <- gets botCommands
|
||||||
|
if command inputMsg == "PRIVMSG"
|
||||||
|
then -- The first word matters as the command
|
||||||
|
let msg = (parameters inputMsg) !! 1
|
||||||
|
pfx = commandPrefix config
|
||||||
|
in if (head msg) == pfx
|
||||||
|
then
|
||||||
|
let key = tail msg -- all but the cmd prefix
|
||||||
|
pluginNames = fromMaybe [] $ M.lookup key cmds
|
||||||
|
plugins' = fromMaybe [] $ mapM (flip M.lookup plugins) pluginNames
|
||||||
|
in mapM_ (sendToPlugin (InputMsg inputMsg)) plugins'
|
||||||
|
else
|
||||||
|
mapM_ (sendToPlugin (InputMsg inputMsg)) (M.elems plugins)
|
||||||
|
else
|
||||||
|
return ()
|
||||||
|
dispatchCommand _ = return ()
|
||||||
|
|
||||||
|
registerCommand :: String -> String -> IrcBot ()
|
||||||
|
registerCommand cmd pluginName' = do
|
||||||
|
bot <- get
|
||||||
|
cmds <- gets botCommands
|
||||||
|
exists <- pluginExists pluginName'
|
||||||
|
if exists
|
||||||
|
then
|
||||||
|
let cmds' = if cmd `M.member` cmds
|
||||||
|
then cmds
|
||||||
|
else M.singleton cmd []
|
||||||
|
newCmds = M.adjust (++ [pluginName']) cmd cmds'
|
||||||
|
in put $ bot { botCommands = newCmds }
|
||||||
|
else
|
||||||
|
traceM $ inColor ("Couldn't register command \"" ++ cmd ++ "\" for plugin \""
|
||||||
|
++ pluginName' ++ "\" : plugin does not exists") [31]
|
||||||
|
|
|
@ -7,6 +7,7 @@ import Control.Concurrent.Chan
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
|
||||||
|
import Hsbot.Command
|
||||||
import Hsbot.IRCParser
|
import Hsbot.IRCParser
|
||||||
import Hsbot.Plugin
|
import Hsbot.Plugin
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
@ -30,12 +31,13 @@ runServer = do
|
||||||
let input = readChan chan
|
let input = readChan chan
|
||||||
msg <- liftIO input
|
msg <- liftIO input
|
||||||
case msg of
|
case msg of
|
||||||
InputMsg inputMsg ->
|
InputMsg inputMsg -> do
|
||||||
|
dispatchCommand $ InputMsg inputMsg
|
||||||
mapM_ (sendToPlugin (InputMsg inputMsg) . snd) (M.toList plugins)
|
mapM_ (sendToPlugin (InputMsg inputMsg) . snd) (M.toList plugins)
|
||||||
OutputMsg outputMsg ->
|
OutputMsg outputMsg ->
|
||||||
sendstr (serializeIrcMsg outputMsg)
|
sendstr (serializeIrcMsg outputMsg)
|
||||||
InternalCmd internalCmd ->
|
InternalCmd _ ->
|
||||||
traceM "TODO"
|
traceM "TODO internal command"
|
||||||
runServer
|
runServer
|
||||||
|
|
||||||
-- | Joins a chan
|
-- | Joins a chan
|
||||||
|
|
|
@ -22,9 +22,9 @@ 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?
|
||||||
|
|
||||||
-- | Configuration data type
|
-- | Configuration data type
|
||||||
data Config = Config {
|
data Config = Config
|
||||||
commandPrefixes :: String, -- command prefixes, for example @[\'>\',\'@\',\'?\']@
|
{ commandPrefix :: Char -- command prefixes, for example @[\'>\',\'@\',\'?\']@
|
||||||
ircServer :: IrcServer -- list of 'Server's to connect to
|
, ircServer :: IrcServer -- list of 'Server's to connect to
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
-- | An IRC server
|
-- | An IRC server
|
||||||
|
@ -76,19 +76,19 @@ data Channel = Channel
|
||||||
, channelAdmins :: [String] -- the bot administrators
|
, channelAdmins :: [String] -- the bot administrators
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
-- | A Bot command
|
-- | An IRC message
|
||||||
data IntCmd = IntCmd
|
|
||||||
{ intCmd :: String -- the bot's internal command
|
|
||||||
, intCmdParams :: [String] -- the parameters
|
|
||||||
} deriving (Show)
|
|
||||||
|
|
||||||
-- |An IRC message.
|
|
||||||
data IrcMsg = IrcMsg
|
data IrcMsg = IrcMsg
|
||||||
{ prefix :: Maybe String -- the message prefix
|
{ prefix :: Maybe String -- the message prefix
|
||||||
, command :: String -- the message command
|
, command :: String -- the message command
|
||||||
, parameters :: [String] -- the message parameters
|
, parameters :: [String] -- the message parameters
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
|
-- | An internal command
|
||||||
|
data IntCmd = IntCmd
|
||||||
|
{ intCmd :: String -- the internal command
|
||||||
|
, intMsg :: IrcMsg -- the IrcMsg associated with the command
|
||||||
|
}
|
||||||
|
|
||||||
-- | A plugin definition
|
-- | A plugin definition
|
||||||
data Plugin = Plugin
|
data Plugin = Plugin
|
||||||
{ pluginName :: String -- The plugin's name
|
{ pluginName :: String -- The plugin's name
|
||||||
|
|
7
TODO
Normal file
7
TODO
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
* implement InternalCommands "register command"
|
||||||
|
* unregister command, add it to internal commands
|
||||||
|
* kill threads
|
||||||
|
* unload plugin
|
||||||
|
|
||||||
|
* restore \r in IRCParser
|
||||||
|
|
Reference in a new issue