mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
classes: make nick_to_uid more versatile against duplicate nicks
This adds a couple of new options: - multi: return all matches for nick instead of just the last result. (Return an empty list if no matches) - filterfunc: if specified, filter matched users by the given function first."""
This commit is contained in:
parent
3b07f8ab2b
commit
011d70e816
14
classes.py
14
classes.py
@ -654,11 +654,21 @@ class PyLinkNetworkCore(structures.CamelCaseToSnakeCase):
|
|||||||
log.debug('(%s) Removing client %s from user + server state', self.name, numeric)
|
log.debug('(%s) Removing client %s from user + server state', self.name, numeric)
|
||||||
|
|
||||||
## State checking functions
|
## State checking functions
|
||||||
def nick_to_uid(self, nick):
|
def nick_to_uid(self, nick, multi=False, filterfunc=None):
|
||||||
"""Looks up the UID of a user with the given nick, if one is present."""
|
"""Looks up the UID of a user with the given nick, or return None if no such nick exists.
|
||||||
|
|
||||||
|
If multi is given, return all matches for nick instead of just the last result. (Return an empty list if no matches)
|
||||||
|
If filterfunc is given, filter matched users by the given function first."""
|
||||||
nick = self.to_lower(nick)
|
nick = self.to_lower(nick)
|
||||||
|
|
||||||
uids = self.users.bynick.get(nick, [])
|
uids = self.users.bynick.get(nick, [])
|
||||||
|
|
||||||
|
if filterfunc:
|
||||||
|
uids = list(filter(filterfunc, uids))
|
||||||
|
|
||||||
|
if multi:
|
||||||
|
return uids
|
||||||
|
else:
|
||||||
if len(uids) > 1:
|
if len(uids) > 1:
|
||||||
log.warning('(%s) Multiple UIDs found for nick %r: %r; using the last one!', self.name, nick, uids)
|
log.warning('(%s) Multiple UIDs found for nick %r: %r; using the last one!', self.name, nick, uids)
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user