# Path: halibot/message.py
# class Message():
# 
# 	def __init__(self, **kwargs):
# 		self.log = logging.getLogger(self.__class__.__name__)
# 		self.uuid = uuid.uuid4()
# 		self.sync = False
# 
# 		self.body = kwargs.get('body', None)
# 		self.type = kwargs.get('type', 'simple')
# 		self.author = kwargs.get('author', None)
# 		self.identity = kwargs.get('identity', None)
# 		self.origin = kwargs.get('origin', None)
# 		self.misc = kwargs.get('misc', jsdict())
# 		self.target = kwargs.get('target', '')
# 
# 	def whom(self):
# 		return '/'.join(self.target.split('/')[1:])
# 
# 	def __repr__(self):
# 		return "halibot.Message(body='{}', type='{}', author='{}', identity='{}', origin='{}', misc='{}', target='{}')".format(self.body, self.type, self.author, self.identity, self.origin, self.misc, self.target)
# 
# 	def __str__(self):
# 		return "body='{}' type='{}' author='{}' origin='{}' target='{}'".format(self.body, self.type, self.author, self.origin, self.target)
# 
# class MalformedMsgException(Exception): 'Bad message object'


import json
import logging
import re
from halibot.message import Message, MalformedMsgException

def hasPermission(perm, reply=False, argnum=None, key="msg"):
	def real_dec(func):
		def wrapper(self, *args, **kwargs):
			if argnum != None and argnum < len(args):
				msg = args[argnum]
			else:
				msg = kwargs[key]

			# Origin must be set for this to be a valid permission check
			if not hasattr(msg, "origin") or not msg.origin:
				self.log.error("Probable module bug! -- hasPermission decorator called on a function that doesn't have a valid Message argument!")
				raise MalformedMsgException("Bad or missing origin attribute")
			# Identity may not be supported by agent, so we'll allow this to be blank
			if not hasattr(msg, "identity"):
				self.log.error("Probable module bug! -- hasPermission decorator called on a function that doesn't have a valid Message argument!")

# Path: halibot/message.py
# class Message():
# 
# 	def __init__(self, **kwargs):
# 		self.log = logging.getLogger(self.__class__.__name__)
# 		self.uuid = uuid.uuid4()
# 		self.sync = False
# 
# 		self.body = kwargs.get('body', None)
# 		self.type = kwargs.get('type', 'simple')
# 		self.author = kwargs.get('author', None)
# 		self.identity = kwargs.get('identity', None)
# 		self.origin = kwargs.get('origin', None)
# 		self.misc = kwargs.get('misc', jsdict())
# 		self.target = kwargs.get('target', '')
# 
# 	def whom(self):
# 		return '/'.join(self.target.split('/')[1:])
# 
# 	def __repr__(self):
# 		return "halibot.Message(body='{}', type='{}', author='{}', identity='{}', origin='{}', misc='{}', target='{}')".format(self.body, self.type, self.author, self.identity, self.origin, self.misc, self.target)
# 
# 	def __str__(self):
# 		return "body='{}' type='{}' author='{}' origin='{}' target='{}'".format(self.body, self.type, self.author, self.origin, self.target)
# 
# class MalformedMsgException(Exception): 'Bad message object'


# Path: halibot/halauth.py
import json
import logging
import re
from halibot.message import Message, MalformedMsgException


def hasPermission(perm, reply=False, argnum=None, key="msg"):
	def real_dec(func):
		def wrapper(self, *args, **kwargs):
			if argnum != None and argnum < len(args):
				msg = args[argnum]
			else:
				msg = kwargs[key]

			# Origin must be set for this to be a valid permission check
			if not hasattr(msg, "origin") or not msg.origin:
				self.log.error("Probable module bug! -- hasPermission decorator called on a function that doesn't have a valid Message argument!")
				raise MalformedMsgException("Bad or missing origin attribute")
			# Identity may not be supported by agent, so we'll allow this to be blank
			if not hasattr(msg, "identity"):
				self.log.error("Probable module bug! -- hasPermission decorator called on a function that doesn't have a valid Message argument!")

				raise MalformedMsgException("Missing identity attribute")