From 04a9fc75e23f2903d8df07e3cd20e7dba64959b1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 2 Aug 2017 00:28:28 +0200 Subject: Change `PluginAction` type to `Message -> String` Have `PluginAction` functions take a Message type instead of a plain string. This gives us access to additional fields on the message: channel and nick. sorbot.cabal: Add `Message` to `exposed-modules` in order to be able to use it when building. Lib.hs: Change our test message to be a `Message` data type to conform to the new `PluginAction` interface. Plugin.hs: Use `Message` where appropriate. When calling `perform`, pass it a `Message` instead of a `String`. This means we have to match the regex within the plugin in order to get the match data. The benefit of that change is that now we have access to the full message in the plugin if we need it, not just the regex-filtered part. GitHubCommit.hs: Do a regex match against the Message text in order to get the SHA we want from the message. --- src/Plugin.hs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/Plugin.hs') diff --git a/src/Plugin.hs b/src/Plugin.hs index 84b4779..d6598ec 100644 --- a/src/Plugin.hs +++ b/src/Plugin.hs @@ -6,23 +6,25 @@ module Plugin import Text.Regex.TDFA +import qualified Message as M import Plugin.Base import Plugin.GitHubCommit -- | Get the first plugin that matches the given message text. -matchPlugin :: String -> Maybe Plugin +matchPlugin :: M.Message -> Maybe Plugin matchPlugin message = firstPlugin $ matchPlugins message plugins where firstPlugin [] = Nothing firstPlugin (p:ps) = Just p -- | Filter the list of plugins to those that match the given message. -matchPlugins :: String -> [Plugin] -> [Plugin] -matchPlugins message plugins = [p | p <- plugins, message =~ matchRegex p] +matchPlugins :: M.Message -> [Plugin] -> [Plugin] +matchPlugins message plugins = + [p | p <- plugins, M.text message =~ matchRegex p] -- | Run the action belonging to the plugin, stored in its `perform` field. -performPlugin :: Plugin -> String -> String -performPlugin p message = perform p $ message =~ matchRegex p +performPlugin :: Plugin -> M.Message -> String +performPlugin p message = perform p $ message -- | The list of plugins to load plugins :: [Plugin] -- cgit v1.2.3