2016-05-18 17:12:46 +00:00
from constants import exceptions
from constants import clientPackets
from objects import glob
from objects import fokabot
from constants import serverPackets
2016-05-19 20:53:09 +00:00
from helpers import discordBotHelper
2016-06-04 10:44:54 +00:00
from helpers import logHelper as log
2016-06-07 20:46:31 +00:00
from helpers import userHelper
import time
2016-04-19 17:40:59 +00:00
def handle ( userToken , packetData ) :
"""
Event called when someone sends a public message
userToken - - request user token
packetData - - request data bytes
"""
try :
2016-05-19 20:53:09 +00:00
# Get userToken data
2016-04-19 17:40:59 +00:00
userID = userToken . userID
username = userToken . username
userRank = userToken . rank
# Public chat packet
packetData = clientPackets . sendPublicMessage ( packetData )
# Receivers
who = [ ]
2016-06-09 08:43:28 +00:00
# Make sure the user is not silenced
if userToken . isSilenced ( ) == True :
raise exceptions . userSilencedException
2016-06-07 20:46:31 +00:00
# Check message length
if len ( packetData [ " message " ] ) > 256 :
if userToken . longMessageWarning == True :
raise exceptions . messageTooLongException
else :
raise exceptions . messageTooLongWarnException
# Get receivers list
2016-04-19 17:40:59 +00:00
# Check #spectator
if packetData [ " to " ] == " #spectator " :
# Spectator channel
# Send this packet to every spectator and host
if userToken . spectating == 0 :
# We have sent to send a message to our #spectator channel
targetToken = userToken
who = targetToken . spectators [ : ]
# No need to remove us because we are the host so we are not in spectators list
else :
# We have sent a message to someone else's #spectator
targetToken = glob . tokens . getTokenFromUserID ( userToken . spectating )
who = targetToken . spectators [ : ]
# Remove us
if userID in who :
who . remove ( userID )
# Add host
who . append ( targetToken . userID )
elif packetData [ " to " ] == " #multiplayer " :
# Multiplayer Channel
# Get match ID and match object
matchID = userToken . matchID
# Make sure we are in a match
if matchID == - 1 :
return
# Make sure the match exists
if matchID not in glob . matches . matches :
return
# The match exists, get object
match = glob . matches . matches [ matchID ]
# Create targets list
who = [ ]
for i in range ( 0 , 16 ) :
uid = match . slots [ i ] [ " userID " ]
if uid > - 1 and uid != userID :
who . append ( uid )
else :
# Standard channel
# Make sure the channel exists
if packetData [ " to " ] not in glob . channels . channels :
raise exceptions . channelUnknownException
# Make sure the channel is not in moderated mode
if glob . channels . channels [ packetData [ " to " ] ] . moderated == True and userRank < = 2 :
raise exceptions . channelModeratedException
# Make sure we have write permissions
if glob . channels . channels [ packetData [ " to " ] ] . publicWrite == False and userRank < = 2 :
raise exceptions . channelNoPermissionsException
# Send this packet to everyone in that channel except us
who = glob . channels . channels [ packetData [ " to " ] ] . getConnectedUsers ( ) [ : ]
if userID in who :
who . remove ( userID )
2016-06-07 20:46:31 +00:00
# We have receivers
2016-04-19 17:40:59 +00:00
# Send packet to required users
glob . tokens . multipleEnqueue ( serverPackets . sendMessage ( username , packetData [ " to " ] , packetData [ " message " ] ) , who , False )
# Fokabot command check
fokaMessage = fokabot . fokabotResponse ( username , packetData [ " to " ] , packetData [ " message " ] )
if fokaMessage != False :
who . append ( userID )
glob . tokens . multipleEnqueue ( serverPackets . sendMessage ( " FokaBot " , packetData [ " to " ] , fokaMessage ) , who , False )
2016-06-04 10:44:54 +00:00
log . chat ( " FokaBot @ {} : {} " . format ( packetData [ " to " ] , str ( fokaMessage . encode ( " UTF-8 " ) ) ) )
2016-04-19 17:40:59 +00:00
2016-06-07 20:46:31 +00:00
# Spam protection
userToken . spamProtection ( )
2016-06-04 10:44:54 +00:00
# Console and file log
log . chat ( " {fro} @ {to} : {message} " . format ( fro = username , to = packetData [ " to " ] , message = str ( packetData [ " message " ] . encode ( " utf-8 " ) ) ) )
2016-05-19 20:53:09 +00:00
2016-06-04 10:44:54 +00:00
# Discord log
discordBotHelper . sendChatlog ( " ** {fro} @ {to} :** {message} " . format ( fro = username , to = packetData [ " to " ] , message = str ( packetData [ " message " ] . encode ( " utf-8 " ) ) [ 2 : - 1 ] ) )
2016-06-09 08:43:28 +00:00
except exceptions . userSilencedException :
userToken . enqueue ( serverPackets . silenceEndTime ( userToken . getSilenceSecondsLeft ( ) ) )
log . warning ( " {} tried to send a message during silence " . format ( username ) )
2016-04-19 17:40:59 +00:00
except exceptions . channelModeratedException :
2016-06-04 10:44:54 +00:00
log . warning ( " {} tried to send a message to a channel that is in moderated mode ( {} ) " . format ( username , packetData [ " to " ] ) )
2016-04-19 17:40:59 +00:00
except exceptions . channelUnknownException :
2016-06-04 10:44:54 +00:00
log . warning ( " {} tried to send a message to an unknown channel ( {} ) " . format ( username , packetData [ " to " ] ) )
2016-04-19 17:40:59 +00:00
except exceptions . channelNoPermissionsException :
2016-06-04 10:44:54 +00:00
log . warning ( " {} tried to send a message to channel {} , but they have no write permissions " . format ( username , packetData [ " to " ] ) )
2016-06-07 20:46:31 +00:00
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 " )