# Path: halibot/halmodule.py
# class HalModule(HalObject):
# 
# 	def reply(self, msg0=None, **kwargs):
# 		# Create the reply message
# 		body = kwargs.get('body', msg0.body)
# 		mtype = kwargs.get('type', msg0.type)
# 		author = kwargs.get('author', msg0.author)
# 		origin = kwargs.get('origin', self.name)
# 
# 		msg = Message(body=body, type=mtype, author=author, origin=origin)
# 
# 		# Synchronous reply?
# 		if msg0.sync:
# 			self.sync_replies[msg0.uuid].append(msg)
# 		else:
# 			self.send_to(msg, [ msg0.origin ])
# 
# 	def hasPermission(self, msg, perm):
# 		return self._hal.auth.hasPermission(msg.origin, msg.identity, perm)


from . import HalModule
	#     use the annotation AsArgs above to override this
	#   msg: not needed in all commands, the message object as received

	def __init__(self, hal, conf={}):
		super().__init__(hal, conf=conf)

		self.commands = {}
		# Get the command prefix if defined, otherwise use "!" as default
		self.prefix = self._hal.config.get("command_prefix", "!")

	# Override only if you know what you are doing!
	def receive(self, msg):
		self._cmd_receive(msg)

	# Actually does the command handling logic. Separate callable so multi-inheritance
	#  can work nicely (maybe)
	def _cmd_receive(self, msg):
		body = msg.body.split(" ", 1)
		if body and not body[0].startswith(self.prefix):
			self.default(msg)
			return

		# Ugly fix for commands without args
		if len(body) == 1:
			body.append("")

		# This maybe should prevent a empty string as a key...
		func = self.commands.get(body[0][1:])

		if func:

# Path: halibot/halmodule.py
# class HalModule(HalObject):
# 
# 	def reply(self, msg0=None, **kwargs):
# 		# Create the reply message
# 		body = kwargs.get('body', msg0.body)
# 		mtype = kwargs.get('type', msg0.type)
# 		author = kwargs.get('author', msg0.author)
# 		origin = kwargs.get('origin', self.name)
# 
# 		msg = Message(body=body, type=mtype, author=author, origin=origin)
# 
# 		# Synchronous reply?
# 		if msg0.sync:
# 			self.sync_replies[msg0.uuid].append(msg)
# 		else:
# 			self.send_to(msg, [ msg0.origin ])
# 
# 	def hasPermission(self, msg, perm):
# 		return self._hal.auth.hasPermission(msg.origin, msg.identity, perm)


# Path: halibot/commandmodule.py
from . import HalModule

	#     use the annotation AsArgs above to override this
	#   msg: not needed in all commands, the message object as received

	def __init__(self, hal, conf={}):
		super().__init__(hal, conf=conf)

		self.commands = {}
		# Get the command prefix if defined, otherwise use "!" as default
		self.prefix = self._hal.config.get("command_prefix", "!")

	# Override only if you know what you are doing!
	def receive(self, msg):
		self._cmd_receive(msg)

	# Actually does the command handling logic. Separate callable so multi-inheritance
	#  can work nicely (maybe)
	def _cmd_receive(self, msg):
		body = msg.body.split(" ", 1)
		if body and not body[0].startswith(self.prefix):
			self.default(msg)
			return

		# Ugly fix for commands without args
		if len(body) == 1:
			body.append("")

		# This maybe should prevent a empty string as a key...
		func = self.commands.get(body[0][1:])

		if func:

			func(body[1], msg=msg)