2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2003-2005 Vincent Hanquez <tab AT snarc.org>
|
|
|
|
# Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Copyright (C) 2005 Alex Mauer <hawke AT hawkesnest.net>
|
|
|
|
# Stéphan Kochen <stephan AT kochen.nl>
|
|
|
|
# Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
# Travis Shirk <travis AT pobox.com>
|
|
|
|
# Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
# Copyright (C) 2006 Junglecow J <junglecow AT gmail.com>
|
|
|
|
# Stefan Bethge <stefan AT lanpartei.de>
|
|
|
|
# Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
# Copyright (C) 2007 Tomasz Melcer <liori AT exroot.org>
|
|
|
|
# Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
# Copyright (C) 2007-2008 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
# Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
# Jonathan Schleifer <js-gajim AT webkeks.org>
|
|
|
|
#
|
|
|
|
# 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/>.
|
2005-04-12 13:46:20 +02:00
|
|
|
|
2018-05-31 17:35:46 +02:00
|
|
|
import sys
|
2005-11-04 09:19:15 +01:00
|
|
|
import random
|
2007-06-03 12:04:20 +02:00
|
|
|
import socket
|
2008-12-03 18:38:16 +01:00
|
|
|
import operator
|
2017-01-04 13:13:55 +01:00
|
|
|
import string
|
2007-12-12 09:44:46 +01:00
|
|
|
import time
|
2015-07-31 22:08:00 +02:00
|
|
|
import hashlib
|
2011-12-18 23:20:30 +01:00
|
|
|
import json
|
2018-04-23 23:47:17 +02:00
|
|
|
import logging
|
2018-05-01 20:32:36 +02:00
|
|
|
import base64
|
2018-07-23 19:51:25 +02:00
|
|
|
import ssl
|
2017-11-11 21:46:34 +01:00
|
|
|
from functools import partial
|
2018-04-23 23:47:17 +02:00
|
|
|
from string import Template
|
2018-05-01 20:32:36 +02:00
|
|
|
from urllib.request import urlopen
|
|
|
|
from urllib.error import URLError
|
2007-12-12 09:44:46 +01:00
|
|
|
|
2018-05-31 17:35:46 +02:00
|
|
|
if sys.platform in ('win32', 'darwin'):
|
2018-04-23 23:47:17 +02:00
|
|
|
import certifi
|
|
|
|
import OpenSSL.crypto
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2018-04-23 23:47:17 +02:00
|
|
|
from nbxmpp import Smacks
|
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim import common
|
|
|
|
from gajim.common import helpers
|
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 gpg
|
|
|
|
from gajim.common import passwords
|
2018-05-21 02:20:30 +02:00
|
|
|
from gajim.common import idle
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.connection_handlers import *
|
2018-02-21 23:09:30 +01:00
|
|
|
from gajim.common.contacts import GC_Contact
|
2018-07-07 13:52:44 +02:00
|
|
|
from gajim.common import modules
|
2017-01-26 21:34:54 +01:00
|
|
|
|
2013-12-06 21:20:22 +01:00
|
|
|
|
2006-12-23 22:18:07 +01:00
|
|
|
log = logging.getLogger('gajim.c.connection')
|
2006-12-23 00:30:23 +01:00
|
|
|
|
2017-12-16 12:22:38 +01:00
|
|
|
SERVICE_START_TLS = 'xmpp-client'
|
|
|
|
SERVICE_DIRECT_TLS = 'xmpps-client'
|
|
|
|
|
2009-11-18 11:06:09 +01:00
|
|
|
class CommonConnection:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
Common connection class, can be derived for normal connection or zeroconf
|
2010-02-08 15:08:40 +01:00
|
|
|
connection
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2018-06-24 17:00:13 +02:00
|
|
|
self._modules = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
# self.connected:
|
|
|
|
# 0=>offline,
|
|
|
|
# 1=>connection in progress,
|
|
|
|
# 2=>online
|
|
|
|
# 3=>free for chat
|
|
|
|
# ...
|
|
|
|
self.connected = 0
|
|
|
|
self.connection = None # xmpppy ClientCommon instance
|
|
|
|
self.on_purpose = False
|
|
|
|
self.is_zeroconf = False
|
|
|
|
self.password = ''
|
|
|
|
self.server_resource = self._compute_resource()
|
|
|
|
self.gpg = None
|
|
|
|
self.USE_GPG = False
|
2018-04-24 19:36:33 +02:00
|
|
|
if app.is_installed('GPG'):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.USE_GPG = True
|
2017-06-08 18:55:05 +02:00
|
|
|
self.gpg = gpg.GnuPG()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = ''
|
|
|
|
self.old_show = ''
|
2017-08-13 13:18:56 +02:00
|
|
|
self.priority = app.get_priority(name, 'offline')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.time_to_reconnect = None
|
|
|
|
|
|
|
|
self.pep = {}
|
|
|
|
# Do we continue connection when we get roster (send presence,get vcard..)
|
|
|
|
self.continue_connect_info = None
|
|
|
|
|
|
|
|
# Remember where we are in the register agent process
|
|
|
|
self.agent_registrations = {}
|
2018-06-21 01:46:16 +02:00
|
|
|
# To know the groupchat jid associated with a stanza ID. Useful to
|
2010-02-08 15:08:40 +01:00
|
|
|
# request vcard or os info... to a real JID but act as if it comes from
|
|
|
|
# the fake jid
|
|
|
|
self.groupchat_jids = {} # {ID : groupchat_jid}
|
|
|
|
|
2011-08-09 17:14:13 +02:00
|
|
|
self.roster_supported = True
|
2013-07-27 14:14:50 +02:00
|
|
|
self.addressing_supported = False
|
2018-09-01 20:34:56 +02:00
|
|
|
self.avatar_conversion = False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.muc_jid = {} # jid of muc server for each transport type
|
|
|
|
self._stun_servers = [] # STUN servers of our jabber server
|
|
|
|
|
2010-07-02 00:25:08 +02:00
|
|
|
self.awaiting_cids = {} # Used for XEP-0231
|
|
|
|
|
2018-07-28 13:32:37 +02:00
|
|
|
# Tracks the calls of the connect_machine() method
|
|
|
|
self._connect_machine_calls = 0
|
2010-12-11 12:57:09 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.get_config_values_or_default()
|
|
|
|
|
|
|
|
def _compute_resource(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
resource = app.config.get_per('accounts', self.name, 'resource')
|
2010-02-08 15:08:40 +01:00
|
|
|
# All valid resource substitution strings should be added to this hash.
|
|
|
|
if resource:
|
2017-01-04 13:13:55 +01:00
|
|
|
rand = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))
|
2010-02-08 15:08:40 +01:00
|
|
|
resource = Template(resource).safe_substitute({
|
2017-01-04 13:13:55 +01:00
|
|
|
'hostname': socket.gethostname(),
|
|
|
|
'rand': rand
|
2010-02-08 15:08:40 +01:00
|
|
|
})
|
2017-11-20 18:10:51 +01:00
|
|
|
app.config.set_per('accounts', self.name, 'resource', resource)
|
2010-02-08 15:08:40 +01:00
|
|
|
return resource
|
|
|
|
|
|
|
|
def dispatch(self, event, data):
|
|
|
|
"""
|
|
|
|
Always passes account name as first param
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.raise_event(event, self.name, data)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-06-24 17:00:13 +02:00
|
|
|
def get_module(self, name):
|
2018-07-07 13:52:44 +02:00
|
|
|
return modules.get(self.name, name)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2017-04-25 14:43:54 +02:00
|
|
|
def reconnect(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
To be implemented by derived classes
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def quit(self, kill_core):
|
2017-08-13 13:18:56 +02:00
|
|
|
if kill_core and app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
|
|
|
|
def test_gpg_passphrase(self, password):
|
|
|
|
"""
|
|
|
|
Returns 'ok', 'bad_pass' or 'expired'
|
|
|
|
"""
|
|
|
|
if not self.gpg:
|
|
|
|
return False
|
|
|
|
self.gpg.passphrase = password
|
2017-08-13 13:18:56 +02:00
|
|
|
keyID = app.config.get_per('accounts', self.name, 'keyid')
|
2010-02-08 15:08:40 +01:00
|
|
|
signed = self.gpg.sign('test', keyID)
|
|
|
|
self.gpg.password = None
|
|
|
|
if signed == 'KEYEXPIRED':
|
|
|
|
return 'expired'
|
2018-09-18 10:14:04 +02:00
|
|
|
if signed == 'BAD_PASSPHRASE':
|
2010-02-08 15:08:40 +01:00
|
|
|
return 'bad_pass'
|
|
|
|
return 'ok'
|
|
|
|
|
2018-09-18 12:06:01 +02:00
|
|
|
def get_signed_msg(self, msg, callback=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Returns the signed message if possible or an empty string if gpg is not
|
|
|
|
used or None if waiting for passphrase
|
|
|
|
|
|
|
|
callback is the function to call when user give the passphrase
|
|
|
|
"""
|
|
|
|
signed = ''
|
2017-08-13 13:18:56 +02:00
|
|
|
keyID = app.config.get_per('accounts', self.name, 'keyid')
|
2010-02-08 15:08:40 +01:00
|
|
|
if keyID and self.USE_GPG:
|
2017-06-08 18:55:05 +02:00
|
|
|
if self.gpg.passphrase is None and not self.gpg.use_agent:
|
2010-02-08 15:08:40 +01:00
|
|
|
# We didn't set a passphrase
|
|
|
|
return None
|
2010-11-29 18:44:22 +01:00
|
|
|
signed = self.gpg.sign(msg, keyID)
|
|
|
|
if signed == 'BAD_PASSPHRASE':
|
|
|
|
self.USE_GPG = False
|
|
|
|
signed = ''
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BadGPGPassphraseEvent(None,
|
2010-11-29 18:44:22 +01:00
|
|
|
conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
return signed
|
|
|
|
|
|
|
|
def _on_disconnected(self):
|
|
|
|
"""
|
|
|
|
Called when a disconnect request has completed successfully
|
|
|
|
"""
|
|
|
|
self.disconnect(on_purpose=True)
|
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
|
|
|
|
|
|
|
def get_status(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
return app.SHOW_LIST[self.connected]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def check_jid(self, jid):
|
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
This function must be implemented by derived classes. It has to return
|
2010-02-08 15:08:40 +01:00
|
|
|
the valid jid, or raise a helpers.InvalidFormat exception
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
def _prepare_message(self, obj):
|
2017-04-04 18:55:19 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return 1
|
2013-04-25 20:38:18 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if isinstance(obj.jid, list):
|
|
|
|
for jid in obj.jid:
|
2013-07-27 14:14:50 +02:00
|
|
|
try:
|
2017-04-04 18:55:19 +02:00
|
|
|
self.check_jid(jid)
|
2013-07-27 14:14:50 +02:00
|
|
|
except helpers.InvalidFormat:
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='invalid-jid', args=jid))
|
2013-07-27 14:14:50 +02:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
try:
|
2017-04-04 18:55:19 +02:00
|
|
|
self.check_jid(obj.jid)
|
2013-07-27 14:14:50 +02:00
|
|
|
except helpers.InvalidFormat:
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
2017-12-10 22:56:20 +01:00
|
|
|
None, dialog_name='invalid-jid', args=obj.jid))
|
2013-07-27 14:14:50 +02:00
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if obj.message and not obj.xhtml and app.config.get(
|
2010-02-08 15:08:40 +01:00
|
|
|
'rst_formatting_outgoing_messages'):
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.rst_xhtml_generator import create_xhtml
|
2017-04-04 18:55:19 +02:00
|
|
|
obj.xhtml = create_xhtml(obj.message)
|
|
|
|
if not obj.message and obj.chatstate is None and obj.form_node is None:
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2013-07-27 14:14:50 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._build_message_stanza(obj)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-04-16 17:16:41 +02:00
|
|
|
def _build_message_stanza(self, obj):
|
2017-08-13 13:18:56 +02:00
|
|
|
if obj.jid == app.get_jid_from_account(self.name):
|
2017-03-19 19:34:30 +01:00
|
|
|
fjid = obj.jid
|
|
|
|
else:
|
|
|
|
fjid = obj.get_full_jid()
|
2017-04-04 18:55:19 +02:00
|
|
|
|
|
|
|
if obj.type_ == 'chat':
|
2017-04-16 17:16:41 +02:00
|
|
|
msg_iq = nbxmpp.Message(body=obj.message, typ=obj.type_,
|
2017-04-04 18:55:19 +02:00
|
|
|
xhtml=obj.xhtml)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.subject:
|
2017-04-16 17:16:41 +02:00
|
|
|
msg_iq = nbxmpp.Message(body=obj.message, typ='normal',
|
2017-04-04 18:55:19 +02:00
|
|
|
subject=obj.subject, xhtml=obj.xhtml)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-04-16 17:16:41 +02:00
|
|
|
msg_iq = nbxmpp.Message(body=obj.message, typ='normal',
|
2017-04-04 18:55:19 +02:00
|
|
|
xhtml=obj.xhtml)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-06-02 01:29:41 +02:00
|
|
|
if obj.correct_id:
|
|
|
|
msg_iq.setTag('replace', attrs={'id': obj.correct_id},
|
|
|
|
namespace=nbxmpp.NS_CORRECT)
|
|
|
|
|
2017-07-28 01:37:06 +02:00
|
|
|
# XEP-0359
|
|
|
|
obj.stanza_id = self.connection.getAnID()
|
|
|
|
msg_iq.setID(obj.stanza_id)
|
|
|
|
if obj.message:
|
|
|
|
msg_iq.setOriginID(obj.stanza_id)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.form_node:
|
|
|
|
msg_iq.addChild(node=obj.form_node)
|
|
|
|
if obj.label:
|
|
|
|
msg_iq.addChild(node=obj.label)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# XEP-0172: user_nickname
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.user_nick:
|
|
|
|
msg_iq.setTag('nick', namespace=nbxmpp.NS_NICK).setData(
|
|
|
|
obj.user_nick)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# XEP-0203
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.delayed:
|
2017-08-13 13:18:56 +02:00
|
|
|
our_jid = app.get_jid_from_account(self.name) + '/' + \
|
2010-02-08 15:08:40 +01:00
|
|
|
self.server_resource
|
2017-04-04 18:55:19 +02:00
|
|
|
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(obj.delayed))
|
2012-12-09 21:37:51 +01:00
|
|
|
msg_iq.addChild('delay', namespace=nbxmpp.NS_DELAY2,
|
2010-02-08 15:08:40 +01:00
|
|
|
attrs={'from': our_jid, 'stamp': timestamp})
|
|
|
|
|
2012-04-09 13:38:28 +02:00
|
|
|
# XEP-0224
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.attention:
|
2012-12-09 21:37:51 +01:00
|
|
|
msg_iq.setTag('attention', namespace=nbxmpp.NS_ATTENTION)
|
2012-04-09 13:38:28 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if isinstance(obj.jid, list):
|
2013-07-27 14:14:50 +02:00
|
|
|
if self.addressing_supported:
|
2017-08-13 13:18:56 +02:00
|
|
|
msg_iq.setTo(app.config.get_per('accounts', self.name, 'hostname'))
|
2013-07-27 14:14:50 +02:00
|
|
|
addresses = msg_iq.addChild('addresses',
|
|
|
|
namespace=nbxmpp.NS_ADDRESS)
|
2017-04-04 18:55:19 +02:00
|
|
|
for j in obj.jid:
|
2018-09-18 12:06:01 +02:00
|
|
|
addresses.addChild('address',
|
|
|
|
attrs={'type': 'to', 'jid': j})
|
2013-07-27 14:14:50 +02:00
|
|
|
else:
|
|
|
|
iqs = []
|
2017-04-04 18:55:19 +02:00
|
|
|
for j in obj.jid:
|
2013-07-27 14:14:50 +02:00
|
|
|
iq = nbxmpp.Message(node=msg_iq)
|
|
|
|
iq.setTo(j)
|
|
|
|
iqs.append(iq)
|
|
|
|
msg_iq = iqs
|
|
|
|
else:
|
2013-07-28 12:58:15 +02:00
|
|
|
msg_iq.setTo(fjid)
|
2017-04-04 18:55:19 +02:00
|
|
|
r_ = obj.resource
|
|
|
|
if not r_ and obj.jid != fjid: # Only if we're not in a pm
|
2017-08-13 13:18:56 +02:00
|
|
|
r_ = app.get_resource_from_jid(fjid)
|
2014-03-28 21:29:42 +01:00
|
|
|
if r_:
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.get_contact(self.name, obj.jid, r_)
|
2013-07-27 14:14:50 +02:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.get_contact_with_highest_priority(
|
2017-04-04 18:55:19 +02:00
|
|
|
self.name, obj.jid)
|
2013-07-27 14:14:50 +02:00
|
|
|
|
2018-02-21 23:09:30 +01:00
|
|
|
# Mark Message as MUC PM
|
|
|
|
if isinstance(contact, GC_Contact):
|
|
|
|
msg_iq.setTag('x', namespace=nbxmpp.NS_MUC_USER)
|
|
|
|
|
2013-07-27 14:14:50 +02:00
|
|
|
# chatstates - if peer supports xep85, send chatstates
|
|
|
|
# please note that the only valid tag inside a message containing a
|
|
|
|
# <body> tag is the active event
|
2018-09-29 21:48:21 +02:00
|
|
|
if obj.chatstate is not None:
|
2017-04-04 18:55:19 +02:00
|
|
|
msg_iq.setTag(obj.chatstate, namespace=nbxmpp.NS_CHATSTATES)
|
2017-04-16 17:16:41 +02:00
|
|
|
if not obj.message:
|
2016-10-18 16:26:03 +02:00
|
|
|
msg_iq.setTag('no-store',
|
|
|
|
namespace=nbxmpp.NS_MSG_HINTS)
|
2013-07-27 14:14:50 +02:00
|
|
|
|
|
|
|
# XEP-0184
|
2017-08-13 13:18:56 +02:00
|
|
|
if obj.jid != app.get_jid_from_account(self.name):
|
|
|
|
request = app.config.get_per('accounts', self.name,
|
2017-04-16 17:16:41 +02:00
|
|
|
'request_receipt')
|
|
|
|
if obj.message and request:
|
2017-03-20 21:09:13 +01:00
|
|
|
msg_iq.setTag('request', namespace=nbxmpp.NS_RECEIPTS)
|
2013-07-27 14:14:50 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.session:
|
2013-07-27 14:14:50 +02:00
|
|
|
# XEP-0201
|
2017-04-04 18:55:19 +02:00
|
|
|
obj.session.last_send = time.time()
|
|
|
|
msg_iq.setThread(obj.session.thread_id)
|
2013-07-27 14:14:50 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._push_stanza_message_outgoing(obj, msg_iq)
|
|
|
|
|
2017-04-13 22:58:51 +02:00
|
|
|
def _push_stanza_message_outgoing(self, obj, msg_iq):
|
2017-04-06 23:21:19 +02:00
|
|
|
obj.conn = self
|
|
|
|
if isinstance(msg_iq, list):
|
|
|
|
for iq in msg_iq:
|
|
|
|
obj.msg_iq = iq
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(
|
2017-04-06 23:21:19 +02:00
|
|
|
StanzaMessageOutgoingEvent(None, **vars(obj)))
|
|
|
|
else:
|
|
|
|
obj.msg_iq = msg_iq
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(
|
2017-04-06 23:21:19 +02:00
|
|
|
StanzaMessageOutgoingEvent(None, **vars(obj)))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-06-14 15:29:19 +02:00
|
|
|
def log_message(self, obj, jid):
|
2017-04-08 20:10:57 +02:00
|
|
|
if not obj.is_loggable:
|
|
|
|
return
|
|
|
|
|
2017-12-27 13:17:13 +01:00
|
|
|
if obj.session and not obj.session.is_loggable():
|
2017-04-08 20:10:57 +02:00
|
|
|
return
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.config.should_log(self.name, jid):
|
2017-04-08 20:10:57 +02:00
|
|
|
return
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if obj.xhtml and app.config.get('log_xhtml_messages'):
|
2017-08-02 23:01:07 +02:00
|
|
|
obj.message = '<body xmlns="%s">%s</body>' % (nbxmpp.NS_XHTML,
|
|
|
|
obj.xhtml)
|
|
|
|
if obj.message is None:
|
2017-04-08 20:10:57 +02:00
|
|
|
return
|
|
|
|
|
2017-11-17 21:42:44 +01:00
|
|
|
app.logger.insert_into_logs(self.name, jid, obj.timestamp, obj.kind,
|
2017-09-29 00:11:29 +02:00
|
|
|
message=obj.message,
|
|
|
|
subject=obj.subject,
|
|
|
|
additional_data=obj.additional_data,
|
|
|
|
stanza_id=obj.stanza_id)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def unsubscribe_agent(self, agent):
|
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
To be implemented by derived classes
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def update_contact(self, jid, name, groups):
|
2018-07-26 00:12:04 +02:00
|
|
|
if self.connection:
|
2018-09-12 00:01:54 +02:00
|
|
|
self.getRoster().set_item(jid=jid, name=name, groups=groups)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def update_contacts(self, contacts):
|
|
|
|
"""
|
|
|
|
Update multiple roster items
|
|
|
|
"""
|
2018-07-26 00:12:04 +02:00
|
|
|
if self.connection:
|
2018-09-12 00:01:54 +02:00
|
|
|
self.getRoster().set_item_multi(contacts)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def new_account(self, name, config, sync=False):
|
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
To be implemented by derived classes
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _on_new_account(self, con=None, con_type=None):
|
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
To be implemented by derived classes
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def account_changed(self, new_name):
|
|
|
|
self.name = new_name
|
|
|
|
|
|
|
|
def send_agent_status(self, agent, ptype):
|
|
|
|
"""
|
2018-06-21 01:46:16 +02:00
|
|
|
To be implemented by derived classes
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def gpg_passphrase(self, passphrase):
|
|
|
|
if self.gpg:
|
2017-06-08 18:55:05 +02:00
|
|
|
if self.gpg.use_agent:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.gpg.passphrase = None
|
|
|
|
else:
|
|
|
|
self.gpg.passphrase = passphrase
|
|
|
|
|
2016-12-17 14:06:36 +01:00
|
|
|
def ask_gpg_keys(self, keyID=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.gpg:
|
2016-12-17 14:06:36 +01:00
|
|
|
if keyID:
|
|
|
|
return self.gpg.get_key(keyID)
|
2010-11-29 18:44:22 +01:00
|
|
|
return self.gpg.get_keys()
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def ask_gpg_secrete_keys(self):
|
|
|
|
if self.gpg:
|
2010-11-29 18:44:22 +01:00
|
|
|
return self.gpg.get_secret_keys()
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _event_dispatcher(self, realm, event, data):
|
|
|
|
if realm == '':
|
2018-08-18 12:35:21 +02:00
|
|
|
if event == 'STANZA RECEIVED':
|
2018-07-07 18:28:43 +02:00
|
|
|
app.nec.push_incoming_event(StanzaReceivedEvent(
|
|
|
|
None, conn=self, stanza_str=str(data)))
|
2018-08-18 12:35:21 +02:00
|
|
|
elif event == 'DATA SENT':
|
2018-07-07 18:28:43 +02:00
|
|
|
app.nec.push_incoming_event(StanzaSentEvent(
|
|
|
|
None, conn=self, stanza_str=str(data)))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def change_status(self, show, msg, auto=False):
|
|
|
|
if not msg:
|
|
|
|
msg = ''
|
|
|
|
sign_msg = False
|
|
|
|
if not auto and not show == 'offline':
|
|
|
|
sign_msg = True
|
|
|
|
if show != 'invisible':
|
|
|
|
# We save it only when privacy list is accepted
|
|
|
|
self.status = msg
|
|
|
|
if show != 'offline' and self.connected < 1:
|
|
|
|
# set old_show to requested 'show' in case we need to
|
|
|
|
# recconect before we auth to server
|
|
|
|
self.old_show = show
|
|
|
|
self.on_purpose = False
|
|
|
|
self.server_resource = self._compute_resource()
|
2018-04-24 19:36:33 +02:00
|
|
|
if app.is_installed('GPG'):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.USE_GPG = True
|
2017-06-08 18:55:05 +02:00
|
|
|
self.gpg = gpg.GnuPG()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
2014-01-14 22:48:51 +01:00
|
|
|
conn=self, show=show, message=msg))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connect_and_init(show, msg, sign_msg)
|
|
|
|
return
|
|
|
|
|
|
|
|
if show == 'offline':
|
|
|
|
self.connected = 0
|
|
|
|
if self.connection:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
2014-01-14 22:48:51 +01:00
|
|
|
conn=self, show=show, message=msg))
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
p = self.get_module('Presence').get_presence(
|
|
|
|
typ='unavailable',
|
|
|
|
status=msg,
|
|
|
|
caps=False)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connection.RegisterDisconnectHandler(self._on_disconnected)
|
|
|
|
self.connection.send(p, now=True)
|
|
|
|
self.connection.start_disconnect()
|
|
|
|
else:
|
|
|
|
self._on_disconnected()
|
|
|
|
return
|
|
|
|
|
|
|
|
if show != 'offline' and self.connected > 0:
|
|
|
|
# dont'try to connect, when we are in state 'connecting'
|
|
|
|
if self.connected == 1:
|
|
|
|
return
|
|
|
|
if show == 'invisible':
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
2014-01-14 22:48:51 +01:00
|
|
|
conn=self, show=show, message=msg))
|
2010-02-08 15:08:40 +01:00
|
|
|
self._change_to_invisible(msg)
|
|
|
|
return
|
|
|
|
if show not in ['offline', 'online', 'chat', 'away', 'xa', 'dnd']:
|
|
|
|
return -1
|
2017-08-13 13:18:56 +02:00
|
|
|
was_invisible = self.connected == app.SHOW_LIST.index('invisible')
|
|
|
|
self.connected = app.SHOW_LIST.index(show)
|
2017-07-26 22:24:19 +02:00
|
|
|
idle_time = None
|
|
|
|
if auto:
|
2018-04-24 19:36:33 +02:00
|
|
|
if app.is_installed('IDLE') and app.config.get('autoaway'):
|
2018-05-21 02:20:30 +02:00
|
|
|
idle_sec = idle.Monitor.get_idle_sec()
|
2017-07-26 22:24:19 +02:00
|
|
|
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
|
|
|
|
time.gmtime(time.time() - idle_sec))
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
2014-01-14 22:48:51 +01:00
|
|
|
conn=self, show=show, message=msg))
|
2010-02-08 15:08:40 +01:00
|
|
|
if was_invisible:
|
|
|
|
self._change_from_invisible()
|
2017-07-26 22:24:19 +02:00
|
|
|
self._update_status(show, msg, idle_time=idle_time)
|
2009-11-18 11:06:09 +01:00
|
|
|
|
|
|
|
class Connection(CommonConnection, ConnectionHandlers):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
CommonConnection.__init__(self, name)
|
|
|
|
ConnectionHandlers.__init__(self)
|
2018-05-17 19:44:10 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# increase/decrease default timeout for server responses
|
|
|
|
self.try_connecting_for_foo_secs = 45
|
|
|
|
# holds the actual hostname to which we are connected
|
|
|
|
self.connected_hostname = None
|
2017-07-27 18:55:13 +02:00
|
|
|
# Holds the full jid we received on the bind event
|
|
|
|
self.registered_name = None
|
2010-02-08 15:08:40 +01:00
|
|
|
self.last_time_to_reconnect = None
|
|
|
|
self.new_account_info = None
|
|
|
|
self.new_account_form = None
|
|
|
|
self.last_sent = []
|
|
|
|
self.password = passwords.get_password(name)
|
|
|
|
|
|
|
|
self.music_track_info = 0
|
2018-07-05 19:14:03 +02:00
|
|
|
|
2017-04-19 17:42:08 +02:00
|
|
|
self.register_supported = False
|
2010-02-08 15:08:40 +01:00
|
|
|
# Do we auto accept insecure connection
|
|
|
|
self.connection_auto_accepted = False
|
|
|
|
self.pasword_callback = None
|
|
|
|
|
|
|
|
self.on_connect_success = None
|
|
|
|
self.on_connect_failure = None
|
|
|
|
self.retrycount = 0
|
|
|
|
self.available_transports = {} # list of available transports on this
|
|
|
|
# server {'icq': ['icq.server.com', 'icq2.server.com'], }
|
2018-07-27 15:46:02 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.streamError = ''
|
2018-03-28 11:36:14 +02:00
|
|
|
self.removing_account = False
|
2011-08-27 22:41:55 +02:00
|
|
|
|
2018-05-01 20:32:36 +02:00
|
|
|
# We only request POSH once
|
|
|
|
self._posh_requested = False
|
|
|
|
# Fingerprints received via POSH
|
|
|
|
self._posh_hashes = []
|
|
|
|
# The SSL Errors that we can override with POSH
|
|
|
|
self._posh_errors = [18, 19]
|
|
|
|
|
2011-08-27 22:41:55 +02:00
|
|
|
self.sm = Smacks(self) # Stream Management
|
|
|
|
|
2018-07-07 13:52:44 +02:00
|
|
|
# Register all modules
|
|
|
|
modules.register(self)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
|
2011-05-18 19:44:43 +02:00
|
|
|
self._nec_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('gc-message-outgoing', ged.OUT_CORE,
|
2013-07-19 11:52:37 +02:00
|
|
|
self._nec_gc_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('gc-stanza-message-outgoing', ged.OUT_CORE,
|
2016-06-24 20:12:37 +02:00
|
|
|
self._nec_gc_stanza_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('stanza-message-outgoing',
|
2015-09-23 22:39:11 +02:00
|
|
|
ged.OUT_CORE, self._nec_stanza_message_outgoing)
|
2010-02-08 15:08:40 +01:00
|
|
|
# END __init__
|
|
|
|
|
2011-01-06 16:50:38 +01:00
|
|
|
def cleanup(self):
|
|
|
|
ConnectionHandlers.cleanup(self)
|
2018-07-07 13:52:44 +02:00
|
|
|
modules.unregister(self)
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('message-outgoing', ged.OUT_CORE,
|
2011-05-18 19:44:43 +02:00
|
|
|
self._nec_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('gc-message-outgoing', ged.OUT_CORE,
|
2013-07-19 11:52:37 +02:00
|
|
|
self._nec_gc_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('gc-stanza-message-outgoing', ged.OUT_CORE,
|
2016-06-24 20:12:37 +02:00
|
|
|
self._nec_gc_stanza_message_outgoing)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('stanza-message-outgoing', ged.OUT_CORE,
|
2015-09-23 22:39:11 +02:00
|
|
|
self._nec_stanza_message_outgoing)
|
2010-11-24 22:48:39 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_config_values_or_default(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'keep_alives_enabled'):
|
|
|
|
self.keepalives = app.config.get_per('accounts', self.name,
|
2010-02-08 15:08:40 +01:00
|
|
|
'keep_alive_every_foo_secs')
|
|
|
|
else:
|
|
|
|
self.keepalives = 0
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'ping_alives_enabled'):
|
|
|
|
self.pingalives = app.config.get_per('accounts', self.name,
|
2010-02-08 15:08:40 +01:00
|
|
|
'ping_alive_every_foo_secs')
|
|
|
|
else:
|
|
|
|
self.pingalives = 0
|
2017-08-13 13:18:56 +02:00
|
|
|
self.client_cert = app.config.get_per('accounts', self.name,
|
2010-04-18 20:43:40 +02:00
|
|
|
'client_cert')
|
2011-07-26 22:44:11 +02:00
|
|
|
self.client_cert_passphrase = ''
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def check_jid(self, jid):
|
|
|
|
return helpers.parse_jid(jid)
|
|
|
|
|
2017-09-03 22:35:56 +02:00
|
|
|
def get_own_jid(self, warn=False):
|
2017-07-23 09:55:13 +02:00
|
|
|
"""
|
2017-07-27 18:55:13 +02:00
|
|
|
Return the last full JID we received on a bind event.
|
|
|
|
In case we were never connected it returns the bare JID from config.
|
2017-07-23 09:55:13 +02:00
|
|
|
"""
|
2017-07-27 18:55:13 +02:00
|
|
|
if self.registered_name:
|
|
|
|
# This returns the full jid we received on the bind event
|
|
|
|
return self.registered_name
|
2018-09-18 10:14:04 +02:00
|
|
|
|
|
|
|
if warn:
|
|
|
|
log.warning('only bare JID available')
|
2018-09-18 13:38:22 +02:00
|
|
|
# This returns the bare jid
|
2018-09-18 10:14:04 +02:00
|
|
|
return nbxmpp.JID(app.get_jid_from_account(self.name))
|
2017-07-23 09:55:13 +02: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
|
|
|
|
if self.connected < 2: # connection failed
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Reconnect')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = 1
|
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='connecting'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.retrycount += 1
|
|
|
|
self.on_connect_auth = self._discover_server_at_connection
|
|
|
|
self.connect_and_init(self.old_show, self.status, self.USE_GPG)
|
|
|
|
else:
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Reconnect successfull')
|
2010-02-08 15:08:40 +01:00
|
|
|
# reconnect succeeded
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
self.retrycount = 0
|
|
|
|
|
|
|
|
# We are doing disconnect at so many places, better use one function in all
|
|
|
|
def disconnect(self, on_purpose=False):
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Disconnect: on_purpose: %s', on_purpose)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.music_track_changed(None, None, self.name)
|
2018-07-03 00:10:55 +02:00
|
|
|
self.get_module('PEP').reset_stored_publish()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.on_purpose = on_purpose
|
|
|
|
self.connected = 0
|
|
|
|
self.time_to_reconnect = None
|
2018-06-30 19:23:10 +02:00
|
|
|
self.get_module('VCardAvatars').avatar_advertised = False
|
2011-06-11 22:29:08 +02:00
|
|
|
if on_purpose:
|
|
|
|
self.sm = Smacks(self)
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connection:
|
|
|
|
# make sure previous connection is completely closed
|
2017-08-13 13:18:56 +02:00
|
|
|
app.proxy65_manager.disconnect(self.connection)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.terminate_sessions()
|
|
|
|
self.remove_all_transfers()
|
|
|
|
self.connection.disconnect()
|
2018-07-07 13:52:44 +02:00
|
|
|
ConnectionHandlers._unregister_handlers(self)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection = None
|
2015-07-26 14:05:47 +02:00
|
|
|
|
2011-06-08 05:50:45 +02:00
|
|
|
def set_oldst(self): # Set old state
|
|
|
|
if self.old_show:
|
2017-08-13 13:18:56 +02:00
|
|
|
self.connected = app.SHOW_LIST.index(self.old_show)
|
2018-10-03 07:53:30 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(
|
2018-10-03 08:07:29 +02:00
|
|
|
None, conn=self, show=self.old_show))
|
2011-06-08 05:50:45 +02:00
|
|
|
else: # we default to online
|
|
|
|
self.connected = 2
|
2018-10-03 07:53:30 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(
|
|
|
|
None, conn=self, show=app.SHOW_LIST[self.connected]))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-04-25 14:43:54 +02:00
|
|
|
def disconnectedReconnCB(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Called when we are disconnected
|
|
|
|
"""
|
|
|
|
log.info('disconnectedReconnCB called')
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
# we cannot change our status to offline or connecting
|
|
|
|
# after we auth to server
|
2017-08-13 13:18:56 +02:00
|
|
|
self.old_show = app.SHOW_LIST[self.connected]
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = 0
|
|
|
|
if not self.on_purpose:
|
2011-06-11 05:07:34 +02:00
|
|
|
if not (self.sm and self.sm.resumption):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2011-08-22 09:45:51 +02:00
|
|
|
show='offline'))
|
2012-02-09 04:06:29 +01:00
|
|
|
else:
|
2012-03-15 04:49:37 +01:00
|
|
|
self.sm.enabled = False
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2012-02-09 04:06:29 +01:00
|
|
|
show='error'))
|
2015-07-28 20:19:19 +02:00
|
|
|
if self.connection:
|
|
|
|
self.connection.UnregisterDisconnectHandler(
|
2017-04-25 14:43:54 +02:00
|
|
|
self.disconnectedReconnCB)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'autoreconnect'):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = -1
|
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='error'))
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.status_before_autoaway[self.name]:
|
2010-02-08 15:08:40 +01:00
|
|
|
# We were auto away. So go back online
|
2017-08-13 13:18:56 +02:00
|
|
|
self.status = app.status_before_autoaway[self.name]
|
|
|
|
app.status_before_autoaway[self.name] = ''
|
2010-02-08 15:08:40 +01:00
|
|
|
self.old_show = 'online'
|
2017-04-25 14:43:54 +02:00
|
|
|
# this check has moved from reconnect method
|
2016-09-18 15:15:10 +02:00
|
|
|
# do exponential backoff until less than 5 minutes
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.retrycount < 2 or self.last_time_to_reconnect is None:
|
|
|
|
self.last_time_to_reconnect = 5
|
2018-05-17 21:13:37 +02:00
|
|
|
self.last_time_to_reconnect += random.randint(0, 5)
|
2016-09-18 15:15:10 +02:00
|
|
|
if self.last_time_to_reconnect < 200:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.last_time_to_reconnect *= 1.5
|
|
|
|
self.time_to_reconnect = int(self.last_time_to_reconnect)
|
|
|
|
log.info("Reconnect to %s in %ss", self.name, self.time_to_reconnect)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.idlequeue.set_alarm(self._reconnect_alarm,
|
2010-02-08 15:08:40 +01:00
|
|
|
self.time_to_reconnect)
|
|
|
|
elif self.on_connect_failure:
|
|
|
|
self.on_connect_failure()
|
|
|
|
self.on_connect_failure = None
|
|
|
|
else:
|
|
|
|
# show error dialog
|
|
|
|
self._connection_lost()
|
|
|
|
else:
|
2018-07-07 20:00:09 +02:00
|
|
|
self.disconnect()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.on_purpose = False
|
|
|
|
# END disconnectedReconnCB
|
|
|
|
|
|
|
|
def _connection_lost(self):
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('_connection_lost')
|
2018-09-18 12:06:01 +02:00
|
|
|
self.disconnect(on_purpose=False)
|
2018-03-28 11:36:14 +02:00
|
|
|
if self.removing_account:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-11-15 17:03:38 +01:00
|
|
|
title=_('Connection with account "%s" has been lost') % self.name,
|
|
|
|
msg=_('Reconnect manually.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-10-05 21:25:15 +02:00
|
|
|
def _on_resume_failed(self):
|
|
|
|
# SM resume failed, set all MUCs offline
|
|
|
|
# and lose the presence state of all contacts
|
|
|
|
app.nec.push_incoming_event(OurShowEvent(
|
|
|
|
None, conn=self, show='offline'))
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _event_dispatcher(self, realm, event, data):
|
|
|
|
CommonConnection._event_dispatcher(self, realm, event, data)
|
2018-10-05 21:25:15 +02:00
|
|
|
if realm == nbxmpp.NS_STREAM_MGMT:
|
|
|
|
if event == 'RESUME FAILED':
|
|
|
|
log.info('Resume failed')
|
|
|
|
self._on_resume_failed()
|
|
|
|
|
|
|
|
elif realm == nbxmpp.NS_REGISTER:
|
2012-12-09 21:37:51 +01:00
|
|
|
if event == nbxmpp.features_nb.REGISTER_DATA_RECEIVED:
|
2010-02-08 15:08:40 +01:00
|
|
|
# data is (agent, DataFrom, is_form, error_msg)
|
|
|
|
if self.new_account_info and \
|
|
|
|
self.new_account_info['hostname'] == data[0]:
|
|
|
|
# it's a new account
|
|
|
|
if not data[1]: # wrong answer
|
2010-11-07 18:26:31 +01:00
|
|
|
reason = _('Server %(name)s answered wrongly to '
|
|
|
|
'register request: %(error)s') % {'name': data[0],
|
|
|
|
'error': data[3]}
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(AccountNotCreatedEvent(
|
2010-11-14 12:22:45 +01:00
|
|
|
None, conn=self, reason=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
is_form = data[2]
|
|
|
|
conf = data[1]
|
2018-09-17 23:03:45 +02:00
|
|
|
if data[4] != '':
|
2010-11-14 12:22:45 +01:00
|
|
|
helpers.replace_dataform_media(conf, data[4])
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.new_account_form:
|
|
|
|
def _on_register_result(result):
|
2012-12-09 21:37:51 +01:00
|
|
|
if not nbxmpp.isResultNode(result):
|
2018-03-24 17:55:32 +01:00
|
|
|
reason = result.getErrorMsg() or result.getError()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(AccountNotCreatedEvent(
|
2018-03-24 17:55:32 +01:00
|
|
|
None, conn=self, reason=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2018-04-24 19:36:33 +02:00
|
|
|
if app.is_installed('GPG'):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.USE_GPG = True
|
2017-06-08 18:55:05 +02:00
|
|
|
self.gpg = gpg.GnuPG()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(
|
2010-11-07 18:26:31 +01:00
|
|
|
AccountCreatedEvent(None, conn=self,
|
2018-09-18 12:06:01 +02:00
|
|
|
account_info=self.new_account_info))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.new_account_info = None
|
|
|
|
self.new_account_form = None
|
|
|
|
if self.connection:
|
|
|
|
self.connection.UnregisterDisconnectHandler(
|
|
|
|
self._on_new_account)
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
# it's the second time we get the form, we have info user
|
|
|
|
# typed, so send them
|
|
|
|
if is_form:
|
|
|
|
#TODO: Check if form has changed
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_REGISTER,
|
2010-11-14 12:22:45 +01:00
|
|
|
to=self._hostname)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq.setTag('query').addChild(node=self.new_account_form)
|
|
|
|
self.connection.SendAndCallForResponse(iq,
|
|
|
|
_on_register_result)
|
|
|
|
else:
|
2013-01-04 23:11:07 +01:00
|
|
|
if list(self.new_account_form.keys()).sort() != \
|
|
|
|
list(conf.keys()).sort():
|
2010-02-08 15:08:40 +01:00
|
|
|
# requested config has changed since first connection
|
2010-11-07 18:26:31 +01:00
|
|
|
reason = _('Server %s provided a different '
|
|
|
|
'registration form') % data[0]
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(AccountNotCreatedEvent(
|
2010-11-07 18:26:31 +01:00
|
|
|
None, conn=self, reason=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.register(self.connection,
|
2010-02-08 15:08:40 +01:00
|
|
|
self._hostname, self.new_account_form,
|
|
|
|
_on_register_result)
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(NewAccountConnectedEvent(None,
|
2010-11-07 18:50:24 +01:00
|
|
|
conn=self, config=conf, is_form=is_form))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.UnregisterDisconnectHandler(
|
2010-11-07 18:50:24 +01:00
|
|
|
self._on_new_account)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
return
|
|
|
|
if not data[1]: # wrong answer
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='invalid-answer',
|
|
|
|
kwargs={'name': data[0], 'error': data[3]}))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
def _select_next_host(self, hosts):
|
|
|
|
"""
|
|
|
|
Selects the next host according to RFC2782 p.3 based on it's priority.
|
|
|
|
Chooses between hosts with the same priority randomly, where the
|
|
|
|
probability of being selected is proportional to the weight of the host
|
|
|
|
"""
|
|
|
|
hosts_by_prio = sorted(hosts, key=operator.itemgetter('prio'))
|
|
|
|
|
|
|
|
try:
|
|
|
|
lowest_prio = hosts_by_prio[0]['prio']
|
|
|
|
except IndexError:
|
|
|
|
raise ValueError("No hosts to choose from!")
|
|
|
|
|
|
|
|
hosts_lowest_prio = [h for h in hosts_by_prio if h['prio'] == lowest_prio]
|
|
|
|
|
|
|
|
if len(hosts_lowest_prio) == 1:
|
|
|
|
return hosts_lowest_prio[0]
|
2018-09-18 10:14:04 +02:00
|
|
|
|
|
|
|
rndint = random.randint(0, sum(h['weight'] for h in hosts_lowest_prio))
|
|
|
|
weightsum = 0
|
|
|
|
for host in sorted(hosts_lowest_prio,
|
|
|
|
key=operator.itemgetter('weight')):
|
|
|
|
weightsum += host['weight']
|
|
|
|
if weightsum >= rndint:
|
|
|
|
return host
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-11-10 20:37:48 +01:00
|
|
|
def connect(self, data=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-02-04 23:29:45 +01:00
|
|
|
Start a connection to the XMPP server
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
Returns connection, and connection type ('tls', 'ssl', 'plain', '') data
|
2017-06-06 21:24:27 +02:00
|
|
|
MUST contain hostname, proxy, use_custom_host, custom_host (if
|
2010-02-08 15:08:40 +01:00
|
|
|
use_custom_host), custom_port (if use_custom_host)
|
|
|
|
"""
|
|
|
|
if self.connection:
|
|
|
|
return self.connection, ''
|
|
|
|
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Connect')
|
2011-06-16 02:37:07 +02:00
|
|
|
if self.sm.resuming and self.sm.location:
|
2011-08-22 09:45:51 +02:00
|
|
|
# If resuming and server gave a location, connect from there
|
2011-06-16 02:37:07 +02:00
|
|
|
hostname = self.sm.location
|
2017-08-13 13:18:56 +02:00
|
|
|
self.try_connecting_for_foo_secs = app.config.get_per('accounts',
|
2011-08-22 09:45:51 +02:00
|
|
|
self.name, 'try_connecting_for_foo_secs')
|
2011-06-16 02:37:07 +02:00
|
|
|
use_custom = False
|
2011-11-10 20:37:48 +01:00
|
|
|
proxy = helpers.get_proxy_info(self.name)
|
2011-08-22 09:45:51 +02:00
|
|
|
|
2011-06-16 02:37:07 +02:00
|
|
|
elif data:
|
2010-02-08 15:08:40 +01:00
|
|
|
hostname = data['hostname']
|
|
|
|
self.try_connecting_for_foo_secs = 45
|
|
|
|
p = data['proxy']
|
2017-08-13 13:18:56 +02:00
|
|
|
if p and p in app.config.get_per('proxies'):
|
2011-11-10 20:37:48 +01:00
|
|
|
proxy = {}
|
2017-08-13 13:18:56 +02:00
|
|
|
proxyptr = app.config.get_per('proxies', p)
|
2011-11-10 20:37:48 +01:00
|
|
|
for key in proxyptr.keys():
|
2012-04-11 21:30:05 +02:00
|
|
|
proxy[key] = proxyptr[key]
|
2011-11-10 20:37:48 +01:00
|
|
|
else:
|
|
|
|
proxy = None
|
2010-02-08 15:08:40 +01:00
|
|
|
use_srv = True
|
|
|
|
use_custom = data['use_custom_host']
|
|
|
|
if use_custom:
|
|
|
|
custom_h = data['custom_host']
|
|
|
|
custom_p = data['custom_port']
|
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
hostname = app.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
self.try_connecting_for_foo_secs = app.config.get_per('accounts',
|
2010-02-08 15:08:40 +01:00
|
|
|
self.name, 'try_connecting_for_foo_secs')
|
2011-11-10 20:37:48 +01:00
|
|
|
proxy = helpers.get_proxy_info(self.name)
|
2017-08-13 13:18:56 +02:00
|
|
|
use_srv = app.config.get_per('accounts', self.name, 'use_srv')
|
2018-07-07 20:00:09 +02:00
|
|
|
|
|
|
|
use_custom = app.config.get_per('accounts', self.name,
|
|
|
|
'use_custom_host')
|
|
|
|
if use_custom:
|
|
|
|
custom_h = app.config.get_per('accounts', self.name,
|
|
|
|
'custom_host')
|
|
|
|
custom_p = app.config.get_per('accounts', self.name,
|
|
|
|
'custom_port')
|
|
|
|
try:
|
|
|
|
helpers.idn_to_ascii(custom_h)
|
|
|
|
except Exception:
|
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='invalid-custom-hostname',
|
|
|
|
args=custom_h))
|
|
|
|
use_custom = False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# create connection if it doesn't already exist
|
|
|
|
self.connected = 1
|
2011-11-10 20:37:48 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
h = hostname
|
|
|
|
p = 5222
|
|
|
|
ssl_p = 5223
|
|
|
|
if use_custom:
|
|
|
|
h = custom_h
|
|
|
|
p = custom_p
|
|
|
|
ssl_p = custom_p
|
2018-07-07 20:00:09 +02:00
|
|
|
use_srv = False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# SRV resolver
|
|
|
|
self._proxy = proxy
|
2017-12-16 12:22:38 +01:00
|
|
|
self._hosts = [
|
2018-01-03 22:09:33 +01:00
|
|
|
{'host': h, 'port': p, 'type': 'tls', 'prio': 10, 'weight': 10, 'alpn': False},
|
|
|
|
{'host': h, 'port': ssl_p, 'type': 'ssl', 'prio': 10, 'weight': 10, 'alpn': False},
|
|
|
|
{'host': h, 'port': p, 'type': 'plain', 'prio': 10, 'weight': 10, 'alpn': False}
|
2017-12-16 12:22:38 +01:00
|
|
|
]
|
2010-02-08 15:08:40 +01:00
|
|
|
self._hostname = hostname
|
2017-09-17 14:02:01 +02:00
|
|
|
|
2018-03-02 17:38:33 +01:00
|
|
|
if h:
|
|
|
|
app.resolver.resolve('_xmppconnect.' + helpers.idn_to_ascii(h),
|
|
|
|
self._on_resolve_txt, type_='txt')
|
|
|
|
|
2017-09-17 14:02:01 +02:00
|
|
|
if use_srv and self._proxy is None:
|
2017-12-16 12:22:38 +01:00
|
|
|
self._srv_hosts = []
|
|
|
|
|
|
|
|
services = [SERVICE_START_TLS, SERVICE_DIRECT_TLS]
|
|
|
|
self._num_pending_srv_records = len(services)
|
|
|
|
|
|
|
|
for service in services:
|
|
|
|
record_name = '_' + service + '._tcp.' + helpers.idn_to_ascii(h)
|
|
|
|
app.resolver.resolve(record_name, self._on_resolve_srv)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-12-16 12:22:38 +01:00
|
|
|
self._connect_to_next_host()
|
|
|
|
|
|
|
|
def _append_srv_record(self, record, con_type):
|
|
|
|
tmp = record.copy()
|
|
|
|
tmp['type'] = con_type
|
|
|
|
|
|
|
|
if tmp in self._srv_hosts:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._srv_hosts.append(tmp)
|
|
|
|
|
|
|
|
def _on_resolve_srv(self, host, result):
|
|
|
|
for record in result:
|
|
|
|
service = host[1:]
|
|
|
|
if service.startswith(SERVICE_START_TLS):
|
2018-01-03 22:09:33 +01:00
|
|
|
record['alpn'] = False
|
2017-12-16 12:22:38 +01:00
|
|
|
self._append_srv_record(record, 'tls')
|
|
|
|
self._append_srv_record(record, 'plain')
|
|
|
|
elif service.startswith(SERVICE_DIRECT_TLS):
|
2018-01-03 22:09:33 +01:00
|
|
|
record['alpn'] = True
|
2017-12-16 12:22:38 +01:00
|
|
|
self._append_srv_record(record, 'ssl')
|
|
|
|
|
|
|
|
self._num_pending_srv_records -= 1
|
|
|
|
if self._num_pending_srv_records:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._srv_hosts:
|
|
|
|
self._hosts = self._srv_hosts.copy()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self._connect_to_next_host()
|
|
|
|
|
2017-11-19 22:35:45 +01:00
|
|
|
def _on_resolve_txt(self, host, result_array):
|
|
|
|
for res in result_array:
|
|
|
|
if res.startswith('_xmpp-client-xbosh='):
|
|
|
|
url = res[19:]
|
|
|
|
found = False
|
|
|
|
proxies = app.config.get_per('proxies')
|
|
|
|
for p in proxies:
|
|
|
|
if app.config.get_per('proxies', p, 'type') == 'bosh' \
|
|
|
|
and app.config.get_per('proxies', p, 'bosh_uri') == url:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
h = app.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
p = 'bosh_' + h
|
|
|
|
i = 0
|
|
|
|
while p in proxies:
|
|
|
|
i += 1
|
|
|
|
p = 'bosh_' + h + str(i)
|
|
|
|
app.config.add_per('proxies', p)
|
|
|
|
app.config.set_per('proxies', p, 'type', 'bosh')
|
|
|
|
app.config.set_per('proxies', p, 'bosh_uri', url)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _connect_to_next_host(self, retry=False):
|
|
|
|
log.debug('Connection to next host')
|
2017-12-16 12:22:38 +01:00
|
|
|
if not self._hosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
if not retry and self.retrycount == 0:
|
|
|
|
log.debug("Out of hosts, giving up connecting to %s", self.name)
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
if self.on_connect_failure:
|
|
|
|
self.on_connect_failure()
|
|
|
|
self.on_connect_failure = None
|
|
|
|
else:
|
|
|
|
# shown error dialog
|
|
|
|
self._connection_lost()
|
|
|
|
else:
|
|
|
|
# try reconnect if connection has failed before auth to server
|
2017-04-25 14:43:54 +02:00
|
|
|
self.disconnectedReconnCB()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-12-16 12:22:38 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
connection_types = ['tls', 'ssl']
|
|
|
|
allow_plaintext_connection = app.config.get_per('accounts', self.name,
|
|
|
|
'allow_plaintext_connection')
|
|
|
|
|
|
|
|
if allow_plaintext_connection:
|
|
|
|
connection_types.append('plain')
|
|
|
|
|
|
|
|
if self._proxy and self._proxy['type'] == 'bosh':
|
|
|
|
# with BOSH, we can't do TLS negotiation with <starttls>, we do only "plain"
|
|
|
|
# connection and TLS with handshake right after TCP connecting ("ssl")
|
|
|
|
scheme = nbxmpp.transports_nb.urisplit(self._proxy['bosh_uri'])[0]
|
|
|
|
if scheme == 'https':
|
|
|
|
connection_types = ['ssl']
|
|
|
|
else:
|
2018-04-29 23:34:46 +02:00
|
|
|
if allow_plaintext_connection:
|
|
|
|
connection_types = ['plain']
|
|
|
|
else:
|
|
|
|
connection_types = []
|
2017-12-16 12:22:38 +01:00
|
|
|
|
|
|
|
host = self._select_next_host(self._hosts)
|
|
|
|
self._hosts.remove(host)
|
|
|
|
|
|
|
|
# Skip record if connection type is not supported.
|
|
|
|
if host['type'] not in connection_types:
|
2018-04-29 23:34:46 +02:00
|
|
|
log.info("Skipping connection record with unsupported type: %s",
|
|
|
|
host['type'])
|
2017-12-16 12:22:38 +01:00
|
|
|
self._connect_to_next_host(retry)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._current_host = host
|
|
|
|
|
|
|
|
self._current_type = self._current_host['type']
|
|
|
|
|
|
|
|
port = self._current_host['port']
|
2018-03-24 00:17:03 +01:00
|
|
|
|
|
|
|
cacerts = ''
|
2018-05-31 17:35:46 +02:00
|
|
|
if sys.platform in ('win32', 'darwin'):
|
2018-03-24 00:17:03 +01:00
|
|
|
cacerts = certifi.where()
|
2018-04-25 20:49:37 +02:00
|
|
|
mycerts = common.configpaths.get('MY_CACERTS')
|
2018-03-24 00:19:27 +01:00
|
|
|
tls_version = app.config.get_per('accounts', self.name, 'tls_version')
|
|
|
|
cipher_list = app.config.get_per('accounts', self.name, 'cipher_list')
|
|
|
|
|
|
|
|
secure_tuple = (self._current_type, cacerts, mycerts, tls_version,
|
|
|
|
cipher_list, self._current_host['alpn'])
|
2017-12-16 12:22:38 +01:00
|
|
|
|
|
|
|
con = nbxmpp.NonBlockingClient(
|
|
|
|
domain=self._hostname,
|
|
|
|
caller=self,
|
|
|
|
idlequeue=app.idlequeue)
|
|
|
|
|
|
|
|
# increase default timeout for server responses
|
|
|
|
nbxmpp.dispatcher_nb.DEFAULT_TIMEOUT_SECONDS = \
|
|
|
|
self.try_connecting_for_foo_secs
|
|
|
|
# FIXME: this is a hack; need a better way
|
2018-09-27 16:03:08 +02:00
|
|
|
if self.on_connect_success == self._on_new_account: # pylint: disable=comparison-with-callable
|
2017-12-16 12:22:38 +01:00
|
|
|
con.RegisterDisconnectHandler(self._on_new_account)
|
|
|
|
|
|
|
|
if self.client_cert and app.config.get_per('accounts', self.name,
|
|
|
|
'client_cert_encrypted'):
|
|
|
|
app.nec.push_incoming_event(ClientCertPassphraseEvent(
|
|
|
|
None, conn=self, con=con, port=port,
|
|
|
|
secure_tuple=secure_tuple))
|
|
|
|
return
|
|
|
|
self.on_client_cert_passphrase('', con, port, secure_tuple)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-07-26 22:44:11 +02:00
|
|
|
def on_client_cert_passphrase(self, passphrase, con, port, secure_tuple):
|
|
|
|
self.client_cert_passphrase = passphrase
|
|
|
|
|
|
|
|
self.log_hosttype_info(port)
|
|
|
|
con.connect(
|
|
|
|
hostname=self._current_host['host'],
|
|
|
|
port=port,
|
|
|
|
on_connect=self.on_connect_success,
|
|
|
|
on_proxy_failure=self.on_proxy_failure,
|
2017-12-16 12:22:38 +01:00
|
|
|
on_connect_failure=self._connect_to_next_host,
|
2012-06-19 19:19:24 +02:00
|
|
|
on_stream_error_cb=self._StreamCB,
|
2011-07-26 22:44:11 +02:00
|
|
|
proxy=self._proxy,
|
2018-01-03 22:09:33 +01:00
|
|
|
secure_tuple=secure_tuple)
|
2011-07-26 22:44:11 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def log_hosttype_info(self, port):
|
|
|
|
msg = '>>>>>> Connecting to %s [%s:%d], type = %s' % (self.name,
|
|
|
|
self._current_host['host'], port, self._current_type)
|
|
|
|
log.info(msg)
|
|
|
|
if self._proxy:
|
|
|
|
msg = '>>>>>> '
|
2018-09-18 12:06:01 +02:00
|
|
|
if self._proxy['type'] == 'bosh':
|
2010-02-08 15:08:40 +01:00
|
|
|
msg = '%s over BOSH %s' % (msg, self._proxy['bosh_uri'])
|
2010-02-10 19:24:11 +01:00
|
|
|
if self._proxy['type'] in ['http', 'socks5'] or self._proxy['bosh_useproxy']:
|
2010-02-08 15:08:40 +01:00
|
|
|
msg = '%s over proxy %s:%s' % (msg, self._proxy['host'], self._proxy['port'])
|
|
|
|
log.info(msg)
|
|
|
|
|
|
|
|
def _connect_failure(self, con_type=None):
|
|
|
|
if not con_type:
|
|
|
|
# we are not retrying, and not conecting
|
|
|
|
if not self.retrycount and self.connected != 0:
|
2018-09-18 12:06:01 +02:00
|
|
|
self.disconnect(on_purpose=True)
|
2013-07-19 21:12:11 +02:00
|
|
|
if self._proxy:
|
|
|
|
pritxt = _('Could not connect to "%(host)s" via proxy "%(proxy)s"') %\
|
|
|
|
{'host': self._hostname, 'proxy': self._proxy['host']}
|
|
|
|
else:
|
|
|
|
pritxt = _('Could not connect to "%(host)s"') % {'host': \
|
|
|
|
self._hostname}
|
2010-02-08 15:08:40 +01:00
|
|
|
sectxt = _('Check your connection or try again later.')
|
|
|
|
if self.streamError:
|
|
|
|
# show error dialog
|
2012-12-09 21:37:51 +01:00
|
|
|
key = nbxmpp.NS_XMPP_STREAMS + ' ' + self.streamError
|
|
|
|
if key in nbxmpp.ERRORS:
|
|
|
|
sectxt2 = _('Server replied: %s') % nbxmpp.ERRORS[key][2]
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(InformationEvent(None,
|
2011-11-22 19:25:15 +01:00
|
|
|
conn=self, level='error', pri_txt=pritxt,
|
|
|
|
sec_txt='%s\n%s' % (sectxt2, sectxt)))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# show popup
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None,
|
2010-11-15 17:03:38 +01:00
|
|
|
conn=self, title=pritxt, msg=sectxt))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def on_proxy_failure(self, reason):
|
2018-09-17 18:57:00 +02:00
|
|
|
log.error('Connection to proxy failed: %s', reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.time_to_reconnect = None
|
|
|
|
self.on_connect_failure = None
|
2018-09-18 12:06:01 +02:00
|
|
|
self.disconnect(on_purpose=True)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-11-15 17:03:38 +01:00
|
|
|
title=_('Connection to proxy failed'), msg=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _connect_success(self, con, con_type):
|
|
|
|
if not self.connected: # We went offline during connecting process
|
|
|
|
# FIXME - not possible, maybe it was when we used threads
|
|
|
|
return
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Connect successfull')
|
2010-02-08 15:08:40 +01:00
|
|
|
_con_type = con_type
|
|
|
|
if _con_type != self._current_type:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.info('Connecting to next host beacuse desired type '
|
|
|
|
'is %s and returned is %s', self._current_type, _con_type)
|
2017-12-16 12:22:38 +01:00
|
|
|
self._connect_to_next_host()
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
con.RegisterDisconnectHandler(self._on_disconnected)
|
2017-08-13 13:18:56 +02:00
|
|
|
if _con_type == 'plain' and app.config.get_per('accounts', self.name,
|
2011-10-31 09:47:01 +01:00
|
|
|
'action_when_plaintext_connection') == 'warn':
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(PlainConnectionEvent(None, conn=self,
|
2010-11-24 15:38:16 +01:00
|
|
|
xmpp_client=con))
|
2010-02-08 15:08:40 +01:00
|
|
|
return True
|
2017-08-13 13:18:56 +02:00
|
|
|
if _con_type == 'plain' and app.config.get_per('accounts', self.name,
|
2011-10-31 09:47:01 +01:00
|
|
|
'action_when_plaintext_connection') == 'disconnect':
|
|
|
|
self.disconnect(on_purpose=True)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2011-10-31 09:47:01 +01:00
|
|
|
show='offline'))
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
if _con_type in ('tls', 'ssl') and con.Connection.ssl_lib != 'PYOPENSSL' \
|
2017-08-13 13:18:56 +02:00
|
|
|
and app.config.get_per('accounts', self.name,
|
2010-02-08 15:08:40 +01:00
|
|
|
'warn_when_insecure_ssl_connection') and \
|
|
|
|
not self.connection_auto_accepted:
|
|
|
|
# Pyopenssl is not used
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(InsecureSSLConnectionEvent(None,
|
2010-11-24 16:32:59 +01:00
|
|
|
conn=self, xmpp_client=con, conn_type=_con_type))
|
2010-02-08 15:08:40 +01:00
|
|
|
return True
|
|
|
|
return self.connection_accepted(con, con_type)
|
|
|
|
|
|
|
|
def connection_accepted(self, con, con_type):
|
|
|
|
if not con or not con.Connection:
|
|
|
|
self.disconnect(on_purpose=True)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-11-15 17:03:38 +01:00
|
|
|
title=_('Could not connect to account %s') % self.name,
|
|
|
|
msg=_('Connection with account %s has been lost. Retry '
|
|
|
|
'connecting.') % self.name))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('Connection accepted')
|
2017-12-16 12:22:38 +01:00
|
|
|
self._hosts = []
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection_auto_accepted = False
|
|
|
|
self.connected_hostname = self._current_host['host']
|
|
|
|
self.on_connect_failure = None
|
|
|
|
con.UnregisterDisconnectHandler(self._on_disconnected)
|
2017-04-25 14:43:54 +02:00
|
|
|
con.RegisterDisconnectHandler(self.disconnectedReconnCB)
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug('Connected to server %s:%s with %s',
|
|
|
|
self._current_host['host'], self._current_host['port'],
|
|
|
|
con_type)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-01 14:03:20 +02:00
|
|
|
self.connection = con
|
|
|
|
|
|
|
|
ssl_errors = con.Connection.ssl_errors
|
|
|
|
ignored_ssl_errors = self._get_ignored_ssl_errors()
|
2018-05-15 22:43:57 +02:00
|
|
|
self._ssl_errors = [n for n in ssl_errors if n not in ignored_ssl_errors]
|
|
|
|
self._ssl_errors.reverse()
|
2018-05-01 14:03:20 +02:00
|
|
|
self.process_ssl_errors()
|
|
|
|
|
|
|
|
def _get_ignored_ssl_errors(self):
|
|
|
|
ignore_ssl_errors = app.config.get_per(
|
|
|
|
'accounts', self.name, 'ignore_ssl_errors').split()
|
|
|
|
return [int(err) for err in ignore_ssl_errors]
|
|
|
|
|
|
|
|
def process_ssl_errors(self):
|
|
|
|
if not self._ssl_errors:
|
|
|
|
self.ssl_certificate_accepted()
|
|
|
|
return
|
|
|
|
|
|
|
|
cert = self.connection.Connection.ssl_certificate
|
|
|
|
errnum = self._ssl_errors.pop()
|
2018-05-01 20:32:36 +02:00
|
|
|
|
|
|
|
# Check if we can verify the cert with POSH
|
|
|
|
if errnum in self._posh_errors:
|
|
|
|
# Request the POSH json file
|
|
|
|
self._get_posh_file(self._hostname)
|
|
|
|
self._posh_requested = True
|
|
|
|
cert_hash256 = self._calculate_cert_sha256(cert)
|
2018-07-09 19:52:39 +02:00
|
|
|
|
2018-05-01 20:32:36 +02:00
|
|
|
if cert_hash256 in self._posh_hashes:
|
|
|
|
# Ignore this error if this cert is
|
|
|
|
# verifyed with POSH
|
|
|
|
self.process_ssl_errors()
|
|
|
|
return
|
|
|
|
|
|
|
|
app.nec.push_incoming_event(SSLErrorEvent(None, conn=self,
|
|
|
|
error_num=errnum,
|
|
|
|
cert=cert))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _calculate_cert_sha256(cert):
|
|
|
|
der_encoded = OpenSSL.crypto.dump_certificate(
|
|
|
|
OpenSSL.crypto.FILETYPE_ASN1, cert)
|
|
|
|
hash_obj = hashlib.sha256(der_encoded)
|
|
|
|
hash256 = base64.b64encode(hash_obj.digest()).decode('utf8')
|
|
|
|
return hash256
|
|
|
|
|
|
|
|
def _get_posh_file(self, hostname=None, redirect=None):
|
|
|
|
if self._posh_requested:
|
|
|
|
# We already have requested POSH
|
|
|
|
return
|
|
|
|
|
|
|
|
if not app.config.get_per('accounts', self.name, 'allow_posh'):
|
|
|
|
return
|
|
|
|
|
|
|
|
if hostname is None and redirect is None:
|
|
|
|
raise ValueError('There must be either a hostname or a url')
|
|
|
|
|
|
|
|
url = redirect
|
|
|
|
if hostname is not None:
|
|
|
|
url = 'https://%s/.well-known/posh/xmpp-client.json' % hostname
|
|
|
|
|
|
|
|
cafile = None
|
2018-05-31 17:35:46 +02:00
|
|
|
if sys.platform in ('win32', 'darwin'):
|
2018-05-01 20:32:36 +02:00
|
|
|
cafile = certifi.where()
|
|
|
|
|
|
|
|
log.info('Request POSH from %s', url)
|
|
|
|
try:
|
|
|
|
file = urlopen(
|
|
|
|
url, cafile=cafile, timeout=2)
|
2018-07-23 19:51:25 +02:00
|
|
|
except (URLError, ssl.CertificateError) as exc:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.info('Error while requesting POSH: %s', exc)
|
2018-05-01 20:32:36 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if file.getcode() != 200:
|
|
|
|
log.info('No POSH file found at %s', url)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
posh = json.loads(file.read())
|
|
|
|
except json.decoder.JSONDecodeError as json_error:
|
|
|
|
log.warning(json_error)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Redirect
|
|
|
|
if 'url' in posh and redirect is None:
|
|
|
|
# We dont allow redirects in redirects
|
|
|
|
log.info('POSH redirect found')
|
|
|
|
self._get_posh_file(redirect=posh['url'])
|
|
|
|
return
|
|
|
|
|
|
|
|
if 'fingerprints' in posh:
|
|
|
|
fingerprints = posh['fingerprints']
|
|
|
|
for fingerprint in fingerprints:
|
|
|
|
if 'sha-256' not in fingerprint:
|
|
|
|
continue
|
|
|
|
self._posh_hashes.append(fingerprint['sha-256'])
|
|
|
|
|
|
|
|
log.info('POSH sha-256 fingerprints found: %s',
|
|
|
|
self._posh_hashes)
|
2018-05-01 14:03:20 +02:00
|
|
|
|
|
|
|
def ssl_certificate_accepted(self):
|
|
|
|
if not self.connection:
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
app.nec.push_incoming_event(
|
|
|
|
ConnectionLostEvent(
|
|
|
|
None, conn=self,
|
|
|
|
title=_('Could not connect to account %s') % self.name,
|
|
|
|
msg=_('Connection with account %s has been lost. '
|
|
|
|
'Retry connecting.') % self.name))
|
|
|
|
return
|
|
|
|
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('SSL Cert accepted')
|
2018-05-01 14:03:20 +02:00
|
|
|
name = None
|
|
|
|
if not app.config.get_per('accounts', self.name, 'anonymous_auth'):
|
2017-08-13 13:18:56 +02:00
|
|
|
name = app.config.get_per('accounts', self.name, 'name')
|
2018-05-01 14:03:20 +02:00
|
|
|
|
|
|
|
self._register_handlers(self.connection, self._current_type)
|
|
|
|
|
|
|
|
auth_mechs = app.config.get_per(
|
|
|
|
'accounts', self.name, 'authentication_mechanisms').split()
|
2014-03-01 21:16:24 +01:00
|
|
|
for mech in auth_mechs:
|
|
|
|
if mech not in nbxmpp.auth_nb.SASL_AUTHENTICATION_MECHANISMS | set(['XEP-0078']):
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning('Unknown authentication mechanisms %s', mech)
|
2018-09-16 01:10:04 +02:00
|
|
|
if not auth_mechs:
|
2014-03-01 21:16:24 +01:00
|
|
|
auth_mechs = None
|
2017-10-16 17:04:35 +02:00
|
|
|
else:
|
|
|
|
auth_mechs = set(auth_mechs)
|
2018-05-01 14:03:20 +02:00
|
|
|
self.connection.auth(user=name,
|
|
|
|
password=self.password,
|
|
|
|
resource=self.server_resource,
|
|
|
|
sasl=True,
|
|
|
|
on_auth=self.__on_auth,
|
|
|
|
auth_mechs=auth_mechs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _register_handlers(self, con, con_type):
|
|
|
|
self.peerhost = con.get_peerhost()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.con_types[self.name] = con_type
|
2010-02-08 15:08:40 +01:00
|
|
|
# notify the gui about con_type
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionTypeEvent(None,
|
2010-11-07 22:45:01 +01:00
|
|
|
conn=self, connection_type=con_type))
|
2010-02-08 15:08:40 +01:00
|
|
|
ConnectionHandlers._register_handlers(self, con, con_type)
|
|
|
|
|
|
|
|
def __on_auth(self, con, auth):
|
2018-07-08 10:29:24 +02:00
|
|
|
log.info('auth')
|
2010-02-08 15:08:40 +01:00
|
|
|
if not con:
|
|
|
|
self.disconnect(on_purpose=True)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
2010-11-15 17:03:38 +01:00
|
|
|
title=_('Could not connect to "%s"') % self._hostname,
|
2013-08-15 10:33:27 +02:00
|
|
|
msg=_('Check your connection or try again later.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.on_connect_auth:
|
|
|
|
self.on_connect_auth(None)
|
|
|
|
self.on_connect_auth = None
|
2015-07-27 13:36:49 +02:00
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if not self.connected: # We went offline during connecting process
|
|
|
|
if self.on_connect_auth:
|
|
|
|
self.on_connect_auth(None)
|
|
|
|
self.on_connect_auth = None
|
|
|
|
return
|
|
|
|
if hasattr(con, 'Resource'):
|
|
|
|
self.server_resource = con.Resource
|
2017-09-06 21:24:12 +02:00
|
|
|
if con._registered_name is not None:
|
|
|
|
log.info('Bound JID: %s', con._registered_name)
|
|
|
|
self.registered_name = con._registered_name
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'anonymous_auth'):
|
2010-02-08 15:08:40 +01:00
|
|
|
# Get jid given by server
|
2017-08-13 13:18:56 +02:00
|
|
|
old_jid = app.get_jid_from_account(self.name)
|
|
|
|
app.config.set_per('accounts', self.name, 'name', con.User)
|
|
|
|
new_jid = app.get_jid_from_account(self.name)
|
|
|
|
app.nec.push_incoming_event(AnonymousAuthEvent(None,
|
2010-10-26 21:25:41 +02:00
|
|
|
conn=self, old_jid=old_jid, new_jid=new_jid))
|
2010-02-08 15:08:40 +01:00
|
|
|
if auth:
|
|
|
|
self.connected = 2
|
|
|
|
self.retrycount = 0
|
|
|
|
if self.on_connect_auth:
|
|
|
|
self.on_connect_auth(con)
|
|
|
|
self.on_connect_auth = None
|
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.config.get_per('accounts', self.name, 'savepass'):
|
2010-08-23 10:04:19 +02:00
|
|
|
# Forget password, it's wrong
|
|
|
|
self.password = None
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug("Couldn't authenticate to %s", self._hostname)
|
2018-09-18 12:06:01 +02:00
|
|
|
self.disconnect(on_purpose=True)
|
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'))
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(InformationEvent(None, conn=self,
|
2011-11-22 19:25:15 +01:00
|
|
|
level='error', pri_txt=_('Authentication failed with "%s"') % \
|
|
|
|
self._hostname, sec_txt=_('Please check your login and password'
|
2012-02-22 10:07:16 +01:00
|
|
|
' for correctness.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.on_connect_auth:
|
|
|
|
self.on_connect_auth(None)
|
|
|
|
self.on_connect_auth = None
|
|
|
|
# END connect
|
|
|
|
|
|
|
|
def send_keepalive(self):
|
|
|
|
# nothing received for the last foo seconds
|
|
|
|
if self.connection:
|
|
|
|
self.connection.send(' ')
|
|
|
|
|
2018-09-18 12:06:01 +02:00
|
|
|
def send_invisible_presence(self, msg, signed, initial=False):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2018-07-22 12:18:24 +02:00
|
|
|
if not self.get_module('PrivacyLists').supported:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show=app.SHOW_LIST[self.connected]))
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='invisibility-not-supported', args=self.name))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# If we are already connected, and privacy rules are supported, send
|
|
|
|
# offline presence first as it's required by XEP-0126
|
2018-07-22 12:18:24 +02:00
|
|
|
if self.connected > 1 and self.get_module('PrivacyLists').supported:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.on_purpose = True
|
2018-09-01 17:07:30 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remove_all_transfers()
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
typ='unavailable',
|
|
|
|
status=msg,
|
|
|
|
caps=False)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# try to set the privacy rule
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('PrivacyLists').set_invisible_rule(
|
2018-07-08 10:29:24 +02:00
|
|
|
callback=self._continue_invisible,
|
|
|
|
msg=msg,
|
|
|
|
signed=signed,
|
|
|
|
initial=initial)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _continue_invisible(self, con, iq_obj, msg, signed, initial):
|
|
|
|
if iq_obj.getType() == 'error': # server doesn't support privacy lists
|
|
|
|
return
|
|
|
|
# active the privacy rule
|
2018-07-08 10:29:24 +02:00
|
|
|
self.get_module('PrivacyLists').set_active_list('invisible')
|
2017-08-13 13:18:56 +02:00
|
|
|
self.connected = app.SHOW_LIST.index('invisible')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = msg
|
2017-08-13 13:18:56 +02:00
|
|
|
priority = app.get_priority(self.name, 'invisible')
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
priority=priority,
|
|
|
|
status=msg,
|
|
|
|
sign=signed)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.priority = priority
|
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
|
|
|
if initial:
|
2018-09-01 20:34:56 +02:00
|
|
|
if not self.avatar_conversion:
|
|
|
|
# ask our VCard
|
|
|
|
self.get_module('VCardTemp').request_vcard()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-07-01 02:16:33 +02:00
|
|
|
# Get bookmarks
|
|
|
|
self.get_module('Bookmarks').get_bookmarks()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Get annotations
|
2018-06-25 20:04:56 +02:00
|
|
|
self.get_module('Annotations').get_annotations()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-07-27 15:46:02 +02:00
|
|
|
# Blocking
|
|
|
|
self.get_module('Blocking').get_blocking_list()
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# Inform GUI we just signed in
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(SignedInEvent(None, conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-09-18 12:06:01 +02:00
|
|
|
def get_signed_presence(self, msg, callback=None):
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'gpg_sign_presence'):
|
2010-02-08 15:08:40 +01:00
|
|
|
return self.get_signed_msg(msg, callback)
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def connect_and_auth(self):
|
|
|
|
self.on_connect_success = self._connect_success
|
|
|
|
self.on_connect_failure = self._connect_failure
|
|
|
|
self.connect()
|
|
|
|
|
|
|
|
def connect_and_init(self, show, msg, sign_msg):
|
|
|
|
self.continue_connect_info = [show, msg, sign_msg]
|
|
|
|
self.on_connect_auth = self._discover_server_at_connection
|
|
|
|
self.connect_and_auth()
|
|
|
|
|
|
|
|
def _discover_server_at_connection(self, con):
|
|
|
|
self.connection = con
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2018-06-24 23:22:49 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.set_send_timeout(self.keepalives, self.send_keepalive)
|
2018-06-24 23:22:49 +02:00
|
|
|
self.connection.set_send_timeout2(
|
|
|
|
self.pingalives, self.get_module('Ping').send_keepalive_ping)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.onreceive(None)
|
2010-02-10 17:59:17 +01:00
|
|
|
|
2011-06-07 05:34:19 +02:00
|
|
|
# If we are not resuming, we ask for discovery info
|
|
|
|
# and archiving preferences
|
2013-02-23 16:41:36 +01:00
|
|
|
if not self.sm.supports_sm or (not self.sm.resuming and self.sm.enabled):
|
2018-07-28 13:32:37 +02:00
|
|
|
# This starts the connect_machine
|
2018-07-22 12:18:24 +02:00
|
|
|
self.get_module('Discovery').discover_server_info()
|
2018-07-27 15:46:02 +02:00
|
|
|
self.get_module('Discovery').discover_account_info()
|
2018-07-29 22:16:06 +02:00
|
|
|
self.get_module('Discovery').discover_server_items()
|
2011-08-22 09:45:51 +02:00
|
|
|
|
2018-07-27 15:46:02 +02:00
|
|
|
self.sm.resuming = False # back to previous state
|
2010-02-08 15:08:40 +01:00
|
|
|
# Discover Stun server(s)
|
2017-09-17 14:02:01 +02:00
|
|
|
if self._proxy is None:
|
|
|
|
hostname = app.config.get_per('accounts', self.name, 'hostname')
|
2018-07-27 15:46:02 +02:00
|
|
|
app.resolver.resolve(
|
|
|
|
'_stun._udp.' + helpers.idn_to_ascii(hostname),
|
|
|
|
self._on_stun_resolved)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _on_stun_resolved(self, host, result_array):
|
2018-09-16 01:10:04 +02:00
|
|
|
if result_array:
|
2010-02-08 15:08:40 +01:00
|
|
|
self._stun_servers = self._hosts = [i for i in result_array]
|
|
|
|
|
2018-07-27 15:46:02 +02:00
|
|
|
@helpers.call_counter
|
2018-07-28 13:32:37 +02:00
|
|
|
def connect_machine(self, restart=False):
|
|
|
|
log.info('Connect machine state: %s', self._connect_machine_calls)
|
|
|
|
if self._connect_machine_calls == 1:
|
2018-07-27 15:46:02 +02:00
|
|
|
self.get_module('MetaContacts').get_metacontacts()
|
2018-07-28 13:32:37 +02:00
|
|
|
elif self._connect_machine_calls == 2:
|
2018-07-27 15:46:02 +02:00
|
|
|
self.get_module('Delimiter').get_roster_delimiter()
|
2018-07-28 13:32:37 +02:00
|
|
|
elif self._connect_machine_calls == 3:
|
2018-07-27 15:46:02 +02:00
|
|
|
self.get_module('Roster').request_roster()
|
2018-07-28 13:32:37 +02:00
|
|
|
elif self._connect_machine_calls == 4:
|
2018-07-27 15:46:02 +02:00
|
|
|
self.send_first_presence()
|
2015-03-07 15:04:41 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_custom_status(self, show, msg, jid):
|
2018-07-27 15:46:02 +02:00
|
|
|
if show not in app.SHOW_LIST:
|
2010-02-08 15:08:40 +01:00
|
|
|
return -1
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
sshow = helpers.get_xmpp_show(show)
|
|
|
|
if not msg:
|
|
|
|
msg = ''
|
|
|
|
if show == 'offline':
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
jid,
|
|
|
|
'unavailable',
|
|
|
|
caps=False,
|
|
|
|
status=msg)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
signed = self.get_signed_presence(msg)
|
2017-08-13 13:18:56 +02:00
|
|
|
priority = app.get_priority(self.name, sshow)
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
jid,
|
|
|
|
priority=priority,
|
|
|
|
show=sshow,
|
|
|
|
status=msg,
|
|
|
|
sign=signed)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _change_to_invisible(self, msg):
|
|
|
|
signed = self.get_signed_presence(msg)
|
|
|
|
self.send_invisible_presence(msg, signed)
|
|
|
|
|
|
|
|
def _change_from_invisible(self):
|
2018-07-22 12:18:24 +02:00
|
|
|
if self.get_module('PrivacyLists').supported:
|
2018-07-08 10:29:24 +02:00
|
|
|
self.get_module('PrivacyLists').set_active_list(None)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-07-26 22:24:19 +02:00
|
|
|
def _update_status(self, show, msg, idle_time=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
xmpp_show = helpers.get_xmpp_show(show)
|
2017-08-13 13:18:56 +02:00
|
|
|
priority = app.get_priority(self.name, xmpp_show)
|
2010-02-08 15:08:40 +01:00
|
|
|
signed = self.get_signed_presence(msg)
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
priority=priority,
|
|
|
|
show=xmpp_show,
|
|
|
|
status=msg,
|
|
|
|
sign=signed,
|
|
|
|
idle_time=idle_time)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connection:
|
|
|
|
self.priority = priority
|
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
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
def send_motd(self, jid, subject='', msg='', xhtml=None):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
msg_iq = nbxmpp.Message(to=jid, body=msg, subject=subject,
|
|
|
|
xhtml=xhtml)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connection.send(msg_iq)
|
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
def _nec_message_outgoing(self, obj):
|
|
|
|
if obj.account != self.name:
|
|
|
|
return
|
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._prepare_message(obj)
|
2016-02-08 22:31:36 +01:00
|
|
|
|
|
|
|
def _nec_stanza_message_outgoing(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2017-05-17 16:58:11 +02:00
|
|
|
|
|
|
|
config_key = '%s-%s' % (self.name, obj.jid)
|
2017-08-13 13:18:56 +02:00
|
|
|
encryption = app.config.get_per('encryption', config_key, 'encryption')
|
2017-05-17 16:58:11 +02:00
|
|
|
if encryption:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.plugin_manager.extension_point(
|
2017-04-13 22:58:51 +02:00
|
|
|
'encrypt' + encryption, self, obj, self.send_message)
|
2017-12-10 14:35:41 +01:00
|
|
|
if not obj.encrypted:
|
|
|
|
# Dont propagate event
|
|
|
|
return True
|
2017-04-13 22:58:51 +02:00
|
|
|
else:
|
|
|
|
self.send_message(obj)
|
|
|
|
|
|
|
|
def send_message(self, obj):
|
2017-08-02 00:08:11 +02:00
|
|
|
obj.timestamp = time.time()
|
2017-07-28 01:37:06 +02:00
|
|
|
obj.stanza_id = self.connection.send(obj.msg_iq, now=obj.now)
|
2016-12-17 14:06:36 +01:00
|
|
|
|
2018-02-26 20:49:01 +01:00
|
|
|
app.nec.push_incoming_event(MessageSentEvent(None, **vars(obj)))
|
2011-05-18 19:44:43 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
if isinstance(obj.jid, list):
|
|
|
|
for j in obj.jid:
|
|
|
|
if obj.session is None:
|
|
|
|
obj.session = self.get_or_create_session(j, '')
|
2017-06-14 15:29:19 +02:00
|
|
|
self.log_message(obj, j)
|
2017-04-06 23:21:19 +02:00
|
|
|
else:
|
2017-06-14 15:29:19 +02:00
|
|
|
self.log_message(obj, obj.jid)
|
2015-09-23 22:39:11 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_stanza(self, stanza):
|
|
|
|
"""
|
|
|
|
Send a stanza untouched
|
|
|
|
"""
|
|
|
|
if not self.connection:
|
|
|
|
return
|
|
|
|
self.connection.send(stanza)
|
|
|
|
|
|
|
|
def unsubscribe_agent(self, agent):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_REGISTER, to=agent)
|
2011-10-31 10:47:28 +01:00
|
|
|
iq.setQuery().setTag('remove')
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
self.awaiting_answers[id_] = (AGENT_REMOVED, agent)
|
|
|
|
self.connection.send(iq)
|
2018-09-12 00:01:54 +02:00
|
|
|
self.getRoster().del_item(agent)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def send_new_account_infos(self, form, is_form):
|
|
|
|
if is_form:
|
|
|
|
# Get username and password and put them in new_account_info
|
|
|
|
for field in form.iter_fields():
|
|
|
|
if field.var == 'username':
|
|
|
|
self.new_account_info['name'] = field.value
|
|
|
|
if field.var == 'password':
|
|
|
|
self.new_account_info['password'] = field.value
|
|
|
|
else:
|
|
|
|
# Get username and password and put them in new_account_info
|
|
|
|
if 'username' in form:
|
|
|
|
self.new_account_info['name'] = form['username']
|
|
|
|
if 'password' in form:
|
|
|
|
self.new_account_info['password'] = form['password']
|
|
|
|
self.new_account_form = form
|
|
|
|
self.new_account(self.name, self.new_account_info)
|
|
|
|
|
|
|
|
def new_account(self, name, config, sync=False):
|
|
|
|
# If a connection already exist we cannot create a new account
|
|
|
|
if self.connection:
|
|
|
|
return
|
|
|
|
self._hostname = config['hostname']
|
|
|
|
self.new_account_info = config
|
|
|
|
self.name = name
|
|
|
|
self.on_connect_success = self._on_new_account
|
|
|
|
self.on_connect_failure = self._on_new_account
|
|
|
|
self.connect(config)
|
2017-11-19 22:35:45 +01:00
|
|
|
app.resolver.resolve('_xmppconnect.' + helpers.idn_to_ascii(
|
|
|
|
self._hostname), self._on_resolve_txt, type_='txt')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _on_new_account(self, con=None, con_type=None):
|
|
|
|
if not con_type:
|
2017-12-16 12:22:38 +01:00
|
|
|
if self._hosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
# There are still other way to try to connect
|
|
|
|
return
|
2010-11-07 18:50:24 +01:00
|
|
|
reason = _('Could not connect to "%s"') % self._hostname
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(NewAccountNotConnectedEvent(None,
|
2010-11-07 18:50:24 +01:00
|
|
|
conn=self, reason=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
self.on_connect_failure = None
|
|
|
|
self.connection = con
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.getRegInfo(con, self._hostname)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-12-15 22:08:42 +01:00
|
|
|
def request_gateway_prompt(self, jid, prompt=None):
|
2010-12-15 21:15:00 +01:00
|
|
|
def _on_prompt_result(resp):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(GatewayPromptReceivedEvent(None,
|
2010-12-15 21:15:00 +01:00
|
|
|
conn=self, stanza=resp))
|
2010-12-15 22:08:42 +01:00
|
|
|
if prompt:
|
|
|
|
typ_ = 'set'
|
|
|
|
else:
|
|
|
|
typ_ = 'get'
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ=typ_, to=jid)
|
|
|
|
query = iq.addChild(name='query', namespace=nbxmpp.NS_GATEWAY)
|
2010-12-15 22:08:42 +01:00
|
|
|
if prompt:
|
|
|
|
query.setTagData('prompt', prompt)
|
2010-12-15 21:15:00 +01:00
|
|
|
self.connection.SendAndCallForResponse(iq, _on_prompt_result)
|
|
|
|
|
2018-07-26 00:12:04 +02:00
|
|
|
def getRoster(self):
|
|
|
|
return self.get_module('Roster')
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_agent_status(self, agent, ptype):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
show = helpers.get_xmpp_show(app.SHOW_LIST[self.connected])
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
agent,
|
|
|
|
ptype,
|
|
|
|
show=show,
|
|
|
|
caps=ptype != 'unavailable')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-06-27 23:09:07 +02:00
|
|
|
def send_captcha(self, jid, form_node):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-06-27 23:09:07 +02:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='set', to=jid)
|
|
|
|
captcha = iq.addChild(name='captcha', namespace=nbxmpp.NS_CAPTCHA)
|
2010-06-27 23:09:07 +02:00
|
|
|
captcha.addChild(node=form_node)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def check_unique_room_id_support(self, server, instance):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='get', to=server)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq.setAttr('id', 'unique1')
|
2012-12-09 21:37:51 +01:00
|
|
|
iq.addChild('unique', namespace=nbxmpp.NS_MUC_UNIQUE)
|
2010-02-08 15:08:40 +01:00
|
|
|
def _on_response(resp):
|
2012-12-09 21:37:51 +01:00
|
|
|
if not nbxmpp.isResultNode(resp):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(UniqueRoomIdNotSupportedEvent(
|
2010-11-24 17:28:18 +01:00
|
|
|
None, conn=self, instance=instance, server=server))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(UniqueRoomIdSupportedEvent(None,
|
2010-11-24 17:28:18 +01:00
|
|
|
conn=self, instance=instance, server=server,
|
|
|
|
room_id=resp.getTag('unique').getData()))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.SendAndCallForResponse(iq, _on_response)
|
|
|
|
|
2012-02-18 19:48:03 +01:00
|
|
|
def join_gc(self, nick, room_jid, password, change_nick=False,
|
|
|
|
rejoin=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
# FIXME: This room JID needs to be normalized; see #1364
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
show = helpers.get_xmpp_show(app.SHOW_LIST[self.connected])
|
2010-02-08 15:08:40 +01:00
|
|
|
if show == 'invisible':
|
|
|
|
# Never join a room when invisible
|
|
|
|
return
|
|
|
|
|
2018-07-22 12:18:24 +02:00
|
|
|
self.get_module('Discovery').disco_muc(
|
2017-11-11 21:46:34 +01:00
|
|
|
room_jid, partial(self._join_gc, nick, show, room_jid,
|
|
|
|
password, change_nick, rejoin))
|
|
|
|
|
|
|
|
def _join_gc(self, nick, show, room_jid, password, change_nick, rejoin):
|
|
|
|
if change_nick:
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
'%s/%s' % (room_jid, nick),
|
|
|
|
show=show,
|
|
|
|
status=self.status)
|
2017-11-11 21:46:34 +01:00
|
|
|
else:
|
2018-09-01 17:07:30 +02:00
|
|
|
self.get_module('MUC').send_muc_join_presence(
|
|
|
|
'%s/%s' % (room_jid, nick),
|
|
|
|
show=show,
|
|
|
|
status=self.status,
|
|
|
|
room_jid=room_jid,
|
|
|
|
password=password,
|
|
|
|
rejoin=rejoin)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2013-07-19 11:52:37 +02:00
|
|
|
def _nec_gc_message_outgoing(self, obj):
|
|
|
|
if obj.account != self.name:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2013-07-19 11:52:37 +02:00
|
|
|
return
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if not obj.xhtml and app.config.get('rst_formatting_outgoing_messages'):
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.rst_xhtml_generator import create_xhtml
|
2013-07-19 11:52:37 +02:00
|
|
|
obj.xhtml = create_xhtml(obj.message)
|
2017-12-27 13:17:13 +01:00
|
|
|
|
2013-07-19 11:52:37 +02:00
|
|
|
msg_iq = nbxmpp.Message(obj.jid, obj.message, typ='groupchat',
|
2016-06-24 20:12:37 +02:00
|
|
|
xhtml=obj.xhtml)
|
2017-06-02 01:29:41 +02:00
|
|
|
|
2017-07-28 01:37:06 +02:00
|
|
|
obj.stanza_id = self.connection.getAnID()
|
|
|
|
msg_iq.setID(obj.stanza_id)
|
|
|
|
if obj.message:
|
|
|
|
msg_iq.setOriginID(obj.stanza_id)
|
|
|
|
|
2017-06-02 01:29:41 +02:00
|
|
|
if obj.correct_id:
|
|
|
|
msg_iq.setTag('replace', attrs={'id': obj.correct_id},
|
|
|
|
namespace=nbxmpp.NS_CORRECT)
|
|
|
|
|
2018-09-29 21:48:21 +02:00
|
|
|
if obj.chatstate is not None:
|
2016-12-24 04:36:21 +01:00
|
|
|
msg_iq.setTag(obj.chatstate, namespace=nbxmpp.NS_CHATSTATES)
|
2018-05-07 18:04:24 +02:00
|
|
|
if not obj.message:
|
|
|
|
msg_iq.setTag('no-store', namespace=nbxmpp.NS_MSG_HINTS)
|
2013-07-19 11:52:37 +02:00
|
|
|
if obj.label is not None:
|
2016-04-03 21:32:53 +02:00
|
|
|
msg_iq.addChild(node=obj.label)
|
2017-06-02 01:29:41 +02:00
|
|
|
|
|
|
|
obj.msg_iq = msg_iq
|
|
|
|
obj.conn = self
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(GcStanzaMessageOutgoingEvent(None, **vars(obj)))
|
2013-07-19 11:52:37 +02:00
|
|
|
|
2016-06-24 20:12:37 +02:00
|
|
|
def _nec_gc_stanza_message_outgoing(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2017-05-17 16:58:11 +02:00
|
|
|
|
|
|
|
config_key = '%s-%s' % (self.name, obj.jid)
|
2017-08-13 13:18:56 +02:00
|
|
|
encryption = app.config.get_per('encryption', config_key, 'encryption')
|
2017-05-17 16:58:11 +02:00
|
|
|
if encryption:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.plugin_manager.extension_point(
|
2017-05-07 14:02:11 +02:00
|
|
|
'gc_encrypt' + encryption, self, obj, self.send_gc_message)
|
|
|
|
else:
|
|
|
|
self.send_gc_message(obj)
|
|
|
|
|
|
|
|
def send_gc_message(self, obj):
|
2017-07-28 01:37:06 +02:00
|
|
|
obj.stanza_id = self.connection.send(obj.msg_iq)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(MessageSentEvent(
|
2016-06-24 20:12:37 +02:00
|
|
|
None, conn=self, jid=obj.jid, message=obj.message, keyID=None,
|
2018-09-29 21:48:21 +02:00
|
|
|
automatic_message=obj.automatic_message,
|
2017-07-28 01:37:06 +02:00
|
|
|
stanza_id=obj.stanza_id, additional_data=obj.additional_data))
|
2016-06-24 20:12:37 +02:00
|
|
|
|
2017-07-26 22:24:19 +02:00
|
|
|
def send_gc_status(self, nick, jid, show, status, auto=False):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
if show == 'invisible':
|
|
|
|
show = 'offline'
|
|
|
|
ptype = None
|
|
|
|
if show == 'offline':
|
|
|
|
ptype = 'unavailable'
|
|
|
|
xmpp_show = helpers.get_xmpp_show(show)
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
idle_time = None
|
|
|
|
if auto and app.is_installed('IDLE') and app.config.get('autoaway'):
|
|
|
|
idle_sec = idle.Monitor.get_idle_sec()
|
|
|
|
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
|
|
|
|
time.gmtime(time.time() - idle_sec))
|
|
|
|
|
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
'%s/%s' % (jid, nick),
|
|
|
|
typ=ptype,
|
|
|
|
show=xmpp_show,
|
|
|
|
status=status,
|
|
|
|
caps=ptype != 'unavailable',
|
|
|
|
idle_time=idle_time)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-02-17 13:35:18 +01:00
|
|
|
def get_password(self, callback, type_):
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('accounts', self.name, 'anonymous_auth') and \
|
2015-08-31 13:47:18 +02:00
|
|
|
type_ != 'ANONYMOUS':
|
2018-09-17 22:20:19 +02:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NonAnonymousServerErrorEvent(None, conn=self))
|
2015-08-31 13:47:18 +02:00
|
|
|
self._on_disconnected()
|
|
|
|
return
|
2010-02-17 13:35:18 +01:00
|
|
|
self.pasword_callback = (callback, type_)
|
2011-12-18 23:20:30 +01:00
|
|
|
if type_ == 'X-MESSENGER-OAUTH2':
|
2017-08-13 13:18:56 +02:00
|
|
|
client_id = app.config.get_per('accounts', self.name,
|
2018-09-17 22:20:19 +02:00
|
|
|
'oauth2_client_id')
|
2017-08-13 13:18:56 +02:00
|
|
|
refresh_token = app.config.get_per('accounts', self.name,
|
2018-09-17 22:20:19 +02:00
|
|
|
'oauth2_refresh_token')
|
2011-12-18 23:20:30 +01:00
|
|
|
if refresh_token:
|
2018-09-17 22:20:19 +02:00
|
|
|
renew_url = (
|
|
|
|
'https://oauth.live.com/token?client_id='
|
|
|
|
'%s&redirect_uri=https%%3A%%2F%%2Foauth.live.'
|
|
|
|
'com%%2Fdesktop&grant_type=refresh_token&'
|
|
|
|
'refresh_token=%s') % (client_id, refresh_token)
|
|
|
|
result = helpers.download_image(self.name, {'src': renew_url})[0]
|
2011-12-18 23:20:30 +01:00
|
|
|
if result:
|
|
|
|
dict_ = json.loads(result)
|
|
|
|
if 'access_token' in dict_:
|
|
|
|
self.set_password(dict_['access_token'])
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
script_url = app.config.get_per('accounts', self.name,
|
2018-09-17 22:20:19 +02:00
|
|
|
'oauth2_redirect_url')
|
|
|
|
token_url = (
|
|
|
|
'https://oauth.live.com/authorize?client_id='
|
|
|
|
'%s&scope=wl.messenger%%20wl.offline_access&'
|
|
|
|
'response_type=code&redirect_uri=%s') % (client_id, script_url)
|
|
|
|
helpers.launch_browser_mailer('url', token_url)
|
2011-12-18 23:20:30 +01:00
|
|
|
self.disconnect(on_purpose=True)
|
2018-09-17 22:20:19 +02:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
Oauth2CredentialsRequiredEvent(None, conn=self))
|
2011-12-18 23:20:30 +01:00
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.password:
|
2010-02-17 13:35:18 +01:00
|
|
|
self.set_password(self.password)
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(PasswordRequiredEvent(None, conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def set_password(self, password):
|
|
|
|
self.password = password
|
|
|
|
if self.pasword_callback:
|
2010-02-17 13:35:18 +01:00
|
|
|
callback, type_ = self.pasword_callback
|
|
|
|
if self._current_type == 'plain' and type_ == 'PLAIN' and \
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.get_per('accounts', self.name,
|
2010-02-17 13:35:18 +01:00
|
|
|
'warn_when_insecure_password'):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(InsecurePasswordEvent(None,
|
2010-11-24 16:18:56 +01:00
|
|
|
conn=self))
|
2010-02-17 13:35:18 +01:00
|
|
|
return
|
|
|
|
callback(password)
|
|
|
|
self.pasword_callback = None
|
|
|
|
|
|
|
|
def accept_insecure_password(self):
|
|
|
|
if self.pasword_callback:
|
2018-09-17 21:11:45 +02:00
|
|
|
callback = self.pasword_callback[0]
|
2010-02-17 13:35:18 +01:00
|
|
|
callback(self.password)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.pasword_callback = None
|
|
|
|
|
|
|
|
def unregister_account(self, on_remove_success):
|
|
|
|
# no need to write this as a class method and keep the value of
|
|
|
|
# on_remove_success as a class property as pass it as an argument
|
|
|
|
def _on_unregister_account_connect(con):
|
|
|
|
self.on_connect_auth = None
|
2018-03-28 11:36:14 +02:00
|
|
|
self.removing_account = True
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.account_is_connected(self.name):
|
|
|
|
hostname = app.config.get_per('accounts', self.name, 'hostname')
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='set', to=hostname)
|
2011-01-05 20:59:10 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
2012-12-09 21:37:51 +01:00
|
|
|
iq.setTag(nbxmpp.NS_REGISTER + ' query').setTag('remove')
|
2011-01-05 20:59:10 +01:00
|
|
|
def _on_answer(con, result):
|
|
|
|
if result.getID() == id_:
|
2010-02-08 15:08:40 +01:00
|
|
|
on_remove_success(True)
|
|
|
|
return
|
2017-12-09 00:15:26 +01:00
|
|
|
app.nec.push_incoming_event(InformationEvent(
|
|
|
|
None, dialog_name='unregister-error',
|
|
|
|
kwargs={'server': hostname, 'error': result.getErrorMsg()}))
|
2010-02-08 15:08:40 +01:00
|
|
|
on_remove_success(False)
|
2011-01-05 20:59:10 +01:00
|
|
|
con.RegisterHandler('iq', _on_answer, 'result', system=True)
|
|
|
|
con.SendAndWaitForResponse(iq)
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
on_remove_success(False)
|
2018-03-28 11:36:14 +02:00
|
|
|
self.removing_account = False
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connected == 0:
|
|
|
|
self.on_connect_auth = _on_unregister_account_connect
|
|
|
|
self.connect_and_auth()
|
|
|
|
else:
|
|
|
|
_on_unregister_account_connect(self.connection)
|
|
|
|
|
|
|
|
def _reconnect_alarm(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.config.get_per('accounts', self.name, 'active'):
|
2010-08-23 14:55:03 +02:00
|
|
|
# Account may have been disabled
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.time_to_reconnect:
|
|
|
|
if self.connected < 2:
|
2017-04-25 14:43:54 +02:00
|
|
|
self.reconnect()
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
self.time_to_reconnect = None
|