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
26
classes.py
26
classes.py
@ -654,17 +654,27 @@ class PyLinkNetworkCore(structures.CamelCaseToSnakeCase):
|
||||
log.debug('(%s) Removing client %s from user + server state', self.name, numeric)
|
||||
|
||||
## State checking functions
|
||||
def nick_to_uid(self, nick):
|
||||
"""Looks up the UID of a user with the given nick, if one is present."""
|
||||
def nick_to_uid(self, nick, multi=False, filterfunc=None):
|
||||
"""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)
|
||||
|
||||
uids = self.users.bynick.get(nick, [])
|
||||
if len(uids) > 1:
|
||||
log.warning('(%s) Multiple UIDs found for nick %r: %r; using the last one!', self.name, nick, uids)
|
||||
try:
|
||||
return uids[-1]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
if filterfunc:
|
||||
uids = list(filter(filterfunc, uids))
|
||||
|
||||
if multi:
|
||||
return uids
|
||||
else:
|
||||
if len(uids) > 1:
|
||||
log.warning('(%s) Multiple UIDs found for nick %r: %r; using the last one!', self.name, nick, uids)
|
||||
try:
|
||||
return uids[-1]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def is_internal_client(self, numeric):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user