Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

124 lignes
5.1KB

  1. {-# LANGUAGE MultiWayIf #-}
  2. module IrcBot.BotCustomCommands where
  3. import Network.Connection
  4. import qualified Data.ByteString.Char8 as C
  5. import IrcBot.BotNetwork
  6. import IrcBot.BotNetworkCommands
  7. import IrcBot.MessageParser
  8. import Data.List.Split
  9. import Data.Maybe
  10. import IrcBot.Definitions.ServerResponse
  11. import IrcBot.Definitions.Options
  12. import IrcBot.Definitions.ServerAddress
  13. import qualified Data.Aeson as A
  14. import qualified Data.ByteString.Lazy as L
  15. import IrcBot.JsonConfigDecoder
  16. import GHC.Generics
  17. import IrcBot.Config.ConfigHelper
  18. import System.IO
  19. import System.Directory
  20. data MethodList = MethodList { methods :: [MethodDefinition]}
  21. hasStringExist :: String -> C.ByteString -> Bool
  22. hasStringExist search source =
  23. do
  24. let byteStringSearch = C.pack search
  25. C.isInfixOf byteStringSearch source
  26. basicEcho :: IServerResponse -> IO String
  27. basicEcho serverResponse = do
  28. return $ prepareSendMessage (messageTarget serverResponse) "EXAMPLE MESSAGE"
  29. ping :: IServerResponse -> IO String
  30. ping serverResponse = do
  31. return $ preparePong (readDataString serverResponse)
  32. join :: IServerResponse -> IO String
  33. join serverResponse = do
  34. let splittedData = splitOn " " (messageText serverResponse)
  35. output <- L.readFile ".connection.json"
  36. let option = fromJust (A.decode output :: Maybe IOptions)
  37. let myServers = (servers option)
  38. let targetChannelName = (splittedData!!1)
  39. result <- mapM (\x -> if (serverName serverResponse) == (server x) then return $ x { channels = ((channels x) ++ [targetChannelName]) } else return x ) (myServers)
  40. -- let newConnectionJson = IOptions { servers = myServers }
  41. -- writeConfigFile newConnectionJson
  42. writeConfigFile option result
  43. return $ prepareJoinChannel (channelName serverResponse)
  44. -- filterExample :: String -> [String] -> [String]
  45. -- filterExample a b = filter (\n -> n /= a) b
  46. leftCommand :: IServerResponse -> IO String
  47. leftCommand serverResponse = do
  48. let currentChannelName = (channelName serverResponse)
  49. output <- L.readFile ".connection.json"
  50. let option = fromJust (A.decode output :: Maybe IOptions)
  51. let myServers = (servers option)
  52. result <- mapM (\x -> if (serverName serverResponse) == (server x) then return $ x { channels = (filter (\n -> n /= currentChannelName) (channels x)) } else return x ) (myServers)
  53. writeConfigFile option result
  54. return $ "just returning string io"
  55. autoconnect :: IServerResponse -> IO String
  56. autoconnect serverResponse = do
  57. return $ prepareJoinChannel (channelName serverResponse)
  58. randomImage :: IServerResponse -> IO String
  59. randomImage serverResponse = do
  60. x <- basicRandomImplementation (messageText serverResponse)
  61. return $ prepareSendMessage (channelName serverResponse) x
  62. botsCommand :: IServerResponse -> IO String
  63. botsCommand serverResponse = do
  64. does <- doesFileExist ".bots.txt"
  65. if does then do
  66. handle <- openFile ".bots.txt" ReadMode
  67. content <- hGetContents handle
  68. if length content > 0 then
  69. return $ prepareSendMessage (channelName serverResponse) content
  70. else return $ prepareSendMessage (channelName serverResponse) "bots.txt is empty"
  71. else return $ prepareSendMessage (channelName serverResponse) "bots.txt doesnt exist"
  72. quitCommand :: IServerResponse -> IO String
  73. quitCommand serverResponse = do
  74. return $ prepareQuit
  75. leaveCommand :: IServerResponse -> IO String
  76. leaveCommand serverResponse = do
  77. return $ prepareDisconnectFromChannel (channelName serverResponse)
  78. data CommandTypes = SERVER | USER | NON_RETURN_ACTION deriving(Eq)
  79. data MethodDefinition = MethodDefinition { callerString :: String, callableFunction :: IServerResponse -> IO String, commandType :: CommandTypes }
  80. basicEchoDefinition = MethodDefinition { callerString = ".echo", callableFunction = basicEcho, commandType = USER }
  81. pingDefinition = MethodDefinition { callerString = "PING", callableFunction = ping, commandType = SERVER }
  82. bostDefinition = MethodDefinition { callerString = ".bots", callableFunction = botsCommand, commandType = USER }
  83. quitDefinition = MethodDefinition { callerString = ".quit", callableFunction = quitCommand, commandType = USER }
  84. leaveDefinition = MethodDefinition { callerString = ".leave", callableFunction = leaveCommand, commandType = USER }
  85. leftDefinition = MethodDefinition { callerString = ".left", callableFunction = leftCommand, commandType = NON_RETURN_ACTION }
  86. autoconnectDefinition = MethodDefinition { callerString = "KICK", callableFunction = autoconnect, commandType = NON_RETURN_ACTION }
  87. myMethodList = MethodList {methods = [basicEchoDefinition,pingDefinition,bostDefinition,quitDefinition,leaveDefinition,autoconnectDefinition] }
  88. myAdminMethodList = MethodList { methods = []}