Commit d0ec9812 authored by Philipp Hörist's avatar Philipp Hörist Committed by Philipp Hörist

Fix Zeroconf

- Port pybonjour code from gajim_0.16 branch
- Fix some other small things that broke zeroconf
parent cfd64926
......@@ -1069,7 +1069,10 @@ class ConnectionHandlersBase:
if not jid_to:
reply = True
else:
fjid_to = helpers.parse_jid(str(jid_to))
fjid_to = str(jid_to)
if self.name != 'Local':
# Dont check precis for zeroconf
fjid_to = helpers.parse_jid(str(jid_to))
jid_to = app.get_jid_without_resource(fjid_to)
if jid_to == app.get_jid_from_account(self.name):
reply = True
......
......@@ -885,6 +885,7 @@ class ZeroconfPresenceReceivedEvent(nec.NetworkIncomingEvent):
self.resource = 'local'
self.prio = 0
self.keyID = None
self.idle_time = None
self.timestamp = 0
self.contact_nickname = None
self.avatar_sha = None
......@@ -1528,7 +1529,7 @@ class ZeroconfMessageReceivedEvent(MessageReceivedEvent):
base_network_events = []
def get_jid_resource(self):
self.fjid =self.stanza.getFrom()
self.fjid = str(self.stanza.getFrom())
if self.fjid is None:
for key in self.conn.connection.zeroconf.contacts:
......@@ -1833,7 +1834,12 @@ class MessageErrorEvent(nec.NetworkIncomingEvent, HelperEvent):
name = 'message-error'
base_network_events = []
def init(self):
self.zeroconf = False
def generate(self):
if self.zeroconf:
return True
self.get_id()
#only alert for errors of explicitly sent messages (see https://trac.gajim.org/ticket/8222)
if self.id_ in self.conn.sent_message_ids:
......
......@@ -442,7 +442,7 @@ class P2PConnection(IdleObject, PlugIn):
if self.state <= 0:
return
r = packet
r = str(packet).encode('utf-8')
if now:
self.sendqueue.insert(0, (r, is_message))
......@@ -532,6 +532,9 @@ class P2PConnection(IdleObject, PlugIn):
if self.state < 0:
return
received = received.decode('utf-8')
if self.on_receive:
if self._owner.sock_type == TYPE_CLIENT:
self.set_timeout(ACTIVITY_TIMEOUT_SECONDS)
......@@ -609,7 +612,8 @@ class P2PConnection(IdleObject, PlugIn):
if self.sent_data and self.sent_data.strip():
log.debug('sent: %s' % self.sent_data)
if hasattr(self._owner, 'Dispatcher'):
self._owner.Dispatcher.Event('', DATA_SENT, self.sent_data)
self._owner.Dispatcher.Event(
'', DATA_SENT, self.sent_data.decode('utf-8'))
self.sent_data = None
if self.buff_is_message:
self._owner.on_message_sent(self.fd)
......@@ -662,7 +666,7 @@ class ClientZeroconf:
def resolve_all(self):
if self.zeroconf:
self.zeroconf.resolve_all()
return self.zeroconf.resolve_all()
def reannounce(self, txt):
self.remove_announce()
......@@ -711,7 +715,7 @@ class ClientZeroconf:
self.disconnect()
def kill_all_connections(self):
for connection in self.connections.values():
for connection in list(self.connections.values()):
connection.force_disconnect()
def add_connection(self, connection, ip, port, recipient):
......
......@@ -69,6 +69,8 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
CommonConnection.__init__(self, name)
self.is_zeroconf = True
app.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
self._nec_message_outgoing)
app.ged.register_event_handler('stanza-message-outgoing', ged.OUT_CORE,
self._nec_stanza_message_outgoing)
......@@ -107,7 +109,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
def check_jid(self, jid):
return jid
def get_own_jid(self):
def get_own_jid(self, *args, **kwargs):
return nbxmpp.JID(self.username + '@' + self.host)
def reconnect(self):
......@@ -123,7 +125,9 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
def _on_resolve_timeout(self):
if self.connected:
self.connection.resolve_all()
if not self.connection.resolve_all():
self._on_disconnected()
return False
diffs = self.roster.getDiffs()
for key in diffs:
self.roster.setItem(key)
......@@ -205,7 +209,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
else: # result is None
app.nec.push_incoming_event(ConnectionLostEvent(None,
conn=self, title=_('Could not start local service'),
msg=_('Please check if avahi-daemon is running.')))
msg=_('Please check if avahi/bonjour-daemon is running.')))
self.disconnect()
return
else:
......@@ -321,23 +325,24 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
title=_('Could not change status of account "%s"') % self.name,
msg=_('Please check if avahi-daemon is running.')))
def _nec_message_outgoing(self, obj):
if obj.account != self.name:
return
self._prepare_message(obj)
def _nec_stanza_message_outgoing(self, obj):
if obj.conn.name != self.name:
return
def on_send_ok(stanza_id):
app.nec.push_incoming_event(MessageSentEvent(None, conn=self,
jid=obj.jid, message=obj.message, keyID=obj.keyID,
automatic_message=obj.automatic_message, chatstate=None,
stanza_id=stanza_id))
app.nec.push_incoming_event(MessageSentEvent(None, **vars(obj)))
self.log_message(obj, obj.jid)
def on_send_not_ok(reason):
reason += ' ' + _('Your message could not be sent.')
app.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
fjid=obj.jid, error_code=-1, error_msg=reason, msg=None,
time_=None, session=obj.session))
app.nec.push_incoming_event(MessageErrorEvent(
None, conn=self, fjid=obj.jid, error_code=-1, error_msg=reason,
msg=None, time_=None, session=obj.session, zeroconf=True))
# Dont propagate event
return True
......@@ -347,10 +352,12 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
if ret == -1:
# Contact Offline
app.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
fjid=obj.jid, error_code=-1, error_msg=_(
'Contact is offline. Your message could not be sent.'),
msg=None, time_=None, session=obj.session))
error_message = _(
'Contact is offline. Your message could not be sent.')
app.nec.push_incoming_event(MessageErrorEvent(
None, conn=self, fjid=obj.jid, error_code=-1,
error_msg=error_message, msg=None, time_=None,
session=obj.session, zeroconf=True))
# Dont propagate event
return True
......@@ -369,9 +376,12 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
thread_id = data[1]
frm = data[0]
session = self.get_or_create_session(frm, thread_id)
error_message = _(
'Connection to host could not be established: '
'Timeout while sending data.')
app.nec.push_incoming_event(MessageErrorEvent(
None, conn=self, fjid=frm, error_code=-1, error_msg=_(
'Connection to host could not be established: Timeout while '
'sending data.'), msg=None, time_=None, session=session))
None, conn=self, fjid=frm, error_code=-1,
error_msg=error_message, msg=None, time_=None,
session=session, zeroconf=True))
# END ConnectionZeroconf
......@@ -160,7 +160,7 @@ class Zeroconf:
# we don't want to see ourselves in the list
if name != self.name:
resolved_info = [(interface, protocol, host, aprotocol, address, port)]
resolved_info = [(interface, protocol, host, aprotocol, address, int(port))]
if name in self.contacts:
# Decide whether to try to merge with existing resolved info:
old_name, old_domain, old_resolved_info, old_bare_name, old_txt = self.contacts[name]
......@@ -186,7 +186,7 @@ class Zeroconf:
# gajim instance on the same machine,
# it will be used when we get a new name.
self.invalid_self_contact[name] = (name, domain,
(interface, protocol, host, aprotocol, address, port),
(interface, protocol, host, aprotocol, address, int(port)),
bare_name, txt)
......@@ -445,7 +445,7 @@ class Zeroconf:
# refresh txt data of all contacts manually (no callback available)
def resolve_all(self):
if not self.connected:
return
return False
for val in self.contacts.values():
# get txt data from last recorded resolved info
# TODO: Better try to get it from last IPv6 mDNS, then last IPv4?
......@@ -456,6 +456,8 @@ class Zeroconf:
reply_handler=self.service_resolved_all_callback,
error_handler=self.error_callback)
return True
def get_contacts(self):
return self.contacts
......
This diff is collapsed.
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