re-work blocking code
This commit is contained in:
		
							parent
							
								
									1c977b8045
								
							
						
					
					
						commit
						e011a28702
					
				
					 2 changed files with 118 additions and 105 deletions
				
			
		| 
						 | 
					@ -1574,6 +1574,95 @@ class Connection(CommonConnection, ConnectionHandlers):
 | 
				
			||||||
        iq.setQuery().setTag('active', {'name': name})
 | 
					        iq.setQuery().setTag('active', {'name': name})
 | 
				
			||||||
        self.connection.send(iq)
 | 
					        self.connection.send(iq)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def block_contacts(self, contact_list, message):
 | 
				
			||||||
 | 
					        if not self.privacy_rules_supported:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        for contact in contact_list:
 | 
				
			||||||
 | 
					            self.send_custom_status('offline', message, contact.jid)
 | 
				
			||||||
 | 
					            new_rule = {'order': '1', 'type': 'jid', 'action': 'deny',
 | 
				
			||||||
 | 
					                'value' : contact.jid, 'child': ['message', 'iq', 'presence-out']}
 | 
				
			||||||
 | 
					            self.blocked_list.append(new_rule)
 | 
				
			||||||
 | 
					            self.blocked_contacts.append(contact.jid)
 | 
				
			||||||
 | 
					        self.set_privacy_list('block', self.blocked_list)
 | 
				
			||||||
 | 
					        if len(self.blocked_list) == 1:
 | 
				
			||||||
 | 
					            self.set_active_list('block')
 | 
				
			||||||
 | 
					            self.set_default_list('block')
 | 
				
			||||||
 | 
					        self.get_privacy_list('block')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def unblock_contacts(self, contact_list):
 | 
				
			||||||
 | 
					        if not self.privacy_rules_supported:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        self.new_blocked_list = []
 | 
				
			||||||
 | 
					        self.to_unblock = []
 | 
				
			||||||
 | 
					        for contact in contact_list:
 | 
				
			||||||
 | 
					            self.to_unblock.append(contact.jid)
 | 
				
			||||||
 | 
					            if contact.jid in self.blocked_contacts:
 | 
				
			||||||
 | 
					                self.blocked_contacts.remove(contact.jid)
 | 
				
			||||||
 | 
					        for rule in self.blocked_list:
 | 
				
			||||||
 | 
					            if rule['action'] != 'deny' or rule['type'] != 'jid' \
 | 
				
			||||||
 | 
					            or rule['value'] not in self.to_unblock:
 | 
				
			||||||
 | 
					                self.new_blocked_list.append(rule)
 | 
				
			||||||
 | 
					        self.set_privacy_list('block', self.new_blocked_list)
 | 
				
			||||||
 | 
					        self.get_privacy_list('block')
 | 
				
			||||||
 | 
					        if len(self.new_blocked_list) == 0:
 | 
				
			||||||
 | 
					            self.blocked_list = []
 | 
				
			||||||
 | 
					            self.blocked_contacts = []
 | 
				
			||||||
 | 
					            self.blocked_groups = []
 | 
				
			||||||
 | 
					            self.set_default_list('')
 | 
				
			||||||
 | 
					            self.set_active_list('')
 | 
				
			||||||
 | 
					            self.del_privacy_list('block')
 | 
				
			||||||
 | 
					        if not gajim.interface.roster.regroup:
 | 
				
			||||||
 | 
					            show = gajim.SHOW_LIST[self.connected]
 | 
				
			||||||
 | 
					        else:   # accounts merged
 | 
				
			||||||
 | 
					            show = helpers.get_global_show()
 | 
				
			||||||
 | 
					        if show == 'invisible':
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        for contact in contact_list:
 | 
				
			||||||
 | 
					            self.send_custom_status(show, self.status, contact.jid)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def block_group(self, group, contact_list, message):
 | 
				
			||||||
 | 
					        if not self.privacy_rules_supported:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        self.blocked_groups.append(group)
 | 
				
			||||||
 | 
					        for contact in contact_list:
 | 
				
			||||||
 | 
					            self.send_custom_status('offline', message, contact.jid)
 | 
				
			||||||
 | 
					        new_rule = {'order': '1', 'type': 'group', 'action': 'deny',
 | 
				
			||||||
 | 
					            'value' : group, 'child': ['message', 'iq', 'presence-out']}
 | 
				
			||||||
 | 
					        self.blocked_list.append(new_rule)
 | 
				
			||||||
 | 
					        self.set_privacy_list('block', self.blocked_list)
 | 
				
			||||||
 | 
					        if len(self.blocked_list) == 1:
 | 
				
			||||||
 | 
					            self.set_active_list('block')
 | 
				
			||||||
 | 
					            self.set_default_list('block')
 | 
				
			||||||
 | 
					        self.get_privacy_list('block')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def unblock_group(self, group, contact_list):
 | 
				
			||||||
 | 
					        if not self.privacy_rules_supported:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        if group in self.blocked_groups:
 | 
				
			||||||
 | 
					            self.blocked_groups.remove(group)
 | 
				
			||||||
 | 
					        self.new_blocked_list = []
 | 
				
			||||||
 | 
					        for rule in self.blocked_list:
 | 
				
			||||||
 | 
					            if rule['action'] != 'deny' or rule['type'] != 'group' or \
 | 
				
			||||||
 | 
					            rule['value'] != group:
 | 
				
			||||||
 | 
					                self.new_blocked_list.append(rule)
 | 
				
			||||||
 | 
					        self.set_privacy_list('block', self.new_blocked_list)
 | 
				
			||||||
 | 
					        self.get_privacy_list('block')
 | 
				
			||||||
 | 
					        if len(self.new_blocked_list) == 0:
 | 
				
			||||||
 | 
					            self.blocked_list = []
 | 
				
			||||||
 | 
					            self.blocked_contacts = []
 | 
				
			||||||
 | 
					            self.blocked_groups = []
 | 
				
			||||||
 | 
					            self.set_default_list('')
 | 
				
			||||||
 | 
					            self.set_active_list('')
 | 
				
			||||||
 | 
					            self.del_privacy_list('block')
 | 
				
			||||||
 | 
					        if not gajim.interface.roster.regroup:
 | 
				
			||||||
 | 
					            show = gajim.SHOW_LIST[self.connected]
 | 
				
			||||||
 | 
					        else:   # accounts merged
 | 
				
			||||||
 | 
					            show = helpers.get_global_show()
 | 
				
			||||||
 | 
					        if show == 'invisible':
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        for contact in contact_list:
 | 
				
			||||||
 | 
					            self.send_custom_status(show, self.status, contact.jid)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def send_invisible_presence(self, msg, signed, initial = False):
 | 
					    def send_invisible_presence(self, msg, signed, initial = False):
 | 
				
			||||||
        if not gajim.account_is_connected(self.name):
 | 
					        if not gajim.account_is_connected(self.name):
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3022,46 +3022,21 @@ class RosterWindow:
 | 
				
			||||||
                # user pressed Cancel to change status message dialog
 | 
					                # user pressed Cancel to change status message dialog
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            accounts = []
 | 
					            accounts = []
 | 
				
			||||||
 | 
					            accounts = set(i[1] for i in list_ if gajim.connections[i[1]].\
 | 
				
			||||||
 | 
					                privacy_rules_supported)
 | 
				
			||||||
            if group is None:
 | 
					            if group is None:
 | 
				
			||||||
                for (contact, account) in list_:
 | 
					                for acct in accounts:
 | 
				
			||||||
                    if account not in accounts:
 | 
					                    l_ = [i[0] for i in list_ if i[1] == acct]
 | 
				
			||||||
                        if not gajim.connections[account].\
 | 
					                    gajim.connections[acct].block_contacts(l_, msg)
 | 
				
			||||||
                        privacy_rules_supported:
 | 
					                    for contact in l_:
 | 
				
			||||||
                            continue
 | 
					                        self.draw_contact(contact.jid, acct)
 | 
				
			||||||
                        accounts.append(account)
 | 
					 | 
				
			||||||
                    self.send_status(account, 'offline', msg, to=contact.jid)
 | 
					 | 
				
			||||||
                    new_rule = {'order': '1', 'type': 'jid',
 | 
					 | 
				
			||||||
                        'action': 'deny', 'value' : contact.jid,
 | 
					 | 
				
			||||||
                        'child': ['message', 'iq', 'presence-out']}
 | 
					 | 
				
			||||||
                    gajim.connections[account].blocked_list.append(new_rule)
 | 
					 | 
				
			||||||
                    # needed for draw_contact:
 | 
					 | 
				
			||||||
                    gajim.connections[account].blocked_contacts.append(
 | 
					 | 
				
			||||||
                        contact.jid)
 | 
					 | 
				
			||||||
                    self.draw_contact(contact.jid, account)
 | 
					 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                for (contact, account) in list_:
 | 
					                for acct in accounts:
 | 
				
			||||||
                    if account not in accounts:
 | 
					                    l_ = [i[0] for i in list_ if i[1] == acct]
 | 
				
			||||||
                        if not gajim.connections[account].\
 | 
					                    gajim.connections[acct].block_group(group, l_, msg)
 | 
				
			||||||
                        privacy_rules_supported:
 | 
					                    self.draw_group(group, acct)
 | 
				
			||||||
                            continue
 | 
					                    for contact in l_:
 | 
				
			||||||
                        accounts.append(account)
 | 
					                        self.draw_contact(contact.jid, acct)
 | 
				
			||||||
                        # needed for draw_group:
 | 
					 | 
				
			||||||
                        gajim.connections[account].blocked_groups.append(group)
 | 
					 | 
				
			||||||
                        self.draw_group(group, account)
 | 
					 | 
				
			||||||
                    self.send_status(account, 'offline', msg, to=contact.jid)
 | 
					 | 
				
			||||||
                    self.draw_contact(contact.jid, account)
 | 
					 | 
				
			||||||
                new_rule = {'order': '1', 'type': 'group', 'action': 'deny',
 | 
					 | 
				
			||||||
                    'value' : group, 'child': ['message', 'iq',
 | 
					 | 
				
			||||||
                    'presence-out']}
 | 
					 | 
				
			||||||
                # account is the  same for all when we block a group
 | 
					 | 
				
			||||||
                gajim.connections[list_[0][1]].blocked_list.append(new_rule)
 | 
					 | 
				
			||||||
            for account in accounts:
 | 
					 | 
				
			||||||
                connection = gajim.connections[account]
 | 
					 | 
				
			||||||
                connection.set_privacy_list('block', connection.blocked_list)
 | 
					 | 
				
			||||||
                if len(connection.blocked_list) == 1:
 | 
					 | 
				
			||||||
                    connection.set_active_list('block')
 | 
					 | 
				
			||||||
                    connection.set_default_list('block')
 | 
					 | 
				
			||||||
                connection.get_privacy_list('block')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def _block_it(is_checked=None):
 | 
					        def _block_it(is_checked=None):
 | 
				
			||||||
            if is_checked is not None: # dialog has been shown
 | 
					            if is_checked is not None: # dialog has been shown
 | 
				
			||||||
| 
						 | 
					@ -3086,75 +3061,24 @@ class RosterWindow:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        When clicked on the 'unblock' button in context menu.
 | 
					        When clicked on the 'unblock' button in context menu.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        accounts = []
 | 
					        accounts = set(i[1] for i in list_ if gajim.connections[i[1]].\
 | 
				
			||||||
 | 
					            privacy_rules_supported)
 | 
				
			||||||
        if group is None:
 | 
					        if group is None:
 | 
				
			||||||
            for (contact, account) in list_:
 | 
					            for acct in accounts:
 | 
				
			||||||
                if account not in accounts:
 | 
					                l_ = [i[0] for i in list_ if i[1] == acct]
 | 
				
			||||||
                    if gajim.connections[account].privacy_rules_supported:
 | 
					                gajim.connections[acct].unblock_contacts(l_)
 | 
				
			||||||
                        accounts.append(account)
 | 
					                for contact in l_:
 | 
				
			||||||
                        gajim.connections[account].new_blocked_list = []
 | 
					                    self.draw_contact(contact.jid, acct)
 | 
				
			||||||
                        gajim.connections[account].to_unblock = []
 | 
					 | 
				
			||||||
                        gajim.connections[account].to_unblock.append(
 | 
					 | 
				
			||||||
                            contact.jid)
 | 
					 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
                    gajim.connections[account].to_unblock.append(contact.jid)
 | 
					            for acct in accounts:
 | 
				
			||||||
                # needed for draw_contact:
 | 
					                l_ = [i[0] for i in list_ if i[1] == acct]
 | 
				
			||||||
                if contact.jid in gajim.connections[account].blocked_contacts:
 | 
					                gajim.connections[acct].unblock_group(group, l_)
 | 
				
			||||||
                    gajim.connections[account].blocked_contacts.remove(
 | 
					                self.draw_group(group, acct)
 | 
				
			||||||
                        contact.jid)
 | 
					                for contact in l_:
 | 
				
			||||||
                self.draw_contact(contact.jid, account)
 | 
					                    self.draw_contact(contact.jid, acct)
 | 
				
			||||||
            for account in accounts:
 | 
					        for acct in accounts:
 | 
				
			||||||
                for rule in gajim.connections[account].blocked_list:
 | 
					            if 'privacy_list_block' in gajim.interface.instances[acct]:
 | 
				
			||||||
                    if rule['action'] != 'deny' or rule['type'] != 'jid' \
 | 
					                del gajim.interface.instances[acct]['privacy_list_block']
 | 
				
			||||||
                    or rule['value'] not in \
 | 
					 | 
				
			||||||
                    gajim.connections[account].to_unblock:
 | 
					 | 
				
			||||||
                        gajim.connections[account].new_blocked_list.append(rule)
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            for (contact, account) in list_:
 | 
					 | 
				
			||||||
                if account not in accounts:
 | 
					 | 
				
			||||||
                    if gajim.connections[account].privacy_rules_supported:
 | 
					 | 
				
			||||||
                        accounts.append(account)
 | 
					 | 
				
			||||||
                        # needed for draw_group:
 | 
					 | 
				
			||||||
                        if group in gajim.connections[account].blocked_groups:
 | 
					 | 
				
			||||||
                            gajim.connections[account].blocked_groups.remove(
 | 
					 | 
				
			||||||
                                group)
 | 
					 | 
				
			||||||
                        self.draw_group(group, account)
 | 
					 | 
				
			||||||
                        gajim.connections[account].new_blocked_list = []
 | 
					 | 
				
			||||||
                        for rule in gajim.connections[account].blocked_list:
 | 
					 | 
				
			||||||
                            if rule['action'] != 'deny' or \
 | 
					 | 
				
			||||||
                            rule['type'] != 'group' or rule['value'] != group:
 | 
					 | 
				
			||||||
                                gajim.connections[account].new_blocked_list.\
 | 
					 | 
				
			||||||
                                    append(rule)
 | 
					 | 
				
			||||||
                self.draw_contact(contact.jid, account)
 | 
					 | 
				
			||||||
        for account in accounts:
 | 
					 | 
				
			||||||
            gajim.connections[account].set_privacy_list('block',
 | 
					 | 
				
			||||||
                gajim.connections[account].new_blocked_list)
 | 
					 | 
				
			||||||
            gajim.connections[account].get_privacy_list('block')
 | 
					 | 
				
			||||||
            if len(gajim.connections[account].new_blocked_list) == 0:
 | 
					 | 
				
			||||||
                gajim.connections[account].blocked_list = []
 | 
					 | 
				
			||||||
                gajim.connections[account].blocked_contacts = []
 | 
					 | 
				
			||||||
                gajim.connections[account].blocked_groups = []
 | 
					 | 
				
			||||||
                gajim.connections[account].set_default_list('')
 | 
					 | 
				
			||||||
                gajim.connections[account].set_active_list('')
 | 
					 | 
				
			||||||
                gajim.connections[account].del_privacy_list('block')
 | 
					 | 
				
			||||||
                if 'privacy_list_block' in gajim.interface.instances[account]:
 | 
					 | 
				
			||||||
                    del gajim.interface.instances[account]['privacy_list_block']
 | 
					 | 
				
			||||||
        for (contact, account) in list_:
 | 
					 | 
				
			||||||
            if not self.regroup:
 | 
					 | 
				
			||||||
                show = gajim.SHOW_LIST[gajim.connections[account].connected]
 | 
					 | 
				
			||||||
            else:   # accounts merged
 | 
					 | 
				
			||||||
                show = helpers.get_global_show()
 | 
					 | 
				
			||||||
            if show == 'invisible':
 | 
					 | 
				
			||||||
                # Don't send our presence if we're invisible
 | 
					 | 
				
			||||||
                continue
 | 
					 | 
				
			||||||
            if account not in accounts:
 | 
					 | 
				
			||||||
                accounts.append(account)
 | 
					 | 
				
			||||||
                if gajim.connections[account].privacy_rules_supported:
 | 
					 | 
				
			||||||
                    self.send_status(account, show,
 | 
					 | 
				
			||||||
                        gajim.connections[account].status, to=contact.jid)
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                self.send_status(account, show,
 | 
					 | 
				
			||||||
                    gajim.connections[account].status, to=contact.jid)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def on_rename(self, widget, row_type, jid, account):
 | 
					    def on_rename(self, widget, row_type, jid, account):
 | 
				
			||||||
        # this function is called either by F2 or by Rename menuitem
 | 
					        # this function is called either by F2 or by Rename menuitem
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue