aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin.hs
blob: a79e815bdd60ec34912f4aaca1c51100446f8b00 (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
module Plugin
    ( matchPlugin
    , performPlugin
    , plugins
    ) where

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 = 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 :: M.Message -> [Plugin] -> [Plugin]
matchPlugins message plugins =
    [p | p <- plugins, M.textStr message =~ matchRegex p]

-- | Run the action belonging to the plugin, stored in its `perform` field.
performPlugin :: Plugin -> PluginAction
performPlugin p message = perform p $ message

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