2018-09-05 02:59:34 +02:00
|
|
|
# Contributors for this file:
|
|
|
|
# - Yann Leboulanger <asterix@lagaule.org>
|
|
|
|
# - Nikos Kouremenos <nkour@jabber.org>
|
|
|
|
# - Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
# - Travis Shirk <travis@pobox.com>
|
|
|
|
# - Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2003-2014 Yann Leboulanger <asterix@lagaule.org>
|
|
|
|
# Copyright (C) 2003-2004 Vincent Hanquez <tab@snarc.org>
|
|
|
|
# Copyright (C) 2006 Nikos Kouremenos <nkour@jabber.org>
|
|
|
|
# Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
# Travis Shirk <travis@pobox.com>
|
|
|
|
# Norman Rasmussen <norman@rasmussen.co.za>
|
|
|
|
# Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
#
|
|
|
|
# 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/>.
|
2006-05-26 16:27:42 +02:00
|
|
|
|
2015-05-10 20:54:26 +02:00
|
|
|
import socket
|
2018-12-05 11:31:24 +01:00
|
|
|
import getpass
|
2019-01-03 10:18:15 +01:00
|
|
|
import logging
|
|
|
|
import time
|
2006-05-26 16:27:42 +02:00
|
|
|
|
2017-12-18 14:44:38 +01:00
|
|
|
import nbxmpp
|
2013-07-29 18:35:49 +02:00
|
|
|
from gi.repository import GLib
|
2006-06-13 23:19:39 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import ged
|
2018-07-08 18:37:53 +02:00
|
|
|
from gajim.common import modules
|
2019-01-03 08:33:35 +01:00
|
|
|
from gajim.common.nec import NetworkEvent
|
2018-10-04 23:55:35 +02:00
|
|
|
from gajim.common.i18n import _
|
2018-12-05 11:31:24 +01:00
|
|
|
from gajim.common.connection import CommonConnection
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.zeroconf import client_zeroconf
|
|
|
|
from gajim.common.zeroconf import zeroconf
|
2019-01-03 10:18:15 +01:00
|
|
|
from gajim.common.zeroconf.connection_handlers_zeroconf import ConnectionHandlersZeroconf
|
|
|
|
from gajim.common.zeroconf.connection_handlers_zeroconf import STATUS_LIST
|
|
|
|
from gajim.common.connection_handlers_events import OurShowEvent
|
|
|
|
from gajim.common.connection_handlers_events import InformationEvent
|
|
|
|
from gajim.common.connection_handlers_events import ConnectionLostEvent
|
|
|
|
from gajim.common.connection_handlers_events import MessageSentEvent
|
|
|
|
from gajim.common.connection_handlers_events import MessageErrorEvent
|
2006-05-29 17:36:18 +02:00
|
|
|
|
2017-09-22 00:06:35 +02:00
|
|
|
log = logging.getLogger('gajim.c.connection_zeroconf')
|
|
|
|
|
2018-12-05 11:31:24 +01:00
|
|
|
|
2009-11-18 11:06:09 +01:00
|
|
|
class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
ConnectionHandlersZeroconf.__init__(self)
|
|
|
|
# system username
|
|
|
|
self.username = None
|
|
|
|
self.server_resource = '' # zeroconf has no resource, fake an empty one
|
|
|
|
self.call_resolve_timeout = False
|
|
|
|
# we don't need a password, but must be non-empty
|
|
|
|
self.password = 'zeroconf'
|
|
|
|
self.autoconnect = False
|
2018-03-11 22:12:00 +01:00
|
|
|
self.httpupload = False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
CommonConnection.__init__(self, name)
|
|
|
|
self.is_zeroconf = True
|
|
|
|
|
2018-07-08 18:37:53 +02:00
|
|
|
# Register all modules
|
|
|
|
modules.register(self)
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
app.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('stanza-message-outgoing', ged.OUT_CORE,
|
2017-04-06 23:21:19 +02:00
|
|
|
self._nec_stanza_message_outgoing)
|
2011-07-19 23:05:32 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_config_values_or_default(self):
|
|
|
|
"""
|
|
|
|
Get name, host, port from config, or create zeroconf account with default
|
|
|
|
values
|
|
|
|
"""
|
2013-01-01 21:06:16 +01:00
|
|
|
self.host = socket.gethostname()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.set_per('accounts', app.ZEROCONF_ACC_NAME, 'hostname',
|
2010-02-08 15:08:40 +01:00
|
|
|
self.host)
|
2017-08-13 13:18:56 +02:00
|
|
|
self.port = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'custom_port')
|
2017-08-13 13:18:56 +02:00
|
|
|
self.autoconnect = app.config.get_per('accounts',
|
|
|
|
app.ZEROCONF_ACC_NAME, 'autoconnect')
|
|
|
|
self.sync_with_global_status = app.config.get_per('accounts',
|
|
|
|
app.ZEROCONF_ACC_NAME, 'sync_with_global_status')
|
|
|
|
self.first = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_first_name')
|
2017-08-13 13:18:56 +02:00
|
|
|
self.last = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_last_name')
|
2017-08-13 13:18:56 +02:00
|
|
|
self.jabber_id = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_jabber_id')
|
2017-08-13 13:18:56 +02:00
|
|
|
self.email = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_email')
|
|
|
|
|
|
|
|
if not self.username:
|
2013-01-01 21:06:16 +01:00
|
|
|
self.username = getpass.getuser()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.set_per('accounts', app.ZEROCONF_ACC_NAME, 'name',
|
2010-12-19 10:39:15 +01:00
|
|
|
self.username)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
self.username = app.config.get_per('accounts',
|
|
|
|
app.ZEROCONF_ACC_NAME, 'name')
|
2010-02-08 15:08:40 +01:00
|
|
|
# END __init__
|
|
|
|
|
|
|
|
def check_jid(self, jid):
|
|
|
|
return jid
|
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def get_own_jid(self, *args, **kwargs):
|
2017-12-18 22:16:22 +01:00
|
|
|
return nbxmpp.JID(self.username + '@' + self.host)
|
2017-12-18 14:44:38 +01:00
|
|
|
|
2017-04-25 14:43:54 +02:00
|
|
|
def reconnect(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
# Do not try to reco while we are already trying
|
|
|
|
self.time_to_reconnect = None
|
2017-09-22 00:06:35 +02:00
|
|
|
log.debug('reconnect')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.disconnect()
|
|
|
|
self.change_status(self.old_show, self.status)
|
|
|
|
|
|
|
|
def disable_account(self):
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def _on_resolve_timeout(self):
|
|
|
|
if self.connected:
|
2018-05-20 17:03:07 +02:00
|
|
|
if not self.connection.resolve_all():
|
2018-12-03 14:11:25 +01:00
|
|
|
self.disconnect()
|
2018-05-20 17:03:07 +02:00
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
diffs = self.roster.getDiffs()
|
|
|
|
for key in diffs:
|
|
|
|
self.roster.setItem(key)
|
2018-07-26 00:12:04 +02:00
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
2018-09-18 12:06:01 +02:00
|
|
|
'roster-info', conn=self, jid=key,
|
2018-07-26 00:12:04 +02:00
|
|
|
nickname=self.roster.getName(key), sub='both',
|
|
|
|
ask='no', groups=self.roster.getGroups(key),
|
|
|
|
avatar_sha=None))
|
2019-01-02 16:10:30 +01:00
|
|
|
self._on_presence(key)
|
2010-02-08 15:08:40 +01:00
|
|
|
#XXX open chat windows don't get refreshed (full name), add that
|
|
|
|
return self.call_resolve_timeout
|
|
|
|
|
|
|
|
# callbacks called from zeroconf
|
|
|
|
def _on_new_service(self, jid):
|
|
|
|
self.roster.setItem(jid)
|
2018-07-26 00:12:04 +02:00
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'roster-info', conn=self, jid=jid,
|
|
|
|
nickname=self.roster.getName(jid), sub='both',
|
|
|
|
ask='no', groups=self.roster.getGroups(jid),
|
|
|
|
avatar_sha=None))
|
2019-01-02 16:10:30 +01:00
|
|
|
self._on_presence(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _on_remove_service(self, jid):
|
|
|
|
self.roster.delItem(jid)
|
|
|
|
# 'NOTIFY' (account, (jid, status, status message, resource, priority,
|
2019-01-01 16:19:35 +01:00
|
|
|
# keyID, timestamp))
|
2019-01-02 16:10:30 +01:00
|
|
|
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)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _on_name_conflictCB(self, alt_name):
|
|
|
|
self.disconnect()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2019-01-03 08:33:35 +01:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('zeroconf-name-conflict',
|
|
|
|
conn=self,
|
|
|
|
alt_name=alt_name))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _on_error(self, message):
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='avahi-error', args=message))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def connect(self, show='online', msg=''):
|
|
|
|
self.get_config_values_or_default()
|
|
|
|
if not self.connection:
|
|
|
|
self.connection = client_zeroconf.ClientZeroconf(self)
|
|
|
|
if not zeroconf.test_zeroconf():
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = 'offline'
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None,
|
2010-12-01 17:57:54 +01:00
|
|
|
conn=self, title=_('Could not connect to "%s"') % self.name,
|
|
|
|
msg=_('Please check if Avahi or Bonjour is installed.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
|
|
|
return
|
|
|
|
result = self.connection.connect(show, msg)
|
|
|
|
if not result:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = 'offline'
|
|
|
|
if result is False:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None,
|
2010-12-01 17:57:54 +01:00
|
|
|
conn=self, title=_('Could not start local service'),
|
|
|
|
msg=_('Unable to bind to port %d.' % self.port)))
|
2010-02-08 15:08:40 +01:00
|
|
|
else: # result is None
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None,
|
2010-12-01 17:57:54 +01:00
|
|
|
conn=self, title=_('Could not start local service'),
|
2018-05-20 17:03:07 +02:00
|
|
|
msg=_('Please check if avahi/bonjour-daemon is running.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.connection.announce()
|
|
|
|
self.roster = self.connection.getRoster()
|
2018-07-26 00:12:04 +02:00
|
|
|
app.nec.push_incoming_event(NetworkEvent('roster-received', conn=self,
|
|
|
|
roster=self.roster.copy(), received_from_server=True))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# display contacts already detected and resolved
|
|
|
|
for jid in self.roster.keys():
|
2018-07-26 00:12:04 +02:00
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'roster-info', conn=self, jid=jid,
|
|
|
|
nickname=self.roster.getName(jid), sub='both',
|
|
|
|
ask='no', groups=self.roster.getGroups(jid),
|
|
|
|
avatar_sha=None))
|
2019-01-02 16:10:30 +01:00
|
|
|
self._on_presence(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connected = STATUS_LIST.index(show)
|
|
|
|
|
|
|
|
# refresh all contacts data every five seconds
|
|
|
|
self.call_resolve_timeout = True
|
2013-07-29 18:35:49 +02:00
|
|
|
GLib.timeout_add_seconds(5, self._on_resolve_timeout)
|
2010-02-08 15:08:40 +01:00
|
|
|
return True
|
|
|
|
|
2018-12-03 14:11:25 +01:00
|
|
|
def disconnect(self, reconnect=True, immediately=True):
|
|
|
|
log.info('Start disconnecting zeroconf')
|
|
|
|
if reconnect:
|
|
|
|
if app.account_is_connected(self.name):
|
|
|
|
# we cannot change our status to offline or connecting
|
|
|
|
# after we auth to server
|
|
|
|
self.old_show = STATUS_LIST[self.connected]
|
|
|
|
|
|
|
|
# random number to show we wait network manager to send
|
|
|
|
# us a reconenct
|
|
|
|
self.time_to_reconnect = 5
|
|
|
|
else:
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = 0
|
|
|
|
if self.connection:
|
|
|
|
self.connection.disconnect()
|
|
|
|
self.connection = None
|
|
|
|
# stop calling the timeout
|
|
|
|
self.call_resolve_timeout = False
|
2018-12-03 14:11:25 +01:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
|
|
|
|
|
|
|
def _on_disconnect(self):
|
|
|
|
self.connected = 0
|
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def reannounce(self):
|
|
|
|
if self.connected:
|
|
|
|
txt = {}
|
2017-08-13 13:18:56 +02:00
|
|
|
txt['1st'] = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_first_name')
|
2017-08-13 13:18:56 +02:00
|
|
|
txt['last'] = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_last_name')
|
2017-08-13 13:18:56 +02:00
|
|
|
txt['jid'] = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'zeroconf_jabber_id')
|
2017-08-13 13:18:56 +02:00
|
|
|
txt['email'] = app.config.get_per('accounts',
|
|
|
|
app.ZEROCONF_ACC_NAME, 'zeroconf_email')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.reannounce(txt)
|
|
|
|
|
|
|
|
def update_details(self):
|
|
|
|
if self.connection:
|
2017-08-13 13:18:56 +02:00
|
|
|
port = app.config.get_per('accounts', app.ZEROCONF_ACC_NAME,
|
2010-02-08 15:08:40 +01:00
|
|
|
'custom_port')
|
|
|
|
if port != self.port:
|
|
|
|
self.port = port
|
|
|
|
last_msg = self.connection.last_msg
|
|
|
|
self.disconnect()
|
|
|
|
if not self.connect(self.status, last_msg):
|
|
|
|
return
|
|
|
|
if self.status != 'invisible':
|
|
|
|
self.connection.announce()
|
|
|
|
else:
|
|
|
|
self.reannounce()
|
|
|
|
|
|
|
|
def connect_and_init(self, show, msg, sign_msg):
|
|
|
|
# to check for errors from zeroconf
|
|
|
|
check = True
|
|
|
|
if not self.connect(show, msg):
|
|
|
|
return
|
|
|
|
if show != 'invisible':
|
|
|
|
check = self.connection.announce()
|
|
|
|
else:
|
|
|
|
self.connected = STATUS_LIST.index(show)
|
2019-01-03 08:33:35 +01:00
|
|
|
app.nec.push_incoming_event(NetworkEvent('signed-in', conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# stay offline when zeroconf does something wrong
|
|
|
|
if check:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show=show))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# show notification that avahi or system bus is down
|
2013-12-31 14:28:24 +01:00
|
|
|
self.connected = 0
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = 'offline'
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-12-01 17:57:54 +01:00
|
|
|
title=_('Could not change status of account "%s"') % self.name,
|
|
|
|
msg=_('Please check if avahi-daemon is running.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _change_to_invisible(self, msg):
|
|
|
|
if self.connection.remove_announce():
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='invisible'))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# show notification that avahi or system bus is down
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = 'offline'
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-12-01 17:57:54 +01:00
|
|
|
title=_('Could not change status of account "%s"') % self.name,
|
|
|
|
msg=_('Please check if avahi-daemon is running.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _change_from_invisible(self):
|
|
|
|
self.connection.announce()
|
|
|
|
|
2017-12-18 22:46:11 +01:00
|
|
|
def _update_status(self, show, msg, idle_time=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connection.set_show_msg(show, msg):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show=show))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# show notification that avahi or system bus is down
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = 'offline'
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-12-01 17:57:54 +01:00
|
|
|
title=_('Could not change status of account "%s"') % self.name,
|
|
|
|
msg=_('Please check if avahi-daemon is running.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-20 17:03:07 +02:00
|
|
|
def _nec_message_outgoing(self, obj):
|
|
|
|
if obj.account != self.name:
|
|
|
|
return
|
|
|
|
self._prepare_message(obj)
|
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
def _nec_stanza_message_outgoing(self, obj):
|
2017-08-02 21:03:12 +02:00
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2011-07-19 23:05:32 +02:00
|
|
|
|
2017-07-28 01:37:06 +02:00
|
|
|
def on_send_ok(stanza_id):
|
2018-05-20 17:03:07 +02:00
|
|
|
app.nec.push_incoming_event(MessageSentEvent(None, **vars(obj)))
|
2017-06-14 15:29:19 +02:00
|
|
|
self.log_message(obj, obj.jid)
|
2017-04-06 23:21:19 +02:00
|
|
|
|
|
|
|
def on_send_not_ok(reason):
|
|
|
|
reason += ' ' + _('Your message could not be sent.')
|
2018-05-20 17:03:07 +02:00
|
|
|
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))
|
2017-12-10 14:35:41 +01:00
|
|
|
# Dont propagate event
|
|
|
|
return True
|
2017-04-06 23:21:19 +02:00
|
|
|
|
2018-09-15 12:00:32 +02:00
|
|
|
obj.timestamp = time.time()
|
2017-04-06 23:21:19 +02:00
|
|
|
ret = self.connection.send(
|
|
|
|
obj.msg_iq, obj.message is not None,
|
|
|
|
on_ok=on_send_ok, on_not_ok=on_send_not_ok)
|
|
|
|
|
|
|
|
if ret == -1:
|
|
|
|
# Contact Offline
|
2018-05-20 17:03:07 +02:00
|
|
|
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))
|
2017-12-10 14:35:41 +01:00
|
|
|
# Dont propagate event
|
|
|
|
return True
|
2011-07-19 23:05:32 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_stanza(self, stanza):
|
|
|
|
# send a stanza untouched
|
|
|
|
if not self.connection:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
if not isinstance(stanza, nbxmpp.Node):
|
|
|
|
stanza = nbxmpp.Protocol(node=stanza)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(stanza)
|
|
|
|
|
|
|
|
def _event_dispatcher(self, realm, event, data):
|
|
|
|
CommonConnection._event_dispatcher(self, realm, event, data)
|
|
|
|
if realm == '':
|
2012-12-09 21:37:51 +01:00
|
|
|
if event == nbxmpp.transports_nb.DATA_ERROR:
|
2010-02-08 15:08:40 +01:00
|
|
|
thread_id = data[1]
|
2013-01-01 21:06:16 +01:00
|
|
|
frm = data[0]
|
2010-02-08 15:08:40 +01:00
|
|
|
session = self.get_or_create_session(frm, thread_id)
|
2018-05-20 17:03:07 +02:00
|
|
|
error_message = _(
|
|
|
|
'Connection to host could not be established: '
|
|
|
|
'Timeout while sending data.')
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(MessageErrorEvent(
|
2018-05-20 17:03:07 +02:00
|
|
|
None, conn=self, fjid=frm, error_code=-1,
|
|
|
|
error_msg=error_message, msg=None, time_=None,
|
|
|
|
session=session, zeroconf=True))
|
2006-09-18 00:57:41 +02:00
|
|
|
|
2006-05-29 21:57:39 +02:00
|
|
|
# END ConnectionZeroconf
|