From ef95ac393a1715550120770af034121ac02b1a0d Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 3 Feb 2023 22:54:25 +0100 Subject: Improved handling of rate limit. Commands can now return True or False to specify whether they should count towards the rate limit. Some non-spammy commands are set to always false, while sending a file will only count if the file could actually be sent. Commands can also now be sent immediately after miaowe startup, without an initial delay. --- miaowe.hs | 58 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/miaowe.hs b/miaowe.hs index e2be098..61b161b 100644 --- a/miaowe.hs +++ b/miaowe.hs @@ -19,6 +19,7 @@ -} import Data.Bits +import Data.Bool import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S.Char8 import qualified Data.ByteString.Internal as S.Internal @@ -66,6 +67,9 @@ helpString = "Commands available: Code; Help; List. \ Anything else is interpreted as a file name." #endif +-- Minimum time between spammy commands. +minimumDelay = 30 + ctrlC = [toEnum 03] :: String ctrlO = [toEnum 15] :: String ctrlV = [toEnum 22] :: String @@ -215,58 +219,68 @@ sendFile channel path = S.readFile path lines . S.Char8.unpack -trySendFile :: IrcChannel -> FilePath -> IO () +trySendFile :: IrcChannel -> FilePath -> IO Bool trySendFile channel path = (doesFileExist (path) >>= (\b -> if b then sendFile channel (path) + >> return True else sendToChannel channel "No such file or command." - >> hFlush stdout)) + >> hFlush stdout + >> return False)) #ifdef FORTUNE getFortune :: IO String getFortune = readCreateProcess ((shell "fortune -e | cowsay -b")) "" #endif -doCommand :: IrcChannel -> MircString -> IO () +-- Takes a channel and command, return True if +-- command should count towards rate limiting, +-- False if not. +doCommand :: IrcChannel -> MircString -> IO Bool doCommand channel command = case map toLower ((words command)!!4) of "list" -> listDirectory "." - >>= sendToChannel channel . - intercalate "\n" + >>= sendToChannel channel . intercalate "\n" + >> return True "code" -> sendToChannel channel codeUrl + >> return False "help" -> sendToChannel channel helpString + >> return False #ifdef FORTUNE "fortune" -> getFortune >>= sendToChannel channel + >> return True #endif _ -> (if isInfixOf "/" ((words command)!!4) then - sendToChannel channel - "Only PWD please." + sendToChannel channel "Only PWD please." + >> return False else - trySendFile channel - ((words command)!!4)) + trySendFile channel ((words command)!!4)) +-- Subtracts t from the current time to get the time t ago. subtractPresent :: NominalDiffTime -> IO UTCTime subtractPresent t = getCurrentTime >>= \c -> return $ addUTCTime (-t) c +-- Takes channel, command, and time elapsed since last command. +-- Returns the time to use for counting, either the current time, +-- or the one of the previous command it throttling is not wished. processCommand :: IrcChannel -> MircString -> NominalDiffTime -> IO UTCTime -processCommand channel command timeElapsed | timeElapsed < 30 = - sendToChannel channel - "Not so quick, partner" - >> subtractPresent timeElapsed - | length (words command) < 5 = - sendToChannel channel - "You need to specify \ +processCommand channel command timeElapsed + | timeElapsed < minimumDelay = + sendToChannel channel "Not so quick, partner" + >> subtractPresent timeElapsed + | length (words command) < 5 = + sendToChannel channel "You need to specify \ a command." - >> subtractPresent timeElapsed - | otherwise = - doCommand channel command - >> getCurrentTime + >> subtractPresent timeElapsed + | otherwise = + doCommand channel command + >>= bool (subtractPresent timeElapsed) getCurrentTime commandChannel :: String -> String commandChannel command = (words command)!!2 @@ -280,7 +294,7 @@ mainLoop channel time = waitForCommand (diffUTCTime t time) else return time) >>= mainLoop channel - + main :: IO () main = @@ -297,4 +311,4 @@ main = joinChannel (args!!0) >> hFlush stdout >> getCurrentTime - >>= mainLoop (args!!0) + >>= \t -> mainLoop (args!!0) (addUTCTime (-minimumDelay) t) -- cgit v1.2.3