summaryrefslogtreecommitdiff
path: root/HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs
diff options
context:
space:
mode:
authorJulien Dessaux2010-07-03 21:26:00 +0200
committerJulien Dessaux2010-07-03 22:40:17 +0200
commit11c2c16835b3e8368be77ccc5b7ddf949021eccd (patch)
tree7733132ee370335156219ff6eb4f0ef2dbd1c8ff /HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs
parentWrote most of the resume code for the core and the irc plugin. (diff)
downloadhsbot-11c2c16835b3e8368be77ccc5b7ddf949021eccd.tar.gz
hsbot-11c2c16835b3e8368be77ccc5b7ddf949021eccd.tar.bz2
hsbot-11c2c16835b3e8368be77ccc5b7ddf949021eccd.zip
Moved files around as a preliminary for architectural changes.
Diffstat (limited to 'HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs')
-rw-r--r--HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs b/HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs
new file mode 100644
index 0000000..1e54d3a
--- /dev/null
+++ b/HsbotIrcBot/Hsbot/Irc/Plugin/Utils.hs
@@ -0,0 +1,66 @@
+module Hsbot.Irc.Plugin.Utils
+ ( IrcPlugin
+ , IrcPluginState (..)
+ , answerMsg
+ , readMsg
+ , sendCommand
+ , sendCommandWithRequest
+ , sendRegisterCommand
+ , sendUnregisterCommand
+ , writeMsg
+ ) where
+
+import Control.Concurrent
+import Control.Concurrent.Chan ()
+import Control.Monad.State
+import Data.Maybe (fromMaybe)
+
+import Hsbot.Irc.Message
+
+-- | The IrcPlugin monad
+type IrcPlugin = StateT IrcPluginState IO
+
+-- | A plugin state
+data IrcPluginState = IrcPluginState
+ { ircPluginName :: String -- The plugin's name
+ , ircPluginChan :: Chan IrcBotMsg -- The plugin chan
+ , ircPluginMasterChan :: Chan IrcBotMsg -- The master's chan
+ }
+
+--- | Basic input output for IrcPlugins
+readMsg :: IrcPlugin (IrcBotMsg)
+readMsg = do
+ chan <- gets ircPluginChan
+ input <- liftIO $ readChan chan
+ return input
+
+writeMsg :: IrcBotMsg -> IrcPlugin ()
+writeMsg (OutIrcMsg msg) = do
+ chan <- gets ircPluginMasterChan
+ liftIO $ writeChan chan (OutIrcMsg msg)
+writeMsg _ = return ()
+
+answerMsg :: IrcMsg -> String -> IrcPlugin ()
+answerMsg request msg = do
+ let chanOrigin = head $ ircMsgParameters request
+ sender = takeWhile (/= '!') $ fromMaybe "" (ircMsgPrefix request)
+ case head chanOrigin of
+ '#' -> writeMsg . OutIrcMsg $ IrcMsg Nothing "PRIVMSG" [chanOrigin, msg]
+ _ -> writeMsg . OutIrcMsg $ IrcMsg Nothing "PRIVMSG" [sender, msg]
+
+-- | Command management
+sendCommand :: String -> String -> String -> IrcPlugin ()
+sendCommand cmd to params = sendCommandWithRequest cmd to params emptyIrcMsg
+
+sendCommandWithRequest :: String -> String -> String -> IrcMsg -> IrcPlugin ()
+sendCommandWithRequest cmd to params originalRequest = do
+ masterChan <- gets ircPluginMasterChan
+ from <- gets ircPluginName
+ liftIO . writeChan masterChan . IntIrcCmd $ IrcCmd cmd from to params originalRequest
+
+sendRegisterCommand :: String -> IrcPlugin ()
+sendRegisterCommand cmd = sendCommand "REGISTER" "CORE" cmd
+
+sendUnregisterCommand :: String -> IrcPlugin ()
+sendUnregisterCommand cmd = sendCommand "UNREGISTER" "CORE" cmd
+