From 2446c7e3ed969e0426e4dae446fffd27a983de9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 15 Oct 2017 18:03:41 +0200 Subject: [PATCH] Refactor Blocking List Fixes #8762 --- gajim/common/connection.py | 15 ++++++------ gajim/common/connection_handlers.py | 20 ++++++++-------- gajim/common/connection_handlers_events.py | 27 ++++++++++++++++++---- gajim/roster_window.py | 2 +- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/gajim/common/connection.py b/gajim/common/connection.py index 0c12f5048..7159dd908 100644 --- a/gajim/common/connection.py +++ b/gajim/common/connection.py @@ -1842,6 +1842,13 @@ class Connection(CommonConnection, ConnectionHandlers): self.awaiting_answers[id_] = (PRIVACY_ARRIVED, ) self.connection.send(iq) + def _request_blocking(self): + if not app.account_is_connected(self.name) or not self.connection: + return + iq = nbxmpp.Iq('get', xmlns=None) + iq.setQuery('blocklist').setNamespace(nbxmpp.NS_BLOCKING) + self.connection.send(iq) + def _continue_connection_request_privacy(self): if self.privacy_rules_supported: if not self.privacy_rules_requested: @@ -1861,13 +1868,7 @@ class Connection(CommonConnection, ConnectionHandlers): 'invisibility.') % self.name)) return if self.blocking_supported: - iq = nbxmpp.Iq('get', xmlns='') - query = iq.setQuery(name='blocklist') - query.setNamespace(nbxmpp.NS_BLOCKING) - id2_ = self.connection.getAnID() - iq.setID(id2_) - self.awaiting_answers[id2_] = (BLOCKING_ARRIVED, ) - self.connection.send(iq) + self._request_blocking() # Ask metacontacts before roster self.get_metacontacts() diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index edb0290bf..0fdf4a13c 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -69,7 +69,6 @@ METACONTACTS_ARRIVED = 'metacontacts_arrived' ROSTER_ARRIVED = 'roster_arrived' DELIMITER_ARRIVED = 'delimiter_arrived' PRIVACY_ARRIVED = 'privacy_arrived' -BLOCKING_ARRIVED = 'blocking_arrived' PEP_CONFIG = 'pep_config' @@ -1463,15 +1462,6 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): # connection process, we don't take the risk self.privacy_rules_supported = False self._continue_connection_request_privacy() - elif self.awaiting_answers[id_][0] == BLOCKING_ARRIVED: - del self.awaiting_answers[id_] - if iq_obj.getType() == 'result': - list_node = iq_obj.getTag('blocklist') - if not list_node: - return - self.blocked_contacts = [] - for i in list_node.iterTags('item'): - self.blocked_contacts.append(i.getAttr('jid')) elif self.awaiting_answers[id_][0] == PEP_CONFIG: del self.awaiting_answers[id_] if iq_obj.getType() == 'error': @@ -2097,11 +2087,19 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): self.connection.send(reply) raise nbxmpp.NodeProcessed + def _BlockingResultCB(self, con, iq_obj): + log.debug('_BlockingResultCB') + app.nec.push_incoming_event( + BlockingEvent(None, conn=self, stanza=iq_obj)) + raise nbxmpp.NodeProcessed + def _nec_blocking(self, obj): if obj.conn.name != self.name: return if obj.unblock_all: self.blocked_contacts = [] + elif obj.blocklist: + self.blocked_contacts = obj.blocklist else: for jid in obj.blocked_jids: if jid not in self.blocked_contacts: @@ -2202,3 +2200,5 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): nbxmpp.NS_PUBKEY_PUBKEY) con.RegisterHandler('iq', self._BlockingSetCB, 'set', nbxmpp.NS_BLOCKING) + con.RegisterHandler('iq', self._BlockingResultCB, 'result', + nbxmpp.NS_BLOCKING) diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index c971e0868..39abdb1da 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -2787,20 +2787,37 @@ class BlockingEvent(nec.NetworkIncomingEvent): base_network_events = [] def init(self): + self.blocklist = [] self.blocked_jids = [] self.unblocked_jids = [] self.unblock_all = False def generate(self): + block_list = self.stanza.getTag( + 'blocklist', namespace=nbxmpp.NS_BLOCKING) + if block_list is not None: + for item in block_list.getTags('item'): + self.blocklist.append(item.getAttr('jid')) + app.log('blocking').info( + 'Blocklist Received: %s', self.blocklist) + return True + block_tag = self.stanza.getTag('block', namespace=nbxmpp.NS_BLOCKING) - if block_tag: + if block_tag is not None: for item in block_tag.getTags('item'): self.blocked_jids.append(item.getAttr('jid')) - unblock_tag = self.stanza.getTag('unblock', - namespace=nbxmpp.NS_BLOCKING) - if unblock_tag: - if not unblock_tag.getTags('item'): # unblock all + app.log('blocking').info( + 'Blocking Push - blocked JIDs: %s', self.blocked_jids) + + unblock_tag = self.stanza.getTag( + 'unblock', namespace=nbxmpp.NS_BLOCKING) + if unblock_tag is not None: + if not unblock_tag.getTags('item'): self.unblock_all = True + app.log('blocking').info('Blocking Push - unblocked all') + return True for item in unblock_tag.getTags('item'): self.unblocked_jids.append(item.getAttr('jid')) + app.log('blocking').info( + 'Blocking Push - unblocked JIDs: %s', self.unblocked_jids) return True diff --git a/gajim/roster_window.py b/gajim/roster_window.py index 73418b848..6e8433d00 100644 --- a/gajim/roster_window.py +++ b/gajim/roster_window.py @@ -2844,7 +2844,7 @@ class RosterWindow: on_response_ok = (remove, list_)) def _nec_blocking(self, obj): - if obj.unblock_all: + if obj.unblock_all or obj.blocklist: jids = app.contacts.get_jid_list(obj.conn.name) self._idle_draw_jids_of_account(jids, obj.conn.name) else: