Exemplo n.º 1
0
def tell(user, message, delay=0, lock=None, prefix=''):
	"""Send a server message to user.
	Note: Splits multiline messages. See prefix, below.
	Optional args:
		delay: Wait delay seconds before sending message. Useful mainly with lock. See below.
		lock: Until message sent (see delay) allow no new messages with the same user and lock value.
			Generally speaking, expected to be a string. But it doesn't really matter.
		prefix: Add given prefix to every line sent, eg '<server>: '.
	Returns bool of whether message was sent (see lock).

	Note: Apart from delay and lock args, this function acts like helpers.tell()
	"""
	message = unicode(message)
	tell_fn = lambda: helpers.tell(user, message, prefix=prefix)

	if delay:
		try:
			schedule.check(lock)
		except KeyError:
			schedule.register(delay, tell_fn, key=lock)
		else:
			return False
	else:
		tell_fn()

	return True
Exemplo n.º 2
0
def display(user, menu_text, options, exit_callback, timeout=None):
	"""Display given menu to player. Menu format is as follows:
		menu_text: The main "body" text of the menu
		options: A list of tuples ("option text", option_callback). option_callback is called if the option is picked.
			Alternately, setting options=('prompt', callback) asks the user for a string instead. Note the strings 'exit' and 'again' are impossible.
		exit_callback: Called if the player tries to exit the menu.
		timeout: Optional. Either a timeout in (float,int) seconds, or a tuple (timeout, callback),
			where the callback is called after timeout. Else the exit_callback is called.
			If not given, has a default timeout menus.DEFAULT_TIMEOUT

		Callback functions act as follows:
			Should take args (user, response)
				Response will generally be the number they picked to pick the given callback option,
				but will be 'exit' for the exit_callback, None if it timed out, or the full response if 'prompt' was given.
			Should return either None (player no longer in a menu),
				or a tuple (menu_text, options, exit_callback, timeout) as per this function's args.
				Note that timeout is still optional.

		If another menu is currently active, raises a menus.MenuLockError
	"""

	if hasattr(user, 'menu') and user.menu is not None:
		raise MenuLockError(user, user.menu)

	if timeout is None:
		timeout = DEFAULT_TIMEOUT
	if type(timeout) is not tuple:
		timeout = (timeout, exit_callback)

	user.menu = (menu_text, options, exit_callback, timeout)
	tell_menu(user, user.menu)
	schedule.register(timeout[0], lambda: on_timeout(user), key=('menu', user))
Exemplo n.º 3
0
def schedule(user_name, cur_item, is_mod, is_owner, websocket):
	cmd = "schedule"

	if not is_mod or not is_owner:
		return None 	# Mod/owner only

	if len(cur_item) < 3:
		return usage.prepCmd(user_name, "schedule", is_mod, is_owner)

	cmd_cmd = cur_item[1]

	if cmd_cmd.lower() == "add":
		if len(cur_item) >= 3:
			text = cur_item[2:]
			response = schedule_mod.register(text, websocket)
		else:
			 return "Usage: !schedule add MESSAGEHERE"

	elif cmd_cmd.lower() == "remove":
		try:
			schedule_id = int(cur_item[2])
		except:
			return "Usage: !schedule remove MESSAGEIDHERE"

		response = schedule_mod.msg_rm(schedule_id, websocket)

	elif cmd_cmd.lower() == "update":
		try:
			schedule_id = int(cur_item[2])
		except:
			return "Usage: !schedule update MESSAGEIDHERE MESSAGETEXTHERE"

		if len(cur_item) >= 3:
			text = cur_item[3:]
			response = schedule_mod.edit_msg(text, schedule_id, websocket)
		else:
			return "Usage: !schedule update MESSAGEIDHERE MESSAGETEXTHERE"
	else:
		return usage.prepCmd(user_name, "schedule", is_mod, is_owner)

	return response
Exemplo n.º 4
0
INTERVAL = 1 # Seconds between refresh


def on_start():
	load_bans()


def load_bans():
	global bans
	try:
		new_bans = open(ban_file, 'rU').read().split('\n')
	except IOError, ex:
		if bans is None:
			raise
		logging.warning("Failed to refresh ban file:", exc_info=True)
	else:
		bans = new_bans

	schedule.register(INTERVAL, load_bans) # Repeat self after INTERVAL


def on_packet(packet, user, to_server):
	if to_server and packet.name() == 'Handshake':
		ip, port = user.addr
		if ip in bans:
			packet = Packet("Disconnect", reason="No U")
			send_packet(packet, user, False)
			return []
	return packet