Commit 27dd631d authored by Yann Leboulanger's avatar Yann Leboulanger

handle XEP-191 (blocking command) in a better way: hability to block a contact...

handle XEP-191 (blocking command) in a better way: hability to block a contact when server doesn't support privacy lists, add / remove jid to blocked list only when we get the result from server, handle blocked / unblocked contacts by other resources
parent 75d213db
......@@ -1688,7 +1688,6 @@ def block_contacts(self, contact_list, message):
query = iq.setQuery(name='block')
query.setNamespace(nbxmpp.NS_BLOCKING)
for contact in contact_list:
self.blocked_contacts.append(contact.jid)
query.addChild(name='item', attrs={'jid': contact.jid})
self.connection.send(iq)
return
......@@ -1713,7 +1712,6 @@ def unblock_contacts(self, contact_list):
query = iq.setQuery(name='unblock')
query.setNamespace(nbxmpp.NS_BLOCKING)
for contact in contact_list:
self.blocked_contacts.append(contact.jid)
query.addChild(name='item', attrs={'jid': contact.jid})
self.connection.send(iq)
return
......
......@@ -575,9 +575,9 @@ def _IqCB(self, con, iq_obj):
iq = nbxmpp.Iq('get', xmlns='')
query = iq.setQuery(name='blocklist')
query.setNamespace(nbxmpp.NS_BLOCKING)
id_ = self.connection.getAnID()
iq.setID(id_)
self.awaiting_answers[id_] = (BLOCKING_ARRIVED, )
id2_ = self.connection.getAnID()
iq.setID(id2_)
self.awaiting_answers[id2_] = (BLOCKING_ARRIVED, )
self.connection.send(iq)
if self.continue_connect_info and self.continue_connect_info[0]\
......@@ -1523,6 +1523,8 @@ def __init__(self):
self._nec_agent_removed)
gajim.ged.register_event_handler('stream-other-host-received', ged.CORE,
self._nec_stream_other_host_received)
gajim.ged.register_event_handler('blocking', ged.CORE,
self._nec_blocking)
def cleanup(self):
ConnectionHandlersBase.cleanup(self)
......@@ -1567,6 +1569,7 @@ def cleanup(self):
self._nec_agent_removed)
gajim.ged.remove_event_handler('stream-other-host-received', ged.CORE,
self._nec_stream_other_host_received)
gajim.ged.remove_event_handler('blocking', ged.CORE, self._nec_blocking)
def build_http_auth_answer(self, iq_obj, answer):
if not self.connection or self.connected < 2:
......@@ -2257,6 +2260,25 @@ def _PubkeyResultCB(self, con, iq_obj):
jid_from = helpers.get_full_jid_from_iq(iq_obj)
jingle_xtls.handle_new_cert(con, iq_obj, jid_from)
def _BlockingSetCB(self, con, iq_obj):
log.debug('_BlockingSetCB')
gajim.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 = []
else:
for jid in obj.blocked_jids:
if jid not in self.blocked_contacts:
self.blocked_contacts.append(jid)
for jid in obj.unblocked_jids:
if jid in self.blocked_contacts:
self.blocked_contacts.remove(jid)
def _nec_stream_other_host_received(self, obj):
if obj.conn.name != self.name:
return
......@@ -2351,3 +2373,5 @@ def _register_handlers(self, con, con_type):
nbxmpp.NS_PUBKEY_PUBKEY)
con.RegisterHandler('iq', self._PubkeyResultCB, 'result',
nbxmpp.NS_PUBKEY_PUBKEY)
con.RegisterHandler('iq', self._BlockingSetCB, 'set',
nbxmpp.NS_BLOCKING)
......@@ -2529,3 +2529,26 @@ class InformationEvent(nec.NetworkIncomingEvent):
def init(self):
self.popup = True
class BlockingEvent(nec.NetworkIncomingEvent):
name = 'blocking'
base_network_events = []
def init(self):
self.blocked_jids = []
self.unblocked_jids = []
self.unblock_all = False
def generate(self):
block_tag = self.stanza.getTag('block', namespace=nbxmpp.NS_BLOCKING)
if block_tag:
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
self.unblock_all = True
for item in unblock_tag.getTags('item'):
self.unblocked_jids.append(item.getAttr('jid'))
return True
......@@ -3017,6 +3017,14 @@ def remove(list_):
dialogs.ConfirmationDialog(pritext, sectext,
on_response_ok = (remove, list_))
def _nec_blocking(self, obj):
if obj.unblock_all:
jids = gajim.contacts.get_jid_list(obj.conn.name)
self._idle_draw_jids_of_account(jids, obj.conn.name)
else:
for jid in obj.blocked_jids + obj.unblocked_jids:
self.draw_contact(jid, obj.conn.name)
def on_block(self, widget, list_, group=None):
"""
When clicked on the 'block' button in context menu. list_ is a list of
......@@ -3026,8 +3034,9 @@ def on_continue(msg, pep_dict):
if msg is None:
# user pressed Cancel to change status message dialog
return
accounts = set(i[1] for i in list_ if gajim.connections[i[1]].\
privacy_rules_supported)
accounts = set(i[1] for i in list_ if (gajim.connections[i[1]].\
privacy_rules_supported or (group is None and gajim.\
connections[i[1]].blocking_supported)))
if group is None:
for acct in accounts:
l_ = [i[0] for i in list_ if i[1] == acct]
......@@ -3065,8 +3074,9 @@ def on_unblock(self, widget, list_, group=None):
"""
When clicked on the 'unblock' button in context menu.
"""
accounts = set(i[1] for i in list_ if gajim.connections[i[1]].\
privacy_rules_supported)
accounts = set(i[1] for i in list_ if (gajim.connections[i[1]].\
privacy_rules_supported or (group is None and gajim.\
connections[i[1]].blocking_supported)))
if group is None:
for acct in accounts:
l_ = [i[0] for i in list_ if i[1] == acct]
......@@ -6607,3 +6617,5 @@ def _open_wizard():
self._nec_signed_in)
gajim.ged.register_event_handler('decrypted-message-received', ged.GUI2,
self._nec_decrypted_message_received)
gajim.ged.register_event_handler('blocking', ged.GUI1,
self._nec_blocking)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment