2016-10-02 20:48:14 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from common.constants import gameModes, actions
|
|
|
|
from common.log import logUtils as log
|
|
|
|
from common.ripple import userUtils
|
2016-12-11 10:07:35 +00:00
|
|
|
from constants import exceptions
|
2016-05-18 17:12:46 +00:00
|
|
|
from constants import serverPackets
|
|
|
|
from events import logoutEvent
|
2016-07-14 10:37:07 +00:00
|
|
|
from helpers import chatHelper as chat
|
2016-10-02 20:48:14 +00:00
|
|
|
from objects import glob
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-09-02 15:45:10 +00:00
|
|
|
class token:
|
2016-10-05 21:28:26 +00:00
|
|
|
def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0, tournament = False):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Create a token object and set userID and token
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param userID: user associated to this token
|
|
|
|
:param token_: if passed, set token to that value
|
|
|
|
if not passed, token will be generated
|
|
|
|
:param ip: client ip. optional.
|
|
|
|
:param irc: if True, set this token as IRC client. Default: False.
|
|
|
|
:param timeOffset: the time offset from UTC for this user. Default: 0.
|
|
|
|
:param tournament: if True, flag this client as a tournement client. Default: True.
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
# Set stuff
|
2016-08-17 14:41:05 +00:00
|
|
|
self.userID = userID
|
2016-10-02 20:48:14 +00:00
|
|
|
self.username = userUtils.getUsername(self.userID)
|
2016-11-17 14:27:27 +00:00
|
|
|
self.safeUsername = userUtils.getSafeUsername(self.userID)
|
2016-10-02 20:48:14 +00:00
|
|
|
self.privileges = userUtils.getPrivileges(self.userID)
|
2017-01-16 18:37:41 +00:00
|
|
|
self.admin = userUtils.isInPrivilegeGroup(self.userID, "developer")\
|
|
|
|
or userUtils.isInPrivilegeGroup(self.userID, "community manager")\
|
|
|
|
or userUtils.isInPrivilegeGroup(self.userID, "chat mod")
|
2016-07-14 10:37:07 +00:00
|
|
|
self.irc = irc
|
2016-11-20 10:31:51 +00:00
|
|
|
self.kicked = False
|
2016-10-02 20:48:14 +00:00
|
|
|
self.restricted = userUtils.isRestricted(self.userID)
|
2016-04-19 17:40:59 +00:00
|
|
|
self.loginTime = int(time.time())
|
|
|
|
self.pingTime = self.loginTime
|
2016-08-01 18:38:26 +00:00
|
|
|
self.timeOffset = timeOffset
|
2017-07-28 20:32:30 +00:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.bufferLock = threading.Lock()
|
2016-10-01 19:19:03 +00:00
|
|
|
self.streams = []
|
2016-10-05 21:28:26 +00:00
|
|
|
self.tournament = tournament
|
2016-12-11 22:12:06 +00:00
|
|
|
self.messagesBuffer = []
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Default variables
|
|
|
|
self.spectators = []
|
2016-10-07 10:42:02 +00:00
|
|
|
|
|
|
|
# TODO: Move those two vars to a class
|
2016-10-04 20:10:07 +00:00
|
|
|
self.spectating = None
|
2016-10-07 10:42:02 +00:00
|
|
|
self.spectatingUserID = 0 # we need this in case we the host gets DCed
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
self.location = [0,0]
|
|
|
|
self.joinedChannels = []
|
2016-06-10 11:15:42 +00:00
|
|
|
self.ip = ip
|
2016-05-19 18:17:20 +00:00
|
|
|
self.country = 0
|
|
|
|
self.location = [0,0]
|
2016-04-19 17:40:59 +00:00
|
|
|
self.awayMessage = ""
|
2016-11-13 11:23:45 +00:00
|
|
|
self.sentAway = []
|
2016-04-19 17:40:59 +00:00
|
|
|
self.matchID = -1
|
2016-05-19 20:10:50 +00:00
|
|
|
self.tillerino = [0,0,-1.0] # beatmap, mods, acc
|
2016-06-09 08:43:28 +00:00
|
|
|
self.silenceEndTime = 0
|
2016-05-19 18:17:20 +00:00
|
|
|
self.queue = bytes()
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-07 20:46:31 +00:00
|
|
|
# Spam protection
|
|
|
|
self.spamRate = 0
|
|
|
|
|
2016-06-16 11:38:17 +00:00
|
|
|
# Stats cache
|
2016-09-02 15:16:22 +00:00
|
|
|
self.actionID = actions.IDLE
|
2016-06-16 11:38:17 +00:00
|
|
|
self.actionText = ""
|
|
|
|
self.actionMd5 = ""
|
|
|
|
self.actionMods = 0
|
2016-10-02 20:48:14 +00:00
|
|
|
self.gameMode = gameModes.STD
|
2016-09-13 09:56:53 +00:00
|
|
|
self.beatmapID = 0
|
2016-06-16 11:38:17 +00:00
|
|
|
self.rankedScore = 0
|
|
|
|
self.accuracy = 0.0
|
|
|
|
self.playcount = 0
|
|
|
|
self.totalScore = 0
|
|
|
|
self.gameRank = 0
|
|
|
|
self.pp = 0
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
# Generate/set token
|
2016-09-02 15:45:10 +00:00
|
|
|
if token_ is not None:
|
|
|
|
self.token = token_
|
2016-04-19 17:40:59 +00:00
|
|
|
else:
|
|
|
|
self.token = str(uuid.uuid4())
|
|
|
|
|
2016-06-16 11:38:17 +00:00
|
|
|
# Set stats
|
|
|
|
self.updateCachedStats()
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
# If we have a valid ip, save bancho session in DB so we can cache LETS logins
|
|
|
|
if ip != "":
|
2016-10-02 20:48:14 +00:00
|
|
|
userUtils.saveBanchoSession(self.userID, self.ip)
|
2016-06-10 11:15:42 +00:00
|
|
|
|
2016-10-01 19:19:03 +00:00
|
|
|
# Join main stream
|
|
|
|
self.joinStream("main")
|
|
|
|
|
2016-09-02 15:45:10 +00:00
|
|
|
def enqueue(self, bytes_):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Add bytes (packets) to queue
|
|
|
|
|
2016-12-26 09:33:05 +00:00
|
|
|
:param bytes_: (packet) bytes to enqueue
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
2017-07-28 20:46:24 +00:00
|
|
|
try:
|
|
|
|
# Acquire the buffer lock
|
|
|
|
self.bufferLock.acquire()
|
|
|
|
|
|
|
|
# Never enqueue for IRC clients or Foka
|
|
|
|
if self.irc or self.userID < 999:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Avoid memory leaks
|
|
|
|
if len(bytes_) < 10 * 10 ** 6:
|
|
|
|
self.queue += bytes_
|
|
|
|
else:
|
|
|
|
log.warning("{}'s packets buffer is above 10M!! Lost some data!".format(self.username))
|
|
|
|
finally:
|
|
|
|
# Release the buffer lock
|
|
|
|
self.bufferLock.release()
|
2017-07-28 20:32:30 +00:00
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
def resetQueue(self):
|
|
|
|
"""Resets the queue. Call when enqueued packets have been sent"""
|
2017-07-28 20:46:24 +00:00
|
|
|
try:
|
|
|
|
self.bufferLock.acquire()
|
|
|
|
self.queue = bytes()
|
|
|
|
finally:
|
|
|
|
self.bufferLock.release()
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
def joinChannel(self, channelObject):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
Join a channel
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
:param channelObject: channel object
|
|
|
|
:raises: exceptions.userAlreadyInChannelException()
|
|
|
|
exceptions.channelNoPermissionsException()
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
if channelObject.name in self.joinedChannels:
|
|
|
|
raise exceptions.userAlreadyInChannelException()
|
|
|
|
if channelObject.publicRead == False and self.admin == False:
|
|
|
|
raise exceptions.channelNoPermissionsException()
|
|
|
|
self.joinedChannels.append(channelObject.name)
|
|
|
|
self.joinStream("chat/{}".format(channelObject.name))
|
|
|
|
self.enqueue(serverPackets.channelJoinSuccess(self.userID, channelObject.clientName))
|
|
|
|
|
|
|
|
def partChannel(self, channelObject):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
|
|
|
Remove channel from joined channels list
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
:param channelObject: channel object
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
self.joinedChannels.remove(channelObject.name)
|
|
|
|
self.leaveStream("chat/{}".format(channelObject.name))
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
def setLocation(self, latitude, longitude):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Set client location
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-12-26 09:33:05 +00:00
|
|
|
:param latitude: latitude
|
|
|
|
:param longitude: longitude
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
self.location = (latitude, longitude)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def getLatitude(self):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
|
|
|
Get latitude
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return: latitude
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
return self.location[0]
|
|
|
|
|
|
|
|
def getLongitude(self):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
|
|
|
Get longitude
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return: longitude
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
return self.location[1]
|
|
|
|
|
2016-10-04 20:10:07 +00:00
|
|
|
def startSpectating(self, host):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Set the spectating user to userID, join spectator stream and chat channel
|
|
|
|
and send required packets to host
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param host: host osuToken object
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-10-04 20:10:07 +00:00
|
|
|
# Stop spectating old client
|
|
|
|
self.stopSpectating()
|
|
|
|
|
|
|
|
# Set new spectator host
|
2016-10-07 10:42:02 +00:00
|
|
|
self.spectating = host.token
|
|
|
|
self.spectatingUserID = host.userID
|
2016-10-04 20:10:07 +00:00
|
|
|
|
|
|
|
# Add us to host's spectator list
|
2016-10-07 10:42:02 +00:00
|
|
|
host.spectators.append(self.token)
|
2016-10-04 20:10:07 +00:00
|
|
|
|
|
|
|
# Create and join spectator stream
|
2016-10-07 10:42:02 +00:00
|
|
|
streamName = "spect/{}".format(host.userID)
|
2016-10-04 20:10:07 +00:00
|
|
|
glob.streams.add(streamName)
|
|
|
|
self.joinStream(streamName)
|
|
|
|
host.joinStream(streamName)
|
|
|
|
|
|
|
|
# Send spectator join packet to host
|
|
|
|
host.enqueue(serverPackets.addSpectator(self.userID))
|
|
|
|
|
|
|
|
# Create and join #spectator (#spect_userid) channel
|
|
|
|
glob.channels.addTempChannel("#spect_{}".format(host.userID))
|
|
|
|
chat.joinChannel(token=self, channel="#spect_{}".format(host.userID))
|
|
|
|
if len(host.spectators) == 1:
|
|
|
|
# First spectator, send #spectator join to host too
|
|
|
|
chat.joinChannel(token=host, channel="#spect_{}".format(host.userID))
|
|
|
|
|
|
|
|
# Send fellow spectator join to all clients
|
|
|
|
glob.streams.broadcast(streamName, serverPackets.fellowSpectatorJoined(self.userID))
|
|
|
|
|
|
|
|
# Get current spectators list
|
|
|
|
for i in host.spectators:
|
2016-10-07 10:42:02 +00:00
|
|
|
if i != self.token and i in glob.tokens.tokens:
|
|
|
|
self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID))
|
2016-10-04 20:10:07 +00:00
|
|
|
|
|
|
|
# Log
|
2016-10-09 17:12:18 +00:00
|
|
|
log.info("{} is spectating {}".format(self.username, host.username))
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def stopSpectating(self):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Stop spectating, leave spectator stream and channel
|
|
|
|
and send required packets to host
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2016-09-02 10:41:19 +00:00
|
|
|
# Remove our userID from host's spectators
|
2016-10-07 10:42:02 +00:00
|
|
|
if self.spectating is None:
|
2016-09-04 10:42:14 +00:00
|
|
|
return
|
2016-10-07 10:42:02 +00:00
|
|
|
if self.spectating in glob.tokens.tokens:
|
|
|
|
hostToken = glob.tokens.tokens[self.spectating]
|
|
|
|
else:
|
|
|
|
hostToken = None
|
|
|
|
streamName = "spect/{}".format(self.spectatingUserID)
|
2016-09-02 10:41:19 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
# Remove us from host's spectators list,
|
|
|
|
# leave spectator stream
|
|
|
|
# and end the spectator left packet to host
|
2016-10-04 20:10:07 +00:00
|
|
|
self.leaveStream(streamName)
|
2016-10-07 10:42:02 +00:00
|
|
|
if hostToken is not None:
|
|
|
|
hostToken.spectators.remove(self.token)
|
|
|
|
hostToken.enqueue(serverPackets.removeSpectator(self.userID))
|
2016-09-02 10:41:19 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
# and to all other spectators
|
|
|
|
for i in hostToken.spectators:
|
|
|
|
if i in glob.tokens.tokens:
|
|
|
|
glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID))
|
2016-09-02 10:41:19 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
# If nobody is spectating the host anymore, close #spectator channel
|
|
|
|
# and remove host from spect stream too
|
|
|
|
if len(hostToken.spectators) == 0:
|
|
|
|
chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True)
|
|
|
|
hostToken.leaveStream(streamName)
|
2016-10-04 20:10:07 +00:00
|
|
|
|
2016-12-21 22:26:47 +00:00
|
|
|
# Console output
|
|
|
|
log.info("{} is no longer spectating {}. Current spectators: {}".format(self.username, self.spectatingUserID, hostToken.spectators))
|
|
|
|
|
2016-10-04 20:10:07 +00:00
|
|
|
# Part #spectator channel
|
2016-10-07 10:42:02 +00:00
|
|
|
chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-10-04 20:10:07 +00:00
|
|
|
# Set our spectating user to 0
|
|
|
|
self.spectating = None
|
2016-10-07 10:42:02 +00:00
|
|
|
self.spectatingUserID = 0
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
def updatePingTime(self):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Update latest ping time to current time
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return:
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
self.pingTime = int(time.time())
|
|
|
|
|
2016-09-02 10:41:19 +00:00
|
|
|
def joinMatch(self, matchID):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
2016-10-04 21:43:02 +00:00
|
|
|
Set match to matchID, join match stream and channel
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param matchID: new match ID
|
|
|
|
:return:
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
2016-10-04 21:43:02 +00:00
|
|
|
# Make sure the match exists
|
|
|
|
if matchID not in glob.matches.matches:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Match exists, get object
|
|
|
|
match = glob.matches.matches[matchID]
|
|
|
|
|
|
|
|
# Stop spectating
|
|
|
|
self.stopSpectating()
|
|
|
|
|
|
|
|
# Leave other matches
|
|
|
|
if self.matchID > -1 and self.matchID != matchID:
|
|
|
|
self.leaveMatch()
|
|
|
|
|
|
|
|
# Try to join match
|
|
|
|
joined = match.userJoin(self)
|
|
|
|
if not joined:
|
|
|
|
self.enqueue(serverPackets.matchJoinFail())
|
|
|
|
return
|
|
|
|
|
|
|
|
# Set matchID, join stream, channel and send packet
|
2016-09-02 10:41:19 +00:00
|
|
|
self.matchID = matchID
|
2016-10-04 21:43:02 +00:00
|
|
|
self.joinStream(match.streamName)
|
|
|
|
chat.joinChannel(token=self, channel="#multi_{}".format(self.matchID))
|
|
|
|
self.enqueue(serverPackets.matchJoinSuccess(matchID))
|
|
|
|
|
|
|
|
def leaveMatch(self):
|
|
|
|
"""
|
|
|
|
Leave joined match, match stream and match channel
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
# Make sure we are in a match
|
|
|
|
if self.matchID == -1:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Part #multiplayer channel and streams (/ and /playing)
|
|
|
|
chat.partChannel(token=self, channel="#multi_{}".format(self.matchID), kick=True)
|
|
|
|
self.leaveStream("multi/{}".format(self.matchID))
|
|
|
|
self.leaveStream("multi/{}/playing".format(self.matchID)) # optional
|
|
|
|
|
|
|
|
# Make sure the match exists
|
|
|
|
if self.matchID not in glob.matches.matches:
|
|
|
|
return
|
|
|
|
|
|
|
|
# The match exists, get object
|
|
|
|
match = glob.matches.matches[self.matchID]
|
|
|
|
|
|
|
|
# Set slot to free
|
|
|
|
match.userLeft(self)
|
|
|
|
|
|
|
|
# Set usertoken match to -1
|
|
|
|
self.matchID = -1
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-10-09 17:12:18 +00:00
|
|
|
def kick(self, message="You have been kicked from the server. Please login again.", reason="kick"):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
|
|
|
Kick this user from the server
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param message: Notification message to send to this user.
|
|
|
|
Default: "You have been kicked from the server. Please login again."
|
|
|
|
:param reason: Kick reason, used in logs. Default: "kick"
|
|
|
|
:return:
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
# Send packet to target
|
2016-10-09 17:12:18 +00:00
|
|
|
log.info("{} has been disconnected. ({})".format(self.username, reason))
|
2016-09-02 10:41:19 +00:00
|
|
|
if message != "":
|
|
|
|
self.enqueue(serverPackets.notification(message))
|
2016-04-19 17:40:59 +00:00
|
|
|
self.enqueue(serverPackets.loginFailed())
|
|
|
|
|
|
|
|
# Logout event
|
2016-12-10 18:10:00 +00:00
|
|
|
logoutEvent.handle(self, deleteToken=self.irc)
|
2016-06-07 20:46:31 +00:00
|
|
|
|
2016-11-20 10:31:51 +00:00
|
|
|
def silence(self, seconds = None, reason = "", author = 999):
|
2016-06-07 20:46:31 +00:00
|
|
|
"""
|
2016-06-09 08:43:28 +00:00
|
|
|
Silences this user (db, packet and token)
|
2016-06-07 20:46:31 +00:00
|
|
|
|
2016-11-20 10:31:51 +00:00
|
|
|
:param seconds: silence length in seconds. If None, get it from db. Default: None
|
|
|
|
:param reason: silence reason. Default: empty string
|
2016-11-17 18:13:06 +00:00
|
|
|
:param author: userID of who has silenced the user. Default: 999 (FokaBot)
|
|
|
|
:return:
|
2016-06-07 20:46:31 +00:00
|
|
|
"""
|
2016-11-20 10:31:51 +00:00
|
|
|
if seconds is None:
|
|
|
|
# Get silence expire from db if needed
|
|
|
|
seconds = max(0, userUtils.getSilenceEnd(self.userID) - int(time.time()))
|
|
|
|
else:
|
|
|
|
# Silence in db and token
|
|
|
|
userUtils.silence(self.userID, seconds, reason, author)
|
|
|
|
|
|
|
|
# Silence token
|
|
|
|
self.silenceEndTime = int(time.time()) + seconds
|
2016-06-07 20:46:31 +00:00
|
|
|
|
2016-11-20 10:31:51 +00:00
|
|
|
# Send silence packet to user
|
2016-06-07 20:46:31 +00:00
|
|
|
self.enqueue(serverPackets.silenceEndTime(seconds))
|
|
|
|
|
|
|
|
# Send silenced packet to everyone else
|
2016-10-01 19:19:03 +00:00
|
|
|
glob.streams.broadcast("main", serverPackets.userSilenced(self.userID))
|
2016-06-07 20:46:31 +00:00
|
|
|
|
|
|
|
def spamProtection(self, increaseSpamRate = True):
|
|
|
|
"""
|
|
|
|
Silences the user if is spamming.
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param increaseSpamRate: set to True if the user has sent a new message. Default: True
|
|
|
|
:return:
|
2016-06-07 20:46:31 +00:00
|
|
|
"""
|
2016-06-10 14:22:14 +00:00
|
|
|
# Increase the spam rate if needed
|
2016-09-02 15:45:10 +00:00
|
|
|
if increaseSpamRate:
|
2016-06-10 14:22:14 +00:00
|
|
|
self.spamRate += 1
|
2016-06-07 20:46:31 +00:00
|
|
|
|
|
|
|
# Silence the user if needed
|
|
|
|
if self.spamRate > 10:
|
|
|
|
self.silence(1800, "Spamming (auto spam protection)")
|
2016-06-09 08:43:28 +00:00
|
|
|
|
|
|
|
def isSilenced(self):
|
|
|
|
"""
|
|
|
|
Returns True if this user is silenced, otherwise False
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return: True if this user is silenced, otherwise False
|
2016-06-09 08:43:28 +00:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return: silence seconds left (or 0)
|
2016-06-09 08:43:28 +00:00
|
|
|
"""
|
|
|
|
return max(0, self.silenceEndTime-int(time.time()))
|
2016-06-16 11:38:17 +00:00
|
|
|
|
|
|
|
def updateCachedStats(self):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Update all cached stats for this token
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2016-10-02 20:48:14 +00:00
|
|
|
stats = userUtils.getUserStats(self.userID, self.gameMode)
|
2016-06-16 11:38:17 +00:00
|
|
|
log.debug(str(stats))
|
2016-09-02 15:45:10 +00:00
|
|
|
if stats is None:
|
2016-06-16 11:38:17 +00:00
|
|
|
log.warning("Stats query returned None")
|
|
|
|
return
|
|
|
|
self.rankedScore = stats["rankedScore"]
|
|
|
|
self.accuracy = stats["accuracy"]/100
|
|
|
|
self.playcount = stats["playcount"]
|
|
|
|
self.totalScore = stats["totalScore"]
|
|
|
|
self.gameRank = stats["gameRank"]
|
|
|
|
self.pp = stats["pp"]
|
2016-07-03 18:51:19 +00:00
|
|
|
|
2016-11-20 10:31:51 +00:00
|
|
|
def checkRestricted(self):
|
2016-07-14 10:37:07 +00:00
|
|
|
"""
|
|
|
|
Check if this token is restricted. If so, send fokabot message
|
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return:
|
2016-07-14 10:37:07 +00:00
|
|
|
"""
|
2016-11-20 10:31:51 +00:00
|
|
|
oldRestricted = self.restricted
|
|
|
|
self.restricted = userUtils.isRestricted(self.userID)
|
2016-09-02 15:45:10 +00:00
|
|
|
if self.restricted:
|
2016-07-14 10:37:07 +00:00
|
|
|
self.setRestricted()
|
2016-11-20 10:31:51 +00:00
|
|
|
elif not self.restricted and oldRestricted != self.restricted:
|
|
|
|
self.resetRestricted()
|
|
|
|
|
|
|
|
def checkBanned(self):
|
|
|
|
"""
|
|
|
|
Check if this user is banned. If so, disconnect it.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if userUtils.isBanned(self.userID):
|
|
|
|
self.enqueue(serverPackets.loginBanned())
|
|
|
|
logoutEvent.handle(self, deleteToken=False)
|
|
|
|
|
2016-07-14 10:37:07 +00:00
|
|
|
|
2016-07-03 18:51:19 +00:00
|
|
|
def setRestricted(self):
|
|
|
|
"""
|
|
|
|
Set this token as restricted, send FokaBot message to user
|
|
|
|
and send offline packet to everyone
|
2016-11-17 18:13:06 +00:00
|
|
|
|
|
|
|
:return:
|
2016-07-03 18:51:19 +00:00
|
|
|
"""
|
|
|
|
self.restricted = True
|
2016-11-20 10:31:51 +00:00
|
|
|
chat.sendMessage("FokaBot", self.username, "Your account is currently in restricted mode. Please visit ripple's website for more information.")
|
|
|
|
|
|
|
|
def resetRestricted(self):
|
|
|
|
"""
|
|
|
|
Send FokaBot message to alert the user that he has been unrestricted
|
|
|
|
and he has to log in again.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
chat.sendMessage("FokaBot", self.username, "Your account has been unrestricted! Please log in again.")
|
2016-10-01 19:19:03 +00:00
|
|
|
|
|
|
|
def joinStream(self, name):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Join a packet stream, or create it if the stream doesn't exist.
|
|
|
|
|
|
|
|
:param name: stream name
|
|
|
|
:return:
|
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
glob.streams.join(name, token=self.token)
|
2016-10-01 19:19:03 +00:00
|
|
|
if name not in self.streams:
|
|
|
|
self.streams.append(name)
|
|
|
|
|
|
|
|
def leaveStream(self, name):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Leave a packets stream
|
|
|
|
|
|
|
|
:param name: stream name
|
|
|
|
:return:
|
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
glob.streams.leave(name, token=self.token)
|
2016-10-01 19:19:03 +00:00
|
|
|
if name in self.streams:
|
|
|
|
self.streams.remove(name)
|
|
|
|
|
|
|
|
def leaveAllStreams(self):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Leave all joined packet streams
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2016-10-01 19:19:03 +00:00
|
|
|
for i in self.streams:
|
2016-11-13 11:23:45 +00:00
|
|
|
self.leaveStream(i)
|
|
|
|
|
|
|
|
def awayCheck(self, userID):
|
|
|
|
"""
|
|
|
|
Returns True if userID doesn't know that we are away
|
|
|
|
Returns False if we are not away or if userID already knows we are away
|
|
|
|
|
|
|
|
:param userID: original sender userID
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if self.awayMessage == "" or userID in self.sentAway:
|
|
|
|
return False
|
|
|
|
self.sentAway.append(userID)
|
2016-12-11 22:12:06 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def addMessageInBuffer(self, chan, message):
|
|
|
|
"""
|
|
|
|
Add a message in messages buffer (10 messages, truncated at 50 chars).
|
|
|
|
Used as proof when the user gets reported.
|
|
|
|
|
|
|
|
:param chan: channel
|
|
|
|
:param message: message content
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if len(self.messagesBuffer) > 9:
|
|
|
|
self.messagesBuffer = self.messagesBuffer[1:]
|
2016-12-12 21:45:31 +00:00
|
|
|
self.messagesBuffer.append("{time} - {user}@{channel}: {message}".format(time=time.strftime("%H:%M", time.localtime()), user=self.username, channel=chan, message=message[:50]))
|
2016-12-11 22:12:06 +00:00
|
|
|
|
|
|
|
def getMessagesBufferString(self):
|
|
|
|
"""
|
|
|
|
Get the content of the messages buffer as a string
|
|
|
|
|
|
|
|
:return: messages buffer content as a string
|
|
|
|
"""
|
|
|
|
return "\n".join(x for x in self.messagesBuffer)
|