use NEC to handle roster set events

This commit is contained in:
Yann Leboulanger 2010-08-23 14:35:03 +02:00
parent 06da4df46f
commit df7096167f
3 changed files with 78 additions and 53 deletions

View File

@ -1088,6 +1088,8 @@ ConnectionJingle, ConnectionIBBytestream):
self._nec_time_request_received)
gajim.ged.register_event_handler('time-revised-request-received',
ged.CORE, self._nec_time_revised_request_received)
gajim.ged.register_event_handler('roster-set-received',
ged.CORE, self._nec_roster_set_received)
def build_http_auth_answer(self, iq_obj, answer):
if not self.connection or self.connected < 2:
@ -1235,33 +1237,23 @@ ConnectionJingle, ConnectionIBBytestream):
def _rosterSetCB(self, con, iq_obj):
log.debug('rosterSetCB')
version = iq_obj.getTagAttr('query', 'ver')
for item in iq_obj.getTag('query').getChildren():
try:
jid = helpers.parse_jid(item.getAttr('jid'))
except common.helpers.InvalidFormat:
log.warn('Invalid JID: %s, ignoring it' % item.getAttr('jid'))
continue
name = item.getAttr('name')
sub = item.getAttr('subscription')
ask = item.getAttr('ask')
groups = []
for group in item.getTags('group'):
groups.append(group.getData())
self.dispatch('ROSTER_INFO', (jid, name, sub, ask, groups))
account_jid = gajim.get_jid_from_account(self.name)
gajim.logger.add_or_update_contact(account_jid, jid, name, sub, ask,
groups)
if version:
gajim.config.set_per('accounts', self.name, 'roster_version',
version)
if not self.connection or self.connected < 2:
raise common.xmpp.NodeProcessed
reply = common.xmpp.Iq(typ='result', attrs={'id': iq_obj.getID()},
to=iq_obj.getFrom(), frm=iq_obj.getTo(), xmlns=None)
self.connection.send(reply)
gajim.nec.push_incoming_event(RosterSetReceivedEvent(None, conn=self,
iq_obj=iq_obj))
raise common.xmpp.NodeProcessed
def _nec_roster_set_received(self, obj):
for jid in obj.items:
item = obj.items[jid]
gajim.nec.push_incoming_event(RosterInfoEvent(None, conn=self,
jid=jid, nickname=item['name'], sub=item['sub'], ask=item['ask'],
groups=item['groups']))
account_jid = gajim.get_jid_from_account(self.name)
gajim.logger.add_or_update_contact(account_jid, jid, item['name'],
item['sub'], item['ask'], item['groups'])
if obj.version:
gajim.config.set_per('accounts', self.name, 'roster_version',
obj.version)
def _VersionCB(self, con, iq_obj):
log.debug('VersionCB')
if not self.connection or self.connected < 2:
@ -2681,4 +2673,36 @@ class TimeRequestEvent(nec.NetworkIncomingEvent):
class TimeRevisedRequestEvent(nec.NetworkIncomingEvent):
name = 'time-revised-request-received'
base_network_events = []
base_network_events = []
class RosterSetReceivedEvent(nec.NetworkIncomingEvent):
name = 'roster-set-received'
base_network_events = []
def generate(self):
self.version = self.iq_obj.getTagAttr('query', 'ver')
self.items = {}
for item in self.iq_obj.getTag('query').getChildren():
try:
jid = helpers.parse_jid(item.getAttr('jid'))
except common.helpers.InvalidFormat:
log.warn('Invalid JID: %s, ignoring it' % item.getAttr('jid'))
continue
name = item.getAttr('name')
sub = item.getAttr('subscription')
ask = item.getAttr('ask')
groups = []
for group in item.getTags('group'):
groups.append(group.getData())
self.items[jid] = {'name': name, 'sub': sub, 'ask': ask,
'groups': groups}
if self.conn.connection and self.conn.connected > 1:
reply = common.xmpp.Iq(typ='result',
attrs={'id': self.iq_obj.getID()}, to=self.iq_obj.getFrom(),
frm=self.iq_obj.getTo(), xmlns=None)
self.conn.connection.send(reply)
return True
class RosterInfoEvent(nec.NetworkIncomingEvent):
name = 'roster-info'
base_network_events = []

View File

@ -1211,52 +1211,47 @@ class Interface:
_('Password Required'), text, _('Save password'), ok_handler=on_ok,
cancel_handler=on_cancel)
def handle_event_roster_info(self, account, array):
def handle_event_roster_info(self, obj):
#('ROSTER_INFO', account, (jid, name, sub, ask, groups))
jid = array[0]
name = array[1]
sub = array[2]
ask = array[3]
groups = array[4]
contacts = gajim.contacts.get_contacts(account, jid)
if (not sub or sub == 'none') and (not ask or ask == 'none') and \
not name and not groups:
account = obj.conn.name
contacts = gajim.contacts.get_contacts(account, obj.jid)
if (not obj.sub or obj.sub == 'none') and \
(not obj.ask or obj.ask == 'none') and not obj.nickname and \
not obj.groups:
# contact removed us.
if contacts:
self.roster.remove_contact(jid, account, backend=True)
self.roster.remove_contact(obj.jid, account, backend=True)
return
elif not contacts:
if sub == 'remove':
if obj.sub == 'remove':
return
# Add new contact to roster
contact = gajim.contacts.create_contact(jid=jid, account=account,
name=name, groups=groups, show='offline', sub=sub, ask=ask)
contact = gajim.contacts.create_contact(jid=obj.jid,
account=account, name=obj.nickname, groups=obj.groups,
show='offline', sub=obj.sub, ask=obj.ask)
gajim.contacts.add_contact(account, contact)
self.roster.add_contact(jid, account)
self.roster.add_contact(obj.jid, account)
else:
# If contact has changed (sub, ask or group) update roster
# Mind about observer status changes:
# According to xep 0162, a contact is not an observer anymore when
# we asked for auth, so also remove him if ask changed
old_groups = contacts[0].groups
if contacts[0].sub != sub or contacts[0].ask != ask\
or old_groups != groups:
if contacts[0].sub != obj.sub or contacts[0].ask != obj.ask\
or old_groups != obj.groups:
# c.get_shown_groups() has changed. Reflect that in
# roster_winodow
self.roster.remove_contact(jid, account, force=True)
self.roster.remove_contact(obj.jid, account, force=True)
for contact in contacts:
contact.name = name or ''
contact.sub = sub
contact.ask = ask
contact.groups = groups or []
self.roster.add_contact(jid, account)
contact.name = obj.nickname or ''
contact.sub = obj.sub
contact.ask = obj.ask
contact.groups = obj.groups or []
self.roster.add_contact(obj.jid, account)
# Refilter and update old groups
for group in old_groups:
self.roster.draw_group(group, account)
self.roster.draw_contact(jid, account)
if self.remote_ctrl:
self.remote_ctrl.raise_signal('RosterInfo', (account, array))
self.roster.draw_contact(obj.jid, account)
def handle_event_bookmarks(self, account, bms):
# ('BOOKMARKS', account, [{name,jid,autojoin,password,nick}, {}])
@ -2116,7 +2111,6 @@ class Interface:
'GC_PASSWORD_REQUIRED': [self.handle_event_gc_password_required],
'GC_ERROR': [self.handle_event_gc_error],
'BAD_PASSPHRASE': [self.handle_event_bad_passphrase],
'ROSTER_INFO': [self.handle_event_roster_info],
'BOOKMARKS': [self.handle_event_bookmarks],
'CON_TYPE': [self.handle_event_con_type],
'CONNECTION_LOST': [self.handle_event_connection_lost],
@ -2172,6 +2166,7 @@ class Interface:
'last-result-received': [self.handle_event_last_status_time],
'roster-item-exchange-received': \
[self.handle_event_roster_item_exchange],
'roster-info': [self.handle_event_roster_info],
}
def register_core_handlers(self):

View File

@ -113,6 +113,8 @@ class Remote:
self.on_time)
gajim.ged.register_event_handler('gmail-nofify', ged.POSTGUI,
self.on_gmail_notify)
gajim.ged.register_event_handler('roster-info', ged.POSTGUI,
self.on_roster_info)
def on_last_status_time(self, obj):
self.raise_signal('LastStatusTime', (obj.conn.name, [
@ -130,6 +132,10 @@ class Remote:
self.raise_signal('NewGmail', (obj.conn.name, [obj.jid, obj.newmsgs,
obj.gmail_messages_list]))
def on_roster_info(self, obj):
self.raise_signal('RosterInfo', (obj.conn.name, [obj.jid, obj.name,
obj.sub, obj.ask, obj.groups]))
def raise_signal(self, signal, arg):
if self.signal_object:
try: