|
- {-# LANGUAGE MultiWayIf #-}
-
- module IrcBot.BotActions where
-
-
- import Network.Connection
- import Network.Socket hiding (send, sendTo, recv, recvFrom)
- import qualified Data.ByteString.Char8 as C
- import IrcBot.BotNetwork
- import IrcBot.BotNetworkCommands
- import IrcBot.MessageParser
- import IrcBot.BotCustomCommands
- import IrcBot.Definitions.ServerResponse
- import IrcBot.Definitions.ServerAddress
- import qualified Data.ByteString.Lazy as L
- import IrcBot.Definitions.Options
- import qualified Data.Aeson as A
- import Data.Maybe
- import qualified Data.List as DL
-
-
-
-
- -- its kind of interface for actionFunctions
- callActionFunction :: (IServerResponse -> IO()) -> IServerResponse -> IO ()
- callActionFunction myFunc parameter1 = myFunc parameter1
-
-
- callByMethodDefinition :: MethodDefinition -> Connection -> IServerResponse -> IO ()
- callByMethodDefinition methodDefinition sock serverResponse = do
- let commandCallerString = (callerString methodDefinition)
- let commandMethod = (callableFunction methodDefinition)
- if hasStringExist commandCallerString (readDataByteString serverResponse) && (commandType methodDefinition) /= NON_RETURN_ACTION then
- do
- resultString <- commandMethod serverResponse
- writeToSocket sock resultString
-
- else if hasStringExist commandCallerString (C.pack (messageType serverResponse)) && (commandType methodDefinition) == NON_RETURN_ACTION then
- do
- resultString <- commandMethod serverResponse
- writeToSocket sock resultString
- else
- print "COMMAND DOESNT EXIST"
-
-
- createServerReponse :: C.ByteString -> IServerAddress -> IO IServerResponse
- createServerReponse stringData serverAddress =
- do
- let unpackedByteString = C.unpack stringData
- let debugData = "READ DATA FROM SOCKET: " ++ unpackedByteString
- print debugData
- let messageParsedMeta = words $ parseMessageMeta unpackedByteString
- let messageType = parseMessageType messageParsedMeta
- print messageParsedMeta
-
-
- let userNick = parseNick unpackedByteString
- let channelName = parseChannelName unpackedByteString
-
- -- let targetNick = parseMessageTargetFromMeta messageParsedMeta
- let messageText = parseMessageText unpackedByteString
-
-
- let target = targetDeterminer userNick channelName
- return $ IServerResponse {
- serverName = (server serverAddress),
- readDataString = unpackedByteString,
- readDataByteString = stringData,
- channelName = channelName,
- nick = userNick,
- messageText = messageText,
- messageTarget = target,
- messageType = messageType
- }
-
-
-
- messageParse :: Connection -> C.ByteString -> IServerAddress -> IO()
- messageParse sock stringData serverAddress =
- do
-
-
- newServerResponse <- createServerReponse stringData serverAddress
- determineSockOutputOperation sock newServerResponse
-
- -- mapM (\x -> callActionFunction (x) newServerResponse) (methods myMethodList)
- -- if | hasStringExist "PING " stringData -> pong sock unpackedByteString
- -- | hasStringExist ".sup" stringData -> sendMessage sock channelName ("sup " ++ userNick)
- -- | hasStringExist " KICK #" stringData -> joinChannel sock channelName >> {-- auto connect in case of kick --} sendMessage sock channelName "fuck you"
- -- | hasStringExist ".random" stringData -> basicRandomImplementation unpackedByteString >>= \commandOutput -> sendMessage sock channelName commandOutput
- -- | hasStringExist ".quit" stringData -> quitFromServer sock channelName
- -- | hasStringExist ".leave" stringData -> disconnectFromChannel sock channelName
- -- | otherwise -> print "NO CONDITION FOUND TO PARSE"
-
- determineSockOutputOperation :: Connection -> IServerResponse -> IO ()
- determineSockOutputOperation sock newServerResponse = do
-
- output <- L.readFile ".connection.json"
- let option = fromJust (A.decode output :: Maybe IOptions)
- if elem (nick newServerResponse) (admins option)
- then do
- mapM (\x -> callByMethodDefinition x sock newServerResponse) (methods myAdminMethodList)
- mapM (\x -> callByMethodDefinition x sock newServerResponse) (methods myMethodList)
- print "command from admin"
- else do
- mapM (\x -> callByMethodDefinition x sock newServerResponse) (methods myMethodList)
- print "command from normal user"
-
-
-
-
- connectToServer :: String -> PortNumber -> Bool -> IO Connection
- connectToServer server port ssl = do
- open server port ssl
-
- connectionLoop :: Connection -> IServerAddress -> IO Connection
- connectionLoop sock serverAddress =
- do
- stringData <- readFromSocket sock
- messageParse sock stringData serverAddress
- case stringData == C.empty of
- False -> do
- connectionLoop sock serverAddress
- True -> do
- return sock
|