2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/connection.py
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2003-2005 Vincent Hanquez <tab AT snarc.org>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005 Alex Mauer <hawke AT hawkesnest.net>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Stéphan Kochen <stephan AT kochen.nl>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Travis Shirk <travis AT pobox.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2006 Junglecow J <junglecow AT gmail.com>
|
2008-08-15 05:20:23 +02:00
|
|
|
## 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>
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-04-12 13:46:20 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-12-12 09:44:46 +01:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-04-12 13:46:20 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-04-12 13:46:20 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-12 09:44:46 +01:00
|
|
|
##
|
2005-04-12 13:46:20 +02:00
|
|
|
|
|
|
|
import os
|
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
|
2008-03-04 00:00:01 +01:00
|
|
|
import locale
|
2009-10-13 19:26:56 +02:00
|
|
|
import hmac
|
2015-07-31 22:08:00 +02:00
|
|
|
import hashlib
|
2011-12-18 23:20:30 +01:00
|
|
|
import json
|
2007-12-12 09:44:46 +01:00
|
|
|
|
2006-12-08 22:19:01 +01:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
randomsource = random.SystemRandom()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2010-02-08 15:08:40 +01:00
|
|
|
randomsource = random.Random()
|
|
|
|
randomsource.seed()
|
2006-03-19 15:54:00 +01:00
|
|
|
|
2005-10-04 20:22:25 +02:00
|
|
|
import signal
|
2005-10-05 13:05:51 +02:00
|
|
|
if os.name != 'nt':
|
2010-02-08 15:08:40 +01:00
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
2005-07-30 12:59:15 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2005-05-27 07:56:17 +02:00
|
|
|
from common import helpers
|
2005-04-14 09:42:26 +02:00
|
|
|
from common import gajim
|
2010-11-30 11:41:14 +01:00
|
|
|
from common import gpg
|
2006-10-06 01:06:54 +02:00
|
|
|
from common import passwords
|
2007-08-09 17:39:18 +02:00
|
|
|
from common import exceptions
|
2012-01-02 16:39:06 +01:00
|
|
|
from common import check_X509
|
2013-01-02 13:54:02 +01:00
|
|
|
from common.connection_handlers import *
|
2005-04-12 13:46:20 +02:00
|
|
|
|
2017-01-26 21:34:54 +01:00
|
|
|
from gtkgui_helpers import get_action
|
|
|
|
|
2013-12-06 21:20:22 +01:00
|
|
|
if gajim.HAVE_PYOPENSSL:
|
|
|
|
import OpenSSL.crypto
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
from nbxmpp import Smacks
|
2007-06-03 12:04:20 +02:00
|
|
|
from string import Template
|
2006-12-23 00:30:23 +01:00
|
|
|
import logging
|
2006-12-23 22:18:07 +01:00
|
|
|
log = logging.getLogger('gajim.c.connection')
|
2006-12-23 00:30:23 +01:00
|
|
|
|
2007-12-12 09:44:46 +01:00
|
|
|
ssl_error = {
|
2017-02-07 21:48:58 +01:00
|
|
|
2: _("Unable to get issuer certificate"),
|
|
|
|
3: _("Unable to get certificate CRL"),
|
|
|
|
4: _("Unable to decrypt certificate's signature"),
|
|
|
|
5: _("Unable to decrypt CRL's signature"),
|
|
|
|
6: _("Unable to decode issuer public key"),
|
|
|
|
7: _("Certificate signature failure"),
|
|
|
|
8: _("CRL signature failure"),
|
|
|
|
9: _("Certificate is not yet valid"),
|
|
|
|
10: _("Certificate has expired"),
|
|
|
|
11: _("CRL is not yet valid"),
|
|
|
|
12: _("CRL has expired"),
|
|
|
|
13: _("Format error in certificate's notBefore field"),
|
|
|
|
14: _("Format error in certificate's notAfter field"),
|
|
|
|
15: _("Format error in CRL's lastUpdate field"),
|
|
|
|
16: _("Format error in CRL's nextUpdate field"),
|
|
|
|
17: _("Out of memory"),
|
|
|
|
18: _("Self signed certificate"),
|
|
|
|
19: _("Self signed certificate in certificate chain"),
|
|
|
|
20: _("Unable to get local issuer certificate"),
|
|
|
|
21: _("Unable to verify the first certificate"),
|
|
|
|
22: _("Certificate chain too long"),
|
|
|
|
23: _("Certificate revoked"),
|
|
|
|
24: _("Invalid CA certificate"),
|
|
|
|
25: _("Path length constraint exceeded"),
|
|
|
|
26: _("Unsupported certificate purpose"),
|
|
|
|
27: _("Certificate not trusted"),
|
|
|
|
28: _("Certificate rejected"),
|
|
|
|
29: _("Subject issuer mismatch"),
|
|
|
|
30: _("Authority and subject key identifier mismatch"),
|
|
|
|
31: _("Authority and issuer serial number mismatch"),
|
|
|
|
32: _("Key usage does not include certificate signing"),
|
|
|
|
50: _("Application verification failure")
|
|
|
|
#100 is for internal usage: host not correct
|
2007-08-09 17:39:18 +02:00
|
|
|
}
|
2009-11-18 11:06:09 +01:00
|
|
|
|
|
|
|
class CommonConnection:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Common connection class, can be derivated for normal connection or zeroconf
|
|
|
|
connection
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
# 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
|
|
|
|
if gajim.HAVE_GPG:
|
|
|
|
self.USE_GPG = True
|
2010-11-30 11:41:14 +01:00
|
|
|
self.gpg = gpg.GnuPG(gajim.config.get('use_gpg_agent'))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = ''
|
|
|
|
self.old_show = ''
|
|
|
|
self.priority = gajim.get_priority(name, 'offline')
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
self.bookmarks = []
|
|
|
|
|
|
|
|
self.blocked_list = []
|
|
|
|
self.blocked_contacts = []
|
|
|
|
self.blocked_groups = []
|
|
|
|
self.blocked_all = False
|
|
|
|
|
2010-06-07 19:11:44 +02:00
|
|
|
self.seclabel_supported = False
|
|
|
|
self.seclabel_catalogues = {}
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.pep_supported = False
|
|
|
|
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 = {}
|
|
|
|
# To know the groupchat jid associated with a sranza ID. Useful to
|
|
|
|
# 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}
|
|
|
|
|
|
|
|
self.privacy_rules_supported = False
|
|
|
|
self.vcard_supported = False
|
|
|
|
self.private_storage_supported = False
|
2010-08-11 20:12:11 +02:00
|
|
|
self.archiving_supported = False
|
2014-11-26 21:41:09 +01:00
|
|
|
self.archiving_313_supported = False
|
|
|
|
self.archiving_136_supported = False
|
2010-05-25 16:33:40 +02:00
|
|
|
self.archive_pref_supported = False
|
2011-08-09 17:14:13 +02:00
|
|
|
self.roster_supported = True
|
2013-03-30 23:13:48 +01:00
|
|
|
self.blocking_supported = False
|
2013-07-27 14:14:50 +02:00
|
|
|
self.addressing_supported = False
|
2014-01-01 20:49:53 +01:00
|
|
|
self.carbons_enabled = 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
|
|
|
|
|
2010-12-11 12:57:09 +01:00
|
|
|
self.nested_group_delimiter = '::'
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.get_config_values_or_default()
|
|
|
|
|
|
|
|
def _compute_resource(self):
|
|
|
|
resource = gajim.config.get_per('accounts', self.name, 'resource')
|
|
|
|
# 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
|
|
|
})
|
|
|
|
return resource
|
|
|
|
|
|
|
|
def dispatch(self, event, data):
|
|
|
|
"""
|
|
|
|
Always passes account name as first param
|
|
|
|
"""
|
|
|
|
gajim.ged.raise_event(event, self.name, data)
|
|
|
|
|
|
|
|
def _reconnect(self):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def quit(self, kill_core):
|
|
|
|
if kill_core and gajim.account_is_connected(self.name):
|
|
|
|
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
|
|
|
|
keyID = gajim.config.get_per('accounts', self.name, 'keyid')
|
|
|
|
signed = self.gpg.sign('test', keyID)
|
|
|
|
self.gpg.password = None
|
|
|
|
if signed == 'KEYEXPIRED':
|
|
|
|
return 'expired'
|
|
|
|
elif signed == 'BAD_PASSPHRASE':
|
|
|
|
return 'bad_pass'
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
def get_signed_msg(self, msg, callback = None):
|
|
|
|
"""
|
|
|
|
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 = ''
|
|
|
|
keyID = gajim.config.get_per('accounts', self.name, 'keyid')
|
|
|
|
if keyID and self.USE_GPG:
|
|
|
|
use_gpg_agent = gajim.config.get('use_gpg_agent')
|
|
|
|
if self.gpg.passphrase is None and not use_gpg_agent:
|
|
|
|
# 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 = ''
|
|
|
|
gajim.nec.push_incoming_event(BadGPGPassphraseEvent(None,
|
|
|
|
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)
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_status(self):
|
|
|
|
return gajim.SHOW_LIST[self.connected]
|
|
|
|
|
|
|
|
def check_jid(self, jid):
|
|
|
|
"""
|
|
|
|
This function must be implemented by derivated classes. It has to return
|
|
|
|
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:
|
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
2017-02-04 23:29:45 +01:00
|
|
|
conn=self, level='error', pri_txt=_('Invalid JID'),
|
|
|
|
sec_txt=_('It is not possible to send a message '
|
2017-04-04 18:55:19 +02:00
|
|
|
'to %s, this JID is not valid.') % 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:
|
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None, conn=self,
|
2017-02-04 23:29:45 +01:00
|
|
|
level='error', pri_txt=_('Invalid JID'), sec_txt=_(
|
2013-07-27 14:14:50 +02:00
|
|
|
'It is not possible to send a message to %s, this JID is not '
|
2017-04-04 18:55:19 +02:00
|
|
|
'valid.') % obj.jid))
|
2013-07-27 14:14:50 +02:00
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.message and not obj.xhtml and gajim.config.get(
|
2010-02-08 15:08:40 +01:00
|
|
|
'rst_formatting_outgoing_messages'):
|
|
|
|
from 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-04 18:55:19 +02:00
|
|
|
if obj.keyID and self.USE_GPG:
|
2017-04-06 23:21:19 +02:00
|
|
|
self._encrypt_message(obj)
|
2011-12-17 14:54:29 +01:00
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._build_message_stanza(obj)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
def _encrypt_message(self, obj):
|
2017-04-04 21:28:47 +02:00
|
|
|
obj.xhtml = None
|
|
|
|
if obj.keyID == 'UNKNOWN':
|
|
|
|
error = _('Neither the remote presence is signed, nor a key was '
|
|
|
|
'assigned.')
|
|
|
|
elif obj.keyID.endswith('MISMATCH'):
|
|
|
|
error = _('The contact\'s key (%s) does not match the key assigned '
|
|
|
|
'in Gajim.' % obj.keyID[:8])
|
|
|
|
else:
|
|
|
|
myKeyID = gajim.config.get_per('accounts', self.name, 'keyid')
|
|
|
|
key_list = [obj.keyID, myKeyID]
|
|
|
|
def _on_encrypted(output):
|
|
|
|
msgenc, error = output
|
|
|
|
if error.startswith('NOT_TRUSTED'):
|
|
|
|
def _on_always_trust(answer):
|
|
|
|
if answer:
|
|
|
|
gajim.thread_interface(
|
|
|
|
self.gpg.encrypt, [obj.message, key_list, True],
|
|
|
|
_on_encrypted, [])
|
|
|
|
else:
|
2017-04-06 23:21:19 +02:00
|
|
|
self._finished_encrypt(obj, msgenc=msgenc,
|
|
|
|
error=error)
|
2017-04-04 21:28:47 +02:00
|
|
|
gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
|
|
|
|
conn=self, keyID=error.split(' ')[-1],
|
|
|
|
callback=_on_always_trust))
|
|
|
|
else:
|
2017-04-06 23:21:19 +02:00
|
|
|
self._finished_encrypt(obj, msgenc=msgenc, error=error)
|
2017-04-04 21:28:47 +02:00
|
|
|
gajim.thread_interface(
|
|
|
|
self.gpg.encrypt, [obj.message, key_list, False],
|
|
|
|
_on_encrypted, [])
|
|
|
|
return
|
2017-04-06 23:21:19 +02:00
|
|
|
self._finished_encrypt(obj, error=error)
|
2017-04-04 21:28:47 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
def _finished_encrypt(self, obj, msgenc=None, error=None):
|
2017-04-04 21:28:47 +02:00
|
|
|
if error:
|
|
|
|
gajim.nec.push_incoming_event(
|
|
|
|
MessageNotSentEvent(
|
|
|
|
None, conn=self, jid=obj.jid, message=obj.message,
|
|
|
|
error=error, time_=time.time(), session=obj.session))
|
|
|
|
return
|
2017-04-06 23:21:19 +02:00
|
|
|
self._build_message_stanza(obj, msgenc)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
def _build_message_stanza(self, obj, msgenc=None):
|
2017-04-04 21:28:47 +02:00
|
|
|
if msgenc:
|
2010-02-08 15:08:40 +01:00
|
|
|
msgtxt = '[This message is *encrypted* (See :XEP:`27`]'
|
|
|
|
lang = os.getenv('LANG')
|
2011-12-17 14:54:29 +01:00
|
|
|
if lang is not None and not lang.startswith('en'):
|
|
|
|
# we're not english: one in locale and one en
|
2010-02-08 15:08:40 +01:00
|
|
|
msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \
|
|
|
|
' (' + msgtxt + ')'
|
2017-04-04 21:28:47 +02:00
|
|
|
else:
|
2017-04-04 18:55:19 +02:00
|
|
|
msgtxt = obj.message
|
2017-04-04 21:28:47 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
fjid = obj.get_full_jid()
|
|
|
|
|
|
|
|
if obj.correction_msg:
|
|
|
|
id_ = obj.correction_msg.getID()
|
|
|
|
if obj.correction_msg.getTag('replace'):
|
|
|
|
obj.correction_msg.delChild('replace')
|
|
|
|
obj.correction_msg.setTag('replace', attrs={'id': id_},
|
2013-04-25 20:38:18 +02:00
|
|
|
namespace=nbxmpp.NS_CORRECT)
|
|
|
|
id2 = self.connection.getAnID()
|
2017-04-04 18:55:19 +02:00
|
|
|
obj.correction_msg.setID(id2)
|
|
|
|
obj.correction_msg.setBody(msgtxt)
|
|
|
|
if obj.xhtml:
|
|
|
|
obj.correction_msg.setXHTML(obj.xhtml)
|
2013-04-25 20:38:18 +02:00
|
|
|
|
2017-04-04 23:21:11 +02:00
|
|
|
if msgenc:
|
|
|
|
encrypted_tag = obj.correction_msg.getTag(
|
|
|
|
'x', namespace=nbxmpp.NS_ENCRYPTED)
|
|
|
|
obj.correction_msg.delChild(encrypted_tag)
|
|
|
|
obj.correction_msg.setTag(
|
|
|
|
'x', namespace=nbxmpp.NS_ENCRYPTED).setData(msgenc)
|
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.session:
|
|
|
|
obj.session.last_send = time.time()
|
2013-04-25 20:38:18 +02:00
|
|
|
|
|
|
|
# XEP-0200
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.session.enable_encryption:
|
|
|
|
obj.correction_msg = obj.session.encrypt_stanza(obj.correction_msg)
|
2013-04-25 20:38:18 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._push_stanza_message_outgoing(obj, obj.correction_msg)
|
2013-04-25 20:38:18 +02:00
|
|
|
return
|
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.type_ == 'chat':
|
|
|
|
msg_iq = nbxmpp.Message(body=msgtxt, typ=obj.type_,
|
|
|
|
xhtml=obj.xhtml)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.subject:
|
2013-07-27 14:14:50 +02:00
|
|
|
msg_iq = nbxmpp.Message(body=msgtxt, 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:
|
2013-07-27 14:14:50 +02:00
|
|
|
msg_iq = nbxmpp.Message(body=msgtxt, typ='normal',
|
2017-04-04 18:55:19 +02:00
|
|
|
xhtml=obj.xhtml)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.msg_id:
|
|
|
|
msg_iq.setID(obj.msg_id)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if msgenc:
|
2017-04-04 23:21:11 +02:00
|
|
|
msg_iq.setTag('x', namespace=nbxmpp.NS_ENCRYPTED).setData(msgenc)
|
2010-02-08 15:08:40 +01: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:
|
2010-02-08 15:08:40 +01:00
|
|
|
our_jid = gajim.get_jid_from_account(self.name) + '/' + \
|
|
|
|
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:
|
|
|
|
msg_iq.setTo(gajim.config.get_per('accounts', self.name, 'hostname'))
|
|
|
|
addresses = msg_iq.addChild('addresses',
|
|
|
|
namespace=nbxmpp.NS_ADDRESS)
|
2017-04-04 18:55:19 +02:00
|
|
|
for j in obj.jid:
|
2013-07-27 14:14:50 +02:00
|
|
|
addresses.addChild('address', attrs = {'type': 'to',
|
|
|
|
'jid': j})
|
|
|
|
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
|
2014-06-01 16:51:51 +02:00
|
|
|
r_ = gajim.get_resource_from_jid(fjid)
|
2014-03-28 21:29:42 +01:00
|
|
|
if r_:
|
2017-04-04 18:55:19 +02:00
|
|
|
contact = gajim.contacts.get_contact(self.name, obj.jid, r_)
|
2013-07-27 14:14:50 +02:00
|
|
|
else:
|
|
|
|
contact = gajim.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
|
|
|
|
|
|
|
# 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
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.chatstate and contact and contact.supports(nbxmpp.NS_CHATSTATES):
|
|
|
|
msg_iq.setTag(obj.chatstate, namespace=nbxmpp.NS_CHATSTATES)
|
2016-10-18 16:26:03 +02:00
|
|
|
only_chatste = False
|
|
|
|
if not msgtxt:
|
|
|
|
only_chatste = True
|
2017-04-04 18:55:19 +02:00
|
|
|
if only_chatste and not obj.session.enable_encryption:
|
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
|
|
|
|
if msgtxt and gajim.config.get_per('accounts', self.name,
|
|
|
|
'request_receipt') and contact and contact.supports(
|
|
|
|
nbxmpp.NS_RECEIPTS):
|
|
|
|
msg_iq.setTag('request', namespace=nbxmpp.NS_RECEIPTS)
|
|
|
|
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.forward_from:
|
2013-07-27 14:14:50 +02:00
|
|
|
addresses = msg_iq.addChild('addresses',
|
|
|
|
namespace=nbxmpp.NS_ADDRESS)
|
|
|
|
addresses.addChild('address', attrs = {'type': 'ofrom',
|
2017-04-04 18:55:19 +02:00
|
|
|
'jid': obj.forward_from})
|
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
|
|
|
|
|
|
|
# XEP-0200
|
2017-04-04 18:55:19 +02:00
|
|
|
if obj.session.enable_encryption:
|
|
|
|
msg_iq = obj.session.encrypt_stanza(msg_iq)
|
2014-01-01 20:49:53 +01:00
|
|
|
if self.carbons_enabled:
|
|
|
|
msg_iq.addChild(name='private',
|
2016-10-18 16:26:03 +02:00
|
|
|
namespace=nbxmpp.NS_CARBONS)
|
2015-09-13 13:57:04 +02:00
|
|
|
msg_iq.addChild(name='no-permanent-store',
|
2016-10-18 16:26:03 +02:00
|
|
|
namespace=nbxmpp.NS_MSG_HINTS)
|
2014-11-11 15:28:24 +01:00
|
|
|
msg_iq.addChild(name='no-copy',
|
2016-10-18 16:26:03 +02:00
|
|
|
namespace=nbxmpp.NS_MSG_HINTS)
|
|
|
|
if only_chatste:
|
|
|
|
msg_iq.addChild(name='no-store',
|
|
|
|
namespace=nbxmpp.NS_MSG_HINTS)
|
2013-07-27 14:14:50 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
self._push_stanza_message_outgoing(obj, msg_iq)
|
|
|
|
|
|
|
|
def _push_stanza_message_outgoing(self, obj, msg_iq):
|
|
|
|
obj.conn = self
|
|
|
|
if isinstance(msg_iq, list):
|
|
|
|
for iq in msg_iq:
|
|
|
|
obj.msg_iq = iq
|
|
|
|
gajim.nec.push_incoming_event(
|
|
|
|
StanzaMessageOutgoingEvent(None, **vars(obj)))
|
|
|
|
else:
|
|
|
|
obj.msg_iq = msg_iq
|
|
|
|
gajim.nec.push_incoming_event(
|
|
|
|
StanzaMessageOutgoingEvent(None, **vars(obj)))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def log_message(self, jid, msg, forward_from, session, original_message,
|
2017-02-08 03:12:41 +01:00
|
|
|
subject, type_, xhtml=None, additional_data=None):
|
|
|
|
if additional_data is None:
|
|
|
|
additional_data = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
if not forward_from and session and session.is_loggable():
|
|
|
|
ji = gajim.get_jid_without_resource(jid)
|
|
|
|
if gajim.config.should_log(self.name, ji):
|
|
|
|
log_msg = msg
|
|
|
|
if original_message is not None:
|
|
|
|
log_msg = original_message
|
|
|
|
if log_msg:
|
|
|
|
if type_ == 'chat':
|
|
|
|
kind = 'chat_msg_sent'
|
|
|
|
else:
|
|
|
|
kind = 'single_msg_sent'
|
|
|
|
try:
|
2011-05-30 19:47:05 +02:00
|
|
|
if xhtml and gajim.config.get('log_xhtml_messages'):
|
|
|
|
log_msg = '<body xmlns="%s">%s</body>' % (
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_XHTML, xhtml)
|
2016-09-05 01:43:39 +02:00
|
|
|
gajim.logger.write(kind, jid, log_msg, subject=subject, additional_data=additional_data)
|
2013-01-01 23:18:36 +01:00
|
|
|
except exceptions.PysqliteOperationalError as e:
|
2010-03-09 21:48:57 +01:00
|
|
|
self.dispatch('DB_ERROR', (_('Disk Write Error'),
|
|
|
|
str(e)))
|
2010-02-08 15:08:40 +01:00
|
|
|
except exceptions.DatabaseMalformed:
|
|
|
|
pritext = _('Database Error')
|
2011-05-31 17:46:49 +02:00
|
|
|
sectext = _('The database file (%s) cannot be read. Try'
|
|
|
|
' to repair it (see '
|
|
|
|
'http://trac.gajim.org/wiki/DatabaseBackup)'
|
|
|
|
' or remove it (all history will be lost).') % \
|
|
|
|
common.logger.LOG_DB_PATH
|
2010-03-09 21:48:57 +01:00
|
|
|
self.dispatch('DB_ERROR', (pritext, sectext))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def ack_subscribed(self, jid):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def ack_unsubscribed(self, jid):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-02-08 03:12:41 +01:00
|
|
|
def request_subscription(self, jid, msg='', name='', groups=None,
|
2010-02-08 15:08:40 +01:00
|
|
|
auto_auth=False):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def send_authorization(self, jid):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def refuse_authorization(self, jid):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def unsubscribe(self, jid, remove_auth = True):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def unsubscribe_agent(self, agent):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def update_contact(self, jid, name, groups):
|
2011-08-09 17:14:13 +02:00
|
|
|
if self.connection and self.roster_supported:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.getRoster().setItem(jid=jid, name=name, groups=groups)
|
|
|
|
|
|
|
|
def update_contacts(self, contacts):
|
|
|
|
"""
|
|
|
|
Update multiple roster items
|
|
|
|
"""
|
2011-08-09 17:14:13 +02:00
|
|
|
if self.connection and self.roster_supported:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.getRoster().setItemMulti(contacts)
|
|
|
|
|
|
|
|
def new_account(self, name, config, sync=False):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _on_new_account(self, con=None, con_type=None):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def account_changed(self, new_name):
|
|
|
|
self.name = new_name
|
|
|
|
|
2010-03-10 21:58:14 +01:00
|
|
|
def request_last_status_time(self, jid, resource, groupchat_jid=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-03-10 21:58:14 +01:00
|
|
|
groupchat_jid is used when we want to send a request to a real jid and
|
|
|
|
act as if the answer comes from the groupchat_jid
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-03-10 21:58:14 +01:00
|
|
|
return
|
|
|
|
to_whom_jid = jid
|
|
|
|
if resource:
|
|
|
|
to_whom_jid += '/' + resource
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(to=to_whom_jid, typ='get', queryNS=nbxmpp.NS_LAST)
|
2010-03-10 21:58:14 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
if groupchat_jid:
|
|
|
|
self.groupchat_jids[id_] = groupchat_jid
|
|
|
|
self.last_ids.append(id_)
|
|
|
|
self.connection.send(iq)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def request_os_info(self, jid, resource):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_settings(self):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_bookmarks(self):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def store_bookmarks(self):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_metacontacts(self):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def send_agent_status(self, agent, ptype):
|
|
|
|
"""
|
|
|
|
To be implemented by derivated classes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def gpg_passphrase(self, passphrase):
|
|
|
|
if self.gpg:
|
|
|
|
use_gpg_agent = gajim.config.get('use_gpg_agent')
|
|
|
|
if use_gpg_agent:
|
|
|
|
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 load_roster_from_db(self):
|
|
|
|
# Do nothing by default
|
|
|
|
return
|
|
|
|
|
|
|
|
def _event_dispatcher(self, realm, event, data):
|
|
|
|
if realm == '':
|
2012-12-09 21:37:51 +01:00
|
|
|
if event == nbxmpp.transports_nb.DATA_RECEIVED:
|
2010-11-09 20:58:11 +01:00
|
|
|
gajim.nec.push_incoming_event(StanzaReceivedEvent(None,
|
2013-01-01 21:06:16 +01:00
|
|
|
conn=self, stanza_str=data))
|
2012-12-09 21:37:51 +01:00
|
|
|
elif event == nbxmpp.transports_nb.DATA_SENT:
|
2010-11-09 20:58:11 +01:00
|
|
|
gajim.nec.push_incoming_event(StanzaSentEvent(None, conn=self,
|
2012-12-30 23:49:05 +01:00
|
|
|
stanza_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()
|
|
|
|
if gajim.HAVE_GPG:
|
|
|
|
self.USE_GPG = True
|
2010-11-30 11:41:14 +01:00
|
|
|
self.gpg = gpg.GnuPG(gajim.config.get('use_gpg_agent'))
|
2014-01-14 22:48:51 +01:00
|
|
|
gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
|
|
|
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:
|
2014-01-14 22:48:51 +01:00
|
|
|
gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
|
|
|
conn=self, show=show, message=msg))
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(typ = 'unavailable')
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p, False)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
|
|
|
|
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':
|
2014-01-14 22:48:51 +01:00
|
|
|
gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
|
|
|
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
|
|
|
|
was_invisible = self.connected == gajim.SHOW_LIST.index('invisible')
|
|
|
|
self.connected = gajim.SHOW_LIST.index(show)
|
2014-01-14 22:48:51 +01:00
|
|
|
gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
|
|
|
|
conn=self, show=show, message=msg))
|
2010-02-08 15:08:40 +01:00
|
|
|
if was_invisible:
|
|
|
|
self._change_from_invisible()
|
|
|
|
self._update_status(show, msg)
|
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)
|
|
|
|
# this property is used to prevent double connections
|
|
|
|
self.last_connection = None # last ClientCommon instance
|
|
|
|
# If we succeed to connect, remember it so next time we try (after a
|
|
|
|
# disconnection) we try only this type.
|
|
|
|
self.last_connection_type = None
|
|
|
|
self.lang = None
|
|
|
|
if locale.getdefaultlocale()[0]:
|
|
|
|
self.lang = locale.getdefaultlocale()[0].split('_')[0]
|
|
|
|
# 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
|
2012-04-15 23:42:53 +02:00
|
|
|
self.redirected = 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.annotations = {}
|
|
|
|
self.last_io = gajim.idlequeue.current_time()
|
|
|
|
self.last_sent = []
|
|
|
|
self.last_history_time = {}
|
|
|
|
self.password = passwords.get_password(name)
|
|
|
|
|
|
|
|
self.music_track_info = 0
|
|
|
|
self.location_info = {}
|
|
|
|
self.pubsub_supported = False
|
|
|
|
self.pubsub_publish_options_supported = False
|
|
|
|
# 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.jids_for_auto_auth = [] # list of jid to auto-authorize
|
|
|
|
self.available_transports = {} # list of available transports on this
|
|
|
|
# server {'icq': ['icq.server.com', 'icq2.server.com'], }
|
|
|
|
self.private_storage_supported = True
|
2011-08-21 23:13:25 +02:00
|
|
|
self.privacy_rules_requested = False
|
2010-02-08 15:08:40 +01:00
|
|
|
self.streamError = ''
|
2013-01-03 19:01:18 +01:00
|
|
|
self.secret_hmac = str(random.random())[2:].encode('utf-8')
|
2011-08-27 22:41:55 +02:00
|
|
|
|
|
|
|
self.sm = Smacks(self) # Stream Management
|
|
|
|
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.ged.register_event_handler('privacy-list-received', ged.CORE,
|
|
|
|
self._nec_privacy_list_received)
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.ged.register_event_handler('agent-info-error-received', ged.CORE,
|
|
|
|
self._nec_agent_info_error_received)
|
|
|
|
gajim.ged.register_event_handler('agent-info-received', ged.CORE,
|
|
|
|
self._nec_agent_info_received)
|
2011-05-18 19:44:43 +02:00
|
|
|
gajim.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_message_outgoing)
|
2013-07-19 11:52:37 +02:00
|
|
|
gajim.ged.register_event_handler('gc-message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_gc_message_outgoing)
|
2016-06-24 20:12:37 +02:00
|
|
|
gajim.ged.register_event_handler('gc-stanza-message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_gc_stanza_message_outgoing)
|
2015-09-23 22:39:11 +02:00
|
|
|
gajim.ged.register_event_handler('stanza-message-outgoing',
|
|
|
|
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)
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.ged.remove_event_handler('privacy-list-received', ged.CORE,
|
|
|
|
self._nec_privacy_list_received)
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.ged.remove_event_handler('agent-info-error-received', ged.CORE,
|
|
|
|
self._nec_agent_info_error_received)
|
2011-01-06 16:50:38 +01:00
|
|
|
gajim.ged.remove_event_handler('agent-info-received', ged.CORE,
|
|
|
|
self._nec_agent_info_received)
|
2011-05-18 19:44:43 +02:00
|
|
|
gajim.ged.remove_event_handler('message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_message_outgoing)
|
2013-07-19 12:36:46 +02:00
|
|
|
gajim.ged.remove_event_handler('gc-message-outgoing', ged.OUT_CORE,
|
2013-07-19 11:52:37 +02:00
|
|
|
self._nec_gc_message_outgoing)
|
2016-06-24 20:12:37 +02:00
|
|
|
gajim.ged.remove_event_handler('gc-stanza-message-outgoing', ged.OUT_CORE,
|
|
|
|
self._nec_gc_stanza_message_outgoing)
|
2015-09-23 22:39:11 +02:00
|
|
|
gajim.ged.remove_event_handler('stanza-message-outgoing', ged.OUT_CORE,
|
|
|
|
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):
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'):
|
|
|
|
self.keepalives = gajim.config.get_per('accounts', self.name,
|
|
|
|
'keep_alive_every_foo_secs')
|
|
|
|
else:
|
|
|
|
self.keepalives = 0
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'ping_alives_enabled'):
|
|
|
|
self.pingalives = gajim.config.get_per('accounts', self.name,
|
|
|
|
'ping_alive_every_foo_secs')
|
|
|
|
else:
|
|
|
|
self.pingalives = 0
|
2010-04-18 20:43:40 +02:00
|
|
|
self.client_cert = gajim.config.get_per('accounts', self.name,
|
|
|
|
'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)
|
|
|
|
|
|
|
|
def _reconnect(self):
|
|
|
|
# Do not try to reco while we are already trying
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
if self.connected < 2: # connection failed
|
|
|
|
log.debug('reconnect')
|
|
|
|
self.connected = 1
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
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:
|
|
|
|
# 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):
|
|
|
|
gajim.interface.music_track_changed(None, None, self.name)
|
|
|
|
self.reset_awaiting_pep()
|
|
|
|
self.on_purpose = on_purpose
|
|
|
|
self.connected = 0
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
self.privacy_rules_supported = 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
|
|
|
|
gajim.proxy65_manager.disconnect(self.connection)
|
|
|
|
self.terminate_sessions()
|
|
|
|
self.remove_all_transfers()
|
|
|
|
self.connection.disconnect()
|
|
|
|
self.last_connection = None
|
|
|
|
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:
|
|
|
|
self.connected = gajim.SHOW_LIST.index(self.old_show)
|
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show=self.connected))
|
|
|
|
else: # we default to online
|
|
|
|
self.connected = 2
|
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show=gajim.SHOW_LIST[self.connected]))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _disconnectedReconnCB(self):
|
|
|
|
"""
|
|
|
|
Called when we are disconnected
|
|
|
|
"""
|
|
|
|
log.info('disconnectedReconnCB called')
|
|
|
|
if gajim.account_is_connected(self.name):
|
|
|
|
# we cannot change our status to offline or connecting
|
|
|
|
# after we auth to server
|
|
|
|
self.old_show = gajim.SHOW_LIST[self.connected]
|
|
|
|
self.connected = 0
|
|
|
|
if not self.on_purpose:
|
2011-06-11 05:07:34 +02:00
|
|
|
if not (self.sm and self.sm.resumption):
|
|
|
|
gajim.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
|
2012-02-09 04:06:29 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='error'))
|
2015-07-28 20:19:19 +02:00
|
|
|
if self.connection:
|
|
|
|
self.connection.UnregisterDisconnectHandler(
|
|
|
|
self._disconnectedReconnCB)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.disconnect()
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'autoreconnect'):
|
|
|
|
self.connected = -1
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='error'))
|
2010-02-08 15:08:40 +01:00
|
|
|
if gajim.status_before_autoaway[self.name]:
|
|
|
|
# We were auto away. So go back online
|
|
|
|
self.status = gajim.status_before_autoaway[self.name]
|
|
|
|
gajim.status_before_autoaway[self.name] = ''
|
|
|
|
self.old_show = 'online'
|
|
|
|
# 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
|
2016-09-18 15:15:10 +02:00
|
|
|
self.last_time_to_reconnect += randomsource.randint(0, 5)
|
|
|
|
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)
|
|
|
|
gajim.idlequeue.set_alarm(self._reconnect_alarm,
|
|
|
|
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:
|
2012-08-10 10:43:02 +02:00
|
|
|
if self.redirected:
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
self.connect()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.disconnect()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.on_purpose = False
|
|
|
|
# END disconnectedReconnCB
|
|
|
|
|
|
|
|
def _connection_lost(self):
|
|
|
|
log.debug('_connection_lost')
|
|
|
|
self.disconnect(on_purpose = False)
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
|
|
|
title=_('Connection with account "%s" has been lost') % self.name,
|
|
|
|
msg=_('Reconnect manually.')))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _event_dispatcher(self, realm, event, data):
|
|
|
|
CommonConnection._event_dispatcher(self, realm, event, data)
|
2012-12-09 21:37:51 +01:00
|
|
|
if realm == nbxmpp.NS_REGISTER:
|
|
|
|
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]}
|
2010-11-14 12:22:45 +01:00
|
|
|
gajim.nec.push_incoming_event(AccountNotCreatedEvent(
|
|
|
|
None, conn=self, reason=reason))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
is_form = data[2]
|
|
|
|
conf = data[1]
|
2010-11-14 12:22:45 +01:00
|
|
|
if data[4] is not '':
|
|
|
|
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):
|
2010-11-14 12:22:45 +01:00
|
|
|
gajim.nec.push_incoming_event(AccountNotCreatedEvent(
|
2010-11-07 18:26:31 +01:00
|
|
|
None, conn=self, reason=result.getError()))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
if gajim.HAVE_GPG:
|
|
|
|
self.USE_GPG = True
|
2010-11-30 11:41:14 +01:00
|
|
|
self.gpg = gpg.GnuPG(gajim.config.get(
|
2010-02-08 15:08:40 +01:00
|
|
|
'use_gpg_agent'))
|
2010-11-07 18:26:31 +01:00
|
|
|
gajim.nec.push_incoming_event(
|
|
|
|
AccountCreatedEvent(None, conn=self,
|
|
|
|
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]
|
2010-11-14 12:22:45 +01:00
|
|
|
gajim.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
|
2010-11-07 18:50:24 +01:00
|
|
|
gajim.nec.push_incoming_event(NewAccountConnectedEvent(None,
|
|
|
|
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
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
|
|
|
conn=self, level='error', pri_txt=_('Invalid answer'),
|
|
|
|
sec_txt=_('Transport %(name)s answered wrongly to '
|
|
|
|
'register request: %(error)s') % {'name': data[0],
|
2010-11-07 18:50:24 +01:00
|
|
|
'error': data[3]}))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
is_form = data[2]
|
|
|
|
conf = data[1]
|
2010-11-29 11:22:27 +01:00
|
|
|
gajim.nec.push_incoming_event(RegisterAgentInfoReceivedEvent(
|
|
|
|
None, conn=self, agent=data[0], config=conf,
|
|
|
|
is_form=is_form))
|
2012-12-09 21:37:51 +01:00
|
|
|
elif realm == nbxmpp.NS_PRIVACY:
|
|
|
|
if event == nbxmpp.features_nb.PRIVACY_LISTS_RECEIVED:
|
2010-02-08 15:08:40 +01:00
|
|
|
# data is (list)
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.nec.push_incoming_event(PrivacyListsReceivedEvent(None,
|
|
|
|
conn=self, lists_list=data))
|
2012-12-09 21:37:51 +01:00
|
|
|
elif event == nbxmpp.features_nb.PRIVACY_LIST_RECEIVED:
|
2010-02-08 15:08:40 +01:00
|
|
|
# data is (resp)
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
rules = []
|
|
|
|
name = data.getTag('query').getTag('list').getAttr('name')
|
|
|
|
for child in data.getTag('query').getTag('list').getChildren():
|
|
|
|
dict_item = child.getAttrs()
|
|
|
|
childs = []
|
|
|
|
if 'type' in dict_item:
|
|
|
|
for scnd_child in child.getChildren():
|
|
|
|
childs += [scnd_child.getName()]
|
|
|
|
rules.append({'action':dict_item['action'],
|
|
|
|
'type':dict_item['type'], 'order':dict_item['order'],
|
|
|
|
'value':dict_item['value'], 'child':childs})
|
|
|
|
else:
|
|
|
|
for scnd_child in child.getChildren():
|
|
|
|
childs.append(scnd_child.getName())
|
|
|
|
rules.append({'action':dict_item['action'],
|
|
|
|
'order':dict_item['order'], 'child':childs})
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.nec.push_incoming_event(PrivacyListReceivedEvent(None,
|
|
|
|
conn=self, list_name=name, rules=rules))
|
2012-12-09 21:37:51 +01:00
|
|
|
elif event == nbxmpp.features_nb.PRIVACY_LISTS_ACTIVE_DEFAULT:
|
2010-02-08 15:08:40 +01:00
|
|
|
# data is (dict)
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.nec.push_incoming_event(PrivacyListActiveDefaultEvent(
|
|
|
|
None, conn=self, active_list=data['active'],
|
|
|
|
default_list=data['default']))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
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]
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
MUST contain hostname, usessl, proxy, use_custom_host, custom_host (if
|
|
|
|
use_custom_host), custom_port (if use_custom_host)
|
|
|
|
"""
|
|
|
|
if self.connection:
|
|
|
|
return self.connection, ''
|
|
|
|
|
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
|
|
|
|
self.try_connecting_for_foo_secs = gajim.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']
|
2011-11-10 20:37:48 +01:00
|
|
|
if p and p in gajim.config.get_per('proxies'):
|
|
|
|
proxy = {}
|
|
|
|
proxyptr = gajim.config.get_per('proxies', p)
|
|
|
|
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:
|
|
|
|
hostname = gajim.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
usessl = gajim.config.get_per('accounts', self.name, 'usessl')
|
|
|
|
self.try_connecting_for_foo_secs = gajim.config.get_per('accounts',
|
|
|
|
self.name, 'try_connecting_for_foo_secs')
|
2011-11-10 20:37:48 +01:00
|
|
|
proxy = helpers.get_proxy_info(self.name)
|
2010-02-08 15:08:40 +01:00
|
|
|
use_srv = gajim.config.get_per('accounts', self.name, 'use_srv')
|
2012-04-15 23:42:53 +02:00
|
|
|
if self.redirected:
|
|
|
|
use_custom = True
|
|
|
|
custom_h = self.redirected['host']
|
|
|
|
custom_p = self.redirected['port']
|
|
|
|
else:
|
|
|
|
use_custom = gajim.config.get_per('accounts', self.name,
|
2010-02-08 15:08:40 +01:00
|
|
|
'use_custom_host')
|
2014-11-09 14:03:01 +01:00
|
|
|
if use_custom:
|
|
|
|
custom_h = gajim.config.get_per('accounts', self.name,
|
|
|
|
'custom_host')
|
|
|
|
custom_p = gajim.config.get_per('accounts', self.name,
|
|
|
|
'custom_port')
|
|
|
|
try:
|
|
|
|
helpers.idn_to_ascii(custom_h)
|
|
|
|
except Exception:
|
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
|
|
|
conn=self, level='error',
|
|
|
|
pri_txt=_('Wrong Custom Hostname'),
|
|
|
|
sec_txt='Wrong custom hostname "%s". Ignoring it.' \
|
|
|
|
% 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
|
2012-04-15 23:42:53 +02:00
|
|
|
if not self.redirected:
|
|
|
|
use_srv = False
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-04-15 23:42:53 +02:00
|
|
|
self.redirected = None
|
2010-02-08 15:08:40 +01:00
|
|
|
# SRV resolver
|
|
|
|
self._proxy = proxy
|
|
|
|
self._hosts = [ {'host': h, 'port': p, 'ssl_port': ssl_p, 'prio': 10,
|
|
|
|
'weight': 10} ]
|
|
|
|
self._hostname = hostname
|
|
|
|
if use_srv:
|
|
|
|
# add request for srv query to the resolve, on result '_on_resolve'
|
|
|
|
# will be called
|
2011-11-10 20:37:48 +01:00
|
|
|
gajim.resolver.resolve('_xmpp-client._tcp.' + helpers.idn_to_ascii(
|
|
|
|
h), self._on_resolve)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
self._on_resolve('', [])
|
|
|
|
|
|
|
|
def _on_resolve(self, host, result_array):
|
|
|
|
# SRV query returned at least one valid result, we put it in hosts dict
|
|
|
|
if len(result_array) != 0:
|
|
|
|
self._hosts = [i for i in result_array]
|
|
|
|
# Add ssl port
|
|
|
|
ssl_p = 5223
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'use_custom_host'):
|
2011-11-10 20:37:48 +01:00
|
|
|
ssl_p = gajim.config.get_per('accounts', self.name,
|
|
|
|
'custom_port')
|
2010-02-08 15:08:40 +01:00
|
|
|
for i in self._hosts:
|
|
|
|
i['ssl_port'] = ssl_p
|
|
|
|
self._connect_to_next_host()
|
|
|
|
|
|
|
|
|
|
|
|
def _connect_to_next_host(self, retry=False):
|
|
|
|
log.debug('Connection to next host')
|
|
|
|
if len(self._hosts):
|
|
|
|
# No config option exist when creating a new account
|
2012-08-19 22:35:46 +02:00
|
|
|
if self.name in gajim.config.get_per('accounts'):
|
2010-02-08 15:08:40 +01:00
|
|
|
self._connection_types = gajim.config.get_per('accounts', self.name,
|
|
|
|
'connection_types').split()
|
|
|
|
else:
|
2015-11-22 20:10:33 +01:00
|
|
|
self._connection_types = ['tls', 'ssl']
|
2012-08-19 22:35:46 +02:00
|
|
|
if self.last_connection_type:
|
|
|
|
if self.last_connection_type in self._connection_types:
|
|
|
|
self._connection_types.remove(self.last_connection_type)
|
|
|
|
self._connection_types.insert(0, self.last_connection_type)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
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")
|
2012-12-09 21:37:51 +01:00
|
|
|
scheme = nbxmpp.transports_nb.urisplit(self._proxy['bosh_uri'])[0]
|
2010-02-08 15:08:40 +01:00
|
|
|
if scheme=='https':
|
|
|
|
self._connection_types = ['ssl']
|
|
|
|
else:
|
|
|
|
self._connection_types = ['plain']
|
|
|
|
|
|
|
|
host = self._select_next_host(self._hosts)
|
|
|
|
self._current_host = host
|
|
|
|
self._hosts.remove(host)
|
|
|
|
self.connect_to_next_type()
|
|
|
|
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
self._disconnectedReconnCB()
|
|
|
|
|
|
|
|
def connect_to_next_type(self, retry=False):
|
2012-06-19 19:19:24 +02:00
|
|
|
if self.redirected:
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
self.connect()
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if len(self._connection_types):
|
|
|
|
self._current_type = self._connection_types.pop(0)
|
|
|
|
if self.last_connection:
|
|
|
|
self.last_connection.socket.disconnect()
|
|
|
|
self.last_connection = None
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
if self._current_type == 'ssl':
|
|
|
|
# SSL (force TLS on different port than plain)
|
|
|
|
# If we do TLS over BOSH, port of XMPP server should be the standard one
|
|
|
|
# and TLS should be negotiated because TLS on 5223 is deprecated
|
|
|
|
if self._proxy and self._proxy['type']=='bosh':
|
|
|
|
port = self._current_host['port']
|
|
|
|
else:
|
|
|
|
port = self._current_host['ssl_port']
|
|
|
|
elif self._current_type == 'tls':
|
|
|
|
# TLS - negotiate tls after XMPP stream is estabilished
|
|
|
|
port = self._current_host['port']
|
|
|
|
elif self._current_type == 'plain':
|
|
|
|
# plain connection on defined port
|
|
|
|
port = self._current_host['port']
|
|
|
|
|
|
|
|
cacerts = os.path.join(common.gajim.DATA_DIR, 'other', 'cacerts.pem')
|
2013-02-10 11:29:14 +01:00
|
|
|
if not os.path.exists(cacerts):
|
|
|
|
cacerts = ''
|
2010-02-08 15:08:40 +01:00
|
|
|
mycerts = common.gajim.MY_CACERTS
|
2014-01-27 16:31:08 +01:00
|
|
|
tls_version = gajim.config.get_per('accounts', self.name,
|
|
|
|
'tls_version')
|
2013-11-03 17:57:12 +01:00
|
|
|
cipher_list = gajim.config.get_per('accounts', self.name,
|
|
|
|
'cipher_list')
|
2014-01-27 16:31:08 +01:00
|
|
|
secure_tuple = (self._current_type, cacerts, mycerts, tls_version, cipher_list)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
con = nbxmpp.NonBlockingClient(
|
2011-07-26 22:44:11 +02:00
|
|
|
domain=self._hostname,
|
|
|
|
caller=self,
|
|
|
|
idlequeue=gajim.idlequeue)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.last_connection = con
|
|
|
|
# increase default timeout for server responses
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.dispatcher_nb.DEFAULT_TIMEOUT_SECONDS = \
|
2011-07-26 22:44:11 +02:00
|
|
|
self.try_connecting_for_foo_secs
|
2010-02-08 15:08:40 +01:00
|
|
|
# FIXME: this is a hack; need a better way
|
|
|
|
if self.on_connect_success == self._on_new_account:
|
|
|
|
con.RegisterDisconnectHandler(self._on_new_account)
|
|
|
|
|
2011-07-26 22:44:11 +02:00
|
|
|
if self.client_cert and gajim.config.get_per('accounts', self.name,
|
|
|
|
'client_cert_encrypted'):
|
|
|
|
gajim.nec.push_incoming_event(ClientCertPassphraseEvent(
|
|
|
|
None, conn=self, con=con, port=port,
|
|
|
|
secure_tuple=secure_tuple))
|
|
|
|
return
|
2011-07-28 22:42:03 +02:00
|
|
|
self.on_client_cert_passphrase('', con, port, secure_tuple)
|
2011-07-26 22:44:11 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
self._connect_to_next_host(retry)
|
|
|
|
|
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,
|
|
|
|
on_connect_failure=self.connect_to_next_type,
|
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,
|
|
|
|
secure_tuple = secure_tuple)
|
|
|
|
|
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 = '>>>>>> '
|
|
|
|
if self._proxy['type']=='bosh':
|
|
|
|
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:
|
|
|
|
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]
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
|
|
|
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
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.nec.push_incoming_event(ConnectionLostEvent(None,
|
|
|
|
conn=self, title=pritxt, msg=sectxt))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def on_proxy_failure(self, reason):
|
|
|
|
log.error('Connection to proxy failed: %s' % reason)
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
self.on_connect_failure = None
|
|
|
|
self.disconnect(on_purpose = True)
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
|
|
|
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
|
|
|
|
_con_type = con_type
|
|
|
|
if _con_type != self._current_type:
|
|
|
|
log.info('Connecting to next type beacuse desired is %s and returned is %s'
|
|
|
|
% (self._current_type, _con_type))
|
|
|
|
self.connect_to_next_type()
|
|
|
|
return
|
|
|
|
con.RegisterDisconnectHandler(self._on_disconnected)
|
|
|
|
if _con_type == 'plain' and gajim.config.get_per('accounts', self.name,
|
2011-10-31 09:47:01 +01:00
|
|
|
'action_when_plaintext_connection') == 'warn':
|
2010-11-24 15:38:16 +01:00
|
|
|
gajim.nec.push_incoming_event(PlainConnectionEvent(None, conn=self,
|
|
|
|
xmpp_client=con))
|
2010-02-08 15:08:40 +01:00
|
|
|
return True
|
2011-10-31 09:47:01 +01:00
|
|
|
if _con_type == 'plain' and gajim.config.get_per('accounts', self.name,
|
|
|
|
'action_when_plaintext_connection') == 'disconnect':
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
if _con_type in ('tls', 'ssl') and con.Connection.ssl_lib != 'PYOPENSSL' \
|
|
|
|
and gajim.config.get_per('accounts', self.name,
|
|
|
|
'warn_when_insecure_ssl_connection') and \
|
|
|
|
not self.connection_auto_accepted:
|
|
|
|
# Pyopenssl is not used
|
2010-11-24 16:32:59 +01:00
|
|
|
gajim.nec.push_incoming_event(InsecureSSLConnectionEvent(None,
|
|
|
|
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)
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.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))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
self.hosts = []
|
|
|
|
self.connection_auto_accepted = False
|
|
|
|
self.connected_hostname = self._current_host['host']
|
|
|
|
self.on_connect_failure = None
|
|
|
|
con.UnregisterDisconnectHandler(self._on_disconnected)
|
|
|
|
con.RegisterDisconnectHandler(self._disconnectedReconnCB)
|
|
|
|
log.debug('Connected to server %s:%s with %s' % (
|
|
|
|
self._current_host['host'], self._current_host['port'], con_type))
|
|
|
|
|
|
|
|
self.last_connection_type = con_type
|
2015-11-22 20:10:33 +01:00
|
|
|
if con_type == 'tls' and 'ssl' in self._connection_types:
|
|
|
|
# we were about to try ssl after tls, but tls succeed, so
|
|
|
|
# definitively stop trying ssl
|
|
|
|
con_types = gajim.config.get_per('accounts', self.name,
|
|
|
|
'connection_types').split()
|
|
|
|
con_types.remove('ssl')
|
|
|
|
gajim.config.set_per('accounts', self.name, 'connection_types',
|
|
|
|
' '.join(con_types))
|
2010-02-08 15:08:40 +01:00
|
|
|
if gajim.config.get_per('accounts', self.name, 'anonymous_auth'):
|
|
|
|
name = None
|
|
|
|
else:
|
|
|
|
name = gajim.config.get_per('accounts', self.name, 'name')
|
|
|
|
hostname = gajim.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
self.connection = con
|
|
|
|
try:
|
|
|
|
errnum = con.Connection.ssl_errnum
|
|
|
|
except AttributeError:
|
2013-12-06 21:20:22 +01:00
|
|
|
errnum = 0
|
|
|
|
cert = con.Connection.ssl_certificate
|
|
|
|
if errnum > 0 and str(errnum) not in gajim.config.get_per('accounts',
|
|
|
|
self.name, 'ignore_ssl_errors').split():
|
2013-12-06 21:20:22 +01:00
|
|
|
text = _('The authenticity of the %s certificate could be invalid'
|
|
|
|
) % hostname
|
2013-12-06 21:20:22 +01:00
|
|
|
if errnum in ssl_error:
|
|
|
|
text += _('\nSSL Error: <b>%s</b>') % ssl_error[errnum]
|
|
|
|
else:
|
|
|
|
text += _('\nUnknown SSL error: %d') % errnum
|
2014-01-21 15:27:41 +01:00
|
|
|
fingerprint_sha1 = cert.digest('sha1').decode('utf-8')
|
|
|
|
fingerprint_sha256 = cert.digest('sha256').decode('utf-8')
|
2013-12-06 21:20:22 +01:00
|
|
|
pem = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
|
2013-12-30 20:56:09 +01:00
|
|
|
cert).decode('utf-8')
|
2013-12-06 21:20:22 +01:00
|
|
|
gajim.nec.push_incoming_event(SSLErrorEvent(None, conn=self,
|
|
|
|
error_text=text, error_num=errnum, cert=pem,
|
2014-01-21 15:27:41 +01:00
|
|
|
fingerprint_sha1=fingerprint_sha1,
|
|
|
|
fingerprint_sha256=fingerprint_sha256, certificate=cert))
|
2013-12-06 21:20:22 +01:00
|
|
|
return True
|
|
|
|
if cert:
|
2014-01-21 15:27:41 +01:00
|
|
|
fingerprint_sha1 = cert.digest('sha1').decode('utf-8')
|
|
|
|
fingerprint_sha256 = cert.digest('sha256').decode('utf-8')
|
|
|
|
saved_fingerprint_sha1 = gajim.config.get_per('accounts', self.name,
|
2012-01-02 16:39:06 +01:00
|
|
|
'ssl_fingerprint_sha1')
|
2014-01-21 15:27:41 +01:00
|
|
|
if saved_fingerprint_sha1:
|
2010-02-08 15:08:40 +01:00
|
|
|
# Check sha1 fingerprint
|
2014-01-21 15:27:41 +01:00
|
|
|
if fingerprint_sha1 != saved_fingerprint_sha1:
|
2016-04-02 14:06:20 +02:00
|
|
|
if not check_X509.check_certificate(cert, hostname):
|
|
|
|
gajim.nec.push_incoming_event(FingerprintErrorEvent(
|
|
|
|
None, conn=self, certificate=cert,
|
|
|
|
new_fingerprint_sha1=fingerprint_sha1,
|
|
|
|
new_fingerprint_sha256=fingerprint_sha256))
|
|
|
|
return True
|
|
|
|
gajim.config.set_per('accounts', self.name, 'ssl_fingerprint_sha1',
|
|
|
|
fingerprint_sha1)
|
2014-01-21 15:27:41 +01:00
|
|
|
|
|
|
|
saved_fingerprint_sha256 = gajim.config.get_per('accounts', self.name,
|
|
|
|
'ssl_fingerprint_sha256')
|
|
|
|
if saved_fingerprint_sha256:
|
|
|
|
# Check sha256 fingerprint
|
|
|
|
if fingerprint_sha256 != saved_fingerprint_sha256:
|
2016-04-02 14:06:20 +02:00
|
|
|
if not check_X509.check_certificate(cert, hostname):
|
|
|
|
gajim.nec.push_incoming_event(FingerprintErrorEvent(
|
|
|
|
None, conn=self, certificate=cert,
|
|
|
|
new_fingerprint_sha1=fingerprint_sha1,
|
|
|
|
new_fingerprint_sha256=fingerprint_sha256))
|
|
|
|
return True
|
|
|
|
gajim.config.set_per('accounts', self.name,
|
|
|
|
'ssl_fingerprint_sha256', fingerprint_sha256)
|
2014-01-21 15:27:41 +01:00
|
|
|
|
2013-12-06 21:20:22 +01:00
|
|
|
if not check_X509.check_certificate(cert, hostname) and \
|
|
|
|
'100' not in gajim.config.get_per('accounts', self.name,
|
|
|
|
'ignore_ssl_errors').split():
|
2013-12-06 21:20:22 +01:00
|
|
|
pem = OpenSSL.crypto.dump_certificate(
|
2013-12-30 20:56:09 +01:00
|
|
|
OpenSSL.crypto.FILETYPE_PEM, cert).decode('utf-8')
|
2012-01-06 09:45:11 +01:00
|
|
|
txt = _('The authenticity of the %s certificate could be '
|
2013-12-06 21:20:22 +01:00
|
|
|
'invalid.\nThe certificate does not cover this domain.') %\
|
2012-01-06 09:45:11 +01:00
|
|
|
hostname
|
|
|
|
gajim.nec.push_incoming_event(SSLErrorEvent(None, conn=self,
|
2013-12-06 21:20:22 +01:00
|
|
|
error_text=txt, error_num=100, cert=pem,
|
2014-01-21 15:27:41 +01:00
|
|
|
fingerprint_sha1=fingerprint_sha1,
|
|
|
|
fingerprint_sha256=fingerprint_sha256, certificate=cert))
|
2012-01-06 09:45:11 +01:00
|
|
|
return True
|
2012-01-02 16:39:06 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self._register_handlers(con, con_type)
|
2014-03-01 21:16:24 +01:00
|
|
|
auth_mechs = gajim.config.get_per('accounts', self.name, 'authentication_mechanisms')
|
|
|
|
auth_mechs = auth_mechs.split()
|
|
|
|
for mech in auth_mechs:
|
|
|
|
if mech not in nbxmpp.auth_nb.SASL_AUTHENTICATION_MECHANISMS | set(['XEP-0078']):
|
|
|
|
log.warning("Unknown authentication mechanisms %s" % mech)
|
|
|
|
if len(auth_mechs) == 0:
|
|
|
|
auth_mechs = None
|
2012-01-06 09:45:11 +01:00
|
|
|
con.auth(user=name, password=self.password,
|
2014-03-01 21:16:24 +01:00
|
|
|
resource=self.server_resource, sasl=True, on_auth=self.__on_auth, auth_mechs=auth_mechs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def ssl_certificate_accepted(self):
|
|
|
|
if not self.connection:
|
|
|
|
self.disconnect(on_purpose=True)
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.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))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-03-16 23:09:00 +01:00
|
|
|
if gajim.config.get_per('accounts', self.name, 'anonymous_auth'):
|
|
|
|
name = None
|
|
|
|
else:
|
|
|
|
name = gajim.config.get_per('accounts', self.name, 'name')
|
2010-02-08 15:08:40 +01:00
|
|
|
self._register_handlers(self.connection, 'ssl')
|
|
|
|
self.connection.auth(name, self.password, self.server_resource, 1,
|
|
|
|
self.__on_auth)
|
|
|
|
|
|
|
|
def _register_handlers(self, con, con_type):
|
|
|
|
self.peerhost = con.get_peerhost()
|
2010-11-07 22:45:01 +01:00
|
|
|
gajim.con_types[self.name] = con_type
|
2010-02-08 15:08:40 +01:00
|
|
|
# notify the gui about con_type
|
2010-11-07 22:45:01 +01:00
|
|
|
gajim.nec.push_incoming_event(ConnectionTypeEvent(None,
|
|
|
|
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):
|
|
|
|
if not con:
|
|
|
|
self.disconnect(on_purpose=True)
|
2010-11-15 17:03:38 +01:00
|
|
|
gajim.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
|
|
|
|
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
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'anonymous_auth'):
|
|
|
|
# Get jid given by server
|
|
|
|
old_jid = gajim.get_jid_from_account(self.name)
|
|
|
|
gajim.config.set_per('accounts', self.name, 'name', con.User)
|
|
|
|
new_jid = gajim.get_jid_from_account(self.name)
|
2010-10-26 21:25:41 +02:00
|
|
|
gajim.nec.push_incoming_event(AnonymousAuthEvent(None,
|
|
|
|
conn=self, old_jid=old_jid, new_jid=new_jid))
|
2010-02-08 15:08:40 +01:00
|
|
|
if auth:
|
|
|
|
self.last_io = gajim.idlequeue.current_time()
|
|
|
|
self.connected = 2
|
|
|
|
self.retrycount = 0
|
|
|
|
if self.on_connect_auth:
|
|
|
|
self.on_connect_auth(con)
|
|
|
|
self.on_connect_auth = None
|
|
|
|
else:
|
2010-08-23 10:04:19 +02:00
|
|
|
if not gajim.config.get_per('accounts', self.name, 'savepass'):
|
|
|
|
# Forget password, it's wrong
|
|
|
|
self.password = None
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.log.debug("Couldn't authenticate to %s" % self._hostname)
|
|
|
|
self.disconnect(on_purpose = True)
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None, conn=self,
|
|
|
|
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 add_lang(self, stanza):
|
|
|
|
if self.lang:
|
|
|
|
stanza.setAttr('xml:lang', self.lang)
|
|
|
|
|
|
|
|
def get_privacy_lists(self):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.getPrivacyLists(self.connection)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def send_keepalive(self):
|
|
|
|
# nothing received for the last foo seconds
|
|
|
|
if self.connection:
|
|
|
|
self.connection.send(' ')
|
|
|
|
|
|
|
|
def _on_xmpp_ping_answer(self, iq_obj):
|
2013-01-01 21:06:16 +01:00
|
|
|
id_ = iq_obj.getAttr('id')
|
2010-02-08 15:08:40 +01:00
|
|
|
assert id_ == self.awaiting_xmpp_ping_id
|
|
|
|
self.awaiting_xmpp_ping_id = None
|
|
|
|
|
2014-08-22 15:44:07 +02:00
|
|
|
def sendPing(self, pingTo=None, control=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Send XMPP Ping (XEP-0199) request. If pingTo is not set, ping is sent to
|
|
|
|
server to detect connection failure at application level
|
2014-08-22 15:44:07 +02:00
|
|
|
If control is set, display result there
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
if pingTo:
|
|
|
|
to = pingTo.get_full_jid()
|
2010-11-15 17:35:19 +01:00
|
|
|
gajim.nec.push_incoming_event(PingSentEvent(None, conn=self,
|
|
|
|
contact=pingTo))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
to = gajim.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
self.awaiting_xmpp_ping_id = id_
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('get', to=to)
|
|
|
|
iq.addChild(name='ping', namespace=nbxmpp.NS_PING)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq.setID(id_)
|
|
|
|
def _on_response(resp):
|
2016-11-28 22:23:42 +01:00
|
|
|
timePong = time.time()
|
2012-12-09 21:37:51 +01:00
|
|
|
if not nbxmpp.isResultNode(resp):
|
2010-11-15 17:35:19 +01:00
|
|
|
gajim.nec.push_incoming_event(PingErrorEvent(None, conn=self,
|
|
|
|
contact=pingTo))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2010-02-10 19:24:11 +01:00
|
|
|
timeDiff = round(timePong - timePing, 2)
|
2010-11-15 17:35:19 +01:00
|
|
|
gajim.nec.push_incoming_event(PingReplyEvent(None, conn=self,
|
2014-08-22 15:44:07 +02:00
|
|
|
contact=pingTo, seconds=timeDiff, control=control))
|
2010-02-08 15:08:40 +01:00
|
|
|
if pingTo:
|
2016-11-28 22:23:42 +01:00
|
|
|
timePing = time.time()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.SendAndCallForResponse(iq, _on_response)
|
|
|
|
else:
|
|
|
|
self.connection.SendAndCallForResponse(iq, self._on_xmpp_ping_answer)
|
|
|
|
gajim.idlequeue.set_alarm(self.check_pingalive, gajim.config.get_per(
|
2012-12-09 21:37:51 +01:00
|
|
|
'accounts', self.name, 'time_for_ping_alive_answer'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_active_default_lists(self):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.getActiveAndDefaultPrivacyLists(self.connection)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def del_privacy_list(self, privacy_list):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
def _on_del_privacy_list_result(result):
|
|
|
|
if result:
|
2010-11-24 22:48:39 +01:00
|
|
|
gajim.nec.push_incoming_event(PrivacyListRemovedEvent(None,
|
|
|
|
conn=self, list_name=privacy_list))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None, conn=self,
|
|
|
|
level='error', pri_txt=_('Error while removing privacy '
|
|
|
|
'list'), sec_txt=_('Privacy list %s has not been removed. '
|
|
|
|
'It is maybe active in one of your connected resources. '
|
2012-02-22 10:07:16 +01:00
|
|
|
'Deactivate it and try again.') % privacy_list))
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.delPrivacyList(self.connection, privacy_list,
|
|
|
|
_on_del_privacy_list_result)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_privacy_list(self, title):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.getPrivacyList(self.connection, title)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def set_privacy_list(self, listname, tags):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.setPrivacyList(self.connection, listname, tags)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def set_active_list(self, listname):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.setActivePrivacyList(self.connection, listname,
|
|
|
|
'active')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def set_default_list(self, listname):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.features_nb.setDefaultPrivacyList(self.connection, listname)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def build_privacy_rule(self, name, action, order=1):
|
|
|
|
"""
|
|
|
|
Build a Privacy rule stanza for invisibility
|
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_PRIVACY, xmlns='')
|
2011-10-31 10:47:28 +01:00
|
|
|
l = iq.setQuery().setTag('list', {'name': name})
|
2010-02-08 15:08:40 +01:00
|
|
|
i = l.setTag('item', {'action': action, 'order': str(order)})
|
|
|
|
i.setTag('presence-out')
|
|
|
|
return iq
|
|
|
|
|
|
|
|
def build_invisible_rule(self):
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_PRIVACY, xmlns='')
|
2011-10-31 10:47:28 +01:00
|
|
|
l = iq.setQuery().setTag('list', {'name': 'invisible'})
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.name in gajim.interface.status_sent_to_groups and \
|
|
|
|
len(gajim.interface.status_sent_to_groups[self.name]) > 0:
|
|
|
|
for group in gajim.interface.status_sent_to_groups[self.name]:
|
|
|
|
i = l.setTag('item', {'type': 'group', 'value': group,
|
|
|
|
'action': 'allow', 'order': '1'})
|
|
|
|
i.setTag('presence-out')
|
|
|
|
if self.name in gajim.interface.status_sent_to_users and \
|
|
|
|
len(gajim.interface.status_sent_to_users[self.name]) > 0:
|
|
|
|
for jid in gajim.interface.status_sent_to_users[self.name]:
|
|
|
|
i = l.setTag('item', {'type': 'jid', 'value': jid,
|
|
|
|
'action': 'allow', 'order': '2'})
|
|
|
|
i.setTag('presence-out')
|
|
|
|
i = l.setTag('item', {'action': 'deny', 'order': '3'})
|
|
|
|
i.setTag('presence-out')
|
|
|
|
return iq
|
|
|
|
|
|
|
|
def set_invisible_rule(self):
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
|
|
|
iq = self.build_invisible_rule()
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2013-05-26 12:58:19 +02:00
|
|
|
def get_max_blocked_list_order(self):
|
|
|
|
max_order = 0
|
|
|
|
for rule in self.blocked_list:
|
|
|
|
order = int(rule['order'])
|
|
|
|
if order > max_order:
|
|
|
|
max_order = order
|
|
|
|
return max_order
|
|
|
|
|
2013-03-30 20:36:43 +01:00
|
|
|
def block_contacts(self, contact_list, message):
|
2017-01-27 22:24:53 +01:00
|
|
|
if self.privacy_default_list is None:
|
|
|
|
self.privacy_default_list = 'block'
|
2013-03-30 20:36:43 +01:00
|
|
|
if not self.privacy_rules_supported:
|
2013-03-30 23:13:48 +01:00
|
|
|
if self.blocking_supported: #XEP-0191
|
|
|
|
iq = nbxmpp.Iq('set', xmlns='')
|
|
|
|
query = iq.setQuery(name='block')
|
|
|
|
query.setNamespace(nbxmpp.NS_BLOCKING)
|
|
|
|
for contact in contact_list:
|
|
|
|
query.addChild(name='item', attrs={'jid': contact.jid})
|
|
|
|
self.connection.send(iq)
|
2013-03-30 20:36:43 +01:00
|
|
|
return
|
|
|
|
for contact in contact_list:
|
|
|
|
self.send_custom_status('offline', message, contact.jid)
|
2013-05-26 12:58:19 +02:00
|
|
|
max_order = self.get_max_blocked_list_order()
|
2017-01-27 19:36:13 +01:00
|
|
|
new_rule = {'order': str(max_order + 1),
|
|
|
|
'type': 'jid',
|
|
|
|
'action': 'deny',
|
|
|
|
'value': contact.jid}
|
2013-03-30 20:36:43 +01:00
|
|
|
self.blocked_list.append(new_rule)
|
|
|
|
self.blocked_contacts.append(contact.jid)
|
2017-01-27 22:24:53 +01:00
|
|
|
self.set_privacy_list(self.privacy_default_list, self.blocked_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
if len(self.blocked_list) == 1:
|
2017-01-27 22:24:53 +01:00
|
|
|
self.set_default_list(self.privacy_default_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
|
|
|
|
def unblock_contacts(self, contact_list):
|
|
|
|
if not self.privacy_rules_supported:
|
2013-03-30 23:13:48 +01:00
|
|
|
if self.blocking_supported: #XEP-0191
|
|
|
|
iq = nbxmpp.Iq('set', xmlns='')
|
|
|
|
query = iq.setQuery(name='unblock')
|
|
|
|
query.setNamespace(nbxmpp.NS_BLOCKING)
|
|
|
|
for contact in contact_list:
|
|
|
|
query.addChild(name='item', attrs={'jid': contact.jid})
|
|
|
|
self.connection.send(iq)
|
2013-03-30 20:36:43 +01:00
|
|
|
return
|
|
|
|
self.new_blocked_list = []
|
|
|
|
self.to_unblock = []
|
|
|
|
for contact in contact_list:
|
|
|
|
self.to_unblock.append(contact.jid)
|
|
|
|
if contact.jid in self.blocked_contacts:
|
|
|
|
self.blocked_contacts.remove(contact.jid)
|
|
|
|
for rule in self.blocked_list:
|
|
|
|
if rule['action'] != 'deny' or rule['type'] != 'jid' \
|
|
|
|
or rule['value'] not in self.to_unblock:
|
|
|
|
self.new_blocked_list.append(rule)
|
|
|
|
if len(self.new_blocked_list) == 0:
|
|
|
|
self.blocked_list = []
|
|
|
|
self.blocked_contacts = []
|
|
|
|
self.blocked_groups = []
|
|
|
|
self.set_default_list('')
|
2017-01-27 22:24:53 +01:00
|
|
|
self.del_privacy_list(self.privacy_default_list)
|
2017-01-27 23:58:28 +01:00
|
|
|
else:
|
|
|
|
self.set_privacy_list(self.privacy_default_list, self.new_blocked_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
if not gajim.interface.roster.regroup:
|
|
|
|
show = gajim.SHOW_LIST[self.connected]
|
|
|
|
else: # accounts merged
|
|
|
|
show = helpers.get_global_show()
|
|
|
|
if show == 'invisible':
|
|
|
|
return
|
|
|
|
for contact in contact_list:
|
|
|
|
self.send_custom_status(show, self.status, contact.jid)
|
|
|
|
|
|
|
|
def block_group(self, group, contact_list, message):
|
|
|
|
if not self.privacy_rules_supported:
|
|
|
|
return
|
|
|
|
self.blocked_groups.append(group)
|
|
|
|
for contact in contact_list:
|
|
|
|
self.send_custom_status('offline', message, contact.jid)
|
2013-05-26 12:58:19 +02:00
|
|
|
max_order = self.get_max_blocked_list_order()
|
2017-01-27 19:36:13 +01:00
|
|
|
new_rule = {'order': str(max_order + 1),
|
|
|
|
'type': 'group',
|
|
|
|
'action': 'deny',
|
|
|
|
'value': group}
|
2013-03-30 20:36:43 +01:00
|
|
|
self.blocked_list.append(new_rule)
|
2017-01-27 22:24:53 +01:00
|
|
|
self.set_privacy_list(self.privacy_default_list, self.blocked_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
if len(self.blocked_list) == 1:
|
2017-01-27 22:24:53 +01:00
|
|
|
self.set_default_list(self.privacy_default_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
|
|
|
|
def unblock_group(self, group, contact_list):
|
|
|
|
if not self.privacy_rules_supported:
|
|
|
|
return
|
|
|
|
if group in self.blocked_groups:
|
|
|
|
self.blocked_groups.remove(group)
|
|
|
|
self.new_blocked_list = []
|
|
|
|
for rule in self.blocked_list:
|
|
|
|
if rule['action'] != 'deny' or rule['type'] != 'group' or \
|
|
|
|
rule['value'] != group:
|
|
|
|
self.new_blocked_list.append(rule)
|
|
|
|
if len(self.new_blocked_list) == 0:
|
|
|
|
self.blocked_list = []
|
|
|
|
self.blocked_contacts = []
|
|
|
|
self.blocked_groups = []
|
|
|
|
self.set_default_list('')
|
2017-01-27 22:24:53 +01:00
|
|
|
self.del_privacy_list(self.privacy_default_list)
|
2017-01-27 23:58:28 +01:00
|
|
|
else:
|
|
|
|
self.set_privacy_list(self.privacy_default_list, self.new_blocked_list)
|
2013-03-30 20:36:43 +01:00
|
|
|
if not gajim.interface.roster.regroup:
|
|
|
|
show = gajim.SHOW_LIST[self.connected]
|
|
|
|
else: # accounts merged
|
|
|
|
show = helpers.get_global_show()
|
|
|
|
if show == 'invisible':
|
|
|
|
return
|
|
|
|
for contact in contact_list:
|
|
|
|
self.send_custom_status(show, self.status, contact.jid)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_invisible_presence(self, msg, signed, initial = False):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
if not self.privacy_rules_supported:
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show=gajim.SHOW_LIST[self.connected]))
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None, conn=self,
|
2013-09-17 10:24:28 +02:00
|
|
|
level='error', pri_txt=_('Invisibility not supported'),
|
2011-11-22 19:25:15 +01:00
|
|
|
sec_txt=_('Account %s doesn\'t support invisibility.') % \
|
2013-09-17 10:41:32 +02:00
|
|
|
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
|
|
|
|
if self.connected > 1 and self.privacy_rules_supported:
|
|
|
|
self.on_purpose = True
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(typ='unavailable')
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p, False)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
self.remove_all_transfers()
|
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
# try to set the privacy rule
|
|
|
|
iq = self.build_invisible_rule()
|
|
|
|
self.connection.SendAndCallForResponse(iq, self._continue_invisible,
|
|
|
|
{'msg': msg, 'signed': signed, 'initial': initial})
|
|
|
|
|
|
|
|
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
|
2017-01-28 18:44:26 +01:00
|
|
|
self.set_active_list('invisible')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = gajim.SHOW_LIST.index('invisible')
|
|
|
|
self.status = msg
|
2013-01-01 21:06:16 +01:00
|
|
|
priority = gajim.get_priority(self.name, 'invisible')
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(priority=priority)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p, True)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
if signed:
|
2012-12-09 21:37:51 +01:00
|
|
|
p.setTag(nbxmpp.NS_SIGNED + ' x').setData(signed)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(p)
|
|
|
|
self.priority = priority
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='invisible'))
|
2010-02-08 15:08:40 +01:00
|
|
|
if initial:
|
|
|
|
# ask our VCard
|
|
|
|
self.request_vcard(None)
|
|
|
|
|
|
|
|
# Get bookmarks from private namespace
|
|
|
|
self.get_bookmarks()
|
|
|
|
|
|
|
|
# Get annotations
|
|
|
|
self.get_annotations()
|
|
|
|
|
|
|
|
# Inform GUI we just signed in
|
2010-11-29 11:11:24 +01:00
|
|
|
gajim.nec.push_incoming_event(SignedInEvent(None, conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_signed_presence(self, msg, callback = None):
|
|
|
|
if gajim.config.get_per('accounts', self.name, 'gpg_sign_presence'):
|
|
|
|
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
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
self.connection.set_send_timeout(self.keepalives, self.send_keepalive)
|
2012-03-22 20:49:38 +01:00
|
|
|
self.connection.set_send_timeout2(self.pingalives, self.sendPing)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.onreceive(None)
|
2010-02-10 17:59:17 +01:00
|
|
|
|
2011-01-05 20:59:10 +01:00
|
|
|
self.privacy_rules_requested = False
|
|
|
|
|
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):
|
2011-08-22 09:45:51 +02:00
|
|
|
self.discoverInfo(gajim.config.get_per('accounts', self.name,
|
|
|
|
'hostname'), id_prefix='Gajim_')
|
|
|
|
|
2011-06-07 05:34:19 +02:00
|
|
|
self.sm.resuming = False # back to previous state
|
2010-02-08 15:08:40 +01:00
|
|
|
# Discover Stun server(s)
|
|
|
|
gajim.resolver.resolve('_stun._udp.' + helpers.idn_to_ascii(
|
|
|
|
self.connected_hostname), self._on_stun_resolved)
|
|
|
|
|
|
|
|
def _on_stun_resolved(self, host, result_array):
|
|
|
|
if len(result_array) != 0:
|
|
|
|
self._stun_servers = self._hosts = [i for i in result_array]
|
|
|
|
|
|
|
|
def _request_privacy(self):
|
2012-11-24 02:38:36 +01:00
|
|
|
if not gajim.account_is_connected(self.name) or not self.connection:
|
2011-08-29 15:08:04 +02:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('get', nbxmpp.NS_PRIVACY, xmlns='')
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
self.awaiting_answers[id_] = (PRIVACY_ARRIVED, )
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2015-03-07 15:04:41 +01:00
|
|
|
def _continue_connection_request_privacy(self):
|
|
|
|
if self.privacy_rules_supported:
|
|
|
|
if not self.privacy_rules_requested:
|
|
|
|
self.privacy_rules_requested = True
|
|
|
|
self._request_privacy()
|
|
|
|
else:
|
|
|
|
if self.continue_connect_info and self.continue_connect_info[0]\
|
|
|
|
== 'invisible':
|
|
|
|
# Trying to login as invisible but privacy list not
|
|
|
|
# supported
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
show='offline'))
|
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
|
|
|
conn=self, level='error', pri_txt=_('Invisibility not '
|
|
|
|
'supported'), sec_txt=_('Account %s doesn\'t support '
|
|
|
|
'invisibility.') % self.name))
|
|
|
|
return
|
|
|
|
if self.blocking_supported:
|
|
|
|
iq = nbxmpp.Iq('get', xmlns='')
|
|
|
|
query = iq.setQuery(name='blocklist')
|
|
|
|
query.setNamespace(nbxmpp.NS_BLOCKING)
|
|
|
|
id2_ = self.connection.getAnID()
|
|
|
|
iq.setID(id2_)
|
|
|
|
self.awaiting_answers[id2_] = (BLOCKING_ARRIVED, )
|
|
|
|
self.connection.send(iq)
|
|
|
|
# Ask metacontacts before roster
|
|
|
|
self.get_metacontacts()
|
|
|
|
|
2010-11-29 12:53:50 +01:00
|
|
|
def _nec_agent_info_error_received(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
|
|
|
if obj.id_[:6] == 'Gajim_':
|
2015-03-07 15:04:41 +01:00
|
|
|
self._continue_connection_request_privacy()
|
2010-11-29 12:53:50 +01:00
|
|
|
|
|
|
|
def _nec_agent_info_received(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
|
|
|
is_muc = False
|
|
|
|
transport_type = ''
|
|
|
|
for identity in obj.identities:
|
|
|
|
if 'category' in identity and identity['category'] in ('gateway',
|
|
|
|
'headline') and 'type' in identity:
|
|
|
|
transport_type = identity['type']
|
2013-06-02 21:02:33 +02:00
|
|
|
if 'category' in identity and identity['category'] == 'server' and \
|
|
|
|
'type' in identity and identity['type'] == 'im':
|
|
|
|
transport_type = 'jabber' # it's a jabber server
|
2010-11-29 12:53:50 +01:00
|
|
|
if 'category' in identity and identity['category'] == 'conference' \
|
|
|
|
and 'type' in identity and identity['type'] == 'text':
|
|
|
|
is_muc = True
|
|
|
|
|
2013-06-02 21:02:33 +02:00
|
|
|
if transport_type != '' and obj.fjid not in gajim.transport_type:
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.transport_type[obj.fjid] = transport_type
|
|
|
|
gajim.logger.save_transport_type(obj.fjid, transport_type)
|
|
|
|
|
|
|
|
if obj.id_[:6] == 'Gajim_':
|
|
|
|
hostname = gajim.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
our_jid = gajim.get_jid_from_account(self.name)
|
|
|
|
if obj.fjid == hostname:
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_GMAILNOTIFY in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.gmail_domains.append(obj.fjid)
|
|
|
|
self.request_gmail_notifications()
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_SECLABEL in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
self.seclabel_supported = True
|
|
|
|
for identity in obj.identities:
|
|
|
|
if identity['category'] == 'pubsub' and identity.get(
|
|
|
|
'type') == 'pep':
|
|
|
|
self.pep_supported = True
|
|
|
|
break
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_VCARD in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
self.vcard_supported = True
|
2017-01-26 21:34:54 +01:00
|
|
|
get_action(self.name + '-profile').set_enabled(True)
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_PUBSUB in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
self.pubsub_supported = True
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_PUBSUB_PUBLISH_OPTIONS in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
self.pubsub_publish_options_supported = True
|
|
|
|
else:
|
|
|
|
# Remove stored bookmarks accessible to everyone.
|
|
|
|
self.send_pb_purge(our_jid, 'storage:bookmarks')
|
|
|
|
self.send_pb_delete(our_jid, 'storage:bookmarks')
|
2014-11-11 15:28:24 +01:00
|
|
|
if nbxmpp.NS_MAM in obj.features:
|
|
|
|
self.archiving_supported = True
|
|
|
|
self.archiving_313_supported = True
|
2017-01-26 21:34:54 +01:00
|
|
|
get_action(self.name + '-archive').set_enabled(True)
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_ARCHIVE in obj.features:
|
2010-11-29 12:53:50 +01:00
|
|
|
self.archiving_supported = True
|
2014-11-11 15:28:24 +01:00
|
|
|
self.archiving_136_supported = True
|
2015-09-30 20:00:48 +02:00
|
|
|
self.request_message_archiving_preferences()
|
|
|
|
if nbxmpp.NS_ARCHIVE_AUTO in obj.features:
|
|
|
|
self.archive_auto_supported = True
|
|
|
|
if nbxmpp.NS_ARCHIVE_MANAGE in obj.features:
|
|
|
|
self.archive_manage_supported = True
|
|
|
|
if nbxmpp.NS_ARCHIVE_MANUAL in obj.features:
|
|
|
|
self.archive_manual_supported = True
|
|
|
|
if nbxmpp.NS_ARCHIVE_PREF in obj.features:
|
|
|
|
self.archive_pref_supported = True
|
2013-03-30 23:13:48 +01:00
|
|
|
if nbxmpp.NS_BLOCKING in obj.features:
|
|
|
|
self.blocking_supported = True
|
2013-07-27 14:14:50 +02:00
|
|
|
if nbxmpp.NS_ADDRESS in obj.features:
|
|
|
|
self.addressing_supported = True
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_CARBONS in obj.features and gajim.config.get_per(
|
2014-01-01 20:49:53 +01:00
|
|
|
'accounts', self.name, 'enable_message_carbons'):
|
|
|
|
self.carbons_enabled = True
|
2011-11-06 17:20:58 +01:00
|
|
|
# Server supports carbons, activate it
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set')
|
|
|
|
iq.setTag('enable', namespace=nbxmpp.NS_CARBONS)
|
2011-11-06 17:20:58 +01:00
|
|
|
self.connection.send(iq)
|
2015-03-07 15:04:41 +01:00
|
|
|
if nbxmpp.NS_PRIVACY in obj.features:
|
|
|
|
self.privacy_rules_supported = True
|
2017-01-26 21:34:54 +01:00
|
|
|
get_action(self.name + '-privacylists').set_enabled(True)
|
2015-03-07 15:04:41 +01:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_BYTESTREAM in obj.features and \
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.config.get_per('accounts', self.name, 'use_ft_proxies'):
|
|
|
|
our_fjid = helpers.parse_jid(our_jid + '/' + \
|
|
|
|
self.server_resource)
|
2011-10-23 20:26:28 +02:00
|
|
|
testit = gajim.config.get_per('accounts', self.name,
|
|
|
|
'test_ft_proxies_on_startup')
|
2010-11-29 12:53:50 +01:00
|
|
|
gajim.proxy65_manager.resolve(obj.fjid, self.connection,
|
2011-10-23 20:26:28 +02:00
|
|
|
our_fjid, default=self.name, testit=testit)
|
2012-12-09 21:37:51 +01:00
|
|
|
if nbxmpp.NS_MUC in obj.features and is_muc:
|
2010-11-29 12:53:50 +01:00
|
|
|
type_ = transport_type or 'jabber'
|
|
|
|
self.muc_jid[type_] = obj.fjid
|
|
|
|
if transport_type:
|
|
|
|
if transport_type in self.available_transports:
|
|
|
|
self.available_transports[transport_type].append(obj.fjid)
|
|
|
|
else:
|
|
|
|
self.available_transports[transport_type] = [obj.fjid]
|
2015-03-07 15:04:41 +01:00
|
|
|
self._continue_connection_request_privacy()
|
2010-11-29 12:53:50 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_custom_status(self, show, msg, jid):
|
|
|
|
if not show in gajim.SHOW_LIST:
|
|
|
|
return -1
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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':
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(typ='unavailable', to=jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p, False)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
else:
|
|
|
|
signed = self.get_signed_presence(msg)
|
2013-01-01 21:06:16 +01:00
|
|
|
priority = gajim.get_priority(self.name, sshow)
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(typ=None, priority=priority, show=sshow, to=jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
if signed:
|
2012-12-09 21:37:51 +01:00
|
|
|
p.setTag(nbxmpp.NS_SIGNED + ' x').setData(signed)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def _change_to_invisible(self, msg):
|
|
|
|
signed = self.get_signed_presence(msg)
|
|
|
|
self.send_invisible_presence(msg, signed)
|
|
|
|
|
|
|
|
def _change_from_invisible(self):
|
|
|
|
if self.privacy_rules_supported:
|
2017-01-28 18:46:17 +01:00
|
|
|
self.set_active_list('')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _update_status(self, show, msg):
|
|
|
|
xmpp_show = helpers.get_xmpp_show(show)
|
2013-01-01 21:06:16 +01:00
|
|
|
priority = gajim.get_priority(self.name, xmpp_show)
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(typ=None, priority=priority, show=xmpp_show)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
signed = self.get_signed_presence(msg)
|
|
|
|
if signed:
|
2012-12-09 21:37:51 +01:00
|
|
|
p.setTag(nbxmpp.NS_SIGNED + ' x').setData(signed)
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connection:
|
|
|
|
self.connection.send(p)
|
|
|
|
self.priority = priority
|
2010-11-06 10:04:41 +01:00
|
|
|
gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
|
|
|
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):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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
|
|
|
|
obj.msg_id = self.connection.send(obj.msg_iq, now=obj.now)
|
2016-12-17 14:06:36 +01:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
gajim.nec.push_incoming_event(MessageSentEvent(
|
|
|
|
None, conn=self, jid=obj.jid, message=obj.message, keyID=obj.keyID,
|
|
|
|
chatstate=obj.chatstate, automatic_message=obj.automatic_message,
|
|
|
|
msg_id=obj.msg_id, additional_data=obj.additional_data))
|
|
|
|
if obj.callback:
|
|
|
|
obj.callback(obj, obj.msg_iq, *obj.callback_args)
|
2011-05-18 19:44:43 +02:00
|
|
|
|
2017-04-06 23:21:19 +02:00
|
|
|
if not obj.is_loggable:
|
|
|
|
return
|
|
|
|
if isinstance(obj.jid, list):
|
|
|
|
for j in obj.jid:
|
|
|
|
if obj.session is None:
|
|
|
|
obj.session = self.get_or_create_session(j, '')
|
|
|
|
self.log_message(
|
|
|
|
j, obj.message, obj.forward_from, obj.session,
|
|
|
|
obj.original_message, obj.subject, obj.type_, obj.xhtml,
|
|
|
|
obj.additional_data)
|
|
|
|
else:
|
|
|
|
self.log_message(
|
|
|
|
obj.jid, obj.message, obj.forward_from, obj.session,
|
|
|
|
obj.original_message, obj.subject, obj.type_, obj.xhtml,
|
|
|
|
obj.additional_data)
|
2015-09-23 22:39:11 +02:00
|
|
|
|
2013-02-10 18:57:25 +01:00
|
|
|
def send_contacts(self, contacts, fjid, type_='message'):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Send contacts with RosterX (Xep-0144)
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2013-02-10 18:57:25 +01:00
|
|
|
if type_ == 'message':
|
|
|
|
if len(contacts) == 1:
|
|
|
|
msg = _('Sent contact: "%s" (%s)') % (contacts[0].get_full_jid(),
|
2010-02-08 15:08:40 +01:00
|
|
|
contacts[0].get_shown_name())
|
2013-02-10 18:57:25 +01:00
|
|
|
else:
|
|
|
|
msg = _('Sent contacts:')
|
|
|
|
for contact in contacts:
|
|
|
|
msg += '\n "%s" (%s)' % (contact.get_full_jid(),
|
2010-02-08 15:08:40 +01:00
|
|
|
contact.get_shown_name())
|
2013-02-10 18:57:25 +01:00
|
|
|
stanza = nbxmpp.Message(to=gajim.get_jid_without_resource(fjid),
|
|
|
|
body=msg)
|
|
|
|
elif type_ == 'iq':
|
|
|
|
stanza = nbxmpp.Iq(to=fjid, typ='set')
|
|
|
|
x = stanza.addChild(name='x', namespace=nbxmpp.NS_ROSTERX)
|
2010-02-08 15:08:40 +01:00
|
|
|
for contact in contacts:
|
|
|
|
x.addChild(name='item', attrs={'action': 'add', 'jid': contact.jid,
|
2013-02-10 18:57:25 +01:00
|
|
|
'name': contact.get_shown_name()})
|
|
|
|
self.connection.send(stanza)
|
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 ack_subscribed(self, jid):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
log.debug('ack\'ing subscription complete for %s' % jid)
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(jid, 'subscribe')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def ack_unsubscribed(self, jid):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
log.debug('ack\'ing unsubscription complete for %s' % jid)
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(jid, 'unsubscribe')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(p)
|
|
|
|
|
2017-02-08 03:12:41 +01:00
|
|
|
def request_subscription(self, jid, msg='', name='', groups=None,
|
2010-02-08 15:08:40 +01:00
|
|
|
auto_auth=False, user_nick=''):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2017-02-08 03:12:41 +01:00
|
|
|
if groups is None:
|
|
|
|
groups = []
|
2010-02-08 15:08:40 +01:00
|
|
|
log.debug('subscription request for %s' % jid)
|
|
|
|
if auto_auth:
|
|
|
|
self.jids_for_auto_auth.append(jid)
|
|
|
|
# RFC 3921 section 8.2
|
|
|
|
infos = {'jid': jid}
|
|
|
|
if name:
|
|
|
|
infos['name'] = name
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_ROSTER)
|
2011-10-31 10:47:28 +01:00
|
|
|
q = iq.setQuery()
|
2010-02-08 15:08:40 +01:00
|
|
|
item = q.addChild('item', attrs=infos)
|
|
|
|
for g in groups:
|
|
|
|
item.addChild('group').setData(g)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(jid, 'subscribe')
|
2010-02-08 15:08:40 +01:00
|
|
|
if user_nick:
|
2012-12-09 21:37:51 +01:00
|
|
|
p.setTag('nick', namespace = nbxmpp.NS_NICK).setData(user_nick)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p)
|
|
|
|
if msg:
|
|
|
|
p.setStatus(msg)
|
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def send_authorization(self, jid):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(jid, 'subscribed')
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p)
|
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def refuse_authorization(self, jid):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(jid, 'unsubscribed')
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p)
|
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def unsubscribe(self, jid, remove_auth = True):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
if remove_auth:
|
|
|
|
self.connection.getRoster().delItem(jid)
|
|
|
|
jid_list = gajim.config.get_per('contacts')
|
|
|
|
for j in jid_list:
|
|
|
|
if j.startswith(jid):
|
|
|
|
gajim.config.del_per('contacts', j)
|
|
|
|
else:
|
|
|
|
self.connection.getRoster().Unsubscribe(jid)
|
|
|
|
self.update_contact(jid, '', [])
|
|
|
|
|
|
|
|
def unsubscribe_agent(self, agent):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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)
|
|
|
|
self.connection.getRoster().delItem(agent)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def _on_new_account(self, con=None, con_type=None):
|
|
|
|
if not con_type:
|
|
|
|
if len(self._connection_types) or len(self._hosts):
|
|
|
|
# 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
|
|
|
|
gajim.nec.push_incoming_event(NewAccountNotConnectedEvent(None,
|
|
|
|
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
|
|
|
|
|
|
|
def request_os_info(self, jid, resource, groupchat_jid=None):
|
|
|
|
"""
|
|
|
|
groupchat_jid is used when we want to send a request to a real jid and
|
|
|
|
act as if the answer comes from the groupchat_jid
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# If we are invisible, do not request
|
|
|
|
if self.connected == gajim.SHOW_LIST.index('invisible'):
|
|
|
|
self.dispatch('OS_INFO', (jid, resource, _('Not fetched because of invisible status'), _('Not fetched because of invisible status')))
|
|
|
|
return
|
|
|
|
to_whom_jid = jid
|
|
|
|
if resource:
|
|
|
|
to_whom_jid += '/' + resource
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(to=to_whom_jid, typ='get', queryNS=nbxmpp.NS_VERSION)
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
if groupchat_jid:
|
|
|
|
self.groupchat_jids[id_] = groupchat_jid
|
|
|
|
self.version_ids.append(id_)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def request_entity_time(self, jid, resource, groupchat_jid=None):
|
|
|
|
"""
|
|
|
|
groupchat_jid is used when we want to send a request to a real jid and
|
|
|
|
act as if the answer comes from the groupchat_jid
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# If we are invisible, do not request
|
|
|
|
if self.connected == gajim.SHOW_LIST.index('invisible'):
|
|
|
|
self.dispatch('ENTITY_TIME', (jid, resource, _('Not fetched because of invisible status')))
|
|
|
|
return
|
|
|
|
to_whom_jid = jid
|
|
|
|
if resource:
|
|
|
|
to_whom_jid += '/' + resource
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(to=to_whom_jid, typ='get')
|
|
|
|
iq.addChild('time', namespace=nbxmpp.NS_TIME_REVISED)
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
if groupchat_jid:
|
|
|
|
self.groupchat_jids[id_] = groupchat_jid
|
|
|
|
self.entity_time_ids.append(id_)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
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):
|
|
|
|
gajim.nec.push_incoming_event(GatewayPromptReceivedEvent(None,
|
|
|
|
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)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_settings(self):
|
|
|
|
"""
|
|
|
|
Get Gajim settings as described in XEP 0049
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq2.addChild(name='gajim', namespace='gajim:prefs')
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-06-07 19:11:44 +02:00
|
|
|
def seclabel_catalogue(self, to, callback):
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
|
|
|
self.seclabel_catalogue_request(to, callback)
|
2011-10-07 17:57:24 +02:00
|
|
|
server = gajim.get_jid_from_account(self.name).split("@")[1] # Really, no better way?
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='get', to=server)
|
|
|
|
iq2 = iq.addChild(name='catalog', namespace=nbxmpp.NS_SECLABEL_CATALOG)
|
2010-06-07 19:11:44 +02:00
|
|
|
iq2.setAttr('to', to)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-11-24 22:48:39 +01:00
|
|
|
def _nec_privacy_list_received(self, obj):
|
2017-01-27 19:36:13 +01:00
|
|
|
roster = gajim.interface.roster
|
2010-11-24 22:48:39 +01:00
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2017-01-27 22:24:53 +01:00
|
|
|
if obj.list_name != self.privacy_default_list:
|
2010-11-24 22:48:39 +01:00
|
|
|
return
|
|
|
|
self.blocked_contacts = []
|
|
|
|
self.blocked_groups = []
|
|
|
|
self.blocked_list = []
|
|
|
|
self.blocked_all = False
|
|
|
|
for rule in obj.rules:
|
|
|
|
if rule['action'] == 'allow':
|
|
|
|
if not 'type' in rule:
|
|
|
|
self.blocked_all = False
|
|
|
|
elif rule['type'] == 'jid' and rule['value'] in \
|
|
|
|
self.blocked_contacts:
|
|
|
|
self.blocked_contacts.remove(rule['value'])
|
|
|
|
elif rule['type'] == 'group' and rule['value'] in \
|
|
|
|
self.blocked_groups:
|
|
|
|
self.blocked_groups.remove(rule['value'])
|
|
|
|
elif rule['action'] == 'deny':
|
|
|
|
if not 'type' in rule:
|
|
|
|
self.blocked_all = True
|
|
|
|
elif rule['type'] == 'jid' and rule['value'] not in \
|
|
|
|
self.blocked_contacts:
|
|
|
|
self.blocked_contacts.append(rule['value'])
|
|
|
|
elif rule['type'] == 'group' and rule['value'] not in \
|
|
|
|
self.blocked_groups:
|
|
|
|
self.blocked_groups.append(rule['value'])
|
|
|
|
self.blocked_list.append(rule)
|
|
|
|
|
2017-02-02 19:56:24 +01:00
|
|
|
if 'type' in rule:
|
|
|
|
if rule['type'] == 'jid':
|
|
|
|
roster.draw_contact(rule['value'], self.name)
|
|
|
|
if rule['type'] == 'group':
|
|
|
|
roster.draw_group(rule['value'], self.name)
|
2017-01-27 19:36:13 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _request_bookmarks_xml(self):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq2.addChild(name='storage', namespace='storage:bookmarks')
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def _check_bookmarks_received(self):
|
|
|
|
if not self.bookmarks:
|
|
|
|
self._request_bookmarks_xml()
|
|
|
|
|
|
|
|
def get_bookmarks(self, storage_type=None):
|
|
|
|
"""
|
|
|
|
Get Bookmarks from storage or PubSub if supported as described in XEP
|
|
|
|
0048
|
|
|
|
|
|
|
|
storage_type can be set to xml to force request to xml storage
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2016-10-10 19:10:18 +02:00
|
|
|
if self.pubsub_supported and self.pubsub_publish_options_supported \
|
|
|
|
and storage_type != 'xml':
|
2010-02-08 15:08:40 +01:00
|
|
|
self.send_pb_retrieve('', 'storage:bookmarks')
|
|
|
|
# some server (ejabberd) are so slow to answer that we request via XML
|
|
|
|
# if we don't get answer in the next 30 seconds
|
|
|
|
gajim.idlequeue.set_alarm(self._check_bookmarks_received, 30)
|
|
|
|
else:
|
|
|
|
self._request_bookmarks_xml()
|
|
|
|
|
|
|
|
def store_bookmarks(self, storage_type=None):
|
|
|
|
"""
|
|
|
|
Send bookmarks to the storage namespace or PubSub if supported
|
|
|
|
|
|
|
|
storage_type can be set to 'pubsub' or 'xml' so store in only one method
|
|
|
|
else it will be stored on both
|
|
|
|
"""
|
2016-10-10 19:10:18 +02:00
|
|
|
NS_GAJIM_BM = 'xmpp:gajim.org/bookmarks'
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Node(tag='storage', attrs={'xmlns': 'storage:bookmarks'})
|
2010-02-08 15:08:40 +01:00
|
|
|
for bm in self.bookmarks:
|
2016-10-10 19:10:18 +02:00
|
|
|
iq2 = iq.addChild(name="conference")
|
2010-02-08 15:08:40 +01:00
|
|
|
iq2.setAttr('jid', bm['jid'])
|
|
|
|
iq2.setAttr('autojoin', bm['autojoin'])
|
|
|
|
iq2.setAttr('name', bm['name'])
|
2016-10-10 19:10:18 +02:00
|
|
|
iq2.setTag('minimize', namespace=NS_GAJIM_BM). \
|
|
|
|
setData(bm['minimize'])
|
2010-02-08 15:08:40 +01:00
|
|
|
# Only add optional elements if not empty
|
|
|
|
# Note: need to handle both None and '' as empty
|
|
|
|
# thus shouldn't use "is not None"
|
|
|
|
if bm.get('nick', None):
|
|
|
|
iq2.setTagData('nick', bm['nick'])
|
|
|
|
if bm.get('password', None):
|
|
|
|
iq2.setTagData('password', bm['password'])
|
|
|
|
if bm.get('print_status', None):
|
2016-10-10 19:10:18 +02:00
|
|
|
iq2.setTag('print_status', namespace=NS_GAJIM_BM). \
|
|
|
|
setData(bm['print_status'])
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2016-10-10 19:10:18 +02:00
|
|
|
if self.pubsub_supported and self.pubsub_publish_options_supported and\
|
|
|
|
storage_type != 'xml':
|
2012-12-09 21:37:51 +01:00
|
|
|
options = nbxmpp.Node(nbxmpp.NS_DATA + ' x',
|
2016-10-10 19:10:18 +02:00
|
|
|
attrs={'type': 'submit'})
|
|
|
|
f = options.addChild('field',
|
|
|
|
attrs={'var': 'FORM_TYPE', 'type': 'hidden'})
|
2012-12-09 21:37:51 +01:00
|
|
|
f.setTagData('value', nbxmpp.NS_PUBSUB_PUBLISH_OPTIONS)
|
2016-10-10 19:10:18 +02:00
|
|
|
f = options.addChild('field',
|
|
|
|
attrs={'var': 'pubsub#persist_items'})
|
2010-06-21 19:15:08 +02:00
|
|
|
f.setTagData('value', 'true')
|
|
|
|
f = options.addChild('field', attrs={'var': 'pubsub#access_model'})
|
|
|
|
f.setTagData('value', 'whitelist')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.send_pb_publish('', 'storage:bookmarks', iq, 'current',
|
2016-10-10 19:10:18 +02:00
|
|
|
options=options)
|
2010-02-08 15:08:40 +01:00
|
|
|
if storage_type != 'pubsub':
|
2012-12-09 21:37:51 +01:00
|
|
|
iqA = nbxmpp.Iq(typ='set')
|
|
|
|
iqB = iqA.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iqB.addChild(node=iq)
|
|
|
|
self.connection.send(iqA)
|
|
|
|
|
|
|
|
def get_annotations(self):
|
|
|
|
"""
|
|
|
|
Get Annonations from storage as described in XEP 0048, and XEP 0145
|
|
|
|
"""
|
|
|
|
self.annotations = {}
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq2.addChild(name='storage', namespace='storage:rosternotes')
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def store_annotations(self):
|
|
|
|
"""
|
|
|
|
Set Annonations in private storage as described in XEP 0048, and XEP 0145
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq3 = iq2.addChild(name='storage', namespace='storage:rosternotes')
|
|
|
|
for jid in self.annotations.keys():
|
|
|
|
if self.annotations[jid]:
|
|
|
|
iq4 = iq3.addChild(name = "note")
|
|
|
|
iq4.setAttr('jid', jid)
|
|
|
|
iq4.setData(self.annotations[jid])
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-12-11 12:57:09 +01:00
|
|
|
def get_roster_delimiter(self):
|
|
|
|
"""
|
|
|
|
Get roster group delimiter from storage as described in XEP 0083
|
|
|
|
"""
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='get')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-12-11 12:57:09 +01:00
|
|
|
iq2.addChild(name='roster', namespace='roster:delimiter')
|
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
self.awaiting_answers[id_] = (DELIMITER_ARRIVED, )
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def set_roster_delimiter(self, delimiter='::'):
|
|
|
|
"""
|
|
|
|
Set roster group delimiter to the storage namespace
|
|
|
|
"""
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='set')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-12-11 12:57:09 +01:00
|
|
|
iq3 = iq2.addChild(name='roster', namespace='roster:delimiter')
|
|
|
|
iq3.setData(delimiter)
|
|
|
|
|
|
|
|
self.connection.send(iq)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_metacontacts(self):
|
|
|
|
"""
|
|
|
|
Get metacontacts list from storage as described in XEP 0049
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq2.addChild(name='storage', namespace='storage:metacontacts')
|
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
iq.setID(id_)
|
|
|
|
self.awaiting_answers[id_] = (METACONTACTS_ARRIVED, )
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def store_metacontacts(self, tags_list):
|
|
|
|
"""
|
|
|
|
Send meta contacts to the storage namespace
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set')
|
|
|
|
iq2 = iq.addChild(name='query', namespace=nbxmpp.NS_PRIVATE)
|
2010-02-08 15:08:40 +01:00
|
|
|
iq3 = iq2.addChild(name='storage', namespace='storage:metacontacts')
|
|
|
|
for tag in tags_list:
|
|
|
|
for data in tags_list[tag]:
|
|
|
|
jid = data['jid']
|
|
|
|
dict_ = {'jid': jid, 'tag': tag}
|
|
|
|
if 'order' in data:
|
|
|
|
dict_['order'] = data['order']
|
2012-12-09 21:37:51 +01:00
|
|
|
iq3.addChild(name='meta', attrs=dict_)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-12-11 12:57:09 +01:00
|
|
|
def request_roster(self):
|
|
|
|
version = None
|
|
|
|
features = self.connection.Dispatcher.Stream.features
|
|
|
|
if features and features.getTag('ver',
|
2012-12-09 21:37:51 +01:00
|
|
|
namespace=nbxmpp.NS_ROSTER_VER):
|
2010-12-11 12:57:09 +01:00
|
|
|
version = gajim.config.get_per('accounts', self.name,
|
|
|
|
'roster_version')
|
|
|
|
if version and not gajim.contacts.get_contacts_jid_list(
|
|
|
|
self.name):
|
|
|
|
gajim.config.set_per('accounts', self.name, 'roster_version',
|
|
|
|
'')
|
|
|
|
version = None
|
|
|
|
|
|
|
|
iq_id = self.connection.initRoster(version=version)
|
|
|
|
self.awaiting_answers[iq_id] = (ROSTER_ARRIVED, )
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_agent_status(self, agent, ptype):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
show = helpers.get_xmpp_show(gajim.SHOW_LIST[self.connected])
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(to=agent, typ=ptype, show=show)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = self.add_sha(p, ptype != 'unavailable')
|
|
|
|
self.connection.send(p)
|
|
|
|
|
2010-06-27 23:09:07 +02:00
|
|
|
def send_captcha(self, jid, form_node):
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
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):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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):
|
2010-11-24 17:28:18 +01:00
|
|
|
gajim.nec.push_incoming_event(UniqueRoomIdNotSupportedEvent(
|
|
|
|
None, conn=self, instance=instance, server=server))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2010-11-24 17:28:18 +01:00
|
|
|
gajim.nec.push_incoming_event(UniqueRoomIdSupportedEvent(None,
|
|
|
|
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
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
show = helpers.get_xmpp_show(gajim.SHOW_LIST[self.connected])
|
|
|
|
if show == 'invisible':
|
|
|
|
# Never join a room when invisible
|
|
|
|
return
|
|
|
|
|
|
|
|
# last date/time in history to avoid duplicate
|
|
|
|
if room_jid not in self.last_history_time:
|
|
|
|
# Not in memory, get it from DB
|
|
|
|
last_log = None
|
|
|
|
# Do not check if we are not logging for this room
|
|
|
|
if gajim.config.should_log(self.name, room_jid):
|
|
|
|
# Check time first in the FAST table
|
|
|
|
last_log = gajim.logger.get_room_last_message_time(room_jid)
|
|
|
|
if last_log is None:
|
|
|
|
# Not in special table, get it from messages DB
|
|
|
|
last_log = gajim.logger.get_last_date_that_has_logs(room_jid,
|
|
|
|
is_room=True)
|
|
|
|
# Create self.last_history_time[room_jid] even if not logging,
|
|
|
|
# could be used in connection_handlers
|
|
|
|
if last_log is None:
|
|
|
|
last_log = 0
|
|
|
|
self.last_history_time[room_jid] = last_log
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(to='%s/%s' % (room_jid, nick),
|
2010-02-08 15:08:40 +01:00
|
|
|
show=show, status=self.status)
|
2015-07-31 22:08:00 +02:00
|
|
|
h = hmac.new(self.secret_hmac, room_jid.encode('utf-8'), hashlib.md5).\
|
|
|
|
hexdigest()[:6]
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
id_ = 'gajim_muc_' + id_ + '_' + h
|
|
|
|
p.setID(id_)
|
|
|
|
if gajim.config.get('send_sha_in_gc_presence'):
|
|
|
|
p = self.add_sha(p)
|
|
|
|
self.add_lang(p)
|
|
|
|
if not change_nick:
|
2012-12-09 21:37:51 +01:00
|
|
|
t = p.setTag(nbxmpp.NS_MUC + ' x')
|
2011-05-09 20:32:48 +02:00
|
|
|
tags = {}
|
2012-12-25 18:56:05 +01:00
|
|
|
timeout = gajim.config.get_per('rooms', room_jid,
|
2012-12-09 17:07:50 +01:00
|
|
|
'muc_restore_timeout')
|
2012-12-13 10:07:51 +01:00
|
|
|
if timeout is None or timeout == -2:
|
|
|
|
timeout = gajim.config.get('muc_restore_timeout')
|
|
|
|
timeout *= 60
|
2011-05-09 20:32:48 +02:00
|
|
|
if timeout >= 0:
|
|
|
|
last_date = self.last_history_time[room_jid]
|
|
|
|
if last_date == 0:
|
|
|
|
last_date = time.time() - timeout
|
2012-02-22 15:36:35 +01:00
|
|
|
elif not rejoin:
|
2011-05-09 20:32:48 +02:00
|
|
|
last_date = min(last_date, time.time() - timeout)
|
|
|
|
last_date = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(
|
|
|
|
last_date))
|
|
|
|
tags['since'] = last_date
|
2012-12-25 18:56:05 +01:00
|
|
|
nb = gajim.config.get_per('rooms', room_jid, 'muc_restore_lines')
|
2012-12-13 10:07:51 +01:00
|
|
|
if nb is None or nb == -2:
|
2012-12-09 17:07:50 +01:00
|
|
|
nb = gajim.config.get('muc_restore_lines')
|
2011-05-09 20:32:48 +02:00
|
|
|
if nb >= 0:
|
|
|
|
tags['maxstanzas'] = nb
|
|
|
|
if tags:
|
|
|
|
t.setTag('history', tags)
|
2010-02-08 15:08:40 +01:00
|
|
|
if password:
|
|
|
|
t.setTagData('password', password)
|
|
|
|
self.connection.send(p)
|
|
|
|
|
2013-07-19 11:52:37 +02:00
|
|
|
def _nec_gc_message_outgoing(self, obj):
|
|
|
|
if obj.account != self.name:
|
|
|
|
return
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
|
|
|
|
|
|
|
if obj.correction_msg:
|
|
|
|
id_ = obj.correction_msg.getID()
|
|
|
|
if obj.correction_msg.getTag('replace'):
|
|
|
|
obj.correction_msg.delChild('replace')
|
|
|
|
obj.correction_msg.setTag('replace', attrs={'id': id_},
|
2016-06-24 20:12:37 +02:00
|
|
|
namespace=nbxmpp.NS_CORRECT)
|
2013-07-19 11:52:37 +02:00
|
|
|
id2 = self.connection.getAnID()
|
|
|
|
obj.correction_msg.setID(id2)
|
|
|
|
obj.correction_msg.setBody(obj.message)
|
|
|
|
if obj.xhtml:
|
|
|
|
obj.correction_msg.setXHTML(xhtml)
|
2016-06-24 20:12:37 +02:00
|
|
|
gajim.nec.push_incoming_event(GcStanzaMessageOutgoingEvent(
|
|
|
|
None, conn=self, automatic_message=obj.automatic_message,
|
|
|
|
jid=obj.jid, message=obj.message,
|
2016-09-05 01:43:39 +02:00
|
|
|
correction_msg=obj.correction_msg, additional_data=obj.additional_data))
|
2013-07-19 11:52:37 +02:00
|
|
|
if obj.callback:
|
|
|
|
obj.callback(obj.correction_msg, obj.message)
|
|
|
|
return
|
|
|
|
if not obj.xhtml and gajim.config.get('rst_formatting_outgoing_messages'):
|
|
|
|
from common.rst_xhtml_generator import create_xhtml
|
|
|
|
obj.xhtml = create_xhtml(obj.message)
|
|
|
|
msg_iq = nbxmpp.Message(obj.jid, obj.message, typ='groupchat',
|
2016-06-24 20:12:37 +02:00
|
|
|
xhtml=obj.xhtml)
|
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)
|
2016-06-24 20:12:37 +02:00
|
|
|
gajim.nec.push_incoming_event(GcStanzaMessageOutgoingEvent(
|
|
|
|
None, conn=self, msg_iq=msg_iq,
|
|
|
|
automatic_message=obj.automatic_message,
|
2016-09-05 01:43:39 +02:00
|
|
|
jid=obj.jid, message=obj.message, correction_msg=None, additional_data=obj.additional_data))
|
2013-07-19 11:52:37 +02:00
|
|
|
if obj.callback:
|
|
|
|
obj.callback(msg_iq, obj.message)
|
|
|
|
|
2016-06-24 20:12:37 +02:00
|
|
|
def _nec_gc_stanza_message_outgoing(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
|
|
|
if obj.correction_msg:
|
|
|
|
obj.msg_id = self.connection.send(obj.correction_msg)
|
|
|
|
else:
|
|
|
|
obj.msg_id = self.connection.send(obj.msg_iq)
|
|
|
|
gajim.nec.push_incoming_event(MessageSentEvent(
|
|
|
|
None, conn=self, jid=obj.jid, message=obj.message, keyID=None,
|
|
|
|
chatstate=None, automatic_message=obj.automatic_message,
|
2016-09-05 01:43:39 +02:00
|
|
|
msg_id=obj.msg_id, additional_data=obj.additional_data))
|
2016-06-24 20:12:37 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_gc_subject(self, jid, subject):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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(jid, typ='groupchat', subject=subject)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(msg_iq)
|
|
|
|
|
|
|
|
def request_gc_config(self, room_jid):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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', queryNS=nbxmpp.NS_MUC_OWNER,
|
|
|
|
to=room_jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.add_lang(iq)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def destroy_gc_room(self, room_jid, reason = '', jid = ''):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set', queryNS=nbxmpp.NS_MUC_OWNER,
|
|
|
|
to=room_jid)
|
2011-10-31 10:47:28 +01:00
|
|
|
destroy = iq.setQuery().setTag('destroy')
|
2010-02-08 15:08:40 +01:00
|
|
|
if reason:
|
|
|
|
destroy.setTagData('reason', reason)
|
|
|
|
if jid:
|
|
|
|
destroy.setAttr('jid', jid)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def send_gc_status(self, nick, jid, show, status):
|
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
|
|
|
if show == 'invisible':
|
|
|
|
show = 'offline'
|
|
|
|
ptype = None
|
|
|
|
if show == 'offline':
|
|
|
|
ptype = 'unavailable'
|
|
|
|
xmpp_show = helpers.get_xmpp_show(show)
|
2012-12-09 21:37:51 +01:00
|
|
|
p = nbxmpp.Presence(to='%s/%s' % (jid, nick), typ=ptype,
|
|
|
|
show=xmpp_show, status=status)
|
2015-07-31 22:08:00 +02:00
|
|
|
h = hmac.new(self.secret_hmac, jid.encode('utf-8'), hashlib.md5).\
|
|
|
|
hexdigest()[:6]
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
id_ = 'gajim_muc_' + id_ + '_' + h
|
|
|
|
p.setID(id_)
|
|
|
|
if gajim.config.get('send_sha_in_gc_presence') and show != 'offline':
|
|
|
|
p = self.add_sha(p, ptype != 'unavailable')
|
|
|
|
self.add_lang(p)
|
|
|
|
# send instantly so when we go offline, status is sent to gc before we
|
|
|
|
# disconnect from jabber server
|
|
|
|
self.connection.send(p)
|
|
|
|
|
|
|
|
def gc_got_disconnected(self, room_jid):
|
|
|
|
"""
|
|
|
|
A groupchat got disconnected. This can be or purpose or not
|
|
|
|
|
|
|
|
Save the time we had last message to avoid duplicate logs AND be faster
|
|
|
|
than get that date from DB. Save time that we have in mem in a small
|
|
|
|
table (with fast access)
|
|
|
|
"""
|
|
|
|
gajim.logger.set_room_last_message_time(room_jid, self.last_history_time[room_jid])
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
def gc_set_role(self, room_jid, nick, role, reason=''):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Role is for all the life of the room so it's based on nick
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
|
2011-10-31 10:47:28 +01:00
|
|
|
item = iq.setQuery().setTag('item')
|
2010-02-08 15:08:40 +01:00
|
|
|
item.setAttr('nick', nick)
|
|
|
|
item.setAttr('role', role)
|
|
|
|
if reason:
|
2012-12-09 21:37:51 +01:00
|
|
|
item.addChild(name='reason', payload=reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def gc_set_affiliation(self, room_jid, jid, affiliation, reason = ''):
|
|
|
|
"""
|
|
|
|
Affiliation is for all the life of the room so it's based on jid
|
|
|
|
"""
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
|
2011-10-31 10:47:28 +01:00
|
|
|
item = iq.setQuery().setTag('item')
|
2010-02-08 15:08:40 +01:00
|
|
|
item.setAttr('jid', jid)
|
|
|
|
item.setAttr('affiliation', affiliation)
|
|
|
|
if reason:
|
|
|
|
item.addChild(name = 'reason', payload = reason)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def send_gc_affiliation_list(self, room_jid, users_dict):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
|
2011-10-31 10:47:28 +01:00
|
|
|
item = iq.setQuery()
|
2010-02-08 15:08:40 +01:00
|
|
|
for jid in users_dict:
|
|
|
|
item_tag = item.addChild('item', {'jid': jid,
|
|
|
|
'affiliation': users_dict[jid]['affiliation']})
|
|
|
|
if 'reason' in users_dict[jid] and users_dict[jid]['reason']:
|
|
|
|
item_tag.setTagData('reason', users_dict[jid]['reason'])
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def get_affiliation_list(self, room_jid, affiliation):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
|
2011-10-31 10:47:28 +01:00
|
|
|
item = iq.setQuery().setTag('item')
|
2010-02-08 15:08:40 +01:00
|
|
|
item.setAttr('affiliation', affiliation)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def send_gc_config(self, room_jid, form):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.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='set', to=room_jid, queryNS=nbxmpp.NS_MUC_OWNER)
|
2011-10-31 10:47:28 +01:00
|
|
|
query = iq.setQuery()
|
2010-02-08 15:08:40 +01:00
|
|
|
form.setAttr('type', 'submit')
|
|
|
|
query.addChild(node = form)
|
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def change_password(self, password):
|
2010-03-20 19:00:33 +01:00
|
|
|
if not gajim.account_is_connected(self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
hostname = gajim.config.get_per('accounts', self.name, 'hostname')
|
|
|
|
username = gajim.config.get_per('accounts', self.name, 'name')
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='set', to=hostname)
|
|
|
|
q = iq.setTag(nbxmpp.NS_REGISTER + ' query')
|
2010-02-10 19:24:11 +01:00
|
|
|
q.setTagData('username', username)
|
|
|
|
q.setTagData('password', password)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(iq)
|
|
|
|
|
2010-02-17 13:35:18 +01:00
|
|
|
def get_password(self, callback, type_):
|
2015-08-31 13:47:18 +02:00
|
|
|
if gajim.config.get_per('accounts', self.name, 'anonymous_auth') and \
|
|
|
|
type_ != 'ANONYMOUS':
|
|
|
|
gajim.nec.push_incoming_event(NonAnonymousServerErrorEvent(None,
|
|
|
|
conn=self))
|
|
|
|
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':
|
|
|
|
client_id = gajim.config.get_per('accounts', self.name,
|
|
|
|
'oauth2_client_id')
|
|
|
|
refresh_token = gajim.config.get_per('accounts', self.name,
|
|
|
|
'oauth2_refresh_token')
|
|
|
|
if refresh_token:
|
|
|
|
renew_URL = 'https://oauth.live.com/token?client_id=' \
|
|
|
|
'%(client_id)s&redirect_uri=https%%3A%%2F%%2Foauth.live.' \
|
|
|
|
'com%%2Fdesktop&grant_type=refresh_token&refresh_token=' \
|
|
|
|
'%(refresh_token)s' % locals()
|
|
|
|
result = helpers.download_image(self.name, {'src': renew_URL})[0]
|
|
|
|
if result:
|
|
|
|
dict_ = json.loads(result)
|
|
|
|
if 'access_token' in dict_:
|
|
|
|
self.set_password(dict_['access_token'])
|
|
|
|
return
|
|
|
|
script_url = gajim.config.get_per('accounts', self.name,
|
|
|
|
'oauth2_redirect_url')
|
|
|
|
token_URL = 'https://oauth.live.com/authorize?client_id=' \
|
|
|
|
'%(client_id)s&scope=wl.messenger%%20wl.offline_access&' \
|
|
|
|
'response_type=code&redirect_uri=%(script_url)s' % locals()
|
|
|
|
helpers.launch_browser_mailer('url', token_URL)
|
|
|
|
self.disconnect(on_purpose=True)
|
|
|
|
gajim.nec.push_incoming_event(Oauth2CredentialsRequiredEvent(None,
|
|
|
|
conn=self))
|
|
|
|
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
|
2010-11-29 10:00:23 +01:00
|
|
|
gajim.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 \
|
|
|
|
gajim.config.get_per('accounts', self.name,
|
|
|
|
'warn_when_insecure_password'):
|
2010-11-24 16:18:56 +01:00
|
|
|
gajim.nec.push_incoming_event(InsecurePasswordEvent(None,
|
|
|
|
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:
|
|
|
|
callback, type_ = self.pasword_callback
|
|
|
|
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
|
|
|
|
if gajim.account_is_connected(self.name):
|
|
|
|
hostname = gajim.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
|
2011-11-22 19:25:15 +01:00
|
|
|
gajim.nec.push_incoming_event(InformationEvent(None,
|
|
|
|
conn=self, level='error',
|
|
|
|
pri_txt=_('Unregister failed'),
|
|
|
|
sec_txt=_('Unregistration with server %(server)s '
|
|
|
|
'failed: %(error)s') % {'server': hostname,
|
2011-01-05 20:59:10 +01:00
|
|
|
'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)
|
|
|
|
if self.connected == 0:
|
|
|
|
self.on_connect_auth = _on_unregister_account_connect
|
|
|
|
self.connect_and_auth()
|
|
|
|
else:
|
|
|
|
_on_unregister_account_connect(self.connection)
|
|
|
|
|
|
|
|
def send_invite(self, room, to, reason='', continue_tag=False):
|
|
|
|
"""
|
|
|
|
Send invitation
|
|
|
|
"""
|
2014-07-10 20:30:20 +02:00
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
2013-11-28 21:24:17 +01:00
|
|
|
contact = gajim.contacts.get_contact_from_full_jid(self.name, to)
|
|
|
|
if contact and contact.supports(nbxmpp.NS_CONFERENCE):
|
|
|
|
# send direct invite
|
|
|
|
message=nbxmpp.Message(to=to)
|
|
|
|
attrs = {'jid': room}
|
|
|
|
if reason:
|
|
|
|
attrs['reason'] = reason
|
|
|
|
if continue_tag:
|
|
|
|
attrs['continue'] = 'true'
|
2015-12-02 23:05:10 +01:00
|
|
|
password = gajim.gc_passwords.get(room, '')
|
|
|
|
if password:
|
|
|
|
attrs['password'] = password
|
2013-11-28 21:24:17 +01:00
|
|
|
c = message.addChild(name='x', attrs=attrs,
|
|
|
|
namespace=nbxmpp.NS_CONFERENCE)
|
|
|
|
self.connection.send(message)
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
message=nbxmpp.Message(to=room)
|
|
|
|
c = message.addChild(name='x', namespace=nbxmpp.NS_MUC_USER)
|
2012-08-23 21:34:03 +02:00
|
|
|
c = c.addChild(name='invite', attrs={'to': to})
|
2010-02-08 15:08:40 +01:00
|
|
|
if continue_tag:
|
2012-08-23 21:34:03 +02:00
|
|
|
c.addChild(name='continue')
|
|
|
|
if reason != '':
|
|
|
|
c.setTagData('reason', reason)
|
|
|
|
self.connection.send(message)
|
|
|
|
|
|
|
|
def decline_invitation(self, room, to, reason=''):
|
|
|
|
"""
|
|
|
|
decline a groupchat invitation
|
|
|
|
"""
|
2014-07-10 20:30:20 +02:00
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
message=nbxmpp.Message(to=room)
|
|
|
|
c = message.addChild(name='x', namespace=nbxmpp.NS_MUC_USER)
|
2012-08-23 21:34:03 +02:00
|
|
|
c = c.addChild(name='decline', attrs={'to': to})
|
2010-02-08 15:08:40 +01:00
|
|
|
if reason != '':
|
|
|
|
c.setTagData('reason', reason)
|
|
|
|
self.connection.send(message)
|
|
|
|
|
2011-07-29 17:34:53 +02:00
|
|
|
def request_voice(self, room):
|
2011-05-09 20:32:48 +02:00
|
|
|
"""
|
2011-04-26 15:54:46 +02:00
|
|
|
Request voice in a moderated room
|
|
|
|
"""
|
2014-07-10 20:30:20 +02:00
|
|
|
if not gajim.account_is_connected(self.name):
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
message = nbxmpp.Message(to=room)
|
2011-04-26 15:54:46 +02:00
|
|
|
|
2012-12-16 23:53:32 +01:00
|
|
|
x = nbxmpp.DataForm(typ='submit')
|
|
|
|
x.addChild(node=nbxmpp.DataField(name='FORM_TYPE',
|
2012-12-09 21:37:51 +01:00
|
|
|
value=nbxmpp.NS_MUC + '#request'))
|
2012-12-16 23:53:32 +01:00
|
|
|
x.addChild(node=nbxmpp.DataField(name='muc#role', value='participant',
|
2011-04-26 15:54:46 +02:00
|
|
|
typ='text-single'))
|
|
|
|
|
|
|
|
message.addChild(node=x)
|
|
|
|
|
|
|
|
self.connection.send(message)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def check_pingalive(self):
|
2012-12-12 22:03:13 +01:00
|
|
|
if not gajim.config.get_per('accounts', self.name, 'active'):
|
|
|
|
# Account may have been disabled
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.awaiting_xmpp_ping_id:
|
|
|
|
# We haven't got the pong in time, disco and reconnect
|
2013-01-05 11:26:22 +01:00
|
|
|
log.warning("No reply received for keepalive ping. Reconnecting.")
|
2010-02-08 15:08:40 +01:00
|
|
|
self._disconnectedReconnCB()
|
|
|
|
|
|
|
|
def _reconnect_alarm(self):
|
2010-08-23 14:55:03 +02:00
|
|
|
if not gajim.config.get_per('accounts', self.name, 'active'):
|
|
|
|
# Account may have been disabled
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.time_to_reconnect:
|
|
|
|
if self.connected < 2:
|
|
|
|
self._reconnect()
|
|
|
|
else:
|
|
|
|
self.time_to_reconnect = None
|
|
|
|
|
|
|
|
def request_search_fields(self, jid):
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='get', to=jid, queryNS=nbxmpp.NS_SEARCH)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.send(iq)
|
|
|
|
|
|
|
|
def send_search_form(self, jid, form, is_form):
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq(typ='set', to=jid, queryNS=nbxmpp.NS_SEARCH)
|
2011-10-31 10:47:28 +01:00
|
|
|
item = iq.setQuery()
|
2010-02-08 15:08:40 +01:00
|
|
|
if is_form:
|
2010-08-30 21:42:36 +02:00
|
|
|
item.addChild(node=form)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
for i in form.keys():
|
2010-02-10 19:24:11 +01:00
|
|
|
item.setTagData(i, form[i])
|
2010-02-08 15:08:40 +01:00
|
|
|
def _on_response(resp):
|
2010-08-30 21:42:36 +02:00
|
|
|
gajim.nec.push_incoming_event(SearchResultReceivedEvent(None,
|
2010-10-19 17:21:11 +02:00
|
|
|
conn=self, stanza=resp))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connection.SendAndCallForResponse(iq, _on_response)
|
|
|
|
|
|
|
|
def load_roster_from_db(self):
|
2010-10-19 18:14:30 +02:00
|
|
|
gajim.nec.push_incoming_event(RosterReceivedEvent(None, conn=self))
|
2009-07-09 14:33:18 +02:00
|
|
|
|
2005-05-31 15:53:22 +02:00
|
|
|
# END Connection
|