summaryrefslogtreecommitdiff
path: root/Hsbot/Message.hs
blob: d2cb085a7b798c2ef9fe6f7b0cf7800c98b99564 (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
module Hsbot.Message
    ( processInternalMessage
    ) where

import Control.Monad.State
import qualified Data.Map as M

import Hsbot.PluginUtils
import Hsbot.Types

-- | Processes an internal message
processInternalMessage :: BotMsg -> Bot ()
processInternalMessage (IntMsg msg)
  | msgTo msg == "CORE" = processCoreMessage msg
  | otherwise = do
      plugins <- gets botPlugins
      case M.lookup (msgTo msg) plugins of
          Just (plugin, _) -> sendToPlugin (IntMsg msg) plugin
          Nothing          -> return ()
processInternalMessage _ = return ()

processCoreMessage :: Msg -> Bot ()
processCoreMessage msg = do
    case msgCmd msg of
        "UPDATE"     -> processUpdateCommand msg
        _            -> return ()

-- | Process an update command
processUpdateCommand :: Msg -> Bot ()
processUpdateCommand msg = do
    bot <- get
    let oldData = botResumeData bot
        from    = msgFrom msg
        stuff   = msgCmd msg
    put $ bot { botResumeData = M.insert from stuff oldData }