You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.5KB

  1. {-# LANGUAGE MultiWayIf #-}
  2. module IrcBot.BotActions where
  3. import Network.Connection
  4. import Network.Socket hiding (send, sendTo, recv, recvFrom)
  5. import qualified Data.ByteString.Char8 as C
  6. import IrcBot.BotNetwork
  7. import IrcBot.BotNetworkCommands
  8. import IrcBot.MessageParser
  9. import IrcBot.BotCustomCommands
  10. messageParse:: Connection -> C.ByteString -> IO()
  11. -- its kind of interface for actionFunctions
  12. callActionFunction :: (IServerResponse -> IO()) -> IServerResponse -> IO ()
  13. callActionFunction myFunc parameter1 = myFunc parameter1
  14. messageParse sock stringData =
  15. do
  16. let unpackedByteString = C.unpack stringData
  17. let debugData = "READ DATA FROM SOCKET: " ++ unpackedByteString
  18. let userNick = parseNick unpackedByteString
  19. let channelName = parseChannelName unpackedByteString
  20. let messageText = parseMessageText unpackedByteString
  21. let newServerResponse = IServerResponse {
  22. readDataString = unpackedByteString,
  23. readDataByteString = stringData,
  24. sock = sock,
  25. channelName = channelName,
  26. nick = userNick,
  27. messageText = messageText
  28. }
  29. mapM (\x -> callActionFunction (x) newServerResponse) (methods myMethodList)
  30. let d = "PARSE PURSE MESASGE: " ++ messageText
  31. print d
  32. print debugData
  33. -- if | hasStringExist "PING " stringData -> pong sock unpackedByteString
  34. -- | hasStringExist ".sup" stringData -> sendMessage sock channelName ("sup " ++ userNick)
  35. -- | hasStringExist " KICK #" stringData -> joinChannel sock channelName >> {-- auto connect in case of kick --} sendMessage sock channelName "fuck you"
  36. -- | hasStringExist ".random" stringData -> basicRandomImplementation unpackedByteString >>= \commandOutput -> sendMessage sock channelName commandOutput
  37. -- | hasStringExist ".quit" stringData -> quitFromServer sock channelName
  38. -- | hasStringExist ".leave" stringData -> disconnectFromChannel sock channelName
  39. -- | otherwise -> print "NO CONDITION FOUND TO PARSE"
  40. connectToServer :: String -> PortNumber -> Bool -> IO Connection
  41. connectToServer server port ssl = do
  42. open server port ssl
  43. connectionLoop :: Connection -> IO Connection
  44. connectionLoop sock =
  45. do
  46. stringData <- readFromSocket sock
  47. messageParse sock stringData
  48. case stringData == C.empty of
  49. False -> do
  50. connectionLoop sock
  51. True -> do
  52. return sock