From fbb6204f83fe4feab0667138bfb142d6b36804cd Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Wed, 30 Oct 2013 08:49:43 +1300 Subject: [PATCH] this bad --- cloudbot.py | 4 ++++ core/permissions.py | 39 ++++++++++++++++++++++++++++++++++++--- plugins/core_sieve.py | 27 ++++----------------------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/cloudbot.py b/cloudbot.py index 0e94379..60a2aa3 100644 --- a/cloudbot.py +++ b/cloudbot.py @@ -21,8 +21,12 @@ if os.path.exists(os.path.abspath('lib')): print 'CloudBot2 ' def exit_gracefully(signum, frame): + # this doesn't really work that well 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 original_sigint = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, exit_gracefully) diff --git a/core/permissions.py b/core/permissions.py index 9bf499a..4ff3404 100644 --- a/core/permissions.py +++ b/core/permissions.py @@ -1,18 +1,51 @@ +from fnmatch import fnmatch + class PermissionManager(object): def __init__(self, bot, conn): + + # this is all legacy code, needs to be redone with classes and whatnot 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 self.bot = bot self.conn = conn self.config = conn.config + self.group_perms = {} + self.group_users = {} + self.perm_users = {} self.reload() + print self.group_perms + print self.group_users + print self.perm_users def reload(self): - self.logger.error("reload perms stub") - pass \ No newline at end of file + self.logger.info("Reloading permissions for {}.".format(self.conn.name)) + 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 + diff --git a/plugins/core_sieve.py b/plugins/core_sieve.py index 18ef611..50a84c4 100755 --- a/plugins/core_sieve.py +++ b/plugins/core_sieve.py @@ -33,34 +33,15 @@ def sieve_suite(bot, input, func, kind, args): args["permissions"] = ["adminonly"] 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() - for group in allowed_groups: - group_users = conn.conig.get("permissions", {}).get(group, [])["users"] - group_users = [_mask.lower() for _mask in group_users] - for pattern in group_users: - if fnmatch(mask, pattern): - return input + allowed_permissions = args.get('permissions', []) + for perm in allowed_permissions: + if conn.permissions.has_perm_legacy(mask, perm): + return input - target = input.nick input.notice("Sorry, you are not allowed to use this command.") return None