this bad
This commit is contained in:
parent
ee057bc45a
commit
fbb6204f83
3 changed files with 44 additions and 26 deletions
|
@ -21,8 +21,12 @@ if os.path.exists(os.path.abspath('lib')):
|
||||||
print 'CloudBot2 <http://git.io/cloudbotirc>'
|
print 'CloudBot2 <http://git.io/cloudbotirc>'
|
||||||
|
|
||||||
def exit_gracefully(signum, frame):
|
def exit_gracefully(signum, frame):
|
||||||
|
# this doesn't really work that well
|
||||||
cloudbot.stop()
|
cloudbot.stop()
|
||||||
|
|
||||||
|
# restore the original handler so if they do it again it triggers
|
||||||
|
signal.signal(signal.SIGINT, original_sigint)
|
||||||
|
|
||||||
# store the original SIGINT handler
|
# store the original SIGINT handler
|
||||||
original_sigint = signal.getsignal(signal.SIGINT)
|
original_sigint = signal.getsignal(signal.SIGINT)
|
||||||
signal.signal(signal.SIGINT, exit_gracefully)
|
signal.signal(signal.SIGINT, exit_gracefully)
|
||||||
|
|
|
@ -1,18 +1,51 @@
|
||||||
|
from fnmatch import fnmatch
|
||||||
|
|
||||||
class PermissionManager(object):
|
class PermissionManager(object):
|
||||||
def __init__(self, bot, conn):
|
def __init__(self, bot, conn):
|
||||||
|
|
||||||
|
# this is all legacy code, needs to be redone with classes and whatnot
|
||||||
self.logger = bot.logger
|
self.logger = bot.logger
|
||||||
|
|
||||||
self.logger.info("Creating permission manager for {}.".format(conn.name))
|
self.logger.info("Creating temporary legacy permission manager for {}.".format(conn.name))
|
||||||
|
|
||||||
# stuff
|
# stuff
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
self.config = conn.config
|
self.config = conn.config
|
||||||
|
|
||||||
|
|
||||||
self.group_perms = {}
|
self.group_perms = {}
|
||||||
|
self.group_users = {}
|
||||||
|
self.perm_users = {}
|
||||||
|
|
||||||
self.reload()
|
self.reload()
|
||||||
|
print self.group_perms
|
||||||
|
print self.group_users
|
||||||
|
print self.perm_users
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
self.logger.error("reload perms stub")
|
self.logger.info("Reloading permissions for {}.".format(self.conn.name))
|
||||||
pass
|
groups = self.conn.config.get("permissions", [])
|
||||||
|
# work out the permissions and users each group has
|
||||||
|
for key, value in groups.iteritems():
|
||||||
|
self.group_perms[key] = []
|
||||||
|
self.group_users[key] = []
|
||||||
|
for permission in value["perms"]:
|
||||||
|
self.group_perms[key].append(permission)
|
||||||
|
for user in value["users"]:
|
||||||
|
self.group_users[key].append(user)
|
||||||
|
|
||||||
|
for group, users in self.group_users.iteritems():
|
||||||
|
group_perms = self.group_perms[group]
|
||||||
|
for perm in group_perms:
|
||||||
|
self.perm_users[perm] = []
|
||||||
|
self.perm_users[perm] = users
|
||||||
|
|
||||||
|
def has_perm_legacy(self, mask, perm):
|
||||||
|
|
||||||
|
allowed_users = self.perm_users[perm]
|
||||||
|
|
||||||
|
for pattern in allowed_users:
|
||||||
|
if fnmatch(mask.lower(), pattern.lower()):
|
||||||
|
return input
|
||||||
|
|
||||||
|
|
|
@ -33,34 +33,15 @@ def sieve_suite(bot, input, func, kind, args):
|
||||||
args["permissions"] = ["adminonly"]
|
args["permissions"] = ["adminonly"]
|
||||||
|
|
||||||
if args.get('permissions', False):
|
if args.get('permissions', False):
|
||||||
groups = conn.config.get("permissions", [])
|
|
||||||
|
|
||||||
allowed_permissions = args.get('permissions', [])
|
|
||||||
allowed_groups = []
|
|
||||||
|
|
||||||
# loop over every group
|
|
||||||
for key, value in groups.iteritems():
|
|
||||||
# loop over every permission the command allows
|
|
||||||
for permission in allowed_permissions:
|
|
||||||
# see if the group has that permission
|
|
||||||
if permission in value["perms"]:
|
|
||||||
# if so, add the group name to the allowed_groups list
|
|
||||||
allowed_groups.append(key)
|
|
||||||
|
|
||||||
if not allowed_groups:
|
|
||||||
print "Something is wrong. A hook requires {} but" \
|
|
||||||
" there are no groups with that permission!".format(str(allowed_permissions))
|
|
||||||
|
|
||||||
mask = input.mask.lower()
|
mask = input.mask.lower()
|
||||||
|
|
||||||
for group in allowed_groups:
|
allowed_permissions = args.get('permissions', [])
|
||||||
group_users = conn.conig.get("permissions", {}).get(group, [])["users"]
|
for perm in allowed_permissions:
|
||||||
group_users = [_mask.lower() for _mask in group_users]
|
if conn.permissions.has_perm_legacy(mask, perm):
|
||||||
for pattern in group_users:
|
|
||||||
if fnmatch(mask, pattern):
|
|
||||||
return input
|
return input
|
||||||
|
|
||||||
target = input.nick
|
|
||||||
input.notice("Sorry, you are not allowed to use this command.")
|
input.notice("Sorry, you are not allowed to use this command.")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Reference in a new issue