aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sorbot.cabal1
-rw-r--r--src/Lib.hs12
-rw-r--r--src/Plugin.hs46
3 files changed, 52 insertions, 7 deletions
diff --git a/sorbot.cabal b/sorbot.cabal
index 758fdc5..49c21fa 100644
--- a/sorbot.cabal
+++ b/sorbot.cabal
@@ -16,6 +16,7 @@ cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: Lib
+ , Plugin
build-depends: base >= 4.7 && < 5
, regex-tdfa
default-language: Haskell2010
diff --git a/src/Lib.hs b/src/Lib.hs
index ddbd41f..a27ff99 100644
--- a/src/Lib.hs
+++ b/src/Lib.hs
@@ -4,11 +4,9 @@ module Lib
import Text.Regex.TDFA
-someFunc :: IO ()
-someFunc
- | rex == True = putStrLn "Match!!"
- | otherwise = putStrLn "No match"
-
+import Plugin
-rex :: Bool
-rex = "75ac7b18a009ffe7a77a17a61d95c01395f36b44" =~ "^[0-9a-f]{40}$"
+someFunc :: IO ()
+someFunc = do
+ let Just plugin = realMatchPlugin "75ac7b18a009ffe7a77a17a61d95c01395f36b44"
+ putStrLn $ performPlugin plugin
diff --git a/src/Plugin.hs b/src/Plugin.hs
new file mode 100644
index 0000000..95f2461
--- /dev/null
+++ b/src/Plugin.hs
@@ -0,0 +1,46 @@
+module Plugin
+ ( realMatchPlugin
+ , Plugin
+ , performPlugin
+ , plugins
+ ) where
+
+import Text.Regex.TDFA
+
+data Plugin = Plugin
+ { matchRegex :: String
+ , perform :: String -> String
+ }
+
+instance Show Plugin where
+ show (Plugin r p) = "matchRegex = " ++ r
+
+realMatchPlugin :: String -> Maybe Plugin
+realMatchPlugin message = matchPlugin message plugins
+
+matchPlugin :: String -> [Plugin] -> Maybe Plugin
+matchPlugin message plugins = firstPlugin $ matchPlugins message plugins
+
+matchPlugins :: String -> [Plugin] -> [Plugin]
+matchPlugins message plugins = [p | p <- plugins, message =~ matchRegex p]
+
+firstPlugin :: [Plugin] -> Maybe Plugin
+firstPlugin [] = Nothing
+firstPlugin (p:ps) = Just p
+
+-- TODO: Make a type for the `perform` function
+performPlugin :: Plugin -> String
+performPlugin p = perform p $ matchRegex p
+
+gitHubCommit = Plugin
+ { matchRegex = "^[0-9a-f]{40}$"
+ , perform = gitHubCommitAction
+ }
+
+gitHubCommitAction :: String -> String
+gitHubCommitAction match = "https://github.com/" ++ match
+
+plugins :: [Plugin]
+plugins =
+ [ gitHubCommit
+ ]