packages feed

infinity-0.1.1: Plugins/SystemPlugin.hs

module SystemPlugin where
import qualified Data.Map as M
import Control.Monad.Reader
import System.IO
import Data.List
import API


plugin :: InfinityPlugin
plugin = InfinityPlugin {
  name        =  "System",
  commands    =  ["join"
                 ,"part"
                 ,"list"
                 ,"help"
                 ,"version"],
  help        = [("join","joins a channel")
                ,("part","parts a channel")
                ,("list","list commands")
                ,("help","shows help for a command")
                ,("version","show version info")],
  action = system
}

system :: InfAction
system user channel command args = do
  case command of
    "join" -> do
      admins <- asks admins
      if user `elem` admins then do
            mapM_ (\ch -> send "JOIN" ch) (words args)
            return $ "joined " ++ (concat $ intersperse " " $ words args)
       else return "Can't do that..."
    "part" -> do
      admins <- asks admins
      if user `elem` admins then do
            mapM_ (\ch -> send "PART" ch) (words args)
            return $ "parted " ++ (concat $ intersperse " " $ words args)
       else return "Can't do that..."
    "list" -> do
      plist <- asks pluglist
      let plist' = map (\p -> ((name p)++" provides: "++(concat $ intersperse " " $ commands p))) plist
      return $ unlines plist'
    "help" -> do
      plist   <- asks pluglist
      if (not.null) args then do
            let cmd    = (head . words) args
                plist' = filter (\p -> cmd `elem` (commands p)) plist
            if null plist' then do
                  return $ "No plugin with command '"++cmd++"' found..."
             else do
                   let x = head plist'
                   return $ M.findWithDefault "No help available..." cmd $ (M.fromList . help) x
       else return "Need a command, try '!help help'"
    "version" -> do
      return infinityVer