From 011d70e816ab68e5f29f47f94cb9884a25f59232 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 16 Jun 2019 10:30:16 -0700 Subject: [PATCH] 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.""" --- classes.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/classes.py b/classes.py index de0c4b9..8130133 100644 --- a/classes.py +++ b/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): """