2018-05-20 17:03:07 +02:00
|
|
|
# Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
# Copyright (C) 2006 Philipp Hörist <philipp@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/>.
|
|
|
|
|
|
|
|
import logging
|
2007-07-28 13:01:21 +02:00
|
|
|
import select
|
2018-05-20 17:03:07 +02:00
|
|
|
import socket
|
2007-11-19 16:47:17 +01:00
|
|
|
import re
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.zeroconf.zeroconf import Constant
|
2007-07-28 13:01:21 +02:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
|
2017-09-26 07:14:01 +02:00
|
|
|
log = logging.getLogger('gajim.c.z.zeroconf_bonjour')
|
|
|
|
|
2007-07-28 13:01:21 +02:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
import pybonjour
|
2013-01-05 00:03:36 +01:00
|
|
|
except ImportError:
|
2010-02-08 15:08:40 +01:00
|
|
|
pass
|
2007-07-28 13:01:21 +02:00
|
|
|
|
2009-12-28 23:04:23 +01:00
|
|
|
resolve_timeout = 1
|
2007-07-28 13:01:21 +02:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
|
2007-07-28 13:01:21 +02:00
|
|
|
class Zeroconf:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, new_serviceCB, remove_serviceCB, name_conflictCB,
|
2018-05-20 17:03:07 +02:00
|
|
|
disconnected_CB, error_CB, name, host, port):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.stype = '_presence._tcp'
|
2018-05-20 17:03:07 +02:00
|
|
|
self.port = port # listening port that gets announced
|
2010-02-08 15:08:40 +01:00
|
|
|
self.username = name
|
|
|
|
self.host = host
|
2018-05-20 17:03:07 +02:00
|
|
|
self.txt = {} # service data
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
self.contacts = {} # all current local contacts with data
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = False
|
|
|
|
self.announced = False
|
|
|
|
self.invalid_self_contact = {}
|
2018-05-20 17:03:07 +02:00
|
|
|
self.resolved_contacts = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
self.resolved = []
|
2018-05-20 17:03:07 +02:00
|
|
|
self.queried = []
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def browse_callback(self, sdRef, flags, interfaceIndex, errorCode,
|
|
|
|
serviceName, regtype, replyDomain):
|
|
|
|
log.debug('Found service %s in domain %s on %i(type: %s).',
|
|
|
|
serviceName, replyDomain, interfaceIndex, regtype)
|
2010-02-08 15:08:40 +01:00
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
if errorCode != pybonjour.kDNSServiceErr_NoError:
|
2018-05-20 17:03:07 +02:00
|
|
|
log.debug('Error in browse_callback: %s', str(errorCode))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2018-09-16 14:42:05 +02:00
|
|
|
if not flags & pybonjour.kDNSServiceFlagsAdd:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remove_service_callback(serviceName)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2018-05-20 17:03:07 +02:00
|
|
|
# asynchronous resolving
|
|
|
|
resolve_sdRef = None
|
|
|
|
resolve_sdRef = pybonjour.DNSServiceResolve(
|
|
|
|
0, interfaceIndex, serviceName,
|
|
|
|
regtype, replyDomain, self.service_resolved_callback)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
while not self.resolved:
|
|
|
|
ready = select.select([resolve_sdRef], [], [], resolve_timeout)
|
|
|
|
if resolve_sdRef not in ready[0]:
|
2018-05-20 17:03:07 +02:00
|
|
|
log.info('Resolve timed out')
|
2010-02-08 15:08:40 +01:00
|
|
|
break
|
|
|
|
pybonjour.DNSServiceProcessResult(resolve_sdRef)
|
|
|
|
else:
|
|
|
|
self.resolved.pop()
|
2018-05-20 17:03:07 +02:00
|
|
|
|
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
log.info('Error when resolving DNS: %s', error)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
finally:
|
2018-05-20 17:03:07 +02:00
|
|
|
if resolve_sdRef:
|
|
|
|
resolve_sdRef.close()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def remove_service_callback(self, name):
|
2018-09-17 18:57:00 +02:00
|
|
|
log.info('Service %s disappeared.', name)
|
2010-02-08 15:08:40 +01:00
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
if name != self.name:
|
2018-09-18 07:20:49 +02:00
|
|
|
for key in list(self.contacts.keys()):
|
2018-05-20 17:03:07 +02:00
|
|
|
if self.contacts[key][Constant.NAME] == name:
|
2010-02-08 15:08:40 +01:00
|
|
|
del self.contacts[key]
|
|
|
|
self.remove_serviceCB(key)
|
|
|
|
return
|
|
|
|
|
|
|
|
def txt_array_to_dict(self, txt):
|
2018-05-20 17:03:07 +02:00
|
|
|
if isinstance(txt, pybonjour.TXTRecord):
|
|
|
|
items = txt._items
|
|
|
|
else:
|
|
|
|
items = pybonjour.TXTRecord.parse(txt)._items
|
2010-02-08 15:08:40 +01:00
|
|
|
return dict((v[0], v[1]) for v in items.values())
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
@staticmethod
|
|
|
|
def _parse_name(fullname):
|
|
|
|
log.debug('Parse name: %s', fullname)
|
2010-02-08 15:08:40 +01:00
|
|
|
# TODO: do proper decoding...
|
2018-05-20 17:03:07 +02:00
|
|
|
escaping = {r'\.': '.',
|
|
|
|
r'\032': ' ',
|
|
|
|
r'\064': '@',
|
|
|
|
}
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Split on '.' but do not split on '\.'
|
|
|
|
result = re.split('(?<!\\\\)\.', fullname)
|
|
|
|
name = result[0]
|
|
|
|
protocol, domain = result[2:4]
|
|
|
|
|
|
|
|
# Replace the escaped values
|
|
|
|
for src, trg in escaping.items():
|
|
|
|
name = name.replace(src, trg)
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
bare_name = name
|
|
|
|
if '@' not in name:
|
|
|
|
name = name + '@' + name
|
|
|
|
log.debug('End parse: %s %s %s %s',
|
|
|
|
name, bare_name, protocol, domain)
|
|
|
|
|
|
|
|
return name, bare_name, protocol, domain
|
|
|
|
|
|
|
|
def query_txt_callback(self, sdRef, flags, interfaceIndex, errorCode,
|
|
|
|
hosttarget, rrtype, rrclass, rdata, ttl):
|
|
|
|
|
|
|
|
if errorCode != pybonjour.kDNSServiceErr_NoError:
|
|
|
|
log.error('Error in query_record_callback: %s', str(errorCode))
|
|
|
|
return
|
|
|
|
|
2018-09-17 21:11:45 +02:00
|
|
|
name = self._parse_name(hosttarget)[0]
|
2018-05-20 17:03:07 +02:00
|
|
|
|
|
|
|
if name != self.name:
|
|
|
|
# update TXT data only, as intended according to
|
|
|
|
# resolve_all comment
|
|
|
|
old_contact = self.contacts[name]
|
|
|
|
self.contacts[name] = old_contact[0:Constant.TXT] + (rdata,) + old_contact[Constant.TXT+1:]
|
|
|
|
log.debug(self.contacts[name])
|
|
|
|
|
|
|
|
self.queried.append(True)
|
|
|
|
|
|
|
|
def query_record_callback(self, sdRef, flags, interfaceIndex, errorCode,
|
|
|
|
hosttarget, rrtype, rrclass, rdata, ttl):
|
|
|
|
if errorCode != pybonjour.kDNSServiceErr_NoError:
|
|
|
|
log.error('Error in query_record_callback: %s', str(errorCode))
|
|
|
|
return
|
|
|
|
|
|
|
|
fullname, port, txtRecord = self.resolved_contacts[hosttarget]
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
txt = pybonjour.TXTRecord.parse(txtRecord)
|
2018-05-20 17:03:07 +02:00
|
|
|
ip = socket.inet_ntoa(rdata)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
name, bare_name, protocol, domain = self._parse_name(fullname)
|
|
|
|
|
|
|
|
log.info('Service data for service %s on %i:',
|
|
|
|
fullname, interfaceIndex)
|
|
|
|
log.info('Host %s, ip %s, port %i, TXT data: %s',
|
|
|
|
hosttarget, ip, port, txt._items)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
|
|
|
|
# we don't want to see ourselves in the list
|
|
|
|
if name != self.name:
|
2018-05-20 17:03:07 +02:00
|
|
|
resolved_info = [(interfaceIndex, protocol, hosttarget, fullname, ip, port)]
|
2015-04-06 19:14:33 +02:00
|
|
|
self.contacts[name] = (name, domain, resolved_info, bare_name, txtRecord)
|
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.
|
2018-05-20 17:03:07 +02:00
|
|
|
self.invalid_self_contact[name] = \
|
|
|
|
(name, domain,
|
|
|
|
(interfaceIndex, protocol, hosttarget, fullname, ip, port),
|
|
|
|
bare_name, txtRecord)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
self.queried.append(True)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def service_resolved_callback(self, sdRef, flags, interfaceIndex,
|
|
|
|
errorCode, fullname, hosttarget, port,
|
|
|
|
txtRecord):
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
if errorCode != pybonjour.kDNSServiceErr_NoError:
|
|
|
|
log.error('Error in service_resolved_callback: %s', str(errorCode))
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
self.resolved_contacts[hosttarget] = (fullname, port, txtRecord)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
try:
|
|
|
|
query_sdRef = None
|
|
|
|
query_sdRef = \
|
|
|
|
pybonjour.DNSServiceQueryRecord(
|
|
|
|
interfaceIndex=interfaceIndex,
|
|
|
|
fullname=hosttarget,
|
|
|
|
rrtype=pybonjour.kDNSServiceType_A,
|
|
|
|
callBack=self.query_record_callback)
|
|
|
|
|
|
|
|
while not self.queried:
|
|
|
|
ready = select.select([query_sdRef], [], [], resolve_timeout)
|
|
|
|
if query_sdRef not in ready[0]:
|
|
|
|
log.warning('Query record timed out')
|
|
|
|
break
|
|
|
|
pybonjour.DNSServiceProcessResult(query_sdRef)
|
|
|
|
else:
|
|
|
|
self.queried.pop()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
if error.errorCode == pybonjour.kDNSServiceErr_ServiceNotRunning:
|
|
|
|
log.info('Service not running')
|
|
|
|
else:
|
|
|
|
self.error_CB(_('Error while adding service. %s') % error)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if query_sdRef:
|
|
|
|
query_sdRef.close()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
self.resolved.append(True)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def service_added_callback(self, sdRef, flags, errorCode,
|
|
|
|
name, regtype, domain):
|
2010-02-08 15:08:40 +01:00
|
|
|
if errorCode == pybonjour.kDNSServiceErr_NoError:
|
2018-05-20 17:03:07 +02:00
|
|
|
log.info('Service successfully added')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
elif errorCode == pybonjour.kDNSServiceErr_NameConflict:
|
|
|
|
log.error('Error while adding service. %s', errorCode)
|
2010-02-08 15:08:40 +01:00
|
|
|
parts = self.username.split(' ')
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
# check if last part is a number and if, increment it
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
stripped = str(int(parts[-1]))
|
|
|
|
except Exception:
|
|
|
|
stripped = 1
|
2018-05-20 17:03:07 +02:00
|
|
|
alternative_name = self.username + str(stripped + 1)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.name_conflictCB(alternative_name)
|
2018-05-20 17:03:07 +02:00
|
|
|
else:
|
|
|
|
self.error_CB(_('Error while adding service. %s') % str(errorCode))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# make zeroconf-valid names
|
|
|
|
def replace_show(self, show):
|
|
|
|
if show in ['chat', 'online', '']:
|
|
|
|
return 'avail'
|
|
|
|
elif show == 'xa':
|
|
|
|
return 'away'
|
|
|
|
return show
|
|
|
|
|
|
|
|
def create_service(self):
|
|
|
|
txt = {}
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
# remove empty keys
|
|
|
|
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'
|
2018-05-20 17:03:07 +02:00
|
|
|
self.txt = txt
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2018-05-20 17:03:07 +02:00
|
|
|
self.service_sdRef = pybonjour.DNSServiceRegister(
|
|
|
|
name=self.name,
|
|
|
|
regtype=self.stype,
|
|
|
|
port=self.port,
|
|
|
|
txtRecord=pybonjour.TXTRecord(self.txt, strict=True),
|
|
|
|
callBack=self.service_added_callback)
|
|
|
|
|
|
|
|
log.info('Publishing service %s of type %s', self.name, self.stype)
|
|
|
|
|
|
|
|
ready = select.select([self.service_sdRef], [], [])
|
|
|
|
if self.service_sdRef in ready[0]:
|
|
|
|
pybonjour.DNSServiceProcessResult(self.service_sdRef)
|
|
|
|
|
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
if error.errorCode == pybonjour.kDNSServiceErr_ServiceNotRunning:
|
|
|
|
log.info('Service not running')
|
|
|
|
else:
|
|
|
|
self.error_CB(_('Error while adding service. %s') % error)
|
|
|
|
self.disconnect()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def announce(self):
|
|
|
|
if not self.connected:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.create_service()
|
|
|
|
self.announced = True
|
|
|
|
return True
|
|
|
|
|
|
|
|
def remove_announce(self):
|
|
|
|
if not self.announced:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
self.service_sdRef.close()
|
|
|
|
self.announced = False
|
|
|
|
return True
|
2018-05-20 17:03:07 +02:00
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
log.error('Error when removing announce: %s', error)
|
2010-02-08 15:08:40 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def connect(self):
|
2018-05-20 17:03:07 +02:00
|
|
|
self.name = self.username + '@' + self.host # service name
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
# start browsing
|
2018-05-20 17:03:07 +02:00
|
|
|
if self.browse_domain():
|
|
|
|
return True
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2018-05-20 17:03:07 +02:00
|
|
|
self.disconnect()
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
if self.connected:
|
|
|
|
self.connected = False
|
2010-03-19 08:27:35 +01:00
|
|
|
if hasattr(self, 'browse_sdRef'):
|
|
|
|
self.browse_sdRef.close()
|
|
|
|
self.remove_announce()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def browse_domain(self, domain=None):
|
|
|
|
try:
|
2018-05-20 17:03:07 +02:00
|
|
|
self.browse_sdRef = pybonjour.DNSServiceBrowse(
|
|
|
|
regtype=self.stype,
|
|
|
|
domain=domain,
|
|
|
|
callBack=self.browse_callback)
|
|
|
|
log.info('Starting to browse .local')
|
|
|
|
return True
|
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
if error.errorCode == pybonjour.kDNSServiceErr_ServiceNotRunning:
|
|
|
|
log.info('Service not running')
|
|
|
|
else:
|
|
|
|
log.error('Error while browsing for services. %s', error)
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def browse_loop(self):
|
2018-05-20 17:03:07 +02:00
|
|
|
try:
|
|
|
|
ready = select.select([self.browse_sdRef], [], [], 0)
|
|
|
|
if self.browse_sdRef in ready[0]:
|
|
|
|
pybonjour.DNSServiceProcessResult(self.browse_sdRef)
|
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
if error.errorCode == pybonjour.kDNSServiceErr_ServiceNotRunning:
|
|
|
|
log.info('Service not running')
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
log.error('Error while browsing for services. %s', error)
|
|
|
|
return True
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
# resolve_all() is called every X seconds and querys for new clients
|
|
|
|
# and monitors TXT records for changed status
|
2010-02-08 15:08:40 +01:00
|
|
|
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 now put here as this is synchronous
|
2018-05-20 17:03:07 +02:00
|
|
|
if not self.browse_loop():
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
# Monitor TXT Records with DNSServiceQueryRecord because
|
|
|
|
# its more efficient (see pybonjour documentation)
|
2010-02-08 15:08:40 +01:00
|
|
|
for val in self.contacts.values():
|
|
|
|
try:
|
2018-05-20 17:03:07 +02:00
|
|
|
query_sdRef = None
|
|
|
|
query_sdRef = \
|
|
|
|
pybonjour.DNSServiceQueryRecord(
|
|
|
|
interfaceIndex=pybonjour.kDNSServiceInterfaceIndexAny,
|
|
|
|
fullname=val[Constant.RESOLVED_INFO][0][Constant.BARE_NAME],
|
|
|
|
rrtype=pybonjour.kDNSServiceType_TXT,
|
|
|
|
callBack=self.query_txt_callback)
|
|
|
|
|
|
|
|
while not self.queried:
|
|
|
|
ready = select.select(
|
|
|
|
[query_sdRef], [], [], resolve_timeout)
|
|
|
|
if query_sdRef not in ready[0]:
|
|
|
|
log.info('Query record timed out')
|
|
|
|
break
|
|
|
|
pybonjour.DNSServiceProcessResult(query_sdRef)
|
|
|
|
else:
|
|
|
|
self.queried.pop()
|
|
|
|
|
|
|
|
except pybonjour.BonjourError as error:
|
|
|
|
if error.errorCode == pybonjour.kDNSServiceErr_ServiceNotRunning:
|
|
|
|
log.info('Service not running')
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
log.error('Error in query for TXT records. %s', error)
|
2010-02-08 15:08:40 +01:00
|
|
|
finally:
|
2018-05-20 17:03:07 +02:00
|
|
|
if query_sdRef:
|
|
|
|
query_sdRef.close()
|
|
|
|
|
|
|
|
return True
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_contacts(self):
|
|
|
|
return self.contacts
|
|
|
|
|
|
|
|
def get_contact(self, jid):
|
2018-05-20 17:03:07 +02:00
|
|
|
if jid not in self.contacts:
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
return self.contacts[jid]
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def update_txt(self, show=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
if show:
|
|
|
|
self.txt['status'] = self.replace_show(show)
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
txt = pybonjour.TXTRecord(self.txt, strict=True)
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2018-05-20 17:03:07 +02:00
|
|
|
pybonjour.DNSServiceUpdateRecord(self.service_sdRef, None, 0, txt)
|
|
|
|
except pybonjour.BonjourError as e:
|
|
|
|
log.error('Error when updating TXT Record: %s', e)
|
2010-02-08 15:08:40 +01:00
|
|
|
return False
|
|
|
|
return True
|