summaryrefslogtreecommitdiff
path: root/Hsbot/Message.hs
blob: 819018ddfe11b52d3fd23d10a0feb4569741fe6f (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
module Hsbot.Message
    ( answerMsg
    , getChannel
    , getCommand
    , getDestination
    , getSender
    , readMsg
    , writeMsg
    ) where

import Control.Concurrent
import qualified Data.List as L
import Control.Monad.Reader
import qualified Network.IRC as IRC

import Hsbot.Types

-- Plugin Utils
readMsg :: Plugin (Env IO) Message
readMsg = asks pluginChan >>= liftIO . readChan

-- TODO : remove the Plugin layer since it's useless, and use the envChan instead
writeMsg :: Message -> Plugin (Env IO) ()
writeMsg msg = asks pluginMaster >>= liftIO . flip writeChan msg

answerMsg :: IRC.Message -> String -> Plugin (Env IO) ()
answerMsg _ [] = return ()
answerMsg (IRC.Message (Just (IRC.NickName nick _ _)) _ (channel:_)) msg
    | head channel == '#' = writeMsg . OutgoingMsg $ IRC.Message Nothing "PRIVMSG" [channel, msg]
    | otherwise = writeMsg . OutgoingMsg $ IRC.Message Nothing "PRIVMSG" [nick, msg]
answerMsg _ _ = return ()

-- | Get the channel a message has been posted on
getChannel :: IRC.Message -> String
getChannel (IRC.Message _ _ (channel:_)) = channel
getChannel _ = ""

-- | Get the command in the IRC message if there is one
getCommand :: IRC.Message -> Env IO [String]
getCommand (IRC.Message _ _ (_:msg:[])) = getCommandFrom $ words msg
  where
    getCommandFrom :: [String] -> Env IO [String]
    getCommandFrom (cmd:stuff) = do
        currentBotState <- asks envBotState >>= liftIO . readMVar
        if botNickname currentBotState `L.isPrefixOf` cmd
            then return stuff
            else return []
    getCommandFrom _ = return []
getCommand _ = return []

-- | Get the sender of a message
getSender :: IRC.Message -> String
getSender (IRC.Message (Just (IRC.NickName nick _ _)) _ _) = nick
getSender _ = ""

-- | Get the destination of a message
getDestination :: IRC.Message -> String
getDestination (IRC.Message _ _ (dest:_:[])) = dest
getDestination _ = ""