Get rid of python-avahi
This commit is contained in:
parent
bc24ab470d
commit
2d6e7d2eee
|
@ -145,7 +145,7 @@ gajim_optional_features = {}
|
||||||
caps_hash = {}
|
caps_hash = {}
|
||||||
|
|
||||||
_dependencies = {
|
_dependencies = {
|
||||||
'AVAHI': False,
|
'PYTHON-DBUS': False,
|
||||||
'PYBONJOUR': False,
|
'PYBONJOUR': False,
|
||||||
'PYCRYPTO': False,
|
'PYCRYPTO': False,
|
||||||
'PYGPG': False,
|
'PYGPG': False,
|
||||||
|
@ -166,7 +166,7 @@ def is_installed(dependency):
|
||||||
return _dependencies['PYGPG'] and _dependencies['GPG_BINARY']
|
return _dependencies['PYGPG'] and _dependencies['GPG_BINARY']
|
||||||
if dependency == 'ZEROCONF':
|
if dependency == 'ZEROCONF':
|
||||||
# Alias for checking zeroconf libs
|
# Alias for checking zeroconf libs
|
||||||
return _dependencies['AVAHI'] or _dependencies['PYBONJOUR']
|
return _dependencies['PYTHON-DBUS'] or _dependencies['PYBONJOUR']
|
||||||
return _dependencies[dependency]
|
return _dependencies[dependency]
|
||||||
|
|
||||||
def is_flatpak():
|
def is_flatpak():
|
||||||
|
@ -184,8 +184,8 @@ def detect_dependencies():
|
||||||
import pybonjour
|
import pybonjour
|
||||||
_dependencies['PYBONJOUR'] = True
|
_dependencies['PYBONJOUR'] = True
|
||||||
else:
|
else:
|
||||||
import avahi
|
import dbus
|
||||||
_dependencies['AVAHI'] = True
|
_dependencies['PYTHON-DBUS'] = True
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ class ConstantRI(IntEnum):
|
||||||
|
|
||||||
def test_avahi():
|
def test_avahi():
|
||||||
try:
|
try:
|
||||||
import avahi
|
import dbus
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -26,11 +26,11 @@ except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
from gajim.common.zeroconf.zeroconf import Constant, ConstantRI
|
from gajim.common.zeroconf.zeroconf import Constant, ConstantRI
|
||||||
|
from gajim.common.zeroconf.zeroconf_avahi_const import *
|
||||||
|
|
||||||
class Zeroconf:
|
class Zeroconf:
|
||||||
def __init__(self, new_serviceCB, remove_serviceCB, name_conflictCB,
|
def __init__(self, new_serviceCB, remove_serviceCB, name_conflictCB,
|
||||||
disconnected_CB, error_CB, name, host, port):
|
disconnected_CB, error_CB, name, host, port):
|
||||||
self.avahi = None
|
|
||||||
self.domain = None # specific domain to browse
|
self.domain = None # specific domain to browse
|
||||||
self.stype = '_presence._tcp'
|
self.stype = '_presence._tcp'
|
||||||
self.port = port # listening port that gets announced
|
self.port = port # listening port that gets announced
|
||||||
|
@ -81,7 +81,7 @@ class Zeroconf:
|
||||||
|
|
||||||
# synchronous resolving
|
# synchronous resolving
|
||||||
self.server.ResolveService( int(interface), int(protocol), name, stype,
|
self.server.ResolveService( int(interface), int(protocol), name, stype,
|
||||||
domain, self.avahi.PROTO_UNSPEC, dbus.UInt32(0),
|
domain, Protocol.UNSPEC, dbus.UInt32(0),
|
||||||
reply_handler=self.service_resolved_callback,
|
reply_handler=self.service_resolved_callback,
|
||||||
error_handler=self.error_callback1)
|
error_handler=self.error_callback1)
|
||||||
|
|
||||||
|
@ -116,8 +116,8 @@ class Zeroconf:
|
||||||
stype, domain, dbus.UInt32(0))
|
stype, domain, dbus.UInt32(0))
|
||||||
|
|
||||||
self.service_browser = dbus.Interface(self.bus.get_object(
|
self.service_browser = dbus.Interface(self.bus.get_object(
|
||||||
self.avahi.DBUS_NAME, object_path),
|
DBUS_NAME, object_path),
|
||||||
self.avahi.DBUS_INTERFACE_SERVICE_BROWSER)
|
DBUS_INTERFACE_SERVICE_BROWSER)
|
||||||
self.service_browser.connect_to_signal('ItemNew',
|
self.service_browser.connect_to_signal('ItemNew',
|
||||||
self.new_service_callback)
|
self.new_service_callback)
|
||||||
self.service_browser.connect_to_signal('ItemRemove',
|
self.service_browser.connect_to_signal('ItemRemove',
|
||||||
|
@ -146,6 +146,30 @@ class Zeroconf:
|
||||||
txt_dict[key] = val
|
txt_dict[key] = val
|
||||||
return txt_dict
|
return txt_dict
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def string_to_byte_array(s):
|
||||||
|
s = s.encode('utf-8')
|
||||||
|
r = []
|
||||||
|
|
||||||
|
for c in s:
|
||||||
|
r.append(dbus.Byte(c))
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
def dict_to_txt_array(self, txt_dict):
|
||||||
|
l = []
|
||||||
|
|
||||||
|
for k,v in txt_dict.items():
|
||||||
|
if isinstance(k, str):
|
||||||
|
k = k.encode('utf-8')
|
||||||
|
|
||||||
|
if isinstance(v, str):
|
||||||
|
v = v.encode('utf-8')
|
||||||
|
|
||||||
|
l.append(self.string_to_byte_array("%s=%s" % (k,v)))
|
||||||
|
|
||||||
|
return l
|
||||||
|
|
||||||
def service_resolved_callback(self, interface, protocol, name, stype, domain,
|
def service_resolved_callback(self, interface, protocol, name, stype, domain,
|
||||||
host, aprotocol, address, port, txt, flags):
|
host, aprotocol, address, port, txt, flags):
|
||||||
log.debug('Service data for service %s in domain %s on %i.%i:'
|
log.debug('Service data for service %s in domain %s on %i.%i:'
|
||||||
|
@ -222,22 +246,19 @@ class Zeroconf:
|
||||||
|
|
||||||
def server_state_changed_callback(self, state, error):
|
def server_state_changed_callback(self, state, error):
|
||||||
log.debug('server state changed to %s' % state)
|
log.debug('server state changed to %s' % state)
|
||||||
if state == self.avahi.SERVER_RUNNING:
|
if state == ServerState.RUNNING:
|
||||||
self.create_service()
|
self.create_service()
|
||||||
elif state in (self.avahi.SERVER_COLLISION,
|
elif state in (ServerState.COLLISION,
|
||||||
self.avahi.SERVER_REGISTERING):
|
ServerState.REGISTERING):
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.entrygroup.Reset()
|
self.entrygroup.Reset()
|
||||||
elif state == self.avahi.CLIENT_FAILURE:
|
|
||||||
# does it ever go here?
|
|
||||||
log.debug('CLIENT FAILURE')
|
|
||||||
|
|
||||||
def entrygroup_state_changed_callback(self, state, error):
|
def entrygroup_state_changed_callback(self, state, error):
|
||||||
# the name is already present, so recreate
|
# the name is already present, so recreate
|
||||||
if state == self.avahi.ENTRY_GROUP_COLLISION:
|
if state == EntryGroup.COLLISION:
|
||||||
log.debug('zeroconf.py: local name collision')
|
log.debug('zeroconf.py: local name collision')
|
||||||
self.service_add_fail_callback('Local name collision')
|
self.service_add_fail_callback('Local name collision')
|
||||||
elif state == self.avahi.ENTRY_GROUP_FAILURE:
|
elif state == EntryGroup.FAILURE:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.entrygroup.Reset()
|
self.entrygroup.Reset()
|
||||||
log.debug('zeroconf.py: ENTRY_GROUP_FAILURE reached(that'
|
log.debug('zeroconf.py: ENTRY_GROUP_FAILURE reached(that'
|
||||||
|
@ -252,15 +273,15 @@ class Zeroconf:
|
||||||
return show
|
return show
|
||||||
|
|
||||||
def avahi_txt(self):
|
def avahi_txt(self):
|
||||||
return self.avahi.dict_to_txt_array(self.txt)
|
return self.dict_to_txt_array(self.txt)
|
||||||
|
|
||||||
def create_service(self):
|
def create_service(self):
|
||||||
try:
|
try:
|
||||||
if not self.entrygroup:
|
if not self.entrygroup:
|
||||||
# create an EntryGroup for publishing
|
# create an EntryGroup for publishing
|
||||||
self.entrygroup = dbus.Interface(self.bus.get_object(
|
self.entrygroup = dbus.Interface(self.bus.get_object(
|
||||||
self.avahi.DBUS_NAME, self.server.EntryGroupNew()),
|
DBUS_NAME, self.server.EntryGroupNew()),
|
||||||
self.avahi.DBUS_INTERFACE_ENTRY_GROUP)
|
DBUS_INTERFACE_ENTRY_GROUP)
|
||||||
self.entrygroup.connect_to_signal('StateChanged',
|
self.entrygroup.connect_to_signal('StateChanged',
|
||||||
self.entrygroup_state_changed_callback)
|
self.entrygroup_state_changed_callback)
|
||||||
|
|
||||||
|
@ -284,8 +305,8 @@ class Zeroconf:
|
||||||
self.txt = txt
|
self.txt = txt
|
||||||
log.debug('Publishing service %s of type %s' % (self.name,
|
log.debug('Publishing service %s of type %s' % (self.name,
|
||||||
self.stype))
|
self.stype))
|
||||||
self.entrygroup.AddService(self.avahi.IF_UNSPEC,
|
self.entrygroup.AddService(Interface.UNSPEC,
|
||||||
self.avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
Protocol.UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
||||||
'', dbus.UInt16(self.port), self.avahi_txt(),
|
'', dbus.UInt16(self.port), self.avahi_txt(),
|
||||||
reply_handler=self.service_added_callback,
|
reply_handler=self.service_added_callback,
|
||||||
error_handler=self.service_add_fail_callback)
|
error_handler=self.service_add_fail_callback)
|
||||||
|
@ -304,7 +325,7 @@ class Zeroconf:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
state = self.server.GetState()
|
state = self.server.GetState()
|
||||||
if state == self.avahi.SERVER_RUNNING:
|
if state == ServerState.RUNNING:
|
||||||
if self.create_service():
|
if self.create_service():
|
||||||
self.announced = True
|
self.announced = True
|
||||||
return True
|
return True
|
||||||
|
@ -314,7 +335,7 @@ class Zeroconf:
|
||||||
if self.announced == False:
|
if self.announced == False:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
if self.entrygroup.GetState() != self.avahi.ENTRY_GROUP_FAILURE:
|
if self.entrygroup.GetState() != EntryGroup.FAILURE:
|
||||||
self.entrygroup.Reset()
|
self.entrygroup.Reset()
|
||||||
self.entrygroup.Free()
|
self.entrygroup.Free()
|
||||||
# .Free() has mem leaks
|
# .Free() has mem leaks
|
||||||
|
@ -345,6 +366,9 @@ class Zeroconf:
|
||||||
def connect_dbus(self):
|
def connect_dbus(self):
|
||||||
try:
|
try:
|
||||||
import dbus
|
import dbus
|
||||||
|
from dbus.mainloop.glib import DBusGMainLoop
|
||||||
|
main_loop = DBusGMainLoop(set_as_default=True)
|
||||||
|
dbus.set_default_main_loop(main_loop)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log.debug('Error: python-dbus needs to be installed. No '
|
log.debug('Error: python-dbus needs to be installed. No '
|
||||||
'zeroconf support.')
|
'zeroconf support.')
|
||||||
|
@ -368,19 +392,12 @@ class Zeroconf:
|
||||||
def connect_avahi(self):
|
def connect_avahi(self):
|
||||||
if not self.connect_dbus():
|
if not self.connect_dbus():
|
||||||
return False
|
return False
|
||||||
try:
|
|
||||||
import avahi
|
|
||||||
self.avahi = avahi
|
|
||||||
except ImportError:
|
|
||||||
log.debug('Error: python-avahi needs to be installed. No '
|
|
||||||
'zeroconf support.')
|
|
||||||
return False
|
|
||||||
|
|
||||||
if self.server:
|
if self.server:
|
||||||
return True
|
return True
|
||||||
try:
|
try:
|
||||||
self.server = dbus.Interface(self.bus.get_object(self.avahi.DBUS_NAME,
|
self.server = dbus.Interface(self.bus.get_object(DBUS_NAME,
|
||||||
self.avahi.DBUS_PATH_SERVER), self.avahi.DBUS_INTERFACE_SERVER)
|
DBUS_PATH_SERVER), DBUS_INTERFACE_SERVER)
|
||||||
self.server.connect_to_signal('StateChanged',
|
self.server.connect_to_signal('StateChanged',
|
||||||
self.server_state_changed_callback)
|
self.server_state_changed_callback)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -400,21 +417,21 @@ class Zeroconf:
|
||||||
# start browsing
|
# start browsing
|
||||||
if self.domain is None:
|
if self.domain is None:
|
||||||
# Explicitly browse .local
|
# Explicitly browse .local
|
||||||
self.browse_domain(self.avahi.IF_UNSPEC, self.avahi.PROTO_UNSPEC,
|
self.browse_domain(
|
||||||
'local')
|
Interface.UNSPEC, Protocol.UNSPEC, 'local')
|
||||||
|
|
||||||
# Browse for other browsable domains
|
# Browse for other browsable domains
|
||||||
self.domain_browser = dbus.Interface(self.bus.get_object(
|
self.domain_browser = dbus.Interface(self.bus.get_object(
|
||||||
self.avahi.DBUS_NAME, self.server.DomainBrowserNew(
|
DBUS_NAME, self.server.DomainBrowserNew(
|
||||||
self.avahi.IF_UNSPEC, self.avahi.PROTO_UNSPEC, '',
|
Interface.UNSPEC, Protocol.UNSPEC, '',
|
||||||
self.avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))),
|
DomainBrowser.BROWSE, dbus.UInt32(0))),
|
||||||
self.avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
|
DBUS_INTERFACE_DOMAIN_BROWSER)
|
||||||
self.domain_browser.connect_to_signal('ItemNew',
|
self.domain_browser.connect_to_signal('ItemNew',
|
||||||
self.new_domain_callback)
|
self.new_domain_callback)
|
||||||
self.domain_browser.connect_to_signal('Failure', self.error_callback)
|
self.domain_browser.connect_to_signal('Failure', self.error_callback)
|
||||||
else:
|
else:
|
||||||
self.browse_domain(self.avahi.IF_UNSPEC, self.avahi.PROTO_UNSPEC,
|
self.browse_domain(
|
||||||
self.domain)
|
Interface.UNSPEC, Protocol.UNSPEC, self.domain)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -452,7 +469,7 @@ class Zeroconf:
|
||||||
ri = val[Constant.RESOLVED_INFO][0]
|
ri = val[Constant.RESOLVED_INFO][0]
|
||||||
self.server.ResolveService(int(ri[ConstantRI.INTERFACE]), int(ri[ConstantRI.PROTOCOL]),
|
self.server.ResolveService(int(ri[ConstantRI.INTERFACE]), int(ri[ConstantRI.PROTOCOL]),
|
||||||
val[Constant.BARE_NAME], self.stype, val[Constant.DOMAIN],
|
val[Constant.BARE_NAME], self.stype, val[Constant.DOMAIN],
|
||||||
self.avahi.PROTO_UNSPEC, dbus.UInt32(0),
|
Protocol.UNSPEC, dbus.UInt32(0),
|
||||||
reply_handler=self.service_resolved_all_callback,
|
reply_handler=self.service_resolved_all_callback,
|
||||||
error_handler=self.error_callback)
|
error_handler=self.error_callback)
|
||||||
|
|
||||||
|
@ -472,8 +489,8 @@ class Zeroconf:
|
||||||
|
|
||||||
txt = self.avahi_txt()
|
txt = self.avahi_txt()
|
||||||
if self.connected and self.entrygroup:
|
if self.connected and self.entrygroup:
|
||||||
self.entrygroup.UpdateServiceTxt(self.avahi.IF_UNSPEC,
|
self.entrygroup.UpdateServiceTxt(Interface.UNSPEC,
|
||||||
self.avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
Protocol.UNSPEC, dbus.UInt32(0), self.name, self.stype, '',
|
||||||
txt, reply_handler=self.service_updated_callback,
|
txt, reply_handler=self.service_updated_callback,
|
||||||
error_handler=self.error_callback)
|
error_handler=self.error_callback)
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
#
|
||||||
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
|
||||||
|
#
|
||||||
|
# This file is part of Gajim.
|
||||||
|
#
|
||||||
|
# Gajim is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published
|
||||||
|
# by the Free Software Foundation; version 3 only.
|
||||||
|
#
|
||||||
|
# Gajim is distributed in the hope that it will be useful,
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
from enum import IntEnum
|
||||||
|
|
||||||
|
|
||||||
|
DBUS_NAME = "org.freedesktop.Avahi"
|
||||||
|
DBUS_INTERFACE_SERVER = DBUS_NAME + ".Server"
|
||||||
|
DBUS_PATH_SERVER = "/"
|
||||||
|
DBUS_INTERFACE_ENTRY_GROUP = DBUS_NAME + ".EntryGroup"
|
||||||
|
DBUS_INTERFACE_DOMAIN_BROWSER = DBUS_NAME + ".DomainBrowser"
|
||||||
|
DBUS_INTERFACE_SERVICE_TYPE_BROWSER = DBUS_NAME + ".ServiceTypeBrowser"
|
||||||
|
DBUS_INTERFACE_SERVICE_BROWSER = DBUS_NAME + ".ServiceBrowser"
|
||||||
|
DBUS_INTERFACE_ADDRESS_RESOLVER = DBUS_NAME + ".AddressResolver"
|
||||||
|
DBUS_INTERFACE_HOST_NAME_RESOLVER = DBUS_NAME + ".HostNameResolver"
|
||||||
|
DBUS_INTERFACE_SERVICE_RESOLVER = DBUS_NAME + ".ServiceResolver"
|
||||||
|
DBUS_INTERFACE_RECORD_BROWSER = DBUS_NAME + ".RecordBrowser"
|
||||||
|
|
||||||
|
|
||||||
|
class ServerState(IntEnum):
|
||||||
|
INVALID = 0
|
||||||
|
REGISTERING = 1
|
||||||
|
RUNNING = 2
|
||||||
|
COLLISION = 3
|
||||||
|
FAILURE = 4
|
||||||
|
|
||||||
|
|
||||||
|
class EntryGroup(IntEnum):
|
||||||
|
UNCOMMITED = 0
|
||||||
|
REGISTERING = 1
|
||||||
|
ESTABLISHED = 2
|
||||||
|
COLLISION = 3
|
||||||
|
FAILURE = 4
|
||||||
|
|
||||||
|
|
||||||
|
class DomainBrowser(IntEnum):
|
||||||
|
BROWSE = 0
|
||||||
|
BROWSE_DEFAULT = 1
|
||||||
|
REGISTER = 2
|
||||||
|
REGISTER_DEFAULT = 3
|
||||||
|
BROWSE_LEGACY = 4
|
||||||
|
|
||||||
|
|
||||||
|
class Protocol(IntEnum):
|
||||||
|
UNSPEC = -1
|
||||||
|
INET = 0
|
||||||
|
INET6 = 1
|
||||||
|
|
||||||
|
|
||||||
|
class Interface(IntEnum):
|
||||||
|
UNSPEC = -1
|
|
@ -47,7 +47,7 @@ class FeaturesWindow:
|
||||||
self.features = {
|
self.features = {
|
||||||
_('Bonjour / Zeroconf'): (self.zeroconf_available,
|
_('Bonjour / Zeroconf'): (self.zeroconf_available,
|
||||||
_('Serverless chatting with autodetected clients in a local network.'),
|
_('Serverless chatting with autodetected clients in a local network.'),
|
||||||
_('Requires python-avahi.'),
|
_('Requires python-dbus.'),
|
||||||
_('Requires pybonjour and bonjour SDK running (%(url)s)') % {'url': 'https://developer.apple.com/opensource/).'}),
|
_('Requires pybonjour and bonjour SDK running (%(url)s)') % {'url': 'https://developer.apple.com/opensource/).'}),
|
||||||
_('Command line'): (self.dbus_available,
|
_('Command line'): (self.dbus_available,
|
||||||
_('A script to control Gajim via commandline.'),
|
_('A script to control Gajim via commandline.'),
|
||||||
|
|
Loading…
Reference in New Issue