From 4a1d1c6f0e3145ddd78c1549949ad0735acbff2c Mon Sep 17 00:00:00 2001 From: Nyo Date: Thu, 9 Jun 2016 10:43:28 +0200 Subject: [PATCH] .BANCHO. Add check silence in send message --- constants/exceptions.py | 3 ++ events/loginEvent.py | 10 ++++-- events/sendPrivateMessageEvent.py | 7 ++++ events/sendPublicMessageEvent.py | 7 ++++ objects/osuToken.py | 54 +++++++++++++------------------ 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/constants/exceptions.py b/constants/exceptions.py index 1bdbe6b..96657b2 100644 --- a/constants/exceptions.py +++ b/constants/exceptions.py @@ -68,3 +68,6 @@ class messageTooLongWarnException(Exception): class messageTooLongException(Exception): pass + +class userSilencedException(Exception): + pass diff --git a/events/loginEvent.py b/events/loginEvent.py index 1f1831f..023aa5b 100644 --- a/events/loginEvent.py +++ b/events/loginEvent.py @@ -55,8 +55,11 @@ def handle(tornadoRequest): responseToken = glob.tokens.addToken(userID) responseTokenString = responseToken.token - # Get silence end - userSilenceEnd = max(0, userHelper.getSilenceEnd(userID)-int(time.time())) + # Set silence end UNIX time in token + responseToken.silenceEndTime = userHelper.getSilenceEnd(userID) + + # Get only silence remaining seconds + silenceSeconds = responseToken.getSilenceSecondsLeft() # Get supporter/GMT userRank = userHelper.getRankPrivileges(userID) @@ -80,7 +83,7 @@ def handle(tornadoRequest): responseToken.enqueue(serverPackets.notification("Bancho is in maintenance mode. Only mods/admins have full access to the server.\nType !system maintenance off in chat to turn off maintenance mode.")) # Send all needed login packets - responseToken.enqueue(serverPackets.silenceEndTime(userSilenceEnd)) + responseToken.enqueue(serverPackets.silenceEndTime(silenceSeconds)) responseToken.enqueue(serverPackets.userID(userID)) responseToken.enqueue(serverPackets.protocolVersion()) responseToken.enqueue(serverPackets.userSupporterGMT(userSupporter, userGMT)) @@ -104,6 +107,7 @@ def handle(tornadoRequest): if value.publicRead == True: responseToken.enqueue(serverPackets.channelInfo(key)) + # Send friends list responseToken.enqueue(serverPackets.friendList(userID)) # Send main menu icon and login notification if needed diff --git a/events/sendPrivateMessageEvent.py b/events/sendPrivateMessageEvent.py index fab4471..5914590 100644 --- a/events/sendPrivateMessageEvent.py +++ b/events/sendPrivateMessageEvent.py @@ -27,6 +27,10 @@ def handle(userToken, packetData): # Private message packet packetData = clientPackets.sendPrivateMessage(packetData) + # Make sure the user is not silenced + if userToken.isSilenced() == True: + raise exceptions.userSilencedException + # Check message length if len(packetData["message"]) > 256: if userToken.longMessageWarning == True: @@ -62,6 +66,9 @@ def handle(userToken, packetData): # Console and file output log.pm("{} -> {}: {}".format(username, packetData["to"], packetData["message"])) + except exceptions.userSilencedException: + userToken.enqueue(serverPackets.silenceEndTime(userToken.getSilenceSecondsLeft())) + log.warning("{} tried to send a message during silence".format(username)) except exceptions.tokenNotFoundException: # Token not found, user disconnected log.warning("{} tried to send a message to {}, but their token couldn't be found".format(username, packetData["to"])) diff --git a/events/sendPublicMessageEvent.py b/events/sendPublicMessageEvent.py index f8d1c49..3f98a8a 100644 --- a/events/sendPublicMessageEvent.py +++ b/events/sendPublicMessageEvent.py @@ -28,6 +28,10 @@ def handle(userToken, packetData): # Receivers who = [] + # Make sure the user is not silenced + if userToken.isSilenced() == True: + raise exceptions.userSilencedException + # Check message length if len(packetData["message"]) > 256: if userToken.longMessageWarning == True: @@ -116,6 +120,9 @@ def handle(userToken, packetData): # Discord log discordBotHelper.sendChatlog("**{fro} @ {to}:** {message}".format(fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1])) + except exceptions.userSilencedException: + userToken.enqueue(serverPackets.silenceEndTime(userToken.getSilenceSecondsLeft())) + log.warning("{} tried to send a message during silence".format(username)) except exceptions.channelModeratedException: log.warning("{} tried to send a message to a channel that is in moderated mode ({})".format(username, packetData["to"])) except exceptions.channelUnknownException: diff --git a/objects/osuToken.py b/objects/osuToken.py index 88f86b9..0fd17cb 100644 --- a/objects/osuToken.py +++ b/objects/osuToken.py @@ -32,34 +32,6 @@ class token: latestTillerino -- beatmap ID of latest song from tillerino bot """ - '''token = "" - userID = 0 - username = "" - rank = 0 - actionID = actions.idle - actionText = "" - actionMd5 = "" - actionMods = 0 - gameMode = gameModes.std - - country = 0 - location = [0,0] - - queue = bytes() - joinedChannels = [] - - spectating = 0 - spectators = [] - - pingTime = 0 - loginTime = 0 - - awayMessage = "" - - matchID = -1 - - latestTillerino = 0''' - def __init__(self, __userID, __token = None): """ @@ -93,11 +65,11 @@ class token: self.awayMessage = "" self.matchID = -1 self.tillerino = [0,0,-1.0] # beatmap, mods, acc + self.silenceEndTime = 0 self.queue = bytes() - self.longMessageWarning = False - # Spam protection + self.longMessageWarning = False self.spamRate = 0 self.lastMessagetime = 0 @@ -244,13 +216,14 @@ class token: def silence(self, seconds, reason, author = 999): """ - Silences this user (both db and packet) + Silences this user (db, packet and token) seconds -- silence length in seconds reason -- silence reason author -- userID of who has silenced the target. Optional. Default: 999 (fokabot) """ - # Silence user in db + # Silence in db and token + self.silenceEndTime = int(time.time())+seconds userHelper.silence(self.userID, seconds, reason, author) # Send silence packet to target @@ -277,3 +250,20 @@ class token: # Silence the user if needed if self.spamRate > 10: self.silence(1800, "Spamming (auto spam protection)") + + def isSilenced(self): + """ + Returns True if this user is silenced, otherwise False + + return -- True/False + """ + return self.silenceEndTime-int(time.time()) > 0 + + def getSilenceSecondsLeft(self): + """ + Returns the seconds left for this user's silence + (0 if user is not silenced) + + return -- silence seconds left + """ + return max(0, self.silenceEndTime-int(time.time()))