From d121bbf4ab6af6e7d47ff805b1011b90e6385af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Wed, 2 Jan 2019 16:10:30 +0100 Subject: [PATCH] Refactor Zeroconf presence --- gajim/common/connection_handlers_events.py | 22 ---- gajim/common/modules/presence.py | 1 + gajim/common/zeroconf/connection_zeroconf.py | 100 +++++++++++++++++-- 3 files changed, 90 insertions(+), 33 deletions(-) diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index 2ff99c1c2..7f9db35a8 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -144,28 +144,6 @@ class StreamConflictReceivedEvent(nec.NetworkIncomingEvent): class PresenceReceivedEvent(nec.NetworkIncomingEvent): name = 'presence-received' -class ZeroconfPresenceReceivedEvent(nec.NetworkIncomingEvent): - name = 'presence-received' - - def generate(self): - self.jid, self.resource = app.get_room_and_nick_from_fjid(self.fjid) - self.resource = 'local' - self.prio = 0 - self.keyID = None - self.idle_time = None - self.timestamp = 0 - self.avatar_sha = None - self.need_add_in_roster = False - if self.show == 'offline': - self.ptype = 'unavailable' - else: - self.ptype = None - self.user_nick = '' - self.errcode = None - self.errmsg = '' - self.popup = False # Do we want to open chat window ? - return True - class OurShowEvent(nec.NetworkIncomingEvent): name = 'our-show' diff --git a/gajim/common/modules/presence.py b/gajim/common/modules/presence.py index f568acbce..b38256839 100644 --- a/gajim/common/modules/presence.py +++ b/gajim/common/modules/presence.py @@ -136,6 +136,7 @@ class Presence: raise nbxmpp.NodeProcessed def _update_contact(self, event, properties): + # Note: A similar method also exists in connection_zeroconf jid = properties.jid.getBare() resource = properties.jid.getResource() diff --git a/gajim/common/zeroconf/connection_zeroconf.py b/gajim/common/zeroconf/connection_zeroconf.py index 761d3bd8d..9be31535e 100644 --- a/gajim/common/zeroconf/connection_zeroconf.py +++ b/gajim/common/zeroconf/connection_zeroconf.py @@ -131,9 +131,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): nickname=self.roster.getName(key), sub='both', ask='no', groups=self.roster.getGroups(key), avatar_sha=None)) - app.nec.push_incoming_event(ZeroconfPresenceReceivedEvent( - None, conn=self, fjid=key, show=self.roster.getStatus(key), - status=self.roster.getMessage(key))) + self._on_presence(key) #XXX open chat windows don't get refreshed (full name), add that return self.call_resolve_timeout @@ -145,16 +143,98 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): nickname=self.roster.getName(jid), sub='both', ask='no', groups=self.roster.getGroups(jid), avatar_sha=None)) - app.nec.push_incoming_event(ZeroconfPresenceReceivedEvent( - None, conn=self, fjid=jid, show=self.roster.getStatus(jid), - status=self.roster.getMessage(jid))) + self._on_presence(jid) def _on_remove_service(self, jid): self.roster.delItem(jid) # 'NOTIFY' (account, (jid, status, status message, resource, priority, # keyID, timestamp)) - app.nec.push_incoming_event(ZeroconfPresenceReceivedEvent( - None, conn=self, fjid=jid, show='offline', status='')) + self._on_presence(jid, show='offline', status='') + + def _on_presence(self, jid, show=None, status=None): + if status is None: + status = self.roster.getMessage(jid) + if show is None: + show = self.roster.getStatus(jid) + + ptype = 'unavailable' if show == 'offline' else None + + event_attrs = { + 'conn': self, + 'keyID': None, + 'prio': 0, + 'need_add_in_roster': False, + 'popup': False, + 'ptype': ptype, + 'jid': jid, + 'resource': 'local', + 'id_': None, + 'fjid': jid, + 'timestamp': 0, + 'avatar_sha': None, + 'user_nick': '', + 'idle_time': None, + 'show': show, + 'new_show': show, + 'old_show': 0, + 'status': status, + 'contact_list': [], + 'contact': None, + } + + event_ = NetworkEvent('presence-received', **event_attrs) + + self._update_contact(event_) + + app.nec.push_incoming_event(event_) + + def _update_contact(self, event): + jid = event.jid + + status_strings = ['offline', 'error', 'online', 'chat', 'away', + 'xa', 'dnd', 'invisible'] + + event.new_show = status_strings.index(event.show) + + contact = app.contacts.get_contact_strict(self.name, jid, '') + if contact is None: + contact = app.contacts.get_contact_strict(self.name, jid, 'local') + + if contact.show in status_strings: + event.old_show = status_strings.index(contact.show) + + # Update contact with presence data + contact.resource = 'local' + contact.show = event.show + contact.status = event.status + contact.priority = event.prio + attached_keys = app.config.get_per('accounts', self.name, + 'attached_gpg_keys').split() + if jid in attached_keys: + contact.keyID = attached_keys[attached_keys.index(jid) + 1] + else: + # Do not override assigned key + contact.keyID = event.keyID + contact.idle_time = event.idle_time + + event.contact = contact + + # It's not an agent + if event.old_show == 0 and event.new_show > 1: + if not jid in app.newly_added[self.name]: + app.newly_added[self.name].append(jid) + if jid in app.to_be_removed[self.name]: + app.to_be_removed[self.name].remove(jid) + elif event.old_show > 1 and event.new_show == 0 and self.connected > 1: + if not jid in app.to_be_removed[self.name]: + app.to_be_removed[self.name].append(jid) + if jid in app.newly_added[self.name]: + app.newly_added[self.name].remove(jid) + + if event.ptype == 'unavailable': + # TODO: This causes problems when another + # resource signs off! + self.stop_all_active_file_transfers(contact) def _on_name_conflictCB(self, alt_name): self.disconnect() @@ -208,9 +288,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): nickname=self.roster.getName(jid), sub='both', ask='no', groups=self.roster.getGroups(jid), avatar_sha=None)) - app.nec.push_incoming_event(ZeroconfPresenceReceivedEvent( - None, conn=self, fjid=jid, show=self.roster.getStatus(jid), - status=self.roster.getMessage(jid))) + self._on_presence(jid) self.connected = STATUS_LIST.index(show)