From 345d60dfbd0054e75568b2489fbd608c1bbdb9c0 Mon Sep 17 00:00:00 2001 From: Zodiac Date: Fri, 14 Feb 2025 15:15:37 -0800 Subject: [PATCH] updates --- plugins/services/permissions.py | 164 +++++++++++++++----------------- 1 file changed, 77 insertions(+), 87 deletions(-) diff --git a/plugins/services/permissions.py b/plugins/services/permissions.py index a351e86..10c5505 100644 --- a/plugins/services/permissions.py +++ b/plugins/services/permissions.py @@ -1,24 +1,18 @@ # permissions.py # -*- coding: utf-8 -*- -""" -A plugin for irc3 that provides a permission system using TinyDB, including message blocking. - -Author: Your Name -License: MIT -""" +"""A plugin for irc3 that provides a permission system using TinyDB.""" import irc3 from irc3.plugins.command import command -from tinydb import TinyDB, Query +from tinydb import Query, TinyDB import fnmatch from ircstyle import style @irc3.plugin class TinyDBPermissions: - """Integrated permissions plugin with message blocking""" - priority = 10 # Lower number = higher priority - + """Main permission system plugin handling storage and commands.""" + def __init__(self, bot): self.bot = bot self.permission_db = TinyDB('permissions.json') @@ -26,23 +20,10 @@ class TinyDBPermissions: self.User = Query() self.bot.log.info("TinyDB permissions plugin initialized") - @irc3.event(irc3.rfc.PRIVMSG) - async def check_ignored_users(self, mask, event, target, data): - """Block messages from ignored users first""" - hostmask = str(mask) - ignored = self.permission_db.search(self.User.permission == 'ignore') - - for entry in ignored: - if fnmatch.fnmatch(hostmask, entry['mask']): - self.bot.log.debug(f"Blocking message from ignored user: {hostmask}") - return True # Block processing - return False - @command(permission='admin') def perm(self, mask, target, args): - """ - Manage permissions. Use --add, --del, or --list. - + """Manage permissions through command interface. + Usage: %%perm --add %%perm --del @@ -61,61 +42,10 @@ class TinyDBPermissions: ) self.bot.privmsg(target, error_msg) - def _add_permission(self, target, user_mask, perm): - existing = self.permission_db.search( - (self.User.mask == user_mask) & - (self.User.permission == perm) - ) - if existing: - msg = style( - f"Permission '{perm}' already exists for {user_mask}", - fg='yellow', bold=True - ) - else: - self.permission_db.insert({'mask': user_mask, 'permission': perm}) - msg = style( - f"Added permission '{perm}' for {user_mask}", - fg='green', bold=True - ) - self.bot.privmsg(target, msg) - - def _del_permission(self, target, user_mask, perm): - removed = self.permission_db.remove( - (self.User.mask == user_mask) & - (self.User.permission == perm) - ) - if removed: - msg = style( - f"Removed {len(removed)} '{perm}' permission(s) for {user_mask}", - fg='green', bold=True - ) - else: - msg = style( - f"No '{perm}' permissions found for {user_mask}", - fg='red', bold=True - ) - self.bot.privmsg(target, msg) - - def _list_permissions(self, target, mask_filter): - mask_filter = mask_filter or '*' - regex = fnmatch.translate(mask_filter).split('(?ms)')[0].rstrip('\\Z') - entries = self.permission_db.search(self.User.mask.matches(regex)) - - if not entries: - msg = style("No permissions found", fg='red', bold=True) - self.bot.privmsg(target, msg) - return - - for entry in entries: - msg = style( - f"{entry['mask']}: {entry['permission']}", - fg='blue', bold=True - ) - self.bot.privmsg(target, msg) - @command(permission='admin') def ignore(self, mask, target, args): - """Manage ignores + """Manage user ignore list. + Usage: %%ignore --add %%ignore --del @@ -145,19 +75,77 @@ class TinyDBPermissions: self.bot.privmsg(target, msg) + def _add_permission(self, target, user_mask, perm): + """Add a permission to the database.""" + existing = self.permission_db.search( + (self.User.mask == user_mask) & + (self.User.permission == perm) + ) + if existing: + msg = style( + f"Permission '{perm}' already exists for {user_mask}", + fg='yellow', bold=True + ) + else: + self.permission_db.insert({'mask': user_mask, 'permission': perm}) + msg = style( + f"Added permission '{perm}' for {user_mask}", + fg='green', bold=True + ) + self.bot.privmsg(target, msg) + + def _del_permission(self, target, user_mask, perm): + """Remove a permission from the database.""" + removed = self.permission_db.remove( + (self.User.mask == user_mask) & + (self.User.permission == perm) + ) + if removed: + msg = style( + f"Removed {len(removed)} '{perm}' permission(s) for {user_mask}", + fg='green', bold=True + ) + else: + msg = style( + f"No '{perm}' permissions found for {user_mask}", + fg='red', bold=True + ) + self.bot.privmsg(target, msg) + + def _list_permissions(self, target, mask_filter): + """List permissions matching a filter pattern.""" + mask_filter = mask_filter or '*' + regex = fnmatch.translate(mask_filter).split('(?ms)')[0].rstrip('\\Z') + entries = self.permission_db.search( + self.User.mask.matches(regex) + ) + + if not entries: + msg = style("No permissions found", fg='red', bold=True) + self.bot.privmsg(target, msg) + return + + for entry in entries: + msg = style( + f"{entry['mask']}: {entry['permission']}", + fg='blue', bold=True + ) + self.bot.privmsg(target, msg) + class TinyDBPolicy: - """Permission policy enforcement""" - + """Authorization system for command access control.""" + def __init__(self, bot): self.bot = bot self.User = Query() def has_permission(self, client_mask, permission): - # Check ignore list first using fnmatch - ignored_entries = self.bot.permission_db.search( + """Check if a client has required permissions.""" + # Check ignore list first + ignored = self.bot.permission_db.search( self.User.permission == 'ignore' ) - for entry in ignored_entries: + for entry in ignored: if fnmatch.fnmatch(client_mask, entry['mask']): return False @@ -165,16 +153,18 @@ class TinyDBPolicy: if permission is None: return True - perm_entries = self.bot.permission_db.search( - (self.User.permission == permission) | - (self.User.permission == 'all_permissions') + # Check for matching permissions using fnmatch + entries = self.bot.permission_db.search( + self.User.permission.test(lambda p: p in (permission, 'all_permissions')) ) - for entry in perm_entries: + for entry in entries: if fnmatch.fnmatch(client_mask, entry['mask']): return True + return False def __call__(self, predicates, meth, client, target, args): + """Enforce command permissions.""" cmd_name = predicates.get('name', meth.__name__) client_hostmask = str(client)