Exemplo n.º 1
0
def ciTrigger():
    # Ci restart trigger

    # Get ket from GET
    key = flask.request.args.get('k')

    # Get request ip
    requestIP = flask.request.headers.get('X-Real-IP')
    if (requestIP == None):
        requestIP = flask.request.remote_addr

    # Check key
    if (key is None or key != glob.conf.config["ci"]["key"]):
        consoleHelper.printColored(
            "[!] Invalid ci trigger from {}".format(requestIP), bcolors.RED)
        return flask.jsonify({"response": "-1"})

    # Ci event triggered, schedule server shutdown
    consoleHelper.printColored(
        "[!] Ci event triggered from {}".format(requestIP), bcolors.PINK)
    systemHelper.scheduleShutdown(
        5, False,
        "A new Bancho update is available and the server will be restarted in 5 seconds. Thank you for your patience."
    )

    return flask.jsonify({"response": "1"})
Exemplo n.º 2
0
def ciTrigger():
	# Ci restart trigger

	# Get ket from GET
	key = flask.request.args.get('k')

	# Get request ip
	requestIP = flask.request.headers.get('X-Real-IP')
	if (requestIP == None):
		requestIP = flask.request.remote_addr

	# Check key
	if (key is None or key != glob.conf.config["ci"]["key"]):
		consoleHelper.printColored("[!] Invalid ci trigger from {}".format(requestIP), bcolors.RED)
		return flask.jsonify({"response" : "-1"})

	# Ci event triggered, schedule server shutdown
	consoleHelper.printColored("[!] Ci event triggered from {}".format(requestIP), bcolors.PINK)
	systemHelper.scheduleShutdown(5, False, "A new Bancho update is available and the server will be restarted in 5 seconds. Thank you for your patience.")

	return flask.jsonify({"response" : "1"})
Exemplo n.º 3
0
def restartShutdown(restart):
	"""Restart (if restart = True) or shutdown (if restart = False) pep.py safely"""
	msg = "We are performing some maintenance. Bancho will {} in 5 seconds. Thank you for your patience.".format("restart" if restart else "shutdown")
	systemHelper.scheduleShutdown(5, restart, msg)
	return msg
Exemplo n.º 4
0
def restartShutdown(restart):
    """Restart (if restart = True) or shutdown (if restart = False) pep.py safely"""
    msg = "We are performing some maintenance. Bancho will {} in 5 seconds. Thank you for your patience.".format(
        "restart" if restart else "shutdown")
    systemHelper.scheduleShutdown(5, restart, msg)
    return msg
Exemplo n.º 5
0
def fokabotResponse(fro, chan, message):
    """
	Check if a message has triggered fokabot (and return its response)

	fro -- sender username (for permissions stuff with admin commands)
	chan -- channel name
	message -- message

	return -- fokabot's response string or False
	"""

    if "!roll" in message:
        maxPoints = 100
        message = message.split(" ")

        # Get max number if needed
        if (len(message) >= 2):
            if (message[1].isdigit() == True and int(message[1]) > 0):
                maxPoints = int(message[1])

        points = random.randrange(0, maxPoints)
        return fro + " rolls " + str(points) + " points!"
    elif "!faq rules" in message:
        return "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23]."
    elif "!faq swearing" in message:
        return "Please don't abuse swearing"
    elif "!faq spam" in message:
        return "Please don't spam"
    elif "!faq offend" in message:
        return "Please offend other players"
    elif "!help" in message:
        return "Click (here)[https://ripple.moe/index.php?p=16&id=4] for full FokaBot's command list"
    elif "!report" in message:
        return "Report command is not here yet :("

    # Admin commands
    elif "!moderated" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Make sure we are in a channel and not PM
            if (chan.startswith("#") == False):
                raise exceptions.moderatedPMException

            # Split message and default value
            message = message.lower().split(" ")
            enable = True

            # Get on/off
            if (len(message) >= 2):
                if (message[1] == "off"):
                    enable = False

            # Turn on/off moderated mode
            glob.channels.channels[chan].moderated = enable

            return "This channel is {} in moderated mode!".format(
                "now" if enable else "no longer")
        except exceptions.noAdminException:
            consoleHelper.printColored(
                "[!] " + fro + " has tried to put " + chan +
                " in moderated mode, but he is not an admin.", bcolors.RED)
            return False
        except exceptions.moderatedPMException:
            consoleHelper.printColored(
                "[!] " + fro +
                " has tried to put a PM chat in moderated mode.", bcolors.RED)
            return "Are you trying to put a private chat in moderated mode? Are you serious?!? You're fired."
    elif "!system" in message:
        # System commands
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Split message
            message = message.lower().split(" ")

            # Get parameters
            if (len(message) >= 2):
                if (message[1] == "restart"):
                    msg = "We are performing some maintenance. Bancho will restart in 5 seconds. Thank you for your patience."
                    systemHelper.scheduleShutdown(5, True, msg)
                    return msg
                elif (message[1] == "status"):
                    # Print some server info
                    data = systemHelper.getSystemInfo()

                    # Final message
                    msg = "=== PEP.PY STATS ===\n"
                    msg += "Running pep.py server\n"
                    msg += "Webserver: " + data["webServer"] + "\n"
                    msg += "\n"
                    msg += "=== BANCHO STATS ===\n"
                    msg += "Connected users: " + str(
                        data["connectedUsers"]) + "\n"
                    msg += "\n"
                    msg += "=== SYSTEM STATS ===\n"
                    msg += "CPU: " + str(data["cpuUsage"]) + "%\n"
                    msg += "RAM: " + str(data["usedMemory"]) + "GB/" + str(
                        data["totalMemory"]) + "GB\n"
                    if (data["unix"] == True):
                        msg += "Load average: " + str(
                            data["loadAverage"][0]) + "/" + str(
                                data["loadAverage"][1]) + "/" + str(
                                    data["loadAverage"][2]) + "\n"

                    return msg
                elif (message[1] == "reload"):
                    #Reload settings from bancho_settings
                    glob.banchoConf.loadSettings()
                    return "Bancho settings reloaded!"
                elif (message[1] == "maintenance"):
                    # Turn on/off bancho maintenance
                    maintenance = True

                    # Get on/off
                    if (len(message) >= 2):
                        if (message[2] == "off"):
                            maintenance = False

                    # Set new maintenance value in bancho_settings table
                    glob.banchoConf.setMaintenance(maintenance)

                    if (maintenance == True):
                        # We have turned on maintenance mode
                        # Users that will be disconnected
                        who = []

                        # Disconnect everyone but mod/admins
                        for key, value in glob.tokens.tokens.items():
                            if (value.rank <= 2):
                                who.append(value.userID)

                        glob.tokens.enqueueAll(
                            serverPackets.notification(
                                "Our bancho server is in maintenance mode. Please try to login again later."
                            ))
                        glob.tokens.multipleEnqueue(serverPackets.loginError(),
                                                    who)
                        msg = "The server is now in maintenance mode!"
                    else:
                        # We have turned off maintenance mode
                        # Send message if we have turned off maintenance mode
                        msg = "The server is no longer in maintenance mode!"

                    # Chat output
                    return msg
            else:
                raise exceptions.commandSyntaxException

        except exceptions.noAdminException:
            consoleHelper.printColored(
                "[!] " + fro +
                " has tried to run a system command, but he is not an admin.",
                bcolors.RED)
            return False
        except exceptions.commandSyntaxException:
            consoleHelper.printColored("[!] Fokabot command syntax error",
                                       bcolors.RED)
            return False
    elif "!scareall" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Get parameters
            message = message.lower().split(" ")
            if (len(message) < 2):
                raise exceptions.commandSyntaxException
            scaryMessage = ' '.join(message[1:])

            # Send packet to everyone
            consoleHelper.printColored(
                "> {} is turning osu! into an horror game ({})".format(
                    fro, scaryMessage), bcolors.PINK)
            glob.tokens.enqueueAll(serverPackets.jumpscare(scaryMessage))

        except exceptions.noAdminException:
            pass
        except exceptions.commandSyntaxException:
            return "Wrong syntax. !scareall <message>"
        finally:
            # No respobnse
            return False
    elif "!scare" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Get parameters
            message = message.lower().split(" ")
            if (len(message) < 3):
                raise exceptions.commandSyntaxException
            target = message[1]
            scaryMessage = ' '.join(message[2:])

            # Get target token and make sure is connected
            targetToken = glob.tokens.getTokenFromUsername(target)
            if (targetToken == None):
                raise exceptions.tokenNotFoundException

            # Send packet to target
            consoleHelper.printColored(
                "> Rip {}'s heart ({}). ~ <3, {}".format(
                    target, scaryMessage, fro), bcolors.PINK)
            targetToken.enqueue(serverPackets.jumpscare(scaryMessage))

            # No response
            return False
        except exceptions.noAdminException:
            return False
        except exceptions.tokenNotFoundException:
            return "{} is not online".format(message[1])
        except exceptions.commandSyntaxException:
            return "Wrong syntax. !scare <target> <message>"
    elif "!kick" in message:
        try:
            # Admin check
            # TODO: God this sucks
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Get parameters
            message = message.lower().split(" ")
            if (len(message) < 2):
                raise exceptions.commandSyntaxException
            target = message[1]

            # Get target token and make sure is connected
            targetToken = glob.tokens.getTokenFromUsername(target)
            if (targetToken == None):
                raise exceptions.tokenNotFoundException

            # Send packet to target
            consoleHelper.printColored(
                "> {} has been disconnected. (kick)".format(target),
                bcolors.YELLOW)
            targetToken.enqueue(
                serverPackets.notification(
                    "You have been kicked from the server. Please login again."
                ))
            targetToken.enqueue(serverPackets.loginFailed())

            # Bot response
            return "{} has been kicked from the server.".format(message[1])
        except exceptions.noAdminException:
            return False
        except exceptions.tokenNotFoundException:
            return "{} is not online.".format(message[1])
        except exceptions.commandSyntaxException:
            return "Wrong syntax. !kick <target>"
    elif "!silence" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Get parameters
            message = message.lower().split(" ")
            if (len(message) < 4):
                raise exceptions.commandSyntaxException
            target = message[1]
            amount = message[2]
            unit = message[3]
            reason = ' '.join(message[4:])

            # Get target user ID
            targetUserID = userHelper.getUserID(target)

            # Make sure the user exists
            if (targetUserID == False):
                raise exceptions.userNotFoundException

            # Calculate silence seconds
            if (unit == 's'):
                silenceTime = int(amount)
            elif (unit == 'm'):
                silenceTime = int(amount) * 60
            elif (unit == 'h'):
                silenceTime = int(amount) * 3600
            elif (unit == 'd'):
                silenceTime = int(amount) * 86400
            else:
                raise exceptions.commandSyntaxException

            # Max silence time is 7 days
            if (silenceTime > 604800):
                raise exceptions.commandSyntaxException

            # Calculate silence end time
            endTime = int(time.time()) + silenceTime

            # Update silence end in db
            userHelper.silenceUser(targetUserID, endTime, reason)

            # Check if target is connected
            targetToken = glob.tokens.getTokenFromUsername(target)
            if (targetToken == None):
                tokenFound = False
            else:
                tokenFound = True

            # Send silence packets if user is online
            if (tokenFound == True):
                targetToken.enqueue(serverPackets.silenceEndTime(silenceTime))

            consoleHelper.printColored(
                "{} has been silenced for {} seconds the following reason: {}".
                format(target, silenceTime, reason), bcolors.PINK)

            # Bot response
            return "{} has been silenced for the following reason: {}".format(
                target, reason)
        except exceptions.userNotFoundException:
            return "{}: user not found".format(message[1])
        except exceptions.noAdminException:
            return False
        except exceptions.commandSyntaxException:
            return "Wrong syntax. !silence <target> <amount> <unit (s/m/h/d)> <reason>. Max silence time is 7 days."
    elif "!removesilence" in message or "!resetsilence" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Get parameters
            message = message.lower().split(" ")
            if (len(message) < 2):
                raise exceptions.commandSyntaxException
            target = message[1]

            # Make sure the user exists
            targetUserID = userHelper.getUserID(target)
            if (targetUserID == False):
                raise exceptions.userNotFoundException

            # Reset user silence time and reason in db
            userHelper.silenceUser(targetUserID, 0, "")

            # Send new silence end packet to user if he's online
            targetToken = glob.tokens.getTokenFromUsername(target)
            if (targetToken != None):
                targetToken.enqueue(serverPackets.silenceEndTime(0))

            return "{}'s silence reset".format(target)
        except exceptions.commandSyntaxException:
            return "Wrong syntax. !removesilence <target>"
        except exceptions.noAdminException:
            return False
        except exceptions.userNotFoundException:
            return "{}: user not found".format(message[1])
    elif "!fokabot reconnect" in message:
        try:
            # Admin check
            if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
                raise exceptions.noAdminException

            # Check if fokabot is already connected
            if (glob.tokens.getTokenFromUserID(999) != None):
                raise exceptions.alreadyConnectedException

            # Fokabot is not connected, connect it
            connect()
            return False
        except exceptions.noAdminException:
            return False
        except exceptions.alreadyConnectedException:
            return "Fokabot is already connected to Bancho"
    else:
        return False
Exemplo n.º 6
0
def fokabotResponse(fro, chan, message):
	"""
	Check if a message has triggered fokabot (and return its response)

	fro -- sender username (for permissions stuff with admin commands)
	chan -- channel name
	message -- message

	return -- fokabot's response string or False
	"""

	if "!roll" in message:
		maxPoints = 100
		message = message.split(" ")

		# Get max number if needed
		if (len(message) >= 2):
			if (message[1].isdigit() == True and int(message[1]) > 0):
				maxPoints = int(message[1])

		points = random.randrange(0,maxPoints)
		return fro+" rolls "+str(points)+" points!"
	elif "!faq rules" in message:
		return "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23]."
	elif "!faq swearing" in message:
		return "Please don't abuse swearing"
	elif "!faq spam" in message:
		return "Please don't spam"
	elif "!faq offend" in message:
		return "Please offend other players"
	elif "!help" in message:
		return "Click (here)[https://ripple.moe/index.php?p=16&id=4] for full FokaBot's command list"
	elif "!report" in message:
		return "Report command is not here yet :("

	# Admin commands
	elif "!moderated" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Make sure we are in a channel and not PM
			if (chan.startswith("#") == False):
				raise exceptions.moderatedPMException

			# Split message and default value
			message = message.lower().split(" ")
			enable = True

			# Get on/off
			if (len(message) >= 2):
				if (message[1] == "off"):
					enable = False

			# Turn on/off moderated mode
			glob.channels.channels[chan].moderated = enable

			return "This channel is {} in moderated mode!".format("now" if enable else "no longer")
		except exceptions.noAdminException:
			consoleHelper.printColored("[!] "+fro+" has tried to put "+chan+" in moderated mode, but he is not an admin.", bcolors.RED)
			return False
		except exceptions.moderatedPMException:
			consoleHelper.printColored("[!] "+fro+" has tried to put a PM chat in moderated mode.", bcolors.RED)
			return "Are you trying to put a private chat in moderated mode? Are you serious?!? You're fired."
	elif "!system" in message:
		# System commands
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Split message
			message = message.lower().split(" ")

			# Get parameters
			if (len(message) >= 2):
				if (message[1] == "restart"):
					msg = "We are performing some maintenance. Bancho will restart in 5 seconds. Thank you for your patience."
					systemHelper.scheduleShutdown(5, True, msg)
					return msg
				elif (message[1] == "status"):
					# Print some server info
					data = systemHelper.getSystemInfo()

					# Final message
					msg =  "=== PEP.PY STATS ===\n"
					msg += "Running pep.py server\n"
					msg += "Webserver: "+data["webServer"]+"\n"
					msg += "\n"
					msg += "=== BANCHO STATS ===\n"
					msg += "Connected users: "+str(data["connectedUsers"])+"\n"
					msg += "\n"
					msg += "=== SYSTEM STATS ===\n"
					msg += "CPU: "+str(data["cpuUsage"])+"%\n"
					msg += "RAM: "+str(data["usedMemory"])+"GB/"+str(data["totalMemory"])+"GB\n"
					if (data["unix"] == True):
						msg += "Load average: "+str(data["loadAverage"][0])+"/"+str(data["loadAverage"][1])+"/"+str(data["loadAverage"][2])+"\n"

					return msg
				elif (message[1] == "reload"):
					#Reload settings from bancho_settings
					glob.banchoConf.loadSettings()
					return "Bancho settings reloaded!"
				elif (message[1] == "maintenance"):
					# Turn on/off bancho maintenance
					maintenance = True

					# Get on/off
					if (len(message) >= 2):
						if (message[2] == "off"):
							maintenance = False

					# Set new maintenance value in bancho_settings table
					glob.banchoConf.setMaintenance(maintenance)

					if (maintenance == True):
						# We have turned on maintenance mode
						# Users that will be disconnected
						who = []

						# Disconnect everyone but mod/admins
						for key,value in glob.tokens.tokens.items():
							if (value.rank <= 2):
								who.append(value.userID)

						glob.tokens.enqueueAll(serverPackets.notification("Our bancho server is in maintenance mode. Please try to login again later."))
						glob.tokens.multipleEnqueue(serverPackets.loginError(), who)
						msg = "The server is now in maintenance mode!"
					else:
						# We have turned off maintenance mode
						# Send message if we have turned off maintenance mode
						msg = "The server is no longer in maintenance mode!"

					# Chat output
					return msg
			else:
				raise exceptions.commandSyntaxException

		except exceptions.noAdminException:
			consoleHelper.printColored("[!] "+fro+" has tried to run a system command, but he is not an admin.", bcolors.RED)
			return False
		except exceptions.commandSyntaxException:
			consoleHelper.printColored("[!] Fokabot command syntax error", bcolors.RED)
			return False
	elif "!scareall" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			scaryMessage = ' '.join(message[1:])

			# Send packet to everyone
			consoleHelper.printColored("> {} is turning osu! into an horror game ({})".format(fro, scaryMessage), bcolors.PINK)
			glob.tokens.enqueueAll(serverPackets.jumpscare(scaryMessage))

		except exceptions.noAdminException:
			pass
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !scareall <message>"
		finally:
			# No respobnse
			return False
	elif "!scare" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 3):
				raise exceptions.commandSyntaxException
			target = message[1]
			scaryMessage = ' '.join(message[2:])

			# Get target token and make sure is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				raise exceptions.tokenNotFoundException

			# Send packet to target
			consoleHelper.printColored("> Rip {}'s heart ({}). ~ <3, {}".format(target, scaryMessage, fro), bcolors.PINK)
			targetToken.enqueue(serverPackets.jumpscare(scaryMessage))

			# No response
			return False
		except exceptions.noAdminException:
			return False
		except exceptions.tokenNotFoundException:
			return "{} is not online".format(message[1])
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !scare <target> <message>"
	elif "!kick" in message:
		try:
			# Admin check
			# TODO: God this sucks
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			target = message[1]

			# Get target token and make sure is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				raise exceptions.tokenNotFoundException

			# Send packet to target
			consoleHelper.printColored("> {} has been disconnected. (kick)".format(target), bcolors.YELLOW)
			targetToken.enqueue(serverPackets.notification("You have been kicked from the server. Please login again."))
			targetToken.enqueue(serverPackets.loginFailed())

			# Bot response
			return "{} has been kicked from the server.".format(message[1])
		except exceptions.noAdminException:
			return False
		except exceptions.tokenNotFoundException:
			return "{} is not online.".format(message[1])
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !kick <target>"
	elif "!silence" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 4):
				raise exceptions.commandSyntaxException
			target = message[1]
			amount = message[2]
			unit = message[3]
			reason = ' '.join(message[4:])

			# Get target user ID
			targetUserID = userHelper.getUserID(target)

			# Make sure the user exists
			if (targetUserID == False):
				raise exceptions.userNotFoundException

			# Calculate silence seconds
			if (unit == 's'):
				silenceTime = int(amount)
			elif (unit == 'm'):
				silenceTime = int(amount)*60
			elif (unit == 'h'):
				silenceTime = int(amount)*3600
			elif (unit == 'd'):
				silenceTime = int(amount)*86400
			else:
				raise exceptions.commandSyntaxException

			# Max silence time is 7 days
			if (silenceTime > 604800):
				raise exceptions.commandSyntaxException

			# Calculate silence end time
			endTime = int(time.time())+silenceTime

			# Update silence end in db
			userHelper.silenceUser(targetUserID, endTime, reason)

			# Check if target is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				tokenFound = False
			else:
				tokenFound = True

			# Send silence packets if user is online
			if (tokenFound == True):
				targetToken.enqueue(serverPackets.silenceEndTime(silenceTime))

			consoleHelper.printColored("{} has been silenced for {} seconds the following reason: {}".format(target, silenceTime, reason), bcolors.PINK)

			# Bot response
			return "{} has been silenced for the following reason: {}".format(target, reason)
		except exceptions.userNotFoundException:
			return "{}: user not found".format(message[1])
		except exceptions.noAdminException:
			return False
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !silence <target> <amount> <unit (s/m/h/d)> <reason>. Max silence time is 7 days."
	elif "!removesilence" in message or "!resetsilence" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			target = message[1]

			# Make sure the user exists
			targetUserID = userHelper.getUserID(target)
			if (targetUserID == False):
				raise exceptions.userNotFoundException

			# Reset user silence time and reason in db
			userHelper.silenceUser(targetUserID, 0, "")

			# Send new silence end packet to user if he's online
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken != None):
				targetToken.enqueue(serverPackets.silenceEndTime(0))

			return "{}'s silence reset".format(target)
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !removesilence <target>"
		except exceptions.noAdminException:
			return False
		except exceptions.userNotFoundException:
			return "{}: user not found".format(message[1])
	elif "!fokabot reconnect" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Check if fokabot is already connected
			if (glob.tokens.getTokenFromUserID(999) != None):
				raise exceptions.alreadyConnectedException

			# Fokabot is not connected, connect it
			connect()
			return False
		except exceptions.noAdminException:
			return False
		except exceptions.alreadyConnectedException:
			return "Fokabot is already connected to Bancho"
	else:
		return False
Exemplo n.º 7
0
def fokabotResponse(fro, chan, message):
	"""
	Check if a message has triggered fokabot (and return its response)

	fro -- sender username (for permissions stuff with admin commands)
	chan -- channel name
	message -- message

	return -- fokabot's response string or False
	"""

	if "!roll" in message:
		maxPoints = 100
		message = message.split(" ")

		# Get max number if needed
		if (len(message) >= 2):
			if (message[1].isdigit() == True and int(message[1]) > 0):
				maxPoints = int(message[1])

		points = random.randrange(0,maxPoints)
		return "{} rolls {} points!".format(fro, str(points))
	elif "!faq rules" in message:
		return "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23]."
	elif "!faq swearing" in message:
		return "Please don't abuse swearing"
	elif "!faq spam" in message:
		return "Please don't spam"
	elif "!faq offend" in message:
		return "Please don't offend other players"
	elif "!help" in message:
		return "Click (here)[https://ripple.moe/index.php?p=16&id=4] for FokaBot's full command list"
	elif "!report" in message:
		return "Report command is not here, yet :("
	elif "!faq github" in message:
		return "(Ripple's Github page!)[https://github.com/osuripple/ripple]"
	elif "!faq discord" in message:
		return "(Join Ripple's Discord!)[https://discord.gg/0rJcZruIsA6rXuIx]"
	elif "!faq blog" in message:
		return "You can find the latest Ripple news on the (blog)[https://ripple.moe/blog/]!"
	elif "!faq changelog" in message:
		return "Check the (changelog)[https://ripple.moe/index.php?p=17] !"
	elif "!ask" in message:
		if (len(message.split(" ")) >= 2):
			return "{}: {}".format(fro, random.choice(["Yes", "No", "Maybe"]))
		else:
			return False


	# Admin commands
	elif "!moderated" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Make sure we are in a channel and not PM
			if (chan.startswith("#") == False):
				raise exceptions.moderatedPMException

			# Split message and default value
			message = message.lower().split(" ")
			enable = True

			# Get on/off
			if (len(message) >= 2):
				if (message[1] == "off"):
					enable = False

			# Turn on/off moderated mode
			glob.channels.channels[chan].moderated = enable

			return "This channel is {} in moderated mode!".format("now" if enable else "no longer")
		except exceptions.noAdminException:
			consoleHelper.printColored("[!] {} tried to put {} in moderated mode, but they are not an admin.".format(fro, chan), bcolors.RED)
			return False
		except exceptions.moderatedPMException:
			consoleHelper.printColored("[!] {} tried to put a PM chat in moderated mode.".format(fro), bcolors.RED)
			return "You are trying to put a private chat in moderated mode. Are you serious?!? You're fired."
	elif "!system" in message:
		# System commands
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Split message
			message = message.lower().split(" ")

			# Get parameters
			if (len(message) >= 2):
				if (message[1] == "restart" or message[1] == "shutdown"):
					restart = message[1] == "restart"
					msg = "We are performing some maintenance. Bancho will {} in 5 seconds. Thank you for your patience.".format("restart" if restart else "shutdown")
					systemHelper.scheduleShutdown(5, restart, msg)
					return msg
				elif (message[1] == "status"):
					# Print some server info
					data = systemHelper.getSystemInfo()

					# Final message
					msg =  "=== PEP.PY STATS ===\n"
					msg += "Running pep.py server\n"
					msg += "Webserver: {}\n".format(data["webServer"])
					msg += "\n"
					msg += "=== BANCHO STATS ===\n"
					msg += "Connected users: {}\n".format(str(data["connectedUsers"]))
					msg += "\n"
					msg += "=== SYSTEM STATS ===\n"
					msg += "CPU: {}%\n".format(str(data["cpuUsage"]))
					msg += "RAM: {}GB/{}GB\n".format(str(data["usedMemory"]), str(data["totalMemory"]))
					if (data["unix"] == True):
						msg += "Load average: {}/{}/{}\n".format(str(data["loadAverage"][0]), str(data["loadAverage"][1]), str(data["loadAverage"][2]))

					return msg
				elif (message[1] == "reload"):
					#Reload settings from bancho_settings
					glob.banchoConf.loadSettings()

					# Reload channels too
					glob.channels.loadChannels()

					# Send new channels and new bottom icon to everyone
					glob.tokens.enqueueAll(serverPackets.channelInfoEnd())
					for key, value in glob.channels.channels.items():
						glob.tokens.enqueueAll(serverPackets.channelInfo(key))

					glob.tokens.enqueueAll(serverPackets.mainMenuIcon(glob.banchoConf.config["menuIcon"]))

					return "Bancho settings reloaded!"
				elif (message[1] == "maintenance"):
					# Turn on/off bancho maintenance
					maintenance = True

					# Get on/off
					if (len(message) >= 2):
						if (message[2] == "off"):
							maintenance = False

					# Set new maintenance value in bancho_settings table
					glob.banchoConf.setMaintenance(maintenance)

					if (maintenance == True):
						# We have turned on maintenance mode
						# Users that will be disconnected
						who = []

						# Disconnect everyone but mod/admins
						for _, value in glob.tokens.tokens.items():
							if (value.rank <= 2):
								who.append(value.userID)

						glob.tokens.enqueueAll(serverPackets.notification("Our bancho server is in maintenance mode. Please try to login again later."))
						glob.tokens.multipleEnqueue(serverPackets.loginError(), who)
						msg = "The server is now in maintenance mode!"
					else:
						# We have turned off maintenance mode
						# Send message if we have turned off maintenance mode
						msg = "The server is no longer in maintenance mode!"

					# Chat output
					return msg
				else:
					# Command not found
					return False
			else:
				raise exceptions.commandSyntaxException

		except exceptions.noAdminException:
			consoleHelper.printColored("[!] {} tried to run a system command, but they are not an admin.".format(fro), bcolors.RED)
			return False
		except exceptions.commandSyntaxException:
			consoleHelper.printColored("[!] Fokabot command syntax error", bcolors.RED)
			return False
	elif "!scareall" in message:
		return False	# !scareall is currently disabled. Remove this line to activate it.
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			scaryMessage = ' '.join(message[1:])

			# Send packet to everyone
			consoleHelper.printColored("> {} is turning osu! into an horror game ({})".format(fro, scaryMessage), bcolors.PINK)
			glob.tokens.enqueueAll(serverPackets.jumpscare(scaryMessage))

		except exceptions.noAdminException:
			pass
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !scareall <message>"
		finally:
			# No respobnse
			return False
	elif "!scare" in message:
		return False	# !scare is currently disabled. Remove this line to activate it.
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 3):
				raise exceptions.commandSyntaxException
			target = message[1].replace("_", " ")
			scaryMessage = ' '.join(message[2:])

			# Get target token and make sure is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				raise exceptions.tokenNotFoundException

			# Send packet to target
			consoleHelper.printColored("> Rip {}'s heart ({}). ~ <3, {}".format(target, scaryMessage, fro), bcolors.PINK)
			targetToken.enqueue(serverPackets.jumpscare(scaryMessage))

			# No response
			return False
		except exceptions.noAdminException:
			return False
		except exceptions.tokenNotFoundException:
			return "{} is not online".format(message[1])
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !scare <target> <message>"
	elif "!kickall" in message:
		try:
			# Check admin
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Kick everyone but mods/admins
			toKick = []
			for key, value in glob.tokens.tokens.items():
				if (value.rank < 3):
					toKick.append(key)

			# Loop though users to kick (we can't change dictionary size while iterating)
			for i in toKick:
				if (i in glob.tokens.tokens):
					glob.tokens.tokens[i].kick()

			return "Whoops! Rip everyone."
		except exceptions.noAdminException:
			return False
	elif "!kick" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			target = message[1].replace("_", " ")

			# Get target token and make sure is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				raise exceptions.tokenNotFoundException

			# Kick user
			targetToken.kick()

			# Bot response
			return "{} has been kicked from the server.".format(message[1])
		except exceptions.noAdminException:
			return False
		except exceptions.tokenNotFoundException:
			return "{} is not online.".format(message[1])
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !kick <target>"
	elif "!silence" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 4):
				raise exceptions.commandSyntaxException
			target = message[1].replace("_", " ")
			amount = message[2]
			unit = message[3]
			reason = ' '.join(message[4:])

			# Get target user ID
			targetUserID = userHelper.getUserID(target)

			# Make sure the user exists
			if (targetUserID == False):
				raise exceptions.userNotFoundException

			# Calculate silence seconds
			if (unit == 's'):
				silenceTime = int(amount)
			elif (unit == 'm'):
				silenceTime = int(amount)*60
			elif (unit == 'h'):
				silenceTime = int(amount)*3600
			elif (unit == 'd'):
				silenceTime = int(amount)*86400
			else:
				raise exceptions.commandSyntaxException

			# Max silence time is 7 days
			if (silenceTime > 604800):
				raise exceptions.commandSyntaxException

			# Calculate silence end time
			endTime = int(time.time())+silenceTime

			# Update silence end in db
			userHelper.silenceUser(targetUserID, endTime, reason)

			# Check if target is connected
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken == None):
				tokenFound = False
			else:
				tokenFound = True

			# Send silence packets if user is online
			if (tokenFound == True):
				targetToken.enqueue(serverPackets.silenceEndTime(silenceTime))

			consoleHelper.printColored("{} has been silenced for {} seconds the following reason: {}".format(target, silenceTime, reason), bcolors.PINK)

			# Bot response
			return "{} has been silenced for the following reason: {}".format(target, reason)
		except exceptions.userNotFoundException:
			return "{}: user not found".format(message[1])
		except exceptions.noAdminException:
			return False
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !silence <target> <amount> <unit (s/m/h/d)> <reason>. Max silence time is 7 days."
	elif "!removesilence" in message or "!resetsilence" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Get parameters
			message = message.lower().split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException
			target = message[1].replace("_", " ")

			# Make sure the user exists
			targetUserID = userHelper.getUserID(target)
			if (targetUserID == False):
				raise exceptions.userNotFoundException

			# Reset user silence time and reason in db
			userHelper.silenceUser(targetUserID, 0, "")

			# Send new silence end packet to user if he's online
			targetToken = glob.tokens.getTokenFromUsername(target)
			if (targetToken != None):
				targetToken.enqueue(serverPackets.silenceEndTime(0))

			return "{}'s silence reset".format(target)
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !removesilence <target>"
		except exceptions.noAdminException:
			return False
		except exceptions.userNotFoundException:
			return "{}: user not found".format(message[1])
	elif "!fokabot reconnect" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Check if fokabot is already connected
			if (glob.tokens.getTokenFromUserID(999) != None):
				raise exceptions.alreadyConnectedException

			# Fokabot is not connected, connect it
			connect()
			return False
		except exceptions.noAdminException:
			return False
		except exceptions.alreadyConnectedException:
			return "Fokabot is already connected to Bancho"
	elif "!alert" in message:
		try:
			# Admin check
			if (userHelper.getUserRank(userHelper.getUserID(fro)) <= 1):
				raise exceptions.noAdminException

			# Syntax check
			message = message.split(" ")
			if (len(message) < 2):
				raise exceptions.commandSyntaxException

			# Send alert to everyone
			glob.tokens.enqueueAll(serverPackets.notification(' '.join(message[1:])))

			return False
		except exceptions.noAdminException:
			return False
		except exceptions.commandSyntaxException:
			return "Wrong syntax. !alert <message>"
	else:
		return False