connection.py 124 KB
Newer Older
roidelapluie's avatar
roidelapluie committed
1
# -*- coding:utf-8 -*-
roidelapluie's avatar
roidelapluie committed
2
## src/common/connection.py
3
##
roidelapluie's avatar
roidelapluie committed
4
## Copyright (C) 2003-2005 Vincent Hanquez <tab AT snarc.org>
Dicson's avatar
Dicson committed
5
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
roidelapluie's avatar
roidelapluie committed
6
## Copyright (C) 2005 Alex Mauer <hawke AT hawkesnest.net>
roidelapluie's avatar
roidelapluie committed
7
##                    Stéphan Kochen <stephan AT kochen.nl>
roidelapluie's avatar
roidelapluie committed
8 9 10
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
##                         Travis Shirk <travis AT pobox.com>
##                         Nikos Kouremenos <kourem AT gmail.com>
roidelapluie's avatar
roidelapluie committed
11
## Copyright (C) 2006 Junglecow J <junglecow AT gmail.com>
roidelapluie's avatar
roidelapluie committed
12 13 14 15 16 17 18
##                    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>
19
##
Yann Leboulanger's avatar
Yann Leboulanger committed
20 21 22
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
23
## it under the terms of the GNU General Public License as published
Yann Leboulanger's avatar
Yann Leboulanger committed
24
## by the Free Software Foundation; version 3 only.
25
##
Yann Leboulanger's avatar
Yann Leboulanger committed
26
## Gajim is distributed in the hope that it will be useful,
27
## but WITHOUT ANY WARRANTY; without even the implied warranty of
roidelapluie's avatar
roidelapluie committed
28
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 30
## GNU General Public License for more details.
##
Yann Leboulanger's avatar
Yann Leboulanger committed
31
## You should have received a copy of the GNU General Public License
roidelapluie's avatar
roidelapluie committed
32
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
Yann Leboulanger's avatar
Yann Leboulanger committed
33
##
34 35

import os
36
import random
Piotr Gaczkowski's avatar
Piotr Gaczkowski committed
37
import socket
38
import operator
39

Yann Leboulanger's avatar
Yann Leboulanger committed
40
import time
41
import locale
42
import hmac
43
import json
Yann Leboulanger's avatar
Yann Leboulanger committed
44

45
try:
46
    randomsource = random.SystemRandom()
47
except Exception:
48 49
    randomsource = random.Random()
    randomsource.seed()
50

51
import signal
Yann Leboulanger's avatar
Yann Leboulanger committed
52
if os.name != 'nt':
53
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
dkirov's avatar
dkirov committed
54

55
import nbxmpp
Yann Leboulanger's avatar
Yann Leboulanger committed
56
from common import helpers
Yann Leboulanger's avatar
Yann Leboulanger committed
57
from common import gajim
58
from common import gpg
59
from common import passwords
60
from common import exceptions
61
from common import check_X509
62
from connection_handlers import *
63

Yann Leboulanger's avatar
Yann Leboulanger committed
64 65 66
if gajim.HAVE_PYOPENSSL:
    import OpenSSL.crypto

67
from nbxmpp import Smacks
Piotr Gaczkowski's avatar
Piotr Gaczkowski committed
68
from string import Template
69
import logging
70
log = logging.getLogger('gajim.c.connection')
71

Yann Leboulanger's avatar
Yann Leboulanger committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
ssl_error = {
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"),
steve-e's avatar
steve-e committed
89 90 91
18: _("Self signed certificate"),
19: _("Self signed certificate in certificate chain"),
20: _("Unable to get local issuer certificate"),
Yann Leboulanger's avatar
Yann Leboulanger committed
92 93 94 95 96 97 98 99 100 101 102 103 104
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")
105
#100 is for internal usage: host not correct
106
}
107 108

class CommonConnection:
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    """
    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
132
            self.gpg = gpg.GnuPG(gajim.config.get('use_gpg_agent'))
133 134 135 136 137 138 139 140 141 142 143
        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

144 145 146
        self.seclabel_supported = False
        self.seclabel_catalogues = {}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        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
162
        self.archiving_supported = False
163 164
        self.archiving_313_supported = False
        self.archiving_136_supported = False
Yann Leboulanger's avatar
Yann Leboulanger committed
165
        self.archive_pref_supported = False
166
        self.roster_supported = True
167
        self.blocking_supported = False
168
        self.addressing_supported = False
169
        self.carbons_enabled = False
170 171 172 173

        self.muc_jid = {} # jid of muc server for each transport type
        self._stun_servers = [] # STUN servers of our jabber server

174 175
        self.awaiting_cids = {} # Used for XEP-0231

176 177
        self.nested_group_delimiter = '::'

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        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:
            resource = Template(resource).safe_substitute({
                    'hostname': socket.gethostname()
            })
        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
235 236 237 238 239 240
            signed = self.gpg.sign(msg, keyID)
            if signed == 'BAD_PASSPHRASE':
                self.USE_GPG = False
                signed = ''
                gajim.nec.push_incoming_event(BadGPGPassphraseEvent(None,
                    conn=self))
241 242 243 244 245 246 247
        return signed

    def _on_disconnected(self):
        """
        Called when a disconnect request has completed successfully
        """
        self.disconnect(on_purpose=True)
248 249
        gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
            show='offline'))
250 251 252 253 254 255 256 257 258 259 260 261

    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

    def _prepare_message(self, jid, msg, keyID, type_='chat', subject='',
262 263
    chatstate=None, msg_id=None, resource=None, user_nick=None, xhtml=None,
    session=None, forward_from=None, form_node=None, label=None,
264 265
    original_message=None, delayed=None, attention=False, correction_msg=None,
    callback=None):
266 267
        if not self.connection or self.connected < 2:
            return 1
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        if isinstance(jid, list):
            new_list = []
            for j in jid:
                try:
                    new_list.append(self.check_jid(j))
                except helpers.InvalidFormat:
                    gajim.nec.push_incoming_event(InformationEvent(None,
                        conn=self, level='error', pri_txt=_('Invalid Jabber '
                        'ID'), sec_txt=_('It is not possible to send a message '
                        'to %s, this JID is not valid.') % j))
                    return
            fjid = new_list
        else:
            try:
                jid = self.check_jid(jid)
            except helpers.InvalidFormat:
                gajim.nec.push_incoming_event(InformationEvent(None, conn=self,
                    level='error', pri_txt=_('Invalid Jabber ID'), sec_txt=_(
                    'It is not possible to send a message to %s, this JID is not '
                    'valid.') % jid))
                return
            fjid = jid
            if resource:
                fjid += '/' + resource

            if session:
                fjid = session.get_to()
296 297 298 299 300 301 302

        if msg and not xhtml and gajim.config.get(
        'rst_formatting_outgoing_messages'):
            from common.rst_xhtml_generator import create_xhtml
            xhtml = create_xhtml(msg)
        if not msg and chatstate is None and form_node is None:
            return
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317
        msgtxt = msg
        msgenc = ''

        if keyID and self.USE_GPG:
            xhtml = None
            if keyID ==  'UNKNOWN':
                error = _('Neither the remote presence is signed, nor a key was '
                        'assigned.')
            elif keyID.endswith('MISMATCH'):
                error = _('The contact\'s key (%s) does not match the key assigned '
                        'in Gajim.' % keyID[:8])
            else:
                def encrypt_thread(msg, keyID, always_trust=False):
                    # encrypt message. This function returns (msgenc, error)
318 319
                    return self.gpg.encrypt(msg.encode('utf-8'), [keyID],
                        always_trust)
320 321 322 323 324 325 326 327
                def _on_encrypted(output):
                    msgenc, error = output
                    if error == 'NOT_TRUSTED':
                        def _on_always_trust(answer):
                            if answer:
                                gajim.thread_interface(encrypt_thread, [msg, keyID,
                                        True], _on_encrypted, [])
                            else:
328 329 330
                                self._message_encrypted_cb(output, type_, msg,
                                    msgtxt, original_message, fjid, resource,
                                    jid, xhtml, subject, chatstate, msg_id,
331
                                    label, forward_from, delayed, session,
332
                                    form_node, user_nick, keyID, attention,
333
                                    correction_msg, callback)
Yann Leboulanger's avatar
Yann Leboulanger committed
334
                        gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
335
                            conn=self, keyID=keyID, callback=_on_always_trust))
336 337
                    else:
                        self._message_encrypted_cb(output, type_, msg, msgtxt,
338
                            original_message, fjid, resource, jid, xhtml,
339 340
                            subject, chatstate, msg_id, label, forward_from,
                            delayed, session, form_node, user_nick, keyID,
341
                            attention, correction_msg, callback)
342
                gajim.thread_interface(encrypt_thread, [msg, keyID, False],
343
                    _on_encrypted, [])
344 345 346
                return

            self._message_encrypted_cb(('', error), type_, msg, msgtxt,
347
                original_message, fjid, resource, jid, xhtml, subject,
348
                chatstate, msg_id, label, forward_from, delayed, session,
349 350
                form_node, user_nick, keyID, attention, correction_msg,
                callback)
351
            return
352 353

        self._on_continue_message(type_, msg, msgtxt, original_message, fjid,
354
            resource, jid, xhtml, subject, msgenc, keyID, chatstate, msg_id,
355
            label, forward_from, delayed, session, form_node, user_nick,
356
            attention, correction_msg, callback)
357 358 359

    def _message_encrypted_cb(self, output, type_, msg, msgtxt,
    original_message, fjid, resource, jid, xhtml, subject, chatstate, msg_id,
360
    label, forward_from, delayed, session, form_node, user_nick, keyID,
361
    attention, correction_msg, callback):
362 363 364 365 366
        msgenc, error = output

        if msgenc and not error:
            msgtxt = '[This message is *encrypted* (See :XEP:`27`]'
            lang = os.getenv('LANG')
367 368
            if lang is not None and not lang.startswith('en'):
                # we're not english: one in locale and one en
369 370
                msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \
                        ' (' + msgtxt + ')'
371 372
            self._on_continue_message(type_, msg, msgtxt, original_message,
                fjid, resource, jid, xhtml, subject, msgenc, keyID,
373
                chatstate, msg_id, label, forward_from, delayed, session,
374
                form_node, user_nick, attention, correction_msg, callback)
375 376 377
            return
        # Encryption failed, do not send message
        tim = localtime()
378 379
        gajim.nec.push_incoming_event(MessageNotSentEvent(None, conn=self,
            jid=jid, message=msgtxt, error=error, time_=tim, session=session))
380 381

    def _on_continue_message(self, type_, msg, msgtxt, original_message, fjid,
382
    resource, jid, xhtml, subject, msgenc, keyID, chatstate, msg_id,
383
    label, forward_from, delayed, session, form_node, user_nick, attention,
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    correction_msg, callback):

        if correction_msg:
            id_ = correction_msg.getID()
            if correction_msg.getTag('replace'):
                correction_msg.delChild('replace')
            correction_msg.setTag('replace', attrs={'id': id_},
                namespace=nbxmpp.NS_CORRECT)
            id2 = self.connection.getAnID()
            correction_msg.setID(id2)
            correction_msg.setBody(msgtxt)
            if xhtml:
                correction_msg.setXHTML(xhtml)

            if session:
                session.last_send = time.time()

                # XEP-0200
                if session.enable_encryption:
                    correction_msg = session.encrypt_stanza(correction_msg)

            if callback:
                callback(jid, msg, keyID, forward_from, session, original_message,
                    subject, type_, correction_msg, xhtml)
            return

410

411
        if type_ == 'chat':
412
            msg_iq = nbxmpp.Message(body=msgtxt, typ=type_,
413 414 415
                    xhtml=xhtml)
        else:
            if subject:
416
                msg_iq = nbxmpp.Message(body=msgtxt, typ='normal',
417 418
                        subject=subject, xhtml=xhtml)
            else:
419
                msg_iq = nbxmpp.Message(body=msgtxt, typ='normal',
420
                        xhtml=xhtml)
421 422 423 424

        if msg_id:
            msg_iq.setID(msg_id)

425
        if msgenc:
426
            msg_iq.setTag(nbxmpp.NS_ENCRYPTED + ' x').setData(msgenc)
427 428
            if self.carbons_enabled:
                msg_iq.addChild(name='private', namespace=nbxmpp.NS_CARBONS)
Yann Leboulanger's avatar
Yann Leboulanger committed
429 430
            msg_iq.addChild(name='no-permanent-storage',
                namespace=nbxmpp.NS_MSG_HINTS)
431 432 433

        if form_node:
            msg_iq.addChild(node=form_node)
434 435
        if label:
            msg_iq.addChild(node=label)
436 437 438

        # XEP-0172: user_nickname
        if user_nick:
439
            msg_iq.setTag('nick', namespace = nbxmpp.NS_NICK).setData(
440 441 442 443 444 445 446
                    user_nick)

        # XEP-0203
        if delayed:
            our_jid = gajim.get_jid_from_account(self.name) + '/' + \
                    self.server_resource
            timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(delayed))
447
            msg_iq.addChild('delay', namespace=nbxmpp.NS_DELAY2,
448 449
                    attrs={'from': our_jid, 'stamp': timestamp})

450 451
        # XEP-0224
        if attention:
452
            msg_iq.setTag('attention', namespace=nbxmpp.NS_ATTENTION)
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        if isinstance(jid, list):
            if self.addressing_supported:
                msg_iq.setTo(gajim.config.get_per('accounts', self.name, 'hostname'))
                addresses = msg_iq.addChild('addresses',
                    namespace=nbxmpp.NS_ADDRESS)
                for j in jid:
                    addresses.addChild('address', attrs = {'type': 'to',
                        'jid': j})
            else:
                iqs = []
                for j in jid:
                    iq = nbxmpp.Message(node=msg_iq)
                    iq.setTo(j)
                    iqs.append(iq)
                msg_iq = iqs
        else:
Yann Leboulanger's avatar
Yann Leboulanger committed
470
            msg_iq.setTo(fjid)
471 472 473
            r_ = resource
            if not r_ and jid != fjid: # Only if we're not in a pm
                r_ = gajim.get_resource_from_jid(fjid)
474 475
            if r_:
                contact = gajim.contacts.get_contact(self.name, jid, r_)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
            else:
                contact = gajim.contacts.get_contact_with_highest_priority(
                    self.name, jid)

            # 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
            if chatstate and contact and contact.supports(NS_CHATSTATES):
                msg_iq.setTag(chatstate, namespace=NS_CHATSTATES)

            # 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)

            if forward_from:
                addresses = msg_iq.addChild('addresses',
                    namespace=nbxmpp.NS_ADDRESS)
                addresses.addChild('address', attrs = {'type': 'ofrom',
                    'jid': forward_from})

            if session:
                # XEP-0201
                session.last_send = time.time()
                msg_iq.setThread(session.thread_id)

                # XEP-0200
                if session.enable_encryption:
                    msg_iq = session.encrypt_stanza(msg_iq)
506 507 508
                    if self.carbons_enabled:
                        msg_iq.addChild(name='private',
                            namespace=nbxmpp.NS_CARBONS)
Yann Leboulanger's avatar
Yann Leboulanger committed
509 510 511 512
                    msg_iq.addChild(name='no-permanent-storage',
                        namespace=nbxmpp.NS_MSG_HINTS)
                    msg_iq.addChild(name='no-copy',
                        namespace=nbxmpp.NS_MSG_HINTS)
513

514 515
        if callback:
            callback(jid, msg, keyID, forward_from, session, original_message,
516
                subject, type_, msg_iq, xhtml)
517 518

    def log_message(self, jid, msg, forward_from, session, original_message,
519
    subject, type_, xhtml=None):
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        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 subject:
                    log_msg = _('Subject: %(subject)s\n%(message)s') % \
                    {'subject': subject, 'message': log_msg}
                if log_msg:
                    if type_ == 'chat':
                        kind = 'chat_msg_sent'
                    else:
                        kind = 'single_msg_sent'
                    try:
535 536
                        if xhtml and gajim.config.get('log_xhtml_messages'):
                            log_msg = '<body xmlns="%s">%s</body>' % (
537
                                nbxmpp.NS_XHTML, xhtml)
538 539
                        gajim.logger.write(kind, jid, log_msg)
                    except exceptions.PysqliteOperationalError, e:
540 541
                        self.dispatch('DB_ERROR', (_('Disk Write Error'),
                            str(e)))
542 543
                    except exceptions.DatabaseMalformed:
                        pritext = _('Database Error')
Yann Leboulanger's avatar
Yann Leboulanger committed
544 545 546 547 548
                        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
549
                        self.dispatch('DB_ERROR', (pritext, sectext))
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

    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

    def request_subscription(self, jid, msg='', name='', groups=[],
                    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):
595
        if self.connection and self.roster_supported:
596 597 598 599 600 601
            self.connection.getRoster().setItem(jid=jid, name=name, groups=groups)

    def update_contacts(self, contacts):
        """
        Update multiple roster items
        """
602
        if self.connection and self.roster_supported:
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
            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

620
    def request_last_status_time(self, jid, resource, groupchat_jid=None):
621
        """
622 623
        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
624
        """
625
        if not gajim.account_is_connected(self.name):
626 627 628 629
            return
        to_whom_jid = jid
        if resource:
            to_whom_jid += '/' + resource
630
        iq = nbxmpp.Iq(to=to_whom_jid, typ='get', queryNS=nbxmpp.NS_LAST)
631 632 633 634 635 636
        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)
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

    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

    def ask_gpg_keys(self):
        if self.gpg:
684
            return self.gpg.get_keys()
685 686 687 688
        return None

    def ask_gpg_secrete_keys(self):
        if self.gpg:
689
            return self.gpg.get_secret_keys()
690 691 692 693 694 695 696 697
        return None

    def load_roster_from_db(self):
        # Do nothing by default
        return

    def _event_dispatcher(self, realm, event, data):
        if realm == '':
698
            if event == nbxmpp.transports_nb.DATA_RECEIVED:
699
                gajim.nec.push_incoming_event(StanzaReceivedEvent(None,
700
                    conn=self, stanza_str=unicode(data)))
701
            elif event == nbxmpp.transports_nb.DATA_SENT:
702 703
                gajim.nec.push_incoming_event(StanzaSentEvent(None, conn=self,
                    stanza_str=unicode(data)))
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

    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
722
                self.gpg = gpg.GnuPG(gajim.config.get('use_gpg_agent'))
723 724
            gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
                conn=self, show=show, message=msg))
725 726 727 728 729 730
            self.connect_and_init(show, msg, sign_msg)
            return

        if show == 'offline':
            self.connected = 0
            if self.connection:
731 732
                gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
                    conn=self, show=show, message=msg))
733
                p = nbxmpp.Presence(typ = 'unavailable')
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
                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':
750 751
                gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
                    conn=self, show=show, message=msg))
752 753 754 755 756 757
                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)
758 759
            gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
                conn=self, show=show, message=msg))
760 761 762
            if was_invisible:
                self._change_from_invisible()
            self._update_status(show, msg)
763 764

class Connection(CommonConnection, ConnectionHandlers):
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    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
780
        self.redirected = None
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
        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
805
        self.privacy_rules_requested = False
806 807
        self.streamError = ''
        self.secret_hmac = str(random.random())[2:]
808 809 810

        self.sm = Smacks(self) # Stream Management

811 812
        gajim.ged.register_event_handler('privacy-list-received', ged.CORE,
            self._nec_privacy_list_received)
813 814 815 816
        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)
817 818
        gajim.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
            self._nec_message_outgoing)
819 820
        gajim.ged.register_event_handler('gc-message-outgoing', ged.OUT_CORE,
            self._nec_gc_message_outgoing)
821 822
    # END __init__

823 824
    def cleanup(self):
        ConnectionHandlers.cleanup(self)
825 826
        gajim.ged.remove_event_handler('privacy-list-received', ged.CORE,
            self._nec_privacy_list_received)
827 828
        gajim.ged.remove_event_handler('agent-info-error-received', ged.CORE,
            self._nec_agent_info_error_received)
829 830
        gajim.ged.remove_event_handler('agent-info-received', ged.CORE,
            self._nec_agent_info_received)
831 832
        gajim.ged.remove_event_handler('message-outgoing', ged.OUT_CORE,
            self._nec_message_outgoing)
Dicson's avatar
typo  
Dicson committed
833
        gajim.ged.remove_event_handler('gc-message-outgoing', ged.OUT_CORE,
834
            self._nec_gc_message_outgoing)
835

836 837 838 839 840 841 842 843 844 845 846
    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
847 848
        self.client_cert = gajim.config.get_per('accounts', self.name,
            'client_cert')
849
        self.client_cert_passphrase = ''
850 851 852 853 854 855 856 857 858 859

    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
860 861
            gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                show='connecting'))
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
            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
878 879
        if on_purpose:
            self.sm = Smacks(self)
880 881 882 883 884 885 886 887
        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
zimio's avatar
zimio committed
888 889 890 891 892 893 894 895 896
    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]))
897 898 899 900 901 902 903 904 905 906 907 908

    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:
zimio's avatar
zimio committed
909 910
            if not (self.sm and self.sm.resumption):
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
911
                    show='offline'))
912
            else:
913
                self.sm.enabled = False
914 915
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                    show='error'))
916 917 918
            self.disconnect()
            if gajim.config.get_per('accounts', self.name, 'autoreconnect'):
                self.connected = -1
919 920
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                    show='error'))
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
                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
                # do exponential backoff until 15 minutes,
                # then small linear increase
                if self.retrycount < 2 or self.last_time_to_reconnect is None:
                    self.last_time_to_reconnect = 5
                if self.last_time_to_reconnect < 800:
                    self.last_time_to_reconnect *= 1.5
                self.last_time_to_reconnect += randomsource.randint(0, 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:
945 946 947 948 949 950
            if self.redirected:
                self.disconnect(on_purpose=True)
                self.connect()
                return
            else:
                self.disconnect()
951 952 953 954 955 956
        self.on_purpose = False
    # END disconnectedReconnCB

    def _connection_lost(self):
        log.debug('_connection_lost')
        self.disconnect(on_purpose = False)
957 958 959
        gajim.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
            title=_('Connection with account "%s" has been lost') % self.name,
            msg=_('Reconnect manually.')))
960 961 962

    def _event_dispatcher(self, realm, event, data):
        CommonConnection._event_dispatcher(self, realm, event, data)
963 964
        if realm == nbxmpp.NS_REGISTER:
            if event == nbxmpp.features_nb.REGISTER_DATA_RECEIVED:
965 966 967 968 969
                # 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
970 971 972
                        reason = _('Server %(name)s answered wrongly to '
                            'register request: %(error)s') % {'name': data[0],
                            'error': data[3]}
973 974
                        gajim.nec.push_incoming_event(AccountNotCreatedEvent(
                            None, conn=self, reason=reason))
975 976 977
                        return
                    is_form = data[2]
                    conf = data[1]
978 979
                    if data[4] is not '':
                        helpers.replace_dataform_media(conf, data[4])
980 981
                    if self.new_account_form:
                        def _on_register_result(result):
982
                            if not nbxmpp.isResultNode(result):
983
                                gajim.nec.push_incoming_event(AccountNotCreatedEvent(
984
                                    None, conn=self, reason=result.getError()))
985 986 987
                                return
                            if gajim.HAVE_GPG:
                                self.USE_GPG = True
988
                                self.gpg = gpg.GnuPG(gajim.config.get(
989
                                        'use_gpg_agent'))
990 991 992
                            gajim.nec.push_incoming_event(
                                AccountCreatedEvent(None, conn=self,
                                account_info = self.new_account_info))
993 994 995 996 997 998 999 1000 1001 1002
                            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
1003
                            iq = nbxmpp.Iq('set', nbxmpp.NS_REGISTER,
1004
                                to=self._hostname)
1005 1006 1007 1008 1009 1010 1011
                            iq.setTag('query').addChild(node=self.new_account_form)
                            self.connection.SendAndCallForResponse(iq,
                                    _on_register_result)
                        else:
                            if self.new_account_form.keys().sort() != \
                            conf.keys().sort():
                                # requested config has changed since first connection
1012 1013
                                reason = _('Server %s provided a different '
                                    'registration form') % data[0]
1014
                                gajim.nec.push_incoming_event(AccountNotCreatedEvent(
1015
                                    None, conn=self, reason=reason))
1016
                                return
1017
                            nbxmpp.features_nb.register(self.connection,
1018 1019 1020
                                    self._hostname, self.new_account_form,
                                    _on_register_result)
                        return
1021 1022
                    gajim.nec.push_incoming_event(NewAccountConnectedEvent(None,
                        conn=self, config=conf, is_form=is_form))
1023
                    self.connection.UnregisterDisconnectHandler(
1024
                        self._on_new_account)
1025 1026 1027
                    self.disconnect(on_purpose=True)
                    return
                if not data[1]: # wrong answer
1028 1029 1030 1031
                    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],
1032
                        'error': data[3]}))
1033 1034 1035
                    return
                is_form = data[2]
                conf = data[1]
1036 1037 1038
                gajim.nec.push_incoming_event(RegisterAgentInfoReceivedEvent(
                    None, conn=self, agent=data[0], config=conf,
                    is_form=is_form))
1039 1040
        elif realm == nbxmpp.NS_PRIVACY:
            if event == nbxmpp.features_nb.PRIVACY_LISTS_RECEIVED:
1041
                # data is (list)
1042 1043
                gajim.nec.push_incoming_event(PrivacyListsReceivedEvent(None,
                    conn=self, lists_list=data))
1044
            elif event == nbxmpp.features_nb.PRIVACY_LIST_RECEIVED:
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                # 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})
1064 1065
                gajim.nec.push_incoming_event(PrivacyListReceivedEvent(None,
                    conn=self, list_name=name, rules=rules))
1066
            elif event == nbxmpp.features_nb.PRIVACY_LISTS_ACTIVE_DEFAULT:
1067
                # data is (dict)
1068 1069 1070
                gajim.nec.push_incoming_event(PrivacyListActiveDefaultEvent(
                    None, conn=self, active_list=data['active'],
                    default_list=data['default']))
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

    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

1098
    def connect(self, data=None):
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
        """
        Start a connection to the Jabber server

        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, ''

zimio's avatar
zimio committed
1109
        if self.sm.resuming and self.sm.location:
1110
            # If resuming and server gave a location, connect from there
zimio's avatar
zimio committed
1111 1112
            hostname = self.sm.location
            self.try_connecting_for_foo_secs = gajim.config.get_per('accounts',
1113
                self.name, 'try_connecting_for_foo_secs')
zimio's avatar
zimio committed
1114
            use_custom = False
1115
            proxy = helpers.get_proxy_info(self.name)
1116

zimio's avatar
zimio committed
1117
        elif data:
1118 1119 1120
            hostname = data['hostname']
            self.try_connecting_for_foo_secs = 45
            p = data['proxy']
1121 1122 1123 1124
            if p and p in gajim.config.get_per('proxies'):
                proxy = {}
                proxyptr = gajim.config.get_per('proxies', p)
                for key in proxyptr.keys():
1125
                    proxy[key] = proxyptr[key]
1126 1127
            else:
                proxy = None
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
            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')
1138
            proxy = helpers.get_proxy_info(self.name)
1139
            use_srv = gajim.config.get_per('accounts', self.name, 'use_srv')
1140 1141 1142 1143 1144 1145
            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,
1146
                    'use_custom_host')
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
                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
1161 1162 1163

        # create connection if it doesn't already exist
        self.connected = 1
1164

1165 1166 1167 1168 1169 1170 1171
        h = hostname
        p = 5222
        ssl_p = 5223
        if use_custom:
            h = custom_h
            p = custom_p
            ssl_p = custom_p
1172 1173
            if not self.redirected:
                use_srv = False
1174

1175
        self.redirected = None
1176 1177 1178 1179 1180 1181 1182 1183
        # 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
1184 1185
            gajim.resolver.resolve('_xmpp-client._tcp.' + helpers.idn_to_ascii(
                h), self._on_resolve)
1186 1187 1188 1189 1190