diff options
Diffstat (limited to 'poezio/roster.py')
-rw-r--r-- | poezio/roster.py | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/poezio/roster.py b/poezio/roster.py index bedf477b..a52ea23e 100644 --- a/poezio/roster.py +++ b/poezio/roster.py @@ -3,12 +3,13 @@ # This file is part of Poezio. # # Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. +# it under the terms of the GPL-3.0+ license. See the COPYING file. """ Defines the Roster and RosterGroup classes """ import logging -log = logging.getLogger(__name__) + +from typing import List from poezio.config import config from poezio.contact import Contact @@ -16,9 +17,10 @@ from poezio.roster_sorting import SORTING_METHODS, GROUP_SORTING_METHODS from os import path as p from datetime import datetime -from poezio.common import safeJID from slixmpp.exceptions import IqError, IqTimeout +from slixmpp import JID, InvalidJID +log = logging.getLogger(__name__) class Roster: """ @@ -29,6 +31,22 @@ class Roster: DEFAULT_FILTER = (lambda x, y: None, None) def __init__(self): + self.__node = None + + # A tuple(function, *args) function to filter contacts + # on search, for example + self.contact_filter = self.DEFAULT_FILTER + self.groups = {} + self.contacts = {} + self.length = 0 + self.connected = 0 + self.folded_groups = [] + + # Used for caching roster infos + self.last_built = datetime.now() + self.last_modified = datetime.now() + + def reset(self): """ node: the RosterSingle from slixmpp """ @@ -38,7 +56,8 @@ class Roster: # on search, for example self.contact_filter = self.DEFAULT_FILTER self.folded_groups = set( - config.get('folded_roster_groups', section='var').split(':')) + config.getlist('folded_roster_groups', section='var') + ) self.groups = {} self.contacts = {} self.length = 0 @@ -52,12 +71,15 @@ class Roster: self.last_modified = datetime.now() @property - def needs_rebuild(self): + def needs_rebuild(self) -> bool: return self.last_modified >= self.last_built def __getitem__(self, key): """Get a Contact from his bare JID""" - key = safeJID(key).bare + try: + key = JID(key).bare + except InvalidJID: + return None if key in self.contacts and self.contacts[key] is not None: return self.contacts[key] if key in self.jids(): @@ -71,7 +93,10 @@ class Roster: def remove(self, jid): """Send a removal iq to the server""" - jid = safeJID(jid).bare + try: + jid = JID(jid).bare + except InvalidJID: + return if self.__node[jid]: try: self.__node[jid].send_presence(ptype='unavailable') @@ -81,7 +106,10 @@ class Roster: def __delitem__(self, jid): """Remove a contact from the roster view""" - jid = safeJID(jid).bare + try: + jid = JID(jid).bare + except InvalidJID: + return contact = self[jid] if not contact: return @@ -99,10 +127,13 @@ class Roster: def __contains__(self, key): """True if the bare jid is in the roster, false otherwise""" - return safeJID(key).bare in self.jids() + try: + return JID(key).bare in self.jids() + except InvalidJID: + return False @property - def jid(self): + def jid(self) -> JID: """Our JID""" return self.__node.jid @@ -143,7 +174,7 @@ class Roster: """Subscribe to a jid""" self.__node.subscribe(jid) - def jids(self): + def jids(self) -> List[JID]: """List of the contact JIDS""" l = [] for key in self.__node.keys(): @@ -335,11 +366,6 @@ class RosterGroup: return len([1 for contact in self.contacts if len(contact)]) -def create_roster(): - "Create the global roster object" - global roster - roster = Roster() - # Shared roster object -roster = None +roster = Roster() |