aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin.hs
blob: d4aa6f97c49e69798ead892dd51b534972e22fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Plugin
    ( matchPlugin
    , performPlugin
    , plugins
    ) where

import Control.Monad (mzero)
import qualified Data.Text as T

import Text.Regex.TDFA

import Bot (Bot)
import qualified Message as M
import Plugin.Base
import qualified PluginList as PL (plugins)
import Plugin.Help (help)

-- | Get the first plugin that matches the given message text.
matchPlugin :: M.Message -> Maybe (Bot Plugin)
matchPlugin message = do
    -- plugins' <- return $ sequence plugins
    -- firstPlugin $ matchPlugins message plugins'
    return $ firstPlugin $ matchBotPlugins message
  where
    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 :: Bot Plugin -> PluginAction
performPlugin p message = do
    plugin <- p
    perform plugin $ message

plugins :: [Bot Plugin]
plugins = PL.plugins ++ [help]