Refactor Blocking List

Fixes #8762
This commit is contained in:
Philipp Hörist 2017-10-15 18:03:41 +02:00
parent f22fa200ea
commit 2446c7e3ed
4 changed files with 41 additions and 23 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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: