connection.py 123 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
Yann Leboulanger's avatar
Yann Leboulanger committed
163
        self.archive_pref_supported = False
164
        self.roster_supported = True
165
        self.blocking_supported = False
166
        self.addressing_supported = False
167
        self.carbons_enabled = False
168 169 170 171

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

172 173
        self.awaiting_cids = {} # Used for XEP-0231

174 175
        self.nested_group_delimiter = '::'

176 177 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
        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
233 234 235 236 237 238
            signed = self.gpg.sign(msg, keyID)
            if signed == 'BAD_PASSPHRASE':
                self.USE_GPG = False
                signed = ''
                gajim.nec.push_incoming_event(BadGPGPassphraseEvent(None,
                    conn=self))
239 240 241 242 243 244 245
        return signed

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

    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='',
260 261
    chatstate=None, msg_id=None, resource=None, user_nick=None, xhtml=None,
    session=None, forward_from=None, form_node=None, label=None,
262 263
    original_message=None, delayed=None, attention=False, correction_msg=None,
    callback=None):
264 265
        if not self.connection or self.connected < 2:
            return 1
266

267 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
        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()
294 295 296 297 298 299 300

        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
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315
        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)
316 317
                    return self.gpg.encrypt(msg.encode('utf-8'), [keyID],
                        always_trust)
318 319 320 321 322 323 324 325
                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:
326 327 328
                                self._message_encrypted_cb(output, type_, msg,
                                    msgtxt, original_message, fjid, resource,
                                    jid, xhtml, subject, chatstate, msg_id,
329
                                    label, forward_from, delayed, session,
330
                                    form_node, user_nick, keyID, attention,
331
                                    correction_msg, callback)
Yann Leboulanger's avatar
Yann Leboulanger committed
332
                        gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
333
                            conn=self, keyID=keyID, callback=_on_always_trust))
334 335
                    else:
                        self._message_encrypted_cb(output, type_, msg, msgtxt,
336
                            original_message, fjid, resource, jid, xhtml,
337 338
                            subject, chatstate, msg_id, label, forward_from,
                            delayed, session, form_node, user_nick, keyID,
339
                            attention, correction_msg, callback)
340
                gajim.thread_interface(encrypt_thread, [msg, keyID, False],
341
                    _on_encrypted, [])
342 343 344
                return

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

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

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

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

    def _on_continue_message(self, type_, msg, msgtxt, original_message, fjid,
380
    resource, jid, xhtml, subject, msgenc, keyID, chatstate, msg_id,
381
    label, forward_from, delayed, session, form_node, user_nick, attention,
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    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

408

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

        if msg_id:
            msg_iq.setID(msg_id)

423
        if msgenc:
424
            msg_iq.setTag(nbxmpp.NS_ENCRYPTED + ' x').setData(msgenc)
425 426
            if self.carbons_enabled:
                msg_iq.addChild(name='private', namespace=nbxmpp.NS_CARBONS)
427 428 429

        if form_node:
            msg_iq.addChild(node=form_node)
430 431
        if label:
            msg_iq.addChild(node=label)
432 433 434

        # XEP-0172: user_nickname
        if user_nick:
435
            msg_iq.setTag('nick', namespace = nbxmpp.NS_NICK).setData(
436 437 438 439 440 441 442
                    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))
443
            msg_iq.addChild('delay', namespace=nbxmpp.NS_DELAY2,
444 445
                    attrs={'from': our_jid, 'stamp': timestamp})

446 447
        # XEP-0224
        if attention:
448
            msg_iq.setTag('attention', namespace=nbxmpp.NS_ATTENTION)
449

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
        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
466
            msg_iq.setTo(fjid)
467 468 469
            r_ = resource
            if not r_ and jid != fjid: # Only if we're not in a pm
                r_ = gajim.get_resource_from_jid(fjid)
470 471
            if r_:
                contact = gajim.contacts.get_contact(self.name, jid, r_)
472 473 474 475 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
            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)
502 503 504
                    if self.carbons_enabled:
                        msg_iq.addChild(name='private',
                            namespace=nbxmpp.NS_CARBONS)
505

506 507
        if callback:
            callback(jid, msg, keyID, forward_from, session, original_message,
508
                subject, type_, msg_iq, xhtml)
509 510

    def log_message(self, jid, msg, forward_from, session, original_message,
511
    subject, type_, xhtml=None):
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
        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:
527 528
                        if xhtml and gajim.config.get('log_xhtml_messages'):
                            log_msg = '<body xmlns="%s">%s</body>' % (
529
                                nbxmpp.NS_XHTML, xhtml)
530 531
                        gajim.logger.write(kind, jid, log_msg)
                    except exceptions.PysqliteOperationalError, e:
532 533
                        self.dispatch('DB_ERROR', (_('Disk Write Error'),
                            str(e)))
534 535
                    except exceptions.DatabaseMalformed:
                        pritext = _('Database Error')
Yann Leboulanger's avatar
Yann Leboulanger committed
536 537 538 539 540
                        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
541
                        self.dispatch('DB_ERROR', (pritext, sectext))
542 543 544 545 546 547 548 549 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

    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):
587
        if self.connection and self.roster_supported:
588 589 590 591 592 593
            self.connection.getRoster().setItem(jid=jid, name=name, groups=groups)

    def update_contacts(self, contacts):
        """
        Update multiple roster items
        """
594
        if self.connection and self.roster_supported:
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
            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

612
    def request_last_status_time(self, jid, resource, groupchat_jid=None):
613
        """
614 615
        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
616
        """
617
        if not gajim.account_is_connected(self.name):
618 619 620 621
            return
        to_whom_jid = jid
        if resource:
            to_whom_jid += '/' + resource
622
        iq = nbxmpp.Iq(to=to_whom_jid, typ='get', queryNS=nbxmpp.NS_LAST)
623 624 625 626 627 628
        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)
629 630 631 632 633 634 635 636 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

    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:
676
            return self.gpg.get_keys()
677 678 679 680
        return None

    def ask_gpg_secrete_keys(self):
        if self.gpg:
681
            return self.gpg.get_secret_keys()
682 683 684 685 686 687 688 689
        return None

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

    def _event_dispatcher(self, realm, event, data):
        if realm == '':
690
            if event == nbxmpp.transports_nb.DATA_RECEIVED:
691
                gajim.nec.push_incoming_event(StanzaReceivedEvent(None,
692
                    conn=self, stanza_str=unicode(data)))
693
            elif event == nbxmpp.transports_nb.DATA_SENT:
694 695
                gajim.nec.push_incoming_event(StanzaSentEvent(None, conn=self,
                    stanza_str=unicode(data)))
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

    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
714
                self.gpg = gpg.GnuPG(gajim.config.get('use_gpg_agent'))
715 716
            gajim.nec.push_incoming_event(BeforeChangeShowEvent(None,
                conn=self, show=show, message=msg))
717 718 719 720 721 722
            self.connect_and_init(show, msg, sign_msg)
            return

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

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

        self.sm = Smacks(self) # Stream Management

803 804
        gajim.ged.register_event_handler('privacy-list-received', ged.CORE,
            self._nec_privacy_list_received)
805 806 807 808
        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)
809 810
        gajim.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
            self._nec_message_outgoing)
811 812
        gajim.ged.register_event_handler('gc-message-outgoing', ged.OUT_CORE,
            self._nec_gc_message_outgoing)
813 814
    # END __init__

815 816
    def cleanup(self):
        ConnectionHandlers.cleanup(self)
817 818
        gajim.ged.remove_event_handler('privacy-list-received', ged.CORE,
            self._nec_privacy_list_received)
819 820
        gajim.ged.remove_event_handler('agent-info-error-received', ged.CORE,
            self._nec_agent_info_error_received)
821 822
        gajim.ged.remove_event_handler('agent-info-received', ged.CORE,
            self._nec_agent_info_received)
823 824
        gajim.ged.remove_event_handler('message-outgoing', ged.OUT_CORE,
            self._nec_message_outgoing)
Dicson's avatar
typo  
Dicson committed
825
        gajim.ged.remove_event_handler('gc-message-outgoing', ged.OUT_CORE,
826
            self._nec_gc_message_outgoing)
827

828 829 830 831 832 833 834 835 836 837 838
    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
839 840
        self.client_cert = gajim.config.get_per('accounts', self.name,
            'client_cert')
841
        self.client_cert_passphrase = ''
842 843 844 845 846 847 848 849 850 851

    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
852 853
            gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                show='connecting'))
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
            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
870 871
        if on_purpose:
            self.sm = Smacks(self)
872 873 874 875 876 877 878 879
        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
880 881 882 883 884 885 886 887 888
    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]))
889 890 891 892 893 894 895 896 897 898 899 900

    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
901 902
            if not (self.sm and self.sm.resumption):
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
903
                    show='offline'))
904
            else:
905
                self.sm.enabled = False
906 907
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                    show='error'))
908 909 910
            self.disconnect()
            if gajim.config.get_per('accounts', self.name, 'autoreconnect'):
                self.connected = -1
911 912
                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
                    show='error'))
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
                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:
937 938 939 940 941 942
            if self.redirected:
                self.disconnect(on_purpose=True)
                self.connect()
                return
            else:
                self.disconnect()
943 944 945 946 947 948
        self.on_purpose = False
    # END disconnectedReconnCB

    def _connection_lost(self):
        log.debug('_connection_lost')
        self.disconnect(on_purpose = False)
949 950 951
        gajim.nec.push_incoming_event(ConnectionLostEvent(None, conn=self,
            title=_('Connection with account "%s" has been lost') % self.name,
            msg=_('Reconnect manually.')))
952 953 954

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

    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

1090
    def connect(self, data=None):
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        """
        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
1101
        if self.sm.resuming and self.sm.location:
1102
            # If resuming and server gave a location, connect from there
zimio's avatar
zimio committed
1103 1104
            hostname = self.sm.location
            self.try_connecting_for_foo_secs = gajim.config.get_per('accounts',
1105
                self.name, 'try_connecting_for_foo_secs')
zimio's avatar
zimio committed
1106
            use_custom = False
1107
            proxy = helpers.get_proxy_info(self.name)
1108

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

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

1157 1158 1159 1160 1161 1162 1163
        h = hostname
        p = 5222
        ssl_p = 5223
        if use_custom:
            h = custom_h
            p = custom_p
            ssl_p = custom_p
1164 1165
            if not self.redirected:
                use_srv = False
1166

1167
        self.redirected = None
1168 1169 1170 1171 1172 1173 1174 1175
        # 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
1176 1177
            gajim.resolver.resolve('_xmpp-client._tcp.' + helpers.idn_to_ascii(
                h), self._on_resolve)
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        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'):
1188 1189
                ssl_p = gajim.config.get_per('accounts', self.name,
                    'custom_port')
1190 1191