|
- {-# LANGUAGE MultiWayIf #-}
-
- module IrcBot.BotCustomCommands where
-
- import Network.Connection
- import qualified Data.ByteString.Char8 as C
- import IrcBot.BotNetwork
- import IrcBot.BotNetworkCommands
- import IrcBot.MessageParser
- import Data.List.Split
- import Data.Maybe
- import IrcBot.Definitions.ServerResponse
- import IrcBot.Definitions.Options
- import IrcBot.Definitions.ServerAddress
- import qualified Data.Aeson as A
- import qualified Data.ByteString.Lazy as L
- import IrcBot.JsonConfigDecoder
- import GHC.Generics
- import IrcBot.Config.ConfigHelper
- import System.IO
- import System.Directory
-
-
-
- data MethodList = MethodList { methods :: [MethodDefinition]}
-
-
-
- hasStringExist :: String -> C.ByteString -> Bool
- hasStringExist search source =
- do
- let byteStringSearch = C.pack search
- C.isInfixOf byteStringSearch source
-
-
-
- basicEcho :: IServerResponse -> IO String
- basicEcho serverResponse = do
- return $ prepareSendMessage (messageTarget serverResponse) "EXAMPLE MESSAGE"
-
-
- ping :: IServerResponse -> IO String
- ping serverResponse = do
- return $ preparePong (readDataString serverResponse)
-
-
- join :: IServerResponse -> IO String
- join serverResponse = do
- let splittedData = splitOn " " (messageText serverResponse)
- output <- L.readFile ".connection.json"
- let option = fromJust (A.decode output :: Maybe IOptions)
- let myServers = (servers option)
- let targetChannelName = (splittedData!!1)
- result <- mapM (\x -> if (serverName serverResponse) == (server x) then return $ x { channels = ((channels x) ++ [targetChannelName]) } else return x ) (myServers)
- -- let newConnectionJson = IOptions { servers = myServers }
- -- writeConfigFile newConnectionJson
- writeConfigFile option result
- return $ prepareJoinChannel (channelName serverResponse)
-
-
- -- filterExample :: String -> [String] -> [String]
- -- filterExample a b = filter (\n -> n /= a) b
-
- leftCommand :: IServerResponse -> IO String
- leftCommand serverResponse = do
- let currentChannelName = (channelName serverResponse)
- output <- L.readFile ".connection.json"
- let option = fromJust (A.decode output :: Maybe IOptions)
- let myServers = (servers option)
- result <- mapM (\x -> if (serverName serverResponse) == (server x) then return $ x { channels = (filter (\n -> n /= currentChannelName) (channels x)) } else return x ) (myServers)
- writeConfigFile option result
- return $ "just returning string io"
-
- autoconnect :: IServerResponse -> IO String
- autoconnect serverResponse = do
- return $ prepareJoinChannel (channelName serverResponse)
-
-
- randomImage :: IServerResponse -> IO String
- randomImage serverResponse = do
- x <- basicRandomImplementation (messageText serverResponse)
- return $ prepareSendMessage (channelName serverResponse) x
-
-
-
- botsCommand :: IServerResponse -> IO String
- botsCommand serverResponse = do
- does <- doesFileExist ".bots.txt"
- if does then do
- handle <- openFile ".bots.txt" ReadMode
- content <- hGetContents handle
- if length content > 0 then
- return $ prepareSendMessage (channelName serverResponse) content
- else return $ prepareSendMessage (channelName serverResponse) "bots.txt is empty"
- else return $ prepareSendMessage (channelName serverResponse) "bots.txt doesnt exist"
-
-
- quitCommand :: IServerResponse -> IO String
- quitCommand serverResponse = do
- return $ prepareQuit
-
-
-
- leaveCommand :: IServerResponse -> IO String
- leaveCommand serverResponse = do
- return $ prepareDisconnectFromChannel (channelName serverResponse)
-
-
- data CommandTypes = SERVER | USER | NON_RETURN_ACTION deriving(Eq)
-
-
- data MethodDefinition = MethodDefinition { callerString :: String, callableFunction :: IServerResponse -> IO String, commandType :: CommandTypes }
-
- basicEchoDefinition = MethodDefinition { callerString = ".echo", callableFunction = basicEcho, commandType = USER }
- pingDefinition = MethodDefinition { callerString = "PING", callableFunction = ping, commandType = SERVER }
- bostDefinition = MethodDefinition { callerString = ".bots", callableFunction = botsCommand, commandType = USER }
- quitDefinition = MethodDefinition { callerString = ".quit", callableFunction = quitCommand, commandType = USER }
- leaveDefinition = MethodDefinition { callerString = ".leave", callableFunction = leaveCommand, commandType = USER }
- leftDefinition = MethodDefinition { callerString = ".left", callableFunction = leftCommand, commandType = NON_RETURN_ACTION }
- autoconnectDefinition = MethodDefinition { callerString = "KICK", callableFunction = autoconnect, commandType = NON_RETURN_ACTION }
-
-
- myMethodList = MethodList {methods = [basicEchoDefinition,pingDefinition,bostDefinition,quitDefinition,leaveDefinition,autoconnectDefinition] }
- myAdminMethodList = MethodList { methods = []}
|