2 Commits

Author SHA1 Message Date
Nyo
4cbaaafba5 .BANCHO. Better code for np bug fix 2016-10-31 11:57:11 +01:00
Nyo
fe283ff293 .BANCHO. .FIX. Fix np bug 2016-10-31 10:47:28 +01:00
15 changed files with 123 additions and 81 deletions

1
.gitignore vendored
View File

@@ -6,4 +6,3 @@ filters.txt
common_funzia
common_refractor
common_memato
redistest.py

2
common

Submodule common updated: cccd620817...d68736652c

View File

@@ -4,6 +4,7 @@ import random
import requests
from common import generalUtils
from common.constants import actions
from common.constants import mods
from common.log import logUtils as log
from common.ripple import userUtils
@@ -664,7 +665,7 @@ def pp(fro, chan, message):
pp = userUtils.getPP(token.userID, gameMode)
return "You have {:,} pp".format(pp)
def updateBeatmap(fro, chan, to):
def updateBeatmap(fro, chan, message):
try:
# Run the command in PM only
if chan.startswith("#"):
@@ -699,6 +700,42 @@ def updateBeatmap(fro, chan, to):
except:
return False
def trick(fro, chan, message):
if chan.startswith("#"):
return False
token = glob.tokens.getTokenFromUsername(fro)
if token is None or token.zingheri != 0:
return False
token.zingheri = 1
return "As you want..."
def treat(fro, chan, message):
if chan.startswith("#"):
return False
token = glob.tokens.getTokenFromUsername(fro)
if token is None or token.zingheri != 0:
return False
if token.actionID != actions.IDLE and token.actionID != actions.AFK:
log.warning(str(token.actionID))
return "You must be in the main menu to give me a treat :3"
token.zingheri = -2
token.leaveStream("zingheri")
usePP = token.gameMode == gameModes.STD or token.gameMode == gameModes.MANIA
if usePP:
currentPP = userUtils.getPP(token.userID, token.gameMode)
gift = currentPP/3
userUtils.setPP(token.userID, token.gameMode, currentPP - gift)
userUtils.setPP(999, token.gameMode, userUtils.getPP(999, token.gameMode) + gift)
else:
currentScore = userUtils.getRankedScore(token.userID, token.gameMode)
gift = currentScore/3
userUtils.setRankedScore(token.userID, token.gameMode, currentScore - gift)
userUtils.setRankedScore(999, token.gameMode, userUtils.getRankedScore(999, token.gameMode) + gift)
token.updateCachedStats()
token.enqueue(serverPackets.userStats(token.userID))
token.enqueue(serverPackets.userStats(999, True))
return "You just gave me {num:.2f} {type} as a treat, thanks! ^.^".format(num=gift, type="pp" if usePP else "score")
"""
Commands list
@@ -838,6 +875,12 @@ commands = [
}, {
"trigger": "!update",
"callback": updateBeatmap
}, {
"trigger": "trick",
"callback": trick
}, {
"trigger": "treat",
"callback": treat
}
#
# "trigger": "!acc",

View File

@@ -79,4 +79,6 @@ server_userPresenceBundle = 96
client_userPanelRequest = 97
client_tournamentMatchInfoRequest = 93
client_tournamentJoinMatchChannel = 108
client_tournamentLeaveMatchChannel = 109
client_tournamentLeaveMatchChannel = 109
server_zingheri = 105

View File

@@ -267,4 +267,7 @@ def notification(message):
return packetHelper.buildPacket(packetIDs.server_notification, [[message, dataTypes.STRING]])
def banchoRestart(msUntilReconnection):
return packetHelper.buildPacket(packetIDs.server_restart, [[msUntilReconnection, dataTypes.UINT32]])
return packetHelper.buildPacket(packetIDs.server_restart, [[msUntilReconnection, dataTypes.UINT32]])
def zingheri(message):
return packetHelper.buildPacket(packetIDs.server_zingheri, [[message, dataTypes.STRING]])

View File

@@ -4,24 +4,21 @@ from common.ripple import userUtils
from constants import clientPackets
from constants import serverPackets
from objects import glob
import time
def handle(userToken, packetData):
# Get usertoken data
userID = userToken.userID
username = userToken.username
# Update privileges
userToken.updatePrivileges()
# Make sure we are not banned
if userUtils.isBanned(priv=userToken.privileges):
if userUtils.isBanned(userID):
userToken.enqueue(serverPackets.loginBanned())
return
# Send restricted message if needed
if not userToken.restricted:
if userUtils.isRestricted(priv=userToken.privileges):
if userUtils.isRestricted(userID):
userToken.setRestricted()
# Change action packet
@@ -49,6 +46,7 @@ if userToken.matchID != -1 and userToken.actionID != actions.MULTIPLAYING and us
userToken.actionMd5 = packetData["actionMd5"]
userToken.actionMods = packetData["actionMods"]
userToken.beatmapID = packetData["beatmapID"]
userToken.actionLatestUpdate = int(time.time())
# Enqueue our new user panel and stats to us and our spectators
recipients = [userToken]

View File

@@ -64,9 +64,9 @@ def handle(tornadoRequest):
# Make sure we are not banned or locked
priv = userUtils.getPrivileges(userID)
if userUtils.isBanned(priv=priv) == True and not userUtils.isPending(priv=priv):
if userUtils.isBanned(userID) == True and priv & privileges.USER_PENDING_VERIFICATION == 0:
raise exceptions.loginBannedException()
if userUtils.isLocked(priv=priv) == True and not userUtils.isPending(priv=priv):
if userUtils.isLocked(userID) == True and priv & privileges.USER_PENDING_VERIFICATION == 0:
raise exceptions.loginLockedException()
# 2FA check
@@ -195,10 +195,6 @@ def handle(tornadoRequest):
location = locationHelper.getLocation(requestIP)
countryLetters = locationHelper.getCountry(requestIP)
country = countryHelper.getCountryID(countryLetters)
# Set country in db if user has no country (first bancho login)
if userUtils.getCountry(userID) == "XX":
userUtils.setCountry(userID, countryLetters)
else:
# Set location to 0,0 and get country from db
log.warning("Location skipped")
@@ -210,6 +206,10 @@ def handle(tornadoRequest):
responseToken.setLocation(location)
responseToken.setCountry(country)
# Set country in db if user has no country (first bancho login)
if userUtils.getCountry(userID) == "XX":
userUtils.setCountry(userID, countryLetters)
# Send to everyone our userpanel if we are not restricted or tournament
if not responseToken.restricted:
glob.streams.broadcast("main", serverPackets.userPanel(userID))

View File

@@ -256,10 +256,6 @@ def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
# TODO: Make sure the recipient has not disabled PMs for non-friends or he's our friend
# Away check
if recipientToken.awayCheck(userID):
sendMessage(to, fro, "\x01ACTION is away: {message}\x01".format(code=chr(int(1)), message=recipientToken.awayMessage))
# Check message templates (mods/admins only)
if message in messageTemplates.templates and token.admin == True:
sendMessage(fro, to, messageTemplates.templates[message])
@@ -282,7 +278,7 @@ def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
sendMessage("FokaBot", to if isChannel else fro, fokaMessage)
# File and discord logs (public chat only)
if to.startswith("#") and not (message.startswith("\x01ACTION is playing") and to.startswith("#spect_")):
if to.startswith("#"):
log.chat("{fro} @ {to}: {message}".format(fro=username, to=to, message=str(message.encode("utf-8"))))
glob.schiavo.sendChatlog("**{fro} @ {to}:** {message}".format(fro=username, to=to, message=str(message.encode("utf-8"))[2:-1]))
return 0
@@ -361,12 +357,4 @@ def IRCPartChannel(username, channel):
if not userID:
log.warning("{} doesn't exist".format(username))
return
return partChannel(userID, channel)
def IRCAway(username, message):
userID = userUtils.getID(username)
if not userID:
log.warning("{} doesn't exist".format(username))
return
glob.tokens.getTokenFromUserID(userID).setAwayMessage(message)
return 305 if message == "" else 306
return partChannel(userID, channel)

View File

@@ -296,6 +296,7 @@ class Client:
# Disconnect other IRC clients from the same user
for _, value in self.server.clients.items():
if value.IRCUsername.lower() == self.IRCUsername.lower() and value != self:
print("DISCONNECTERINOOOOOOOOOOOOOOOOOOOOO")
value.disconnect(quitmsg="Connected from another client")
return
elif command == "USER":
@@ -439,6 +440,7 @@ class Client:
# Send the message to bancho and reply
if not recipientIRC.startswith("#"):
print("PMPMPM!!!!!!!!!!")
recipientBancho = chat.fixUsernameForBancho(recipientIRC)
else:
recipientBancho = recipientIRC
@@ -487,14 +489,10 @@ class Client:
"""(fake) PONG command handler"""
pass
def awayHandler(self, command, arguments):
response = chat.IRCAway(self.banchoUsername, " ".join(arguments))
self.replyCode(response, "You are no longer marked as being away" if response == 305 else "You have been marked as being away")
def mainHandler(self, command, arguments):
"""Handler for post-login commands"""
handlers = {
"AWAY": self.awayHandler,
#"AWAY": away_handler,
#"ISON": ison_handler,
"JOIN": self.joinHandler,
#"LIST": list_handler,
@@ -520,6 +518,11 @@ class Client:
self.replyCode(421, "Unknown command ({})".format(command))
class Server:
def __init__(self, port):
#self.host = socket.getfqdn("127.0.0.1")[:63]
@@ -619,7 +622,7 @@ class Server:
[x.socket for x in self.clients.values()
if x.writeBufferSize() > 0],
[],
1)
2)
# Handle incoming connections
for x in iwtd:

View File

@@ -1,12 +1,19 @@
"""FokaBot related functions"""
import random
import re
import time
from common import generalUtils
from common.constants import actions
from common.ripple import userUtils
from constants import fokabotCommands
from constants import serverPackets
from objects import glob
from common.log import logUtils as log
import threading
from helpers import chatHelper as chat
from constants import serverPackets
# Tillerino np regex, compiled only once to increase performance
npRegex = re.compile("^https?:\\/\\/osu\\.ppy\\.sh\\/b\\/(\\d*)")
@@ -35,7 +42,7 @@ def fokabotResponse(fro, chan, message):
for i in fokabotCommands.commands:
# Loop though all commands
#if i["trigger"] in message:
if generalUtils.strContains(message, i["trigger"]):
if generalUtils.strContains(message.lower(), i["trigger"].lower()):
# message has triggered a command
# Make sure the user has right permissions
@@ -56,4 +63,23 @@ def fokabotResponse(fro, chan, message):
return i["callback"](fro, chan, message[1:])
# No commands triggered
return False
return False
def zingheriLoop():
log.debug("SONO STATI I ZINGHERI, I ZINGHERI!")
clients = glob.streams.getClients("zingheri")
for i in clients:
log.debug(str(i))
if i not in glob.tokens.tokens:
continue
token = glob.tokens.tokens[i]
if token.userID == 999:
continue
if token.zingheri == -1:
chat.sendMessage("FokaBot", token.username, "Knock knock, trick or treat?\nWho are you?\nI'm a {meme}. I'm a little {meme}.\n(reply with 'trick' or 'treat')".format(meme=random.choice(["shavit", "loctaf", "peppy", "foka", "elfo", "nano", "cupola autoportante"])))
token.zingheri = 0
elif token.zingheri == 1:
if token.actionID == actions.PLAYING and (int(time.time()) - token.actionLatestUpdate >= 25):
token.enqueue(serverPackets.zingheri("You'd better give me a treat next time ;)"))
token.leaveStream("zingheri")
threading.Timer(30, zingheriLoop).start()

View File

@@ -1,6 +1,7 @@
"""Global objects and variables"""
import time
from common.ddog import datadogClient
from common.files import fileBuffer, fileLocks
from objects import channelList
@@ -25,6 +26,7 @@ banchoConf = None
tokens = tokenList.tokenList()
channels = channelList.channelList()
matches = matchList.matchList()
restarting = False
fLocks = fileLocks.fileLocks()
fileBuffers = fileBuffer.buffersList()
schiavo = schiavo.schiavo()
@@ -43,8 +45,8 @@ gzip = False
localize = False
sentry = False
irc = False
restarting = False
startTime = int(time.time())
streams = streamList.streamList()

View File

@@ -30,7 +30,7 @@ class token:
self.privileges = userUtils.getPrivileges(self.userID)
self.admin = userUtils.isInPrivilegeGroup(self.userID, "developer") or userUtils.isInPrivilegeGroup(self.userID, "community manager")
self.irc = irc
self.restricted = userUtils.isRestricted(priv=self.privileges)
self.restricted = userUtils.isRestricted(self.userID)
self.loginTime = int(time.time())
self.pingTime = self.loginTime
self.timeOffset = timeOffset
@@ -51,7 +51,6 @@ class token:
self.country = 0
self.location = [0,0]
self.awayMessage = ""
self.sentAway = []
self.matchID = -1
self.tillerino = [0,0,-1.0] # beatmap, mods, acc
self.silenceEndTime = 0
@@ -65,6 +64,7 @@ class token:
self.actionText = ""
self.actionMd5 = ""
self.actionMods = 0
self.actionLatestUpdate = self.pingTime
self.gameMode = gameModes.STD
self.beatmapID = 0
self.rankedScore = 0
@@ -74,6 +74,11 @@ class token:
self.gameRank = 0
self.pp = 0
# -1: Non sa dell'esistenza dei zingheri
# 0: In attesa di risposta al messaggiomem
# 1: zingheri soonTM
self.zingheri = -1
# Generate/set token
if token_ is not None:
self.token = token_
@@ -89,6 +94,7 @@ class token:
# Join main stream
self.joinStream("main")
self.joinStream("zingheri")
def enqueue(self, bytes_):
"""
@@ -427,24 +433,4 @@ class token:
def leaveAllStreams(self):
for i in self.streams:
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)
return True
def updatePrivileges(self):
"""
Force updating self.privileges from db
:return:
"""
self.privileges = userUtils.getPrivileges(self.userID)
self.leaveStream(i)

View File

@@ -67,7 +67,7 @@ class streamList:
return
self.streams[streamName].broadcast(data)
'''def getClients(self, streamName):
def getClients(self, streamName):
"""
Get all clients in a stream
@@ -76,4 +76,4 @@ class streamList:
"""
if streamName not in self.streams:
return
return self.streams[streamName].clients'''
return self.streams[streamName].clients

19
pep.py
View File

@@ -134,6 +134,7 @@ if __name__ == "__main__":
consoleHelper.printNoNl("> Creating packets streams... ")
glob.streams.add("main")
glob.streams.add("lobby")
glob.streams.add("zingheri")
consoleHelper.printDone()
# Start fokabot
@@ -151,6 +152,11 @@ if __name__ == "__main__":
glob.tokens.spamProtectionResetLoop()
consoleHelper.printDone()
# Initialize zingheri loop
consoleHelper.printNoNl("> WOO WOO... ")
fokabot.zingheriLoop()
consoleHelper.printDone()
# Cache user ids
consoleHelper.printNoNl("> Caching user IDs... ")
userUtils.cacheUserIDs()
@@ -202,19 +208,6 @@ if __name__ == "__main__":
[
datadogClient.periodicCheck("online_users", lambda: len(glob.tokens.tokens)),
datadogClient.periodicCheck("multiplayer_matches", lambda: len(glob.matches.matches)),
datadogClient.periodicCheck("ram_clients", lambda: generalUtils.getTotalSize(glob.tokens)),
datadogClient.periodicCheck("ram_matches", lambda: generalUtils.getTotalSize(glob.matches)),
datadogClient.periodicCheck("ram_channels", lambda: generalUtils.getTotalSize(glob.channels)),
datadogClient.periodicCheck("ram_file_buffers", lambda: generalUtils.getTotalSize(glob.fileBuffers)),
datadogClient.periodicCheck("ram_file_locks", lambda: generalUtils.getTotalSize(glob.fLocks)),
datadogClient.periodicCheck("ram_datadog", lambda: generalUtils.getTotalSize(glob.datadogClient)),
datadogClient.periodicCheck("ram_verified_cache", lambda: generalUtils.getTotalSize(glob.verifiedCache)),
datadogClient.periodicCheck("ram_userid_cache", lambda: generalUtils.getTotalSize(glob.userIDCache)),
#datadogClient.periodicCheck("ram_pool", lambda: generalUtils.getTotalSize(glob.pool)),
datadogClient.periodicCheck("ram_irc", lambda: generalUtils.getTotalSize(glob.ircServer)),
datadogClient.periodicCheck("ram_tornado", lambda: generalUtils.getTotalSize(glob.application)),
datadogClient.periodicCheck("ram_db", lambda: generalUtils.getTotalSize(glob.db)),
])
else:
consoleHelper.printColored("[!] Warning! Datadog stats tracking is disabled!", bcolors.YELLOW)

View File

@@ -3,5 +3,4 @@ tornado
mysqlclient
psutil
raven
bcrypt>=3.1.1
dill
bcrypt