2016-05-17 21:40:34 +00:00
from helpers import consoleHelper
from constants import bcolors
2016-05-18 17:12:46 +00:00
from constants import clientPackets
from constants import serverPackets
from objects import glob
from objects import fokabot
from constants import exceptions
from constants import messageTemplates
2016-06-02 19:50:10 +00:00
from helpers import generalFunctions
2016-06-07 20:46:31 +00:00
from helpers import userHelper
2016-06-04 10:44:54 +00:00
from helpers import logHelper as log
2016-06-07 20:46:31 +00:00
import time
2016-04-19 17:40:59 +00:00
def handle ( userToken , packetData ) :
"""
Event called when someone sends a private message
userToken - - request user token
packetData - - request data bytes
"""
try :
# Get usertoken username
username = userToken . username
2016-06-07 20:46:31 +00:00
userID = userToken . userID
2016-04-19 17:40:59 +00:00
# Private message packet
packetData = clientPackets . sendPrivateMessage ( packetData )
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
2016-04-19 17:40:59 +00:00
if packetData [ " to " ] == " FokaBot " :
# FokaBot command check
fokaMessage = fokabot . fokabotResponse ( username , packetData [ " to " ] , packetData [ " message " ] )
if fokaMessage != False :
userToken . enqueue ( serverPackets . sendMessage ( " FokaBot " , username , fokaMessage ) )
2016-06-04 10:44:54 +00:00
log . pm ( " FokaBot -> {} : {} " . format ( packetData [ " to " ] , str ( fokaMessage . encode ( " UTF-8 " ) ) ) )
2016-04-19 17:40:59 +00:00
else :
# Send packet message to target if it exists
token = glob . tokens . getTokenFromUsername ( packetData [ " to " ] )
if token == None :
raise exceptions . tokenNotFoundException ( )
2016-05-11 17:41:16 +00:00
# Check message templates (mods/admins only)
if packetData [ " message " ] in messageTemplates . templates and userToken . rank > = 3 :
packetData [ " message " ] = messageTemplates . templates [ packetData [ " message " ] ]
2016-04-19 17:40:59 +00:00
# Send message to target
token . enqueue ( serverPackets . sendMessage ( username , packetData [ " to " ] , packetData [ " message " ] ) )
# Send away message to sender if needed
if token . awayMessage != " " :
userToken . enqueue ( serverPackets . sendMessage ( packetData [ " to " ] , username , " This user is away: {} " . format ( token . awayMessage ) ) )
2016-06-07 20:46:31 +00:00
# Spam protection
userToken . spamProtection ( )
2016-06-04 10:44:54 +00:00
# Console and file output
log . pm ( " {} -> {} : {} " . format ( username , packetData [ " to " ] , packetData [ " message " ] ) )
2016-04-19 17:40:59 +00:00
except exceptions . tokenNotFoundException :
# Token not found, user disconnected
2016-06-04 10:44:54 +00:00
log . warning ( " {} tried to send a message to {} , but their token couldn ' t be found " . 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 " )