2007-08-06 23:46:51 +02:00
|
|
|
## common/zeroconf/zeroconf.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2007-08-06 23:46:51 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2007-08-06 23:46:51 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2007-08-06 23:46:51 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
2007-08-06 23:46:51 +02:00
|
|
|
|
2009-07-22 09:49:56 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.z.zeroconf_avahi')
|
2007-08-06 23:46:51 +02:00
|
|
|
|
|
|
|
try:
|
2010-12-01 18:26:48 +01:00
|
|
|
import dbus.exceptions
|
2013-01-05 00:03:36 +01:00
|
|
|
except ImportError:
|
2010-02-08 15:08:40 +01:00
|
|
|
pass
|
2007-08-06 23:46:51 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.zeroconf.zeroconf import Constant, ConstantRI
|
2018-05-23 00:12:43 +02:00
|
|
|
from gajim.common.zeroconf.zeroconf_avahi_const import *
|
2007-08-06 23:46:51 +02:00
|
|
|
|
|
|
|
class Zeroconf:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, new_serviceCB, remove_serviceCB, name_conflictCB,
|
|
|
|
disconnected_CB, error_CB, name, host, port):
|
|
|
|
self.domain = None # specific domain to browse
|
|
|
|
self.stype = '_presence._tcp'
|
|
|
|
self.port = port # listening port that gets announced
|
|
|
|
self.username = name
|
|
|
|
self.host = host
|
|
|
|
self.txt = {} # service data
|
|
|
|
|
|
|
|
#XXX these CBs should be set to None when we destroy the object
|
|
|
|
# (go offline), because they create a circular reference
|
|
|
|
self.new_serviceCB = new_serviceCB
|
|
|
|
self.remove_serviceCB = remove_serviceCB
|
|
|
|
self.name_conflictCB = name_conflictCB
|
|
|
|
self.disconnected_CB = disconnected_CB
|
|
|
|
self.error_CB = error_CB
|
|
|
|
|
|
|
|
self.service_browser = None
|
|
|
|
self.domain_browser = None
|
|
|
|
self.bus = None
|
|
|
|
self.server = None
|
|
|
|
self.contacts = {} # all current local contacts with data
|
|
|
|
self.entrygroup = None
|
|
|
|
self.connected = False
|
|
|
|
self.announced = False
|
|
|
|
self.invalid_self_contact = {}
|
|
|
|
|
|
|
|
|
|
|
|
## handlers for dbus callbacks
|
|
|
|
def entrygroup_commit_error_CB(self, err):
|
|
|
|
# left blank for possible later usage
|
|
|
|
pass
|
|
|
|
|
|
|
|
def error_callback1(self, err):
|
|
|
|
log.debug('Error while resolving: ' + str(err))
|
|
|
|
|
|
|
|
def error_callback(self, err):
|
|
|
|
log.debug(str(err))
|
|
|
|
# timeouts are non-critical
|
|
|
|
if str(err) != 'Timeout reached':
|
|
|
|
self.disconnect()
|
|
|
|
self.disconnected_CB()
|
|
|
|
|
|
|
|
def new_service_callback(self, interface, protocol, name, stype, domain,
|
|
|
|
flags):
|
|
|
|
log.debug('Found service %s in domain %s on %i.%i.' % (name, domain,
|
|
|
|
interface, protocol))
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
|
|
|
|
# synchronous resolving
|
|
|
|
self.server.ResolveService( int(interface), int(protocol), name, stype,
|
2018-05-23 00:12:43 +02:00
|
|
|
domain, Protocol.UNSPEC, dbus.UInt32(0),
|
2010-02-08 15:08:40 +01:00
|
|
|
reply_handler=self.service_resolved_callback,
|
|
|
|
error_handler=self.error_callback1)
|
|
|
|
|
|
|
|
def remove_service_callback(self, interface, protocol, name, stype, domain,
|
|
|
|
flags):
|
|
|
|
log.debug('Service %s in domain %s on %i.%i disappeared.' % (name,
|
|
|
|
domain, interface, protocol))
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
if name != self.name:
|
|
|
|
for key in self.contacts.keys():
|
2015-04-05 02:25:10 +02:00
|
|
|
val = self.contacts[key]
|
2017-02-07 21:18:41 +01:00
|
|
|
if val[Constant.BARE_NAME] == name:
|
2015-04-05 02:25:10 +02:00
|
|
|
# try to reduce instead of delete first
|
2017-02-07 21:18:41 +01:00
|
|
|
resolved_info = val[Constant.RESOLVED_INFO]
|
2015-04-05 02:25:10 +02:00
|
|
|
if len(resolved_info) > 1:
|
|
|
|
for i in range(len(resolved_info)):
|
2017-02-07 21:18:41 +01:00
|
|
|
if resolved_info[i][ConstantRI.INTERFACE] == interface and resolved_info[i][ConstantRI.PROTOCOL] == protocol:
|
|
|
|
del self.contacts[key][Constant.RESOLVED_INFO][i]
|
2015-04-05 02:25:10 +02:00
|
|
|
# if still something left, don't remove
|
2017-02-07 21:18:41 +01:00
|
|
|
if len(self.contacts[key][Constant.RESOLVED_INFO]) > 1: return
|
2010-02-08 15:08:40 +01:00
|
|
|
del self.contacts[key]
|
|
|
|
self.remove_serviceCB(key)
|
|
|
|
return
|
|
|
|
|
|
|
|
def new_service_type(self, interface, protocol, stype, domain, flags):
|
|
|
|
# Are we already browsing this domain for this type?
|
|
|
|
if self.service_browser:
|
|
|
|
return
|
|
|
|
|
|
|
|
object_path = self.server.ServiceBrowserNew(interface, protocol, \
|
|
|
|
stype, domain, dbus.UInt32(0))
|
|
|
|
|
|
|
|
self.service_browser = dbus.Interface(self.bus.get_object(
|
2018-05-23 00:12:43 +02:00
|
|
|
DBUS_NAME, object_path),
|
|
|
|
DBUS_INTERFACE_SERVICE_BROWSER)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.service_browser.connect_to_signal('ItemNew',
|
|
|
|
self.new_service_callback)
|
|
|
|
self.service_browser.connect_to_signal('ItemRemove',
|
|
|
|
self.remove_service_callback)
|
|
|
|
self.service_browser.connect_to_signal('Failure', self.error_callback)
|
|
|
|
|
2010-02-10 19:24:11 +01:00
|
|
|
def new_domain_callback(self, interface, protocol, domain, flags):
|
2010-02-08 15:08:40 +01:00
|
|
|
if domain != 'local':
|
|
|
|
self.browse_domain(interface, protocol, domain)
|
|
|
|
|
|
|
|
def txt_array_to_dict(self, txt_array):
|
|
|
|
txt_dict = {}
|
2018-05-27 21:03:42 +02:00
|
|
|
for array in txt_array:
|
|
|
|
item = bytes(array)
|
|
|
|
item = item.decode('utf-8')
|
|
|
|
item = item.split('=', 1)
|
|
|
|
|
|
|
|
if item[0] and (item[0] not in txt_dict):
|
|
|
|
if len(item) == 1:
|
|
|
|
txt_dict[item[0]] = None
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2018-05-27 21:03:42 +02:00
|
|
|
txt_dict[item[0]] = item[1]
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return txt_dict
|
|
|
|
|
2018-05-23 00:12:43 +02:00
|
|
|
@staticmethod
|
|
|
|
def string_to_byte_array(s):
|
|
|
|
r = []
|
|
|
|
|
|
|
|
for c in s:
|
|
|
|
r.append(dbus.Byte(c))
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
def dict_to_txt_array(self, txt_dict):
|
2018-05-27 21:03:42 +02:00
|
|
|
array = []
|
2018-05-23 00:12:43 +02:00
|
|
|
|
2018-05-27 21:03:42 +02:00
|
|
|
for k, v in txt_dict.items():
|
|
|
|
item = '%s=%s' % (k, v)
|
|
|
|
item = item.encode('utf-8')
|
|
|
|
array.append(self.string_to_byte_array(item))
|
2018-05-23 00:12:43 +02:00
|
|
|
|
2018-05-27 21:03:42 +02:00
|
|
|
return array
|
2018-05-23 00:12:43 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def service_resolved_callback(self, interface, protocol, name, stype, domain,
|
|
|
|
host, aprotocol, address, port, txt, flags):
|
|
|
|
log.debug('Service data for service %s in domain %s on %i.%i:'
|
|
|
|
% (name, domain, interface, protocol))
|
|
|
|
log.debug('Host %s (%s), port %i, TXT data: %s' % (host, address,
|
|
|
|
port, self.txt_array_to_dict(txt)))
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
bare_name = name
|
|
|
|
if name.find('@') == -1:
|
|
|
|
name = name + '@' + name
|
|
|
|
|
|
|
|
# we don't want to see ourselves in the list
|
|
|
|
if name != self.name:
|
2018-05-20 17:03:07 +02:00
|
|
|
resolved_info = [(interface, protocol, host, aprotocol, address, int(port))]
|
2015-04-05 02:25:10 +02:00
|
|
|
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]
|
|
|
|
if name == old_name and domain == old_domain and bare_name == old_bare_name:
|
|
|
|
# Seems similar enough, try to merge resolved info:
|
|
|
|
for i in range(len(old_resolved_info)):
|
|
|
|
# for now, keep a single record for each (interface, protocol) pair
|
|
|
|
#
|
|
|
|
# Note that, theoretically, we could both get IPv4 and
|
|
|
|
# IPv6 aprotocol responses via the same protocol,
|
|
|
|
# so this probably needs to be revised again.
|
|
|
|
if old_resolved_info[i][0:2] == (interface, protocol):
|
|
|
|
log.debug('Deleting resolved info for interface %i, protocol %i, host %s, aprotocol %i, address %s, port %i' % old_resolved_info[i])
|
2016-11-05 16:06:08 +01:00
|
|
|
del old_resolved_info[i]
|
2015-04-05 02:25:10 +02:00
|
|
|
break
|
|
|
|
resolved_info = resolved_info + old_resolved_info
|
|
|
|
log.debug('Collected resolved info is now: %s' % (resolved_info,))
|
|
|
|
self.contacts[name] = (name, domain, resolved_info, bare_name, txt)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.new_serviceCB(name)
|
|
|
|
else:
|
|
|
|
# remember data
|
|
|
|
# In case this is not our own record but of another
|
|
|
|
# gajim instance on the same machine,
|
|
|
|
# it will be used when we get a new name.
|
2015-04-05 02:25:10 +02:00
|
|
|
self.invalid_self_contact[name] = (name, domain,
|
2018-05-20 17:03:07 +02:00
|
|
|
(interface, protocol, host, aprotocol, address, int(port)),
|
2015-04-05 02:25:10 +02:00
|
|
|
bare_name, txt)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
# different handler when resolving all contacts
|
|
|
|
def service_resolved_all_callback(self, interface, protocol, name, stype,
|
|
|
|
domain, host, aprotocol, address, port, txt, flags):
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
bare_name = name
|
|
|
|
if name.find('@') == -1:
|
|
|
|
name = name + '@' + name
|
2015-04-05 02:25:10 +02:00
|
|
|
# update TXT data only, as intended according to resolve_all comment
|
|
|
|
old_contact = self.contacts[name]
|
2017-02-07 21:18:41 +01:00
|
|
|
self.contacts[name] = old_contact[0:Constant.TXT] + (txt,) + old_contact[Constant.TXT+1:]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def service_added_callback(self):
|
|
|
|
log.debug('Service successfully added')
|
|
|
|
|
|
|
|
def service_committed_callback(self):
|
|
|
|
log.debug('Service successfully committed')
|
|
|
|
|
|
|
|
def service_updated_callback(self):
|
|
|
|
log.debug('Service successfully updated')
|
|
|
|
|
|
|
|
def service_add_fail_callback(self, err):
|
|
|
|
log.debug('Error while adding service. %s' % str(err))
|
|
|
|
if 'Local name collision' in str(err):
|
|
|
|
alternative_name = self.server.GetAlternativeServiceName(self.username)
|
|
|
|
self.name_conflictCB(alternative_name)
|
|
|
|
return
|
|
|
|
self.error_CB(_('Error while adding service. %s') % str(err))
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def server_state_changed_callback(self, state, error):
|
|
|
|
log.debug('server state changed to %s' % state)
|
2018-05-23 00:12:43 +02:00
|
|
|
if state == ServerState.RUNNING:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.create_service()
|
2018-05-23 00:12:43 +02:00
|
|
|
elif state in (ServerState.COLLISION,
|
|
|
|
ServerState.REGISTERING):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
|
|
|
self.entrygroup.Reset()
|
|
|
|
|
|
|
|
def entrygroup_state_changed_callback(self, state, error):
|
|
|
|
# the name is already present, so recreate
|
2018-05-23 00:12:43 +02:00
|
|
|
if state == EntryGroup.COLLISION:
|
2010-02-08 15:08:40 +01:00
|
|
|
log.debug('zeroconf.py: local name collision')
|
|
|
|
self.service_add_fail_callback('Local name collision')
|
2018-05-23 00:12:43 +02:00
|
|
|
elif state == EntryGroup.FAILURE:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
|
|
|
self.entrygroup.Reset()
|
|
|
|
log.debug('zeroconf.py: ENTRY_GROUP_FAILURE reached(that'
|
|
|
|
' should not happen)')
|
|
|
|
|
|
|
|
# make zeroconf-valid names
|
|
|
|
def replace_show(self, show):
|
|
|
|
if show in ['chat', 'online', '']:
|
|
|
|
return 'avail'
|
|
|
|
elif show == 'xa':
|
|
|
|
return 'away'
|
|
|
|
return show
|
|
|
|
|
|
|
|
def avahi_txt(self):
|
2018-05-23 00:12:43 +02:00
|
|
|
return self.dict_to_txt_array(self.txt)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def create_service(self):
|
|
|
|
try:
|
|
|
|
if not self.entrygroup:
|
|
|
|
# create an EntryGroup for publishing
|
|
|
|
self.entrygroup = dbus.Interface(self.bus.get_object(
|
2018-05-23 00:12:43 +02:00
|
|
|
DBUS_NAME, self.server.EntryGroupNew()),
|
|
|
|
DBUS_INTERFACE_ENTRY_GROUP)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.entrygroup.connect_to_signal('StateChanged',
|
|
|
|
self.entrygroup_state_changed_callback)
|
|
|
|
|
|
|
|
txt = {}
|
|
|
|
|
|
|
|
# remove empty keys
|
2013-01-02 13:54:02 +01:00
|
|
|
for key, val in self.txt.items():
|
2010-02-08 15:08:40 +01:00
|
|
|
if val:
|
|
|
|
txt[key] = val
|
|
|
|
|
|
|
|
txt['port.p2pj'] = self.port
|
|
|
|
txt['version'] = 1
|
|
|
|
txt['txtvers'] = 1
|
|
|
|
|
|
|
|
# replace gajim's show messages with compatible ones
|
|
|
|
if 'status' in self.txt:
|
|
|
|
txt['status'] = self.replace_show(self.txt['status'])
|
|
|
|
else:
|
|
|
|
txt['status'] = 'avail'
|
|
|
|
|
|
|
|
self.txt = txt
|
|
|
|
log.debug('Publishing service %s of type %s' % (self.name,
|
|
|
|
self.stype))
|
2018-05-23 00:12:43 +02:00
|
|
|
self.entrygroup.AddService(Interface.UNSPEC,
|
|
|
|
Protocol.UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
2010-02-08 15:08:40 +01:00
|
|
|
'', dbus.UInt16(self.port), self.avahi_txt(),
|
|
|
|
reply_handler=self.service_added_callback,
|
|
|
|
error_handler=self.service_add_fail_callback)
|
|
|
|
|
|
|
|
self.entrygroup.Commit(reply_handler=self.service_committed_callback,
|
|
|
|
error_handler=self.entrygroup_commit_error_CB)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2013-01-01 23:18:36 +01:00
|
|
|
except dbus.DBusException as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
log.debug(str(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def announce(self):
|
|
|
|
if not self.connected:
|
|
|
|
return False
|
|
|
|
|
|
|
|
state = self.server.GetState()
|
2018-05-23 00:12:43 +02:00
|
|
|
if state == ServerState.RUNNING:
|
2013-12-31 14:28:24 +01:00
|
|
|
if self.create_service():
|
2015-02-15 21:15:50 +01:00
|
|
|
self.announced = True
|
|
|
|
return True
|
2013-12-31 14:28:24 +01:00
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def remove_announce(self):
|
|
|
|
if self.announced == False:
|
|
|
|
return False
|
|
|
|
try:
|
2018-05-23 00:12:43 +02:00
|
|
|
if self.entrygroup.GetState() != EntryGroup.FAILURE:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.entrygroup.Reset()
|
|
|
|
self.entrygroup.Free()
|
|
|
|
# .Free() has mem leaks
|
|
|
|
self.entrygroup._obj._bus = None
|
|
|
|
self.entrygroup._obj = None
|
|
|
|
self.entrygroup = None
|
|
|
|
self.announced = False
|
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except dbus.DBusException:
|
|
|
|
log.debug("Can't remove service. That should not happen")
|
|
|
|
|
|
|
|
def browse_domain(self, interface, protocol, domain):
|
|
|
|
self.new_service_type(interface, protocol, self.stype, domain, '')
|
|
|
|
|
|
|
|
def avahi_dbus_connect_cb(self, a, connect, disconnect):
|
|
|
|
if connect != "":
|
|
|
|
log.debug('Lost connection to avahi-daemon')
|
|
|
|
self.disconnect()
|
|
|
|
if self.disconnected_CB:
|
|
|
|
self.disconnected_CB()
|
|
|
|
else:
|
|
|
|
log.debug('We are connected to avahi-daemon')
|
|
|
|
|
|
|
|
# connect to dbus
|
|
|
|
def connect_dbus(self):
|
|
|
|
try:
|
|
|
|
import dbus
|
2018-05-23 00:12:43 +02:00
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
main_loop = DBusGMainLoop(set_as_default=True)
|
|
|
|
dbus.set_default_main_loop(main_loop)
|
2010-02-08 15:08:40 +01:00
|
|
|
except ImportError:
|
|
|
|
log.debug('Error: python-dbus needs to be installed. No '
|
|
|
|
'zeroconf support.')
|
|
|
|
return False
|
|
|
|
if self.bus:
|
|
|
|
return True
|
|
|
|
try:
|
|
|
|
self.bus = dbus.SystemBus()
|
|
|
|
self.bus.add_signal_receiver(self.avahi_dbus_connect_cb,
|
|
|
|
'NameOwnerChanged', 'org.freedesktop.DBus',
|
|
|
|
arg0='org.freedesktop.Avahi')
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
# System bus is not present
|
|
|
|
self.bus = None
|
|
|
|
log.debug(str(e))
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# connect to avahi
|
|
|
|
def connect_avahi(self):
|
|
|
|
if not self.connect_dbus():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.server:
|
|
|
|
return True
|
|
|
|
try:
|
2018-05-23 00:12:43 +02:00
|
|
|
self.server = dbus.Interface(self.bus.get_object(DBUS_NAME,
|
|
|
|
DBUS_PATH_SERVER), DBUS_INTERFACE_SERVER)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.server.connect_to_signal('StateChanged',
|
|
|
|
self.server_state_changed_callback)
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
# Avahi service is not present
|
|
|
|
self.server = None
|
|
|
|
log.debug(str(e))
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.name = self.username + '@' + self.host # service name
|
|
|
|
if not self.connect_avahi():
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.connected = True
|
|
|
|
# start browsing
|
|
|
|
if self.domain is None:
|
|
|
|
# Explicitly browse .local
|
2018-05-23 00:12:43 +02:00
|
|
|
self.browse_domain(
|
|
|
|
Interface.UNSPEC, Protocol.UNSPEC, 'local')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Browse for other browsable domains
|
|
|
|
self.domain_browser = dbus.Interface(self.bus.get_object(
|
2018-05-23 00:12:43 +02:00
|
|
|
DBUS_NAME, self.server.DomainBrowserNew(
|
|
|
|
Interface.UNSPEC, Protocol.UNSPEC, '',
|
|
|
|
DomainBrowser.BROWSE, dbus.UInt32(0))),
|
|
|
|
DBUS_INTERFACE_DOMAIN_BROWSER)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.domain_browser.connect_to_signal('ItemNew',
|
|
|
|
self.new_domain_callback)
|
|
|
|
self.domain_browser.connect_to_signal('Failure', self.error_callback)
|
|
|
|
else:
|
2018-05-23 00:12:43 +02:00
|
|
|
self.browse_domain(
|
|
|
|
Interface.UNSPEC, Protocol.UNSPEC, self.domain)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
if self.connected:
|
|
|
|
self.connected = False
|
|
|
|
if self.service_browser:
|
2010-12-01 18:26:48 +01:00
|
|
|
try:
|
|
|
|
self.service_browser.Free()
|
2013-01-01 23:18:36 +01:00
|
|
|
except dbus.DBusException as e:
|
2010-12-01 18:26:48 +01:00
|
|
|
log.debug(str(e))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.service_browser._obj._bus = None
|
|
|
|
self.service_browser._obj = None
|
|
|
|
if self.domain_browser:
|
2010-12-01 18:26:48 +01:00
|
|
|
try:
|
|
|
|
self.domain_browser.Free()
|
2013-01-01 23:18:36 +01:00
|
|
|
except dbus.DBusException as e:
|
2010-12-01 18:26:48 +01:00
|
|
|
log.debug(str(e))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.domain_browser._obj._bus = None
|
|
|
|
self.domain_browser._obj = None
|
|
|
|
self.remove_announce()
|
|
|
|
self.server._obj._bus = None
|
|
|
|
self.server._obj = None
|
|
|
|
self.server = None
|
|
|
|
self.service_browser = None
|
|
|
|
self.domain_browser = None
|
|
|
|
|
|
|
|
# refresh txt data of all contacts manually (no callback available)
|
|
|
|
def resolve_all(self):
|
|
|
|
if not self.connected:
|
2018-05-20 17:03:07 +02:00
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
for val in self.contacts.values():
|
2015-04-05 02:25:10 +02:00
|
|
|
# get txt data from last recorded resolved info
|
|
|
|
# TODO: Better try to get it from last IPv6 mDNS, then last IPv4?
|
2017-02-07 21:18:41 +01:00
|
|
|
ri = val[Constant.RESOLVED_INFO][0]
|
|
|
|
self.server.ResolveService(int(ri[ConstantRI.INTERFACE]), int(ri[ConstantRI.PROTOCOL]),
|
|
|
|
val[Constant.BARE_NAME], self.stype, val[Constant.DOMAIN],
|
2018-05-23 00:12:43 +02:00
|
|
|
Protocol.UNSPEC, dbus.UInt32(0),
|
2010-02-08 15:08:40 +01:00
|
|
|
reply_handler=self.service_resolved_all_callback,
|
|
|
|
error_handler=self.error_callback)
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
return True
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_contacts(self):
|
|
|
|
return self.contacts
|
|
|
|
|
|
|
|
def get_contact(self, jid):
|
|
|
|
if not jid in self.contacts:
|
|
|
|
return None
|
|
|
|
return self.contacts[jid]
|
|
|
|
|
|
|
|
def update_txt(self, show = None):
|
|
|
|
if show:
|
|
|
|
self.txt['status'] = self.replace_show(show)
|
|
|
|
|
|
|
|
txt = self.avahi_txt()
|
|
|
|
if self.connected and self.entrygroup:
|
2018-05-23 00:12:43 +02:00
|
|
|
self.entrygroup.UpdateServiceTxt(Interface.UNSPEC,
|
|
|
|
Protocol.UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
2010-02-08 15:08:40 +01:00
|
|
|
txt, reply_handler=self.service_updated_callback,
|
|
|
|
error_handler=self.error_callback)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2007-08-06 23:46:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
# END Zeroconf
|