Pārlūkot izejas kodu

Implement is_admin(), is_owner().

tags/v0.1^2
Ben Kurtovic pirms 12 gadiem
vecāks
revīzija
992faba70e
1 mainītis faili ar 41 papildinājumiem un 0 dzēšanām
  1. +41
    -0
      earwigbot/config/permissions.py

+ 41
- 0
earwigbot/config/permissions.py Parādīt failu

@@ -26,6 +26,9 @@ from threading import Lock
__all__ = ["PermissionsDB"]

class PermissionsDB(object):
ADMIN = 1
OWNER = 2

def __init__(self, dbfile):
self._dbfile = dbfile
self._db_access_lock = Lock()
@@ -46,6 +49,15 @@ class PermissionsDB(object):
user_rank)"""
conn.execute(query)

def _is_rank(self, user, rank):
"""Return True if the given user has the given rank, else False."""
try:
for rule in self._data[rank]:
if user == rule:
return True
except KeyError:
return False

def load(self):
"""Load permissions from an existing database, or create a new one."""
query = "SELECT user_nick, user_ident, user_host, user_rank FROM users"
@@ -60,9 +72,38 @@ class PermissionsDB(object):
except sqlite.OperationalError:
self._create(conn)

def is_admin(self, nick="*", ident="*", host="*"):
"""Return ``True`` if the given user is a bot admin, else ``False``."""
return self._is_rank(_User(nick, ident, host), rank=self.ADMIN)

def is_owner(self, nick="*", ident="*", host="*"):
"""Return ``True`` if the given user is a bot owner, else ``False``."""
return self._is_rank(_User(nick, ident, host), rank=self.OWNER)

class _User(object):
"""A class that represents an IRC user for the purpose of testing rules."""
def __init__(self, nick, ident, host):
self.nick = nick
self.ident = ident
self.host = host

def __repr__(self):
"""Return the canonical string representation of the User."""
res = "_User(nick={0!r}, ident={1!r}, host={2!r})"
return res.format(self.nick, self.ident, self.host)

def __str__(self):
"""Return a nice string representation of the User."""
return "{0}!{1}@{2}".format(self.nick, self.ident, self.host)

def __eq__(self, user):
if self.nick == user.nick or (self.nick == "*" or user.nick == "*"):
if self.ident == user.ident or (self.ident == "*" or
user.ident == "*"):
if self.host == user.host or (self.host == "*" or
user.host == "*"):
return True
return False

def __ne__(self, user):
return not self == user

Notiek ielāde…
Atcelt
Saglabāt