From affb573718b28e2df9f5c5f10fb7a1fff434495e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 19 Sep 2017 04:52:46 +0200 Subject: Plugin(matchPlugin): Get our functions to actually work with `Bot` Previously we added the `Bot` monad to our signature, but we didn't have a way to filter and match plugins wrapped in the `Bot` monad. This is what we're doing here. It's the same work as before, but we need to account for a wrapped `Plugin` type. There's a lot of extra cruft code here from when I was trying things out to get it working. The short story is that I do some binding and sequencing dances to extract `Plugin`s so that we can actually match against them. I've seen recommendations against `fail`, which is why I tried to use `mzero`, but it just seemed to be too complicated so went with `fail`, which seems to make sense in context. --- src/Plugin.hs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'src/Plugin.hs') diff --git a/src/Plugin.hs b/src/Plugin.hs index a79e815..04a8054 100644 --- a/src/Plugin.hs +++ b/src/Plugin.hs @@ -4,6 +4,7 @@ module Plugin , plugins ) where +import Control.Monad (mzero) import qualified Data.Text as T import Text.Regex.TDFA @@ -16,15 +17,54 @@ import Plugin.Help (help) -- | Get the first plugin that matches the given message text. matchPlugin :: M.Message -> Maybe (Bot Plugin) -matchPlugin message = firstPlugin $ matchPlugins message plugins +matchPlugin message = do + -- plugins' <- return $ sequence plugins + -- firstPlugin $ matchPlugins message plugins' + return $ firstPlugin $ matchBotPlugins message where - firstPlugin [] = Nothing - firstPlugin (p:ps) = Just p + firstPlugin :: Bot [Plugin] -> Bot Plugin + -- firstPlugin [] = Nothing + -- firstPlugin (p:ps) = Just p + firstPlugin plugins = do + plugins' <- plugins + case plugins' of + -- [] -> mzero + [] -> fail "Empty plugin list" + (p:ps) -> return p + + -- matchBotPlugins :: M.Message -> [Bot Plugin] + -- matchBotPlugins message = do + -- -- plugins' <- sequence plugins + -- -- return $ matchPlugins message plugins' + -- return $ matchPlugins message plugins + matchBotPlugins :: M.Message -> Bot [Plugin] + matchBotPlugins message = do + plugins' <- sequence plugins + return $ matchPlugins message plugins' -- | Filter the list of plugins to those that match the given message. matchPlugins :: M.Message -> [Plugin] -> [Plugin] matchPlugins message plugins = [p | p <- plugins, M.textStr message =~ matchRegex p] + -- where + -- matches :: M.Message -> Bot Plugin -> Bool + -- matches message pluginM = do + -- p <- pluginM + -- M.textStr message =~ matchRegex p +-- --- +-- matchPlugins :: M.Message -> [Bot Plugin] -> [Bot Plugin] +-- matchPlugins message plugins = +-- [p | p <- plugins, matches message p] +-- where +-- matches :: M.Message -> Bot Plugin -> Bool +-- matches message pluginM = do +-- p <- pluginM +-- M.textStr message =~ matchRegex p +-- --- +-- matchPlugins :: M.Message -> [Bot Plugin] -> [Bot Plugin] +-- matchPlugins message plugins = do +-- _ <- return $ sequence plugins +-- [return p | p <- plugins', M.textStr message =~ matchRegex p] -- | Run the action belonging to the plugin, stored in its `perform` field. performPlugin :: Plugin -> PluginAction -- cgit v1.2.3