.BANCHO. Add spam protection, better silence handling
This commit is contained in:
@@ -7,7 +7,9 @@ from objects import fokabot
|
||||
from constants import exceptions
|
||||
from constants import messageTemplates
|
||||
from helpers import generalFunctions
|
||||
from helpers import userHelper
|
||||
from helpers import logHelper as log
|
||||
import time
|
||||
|
||||
def handle(userToken, packetData):
|
||||
"""
|
||||
@@ -20,10 +22,18 @@ def handle(userToken, packetData):
|
||||
try:
|
||||
# Get usertoken username
|
||||
username = userToken.username
|
||||
userID = userToken.userID
|
||||
|
||||
# Private message packet
|
||||
packetData = clientPackets.sendPrivateMessage(packetData)
|
||||
|
||||
# Check message length
|
||||
if len(packetData["message"]) > 256:
|
||||
if userToken.longMessageWarning == True:
|
||||
raise exceptions.messageTooLongException
|
||||
else:
|
||||
raise exceptions.messageTooLongWarnException
|
||||
|
||||
if packetData["to"] == "FokaBot":
|
||||
# FokaBot command check
|
||||
fokaMessage = fokabot.fokabotResponse(username, packetData["to"], packetData["message"])
|
||||
@@ -47,12 +57,18 @@ def handle(userToken, packetData):
|
||||
if token.awayMessage != "":
|
||||
userToken.enqueue(serverPackets.sendMessage(packetData["to"], username, "This user is away: {}".format(token.awayMessage)))
|
||||
|
||||
# Spam protection
|
||||
userToken.spamProtection()
|
||||
|
||||
# Console and file output
|
||||
log.pm("{} -> {}: {}".format(username, packetData["to"], packetData["message"]))
|
||||
|
||||
# Log to chatlog_private
|
||||
#with open(".data/chatlog_private.txt", "a") as f:
|
||||
# f.write("[{date}] {fro} -> {to}: {message}\n".format(date=generalFunctions.getTimestamp(), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1]))
|
||||
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"]))
|
||||
except exceptions.messageTooLongWarnException:
|
||||
# Message > 256 warn
|
||||
userToken.longMessageWarning = True
|
||||
userToken.enqueue(serverPackets.sendMessage("FokaBot", username, "Your message was too long and has not been sent. Please keep your messages under 256 characters. This is your last warning."))
|
||||
except exceptions.messageTooLongException:
|
||||
# Message > 256 silence
|
||||
userToken.silence(2*3600, "Sending messages longer than 256 characters")
|
||||
|
@@ -5,6 +5,8 @@ from objects import fokabot
|
||||
from constants import serverPackets
|
||||
from helpers import discordBotHelper
|
||||
from helpers import logHelper as log
|
||||
from helpers import userHelper
|
||||
import time
|
||||
|
||||
def handle(userToken, packetData):
|
||||
"""
|
||||
@@ -26,6 +28,14 @@ def handle(userToken, packetData):
|
||||
# Receivers
|
||||
who = []
|
||||
|
||||
# Check message length
|
||||
if len(packetData["message"]) > 256:
|
||||
if userToken.longMessageWarning == True:
|
||||
raise exceptions.messageTooLongException
|
||||
else:
|
||||
raise exceptions.messageTooLongWarnException
|
||||
|
||||
# Get receivers list
|
||||
# Check #spectator
|
||||
if packetData["to"] == "#spectator":
|
||||
# Spectator channel
|
||||
@@ -87,7 +97,7 @@ def handle(userToken, packetData):
|
||||
if userID in who:
|
||||
who.remove(userID)
|
||||
|
||||
|
||||
# We have receivers
|
||||
# Send packet to required users
|
||||
glob.tokens.multipleEnqueue(serverPackets.sendMessage(username, packetData["to"], packetData["message"]), who, False)
|
||||
|
||||
@@ -98,6 +108,9 @@ def handle(userToken, packetData):
|
||||
glob.tokens.multipleEnqueue(serverPackets.sendMessage("FokaBot", packetData["to"], fokaMessage), who, False)
|
||||
log.chat("FokaBot @ {}: {}".format(packetData["to"], str(fokaMessage.encode("UTF-8"))))
|
||||
|
||||
# Spam protection
|
||||
userToken.spamProtection()
|
||||
|
||||
# Console and file log
|
||||
log.chat("{fro} @ {to}: {message}".format(fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))))
|
||||
|
||||
@@ -109,3 +122,10 @@ def handle(userToken, packetData):
|
||||
log.warning("{} tried to send a message to an unknown channel ({})".format(username, packetData["to"]))
|
||||
except exceptions.channelNoPermissionsException:
|
||||
log.warning("{} tried to send a message to channel {}, but they have no write permissions".format(username, packetData["to"]))
|
||||
except exceptions.messageTooLongWarnException:
|
||||
# Message > 256 warn
|
||||
userToken.longMessageWarning = True
|
||||
userToken.enqueue(serverPackets.sendMessage("FokaBot", username, "Your message was too long and has not been sent. Please keep your messages under 256 characters. This is your last warning."))
|
||||
except exceptions.messageTooLongException:
|
||||
# Message > 256 silence
|
||||
userToken.silence(2*3600, "Sending messages longer than 256 characters")
|
||||
|
Reference in New Issue
Block a user