connection.py 6.79 KB
Newer Older
louiz’'s avatar
louiz’ committed
1
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
2 3 4 5
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
6
# it under the terms of the zlib license. See the COPYING file.
7

8 9 10 11
"""
Defines the Connection class
"""

12 13 14
import logging
log = logging.getLogger(__name__)

15

16
import getpass
17
import sleekxmpp
18

19
from config import config, options
20
import common
21
from common import safeJID
22

23
class Connection(sleekxmpp.ClientXMPP):
24
    """
25 26
    Receives everything from Jabber and emits the
    appropriate signals
27
    """
louiz’'s avatar
louiz’ committed
28
    __init = False
29
    def __init__(self):
30
        resource = config.get('resource', '')
31
        if config.get('jid', ''):
32 33
            # Field used to know if we are anonymous or not.
            # many features will be handled differently
34
            # depending on this setting
35
            self.anon = False
36 37
            jid = '%s' % config.get('jid', '')
            if resource:
38
                jid = '%s/%s'% (jid, resource)
39
            password = config.get('password', '') or getpass.getpass()
40
        else: # anonymous auth
41
            self.anon = True
mathieui's avatar
mathieui committed
42
            jid = config.get('server', 'anon.jeproteste.info')
43 44
            if resource:
                jid = '%s/%s' % (jid, resource)
45
            password = None
46
        jid = safeJID(jid)
mathieui's avatar
mathieui committed
47
        # TODO: use the system language
48 49
        sleekxmpp.ClientXMPP.__init__(self, jid, password,
                                      lang=config.get('lang', 'en'))
50

51
        force_encryption = config.get('force_encryption', True)
mathieui's avatar
mathieui committed
52 53 54 55 56 57
        if force_encryption:
            self['feature_mechanisms'].unencrypted_plain = False
            self['feature_mechanisms'].unencrypted_digest = False
            self['feature_mechanisms'].unencrypted_cram = False
            self['feature_mechanisms'].unencrypted_scram = False

58
        self.core = None
59
        self.auto_reconnect = config.get('auto_reconnect', False)
mathieui's avatar
mathieui committed
60
        self.reconnect_max_attempts = 0
louiz’'s avatar
louiz’ committed
61
        self.auto_authorize = None
62 63
        # prosody defaults, lowest is AES128-SHA, it should be a minimum
        # for anything that came out after 2002
64 65 66
        self.ciphers = config.get('ciphers',
                                  'HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK'
                                    ':!SRP:!3DES:!aNULL')
mathieui's avatar
mathieui committed
67
        self.ca_certs = config.get('ca_cert_path', '') or None
mathieui's avatar
mathieui committed
68
        interval = config.get('whitespace_interval', '300')
69
        if interval.isdecimal() and int(interval) > 0:
mathieui's avatar
mathieui committed
70 71 72
            self.whitespace_keepalive_interval = int(interval)
        else:
            self.whitespace_keepalive_interval = 300
mathieui's avatar
mathieui committed
73 74
        # Hack to check the sleekxmpp version
        # TODO: Remove that when a sufficient time has passed since the move
75
        self.register_plugin('xep_0004')
76
        self.register_plugin('xep_0012')
77
        self.register_plugin('xep_0030')
78
        self.register_plugin('xep_0045')
mathieui's avatar
mathieui committed
79
        self.register_plugin('xep_0048')
80
        self.register_plugin('xep_0060')
81
        self.register_plugin('xep_0066')
82
        self.register_plugin('xep_0071')
83 84
        self.register_plugin('xep_0077')
        self.plugin['xep_0077'].create_account = False
85
        self.register_plugin('xep_0085')
mathieui's avatar
mathieui committed
86
        self.register_plugin('xep_0115')
87
        self.register_plugin('xep_0191')
mathieui's avatar
mathieui committed
88
        self.register_plugin('xep_0198')
89 90
        self.register_plugin('xep_0199')
        self.set_keepalive_values()
91

92
        if config.get('enable_user_tune', True):
93
            self.register_plugin('xep_0118')
mathieui's avatar
mathieui committed
94

95
        if config.get('enable_user_nick', True):
mathieui's avatar
mathieui committed
96
            self.register_plugin('xep_0172')
mathieui's avatar
mathieui committed
97

98
        if config.get('enable_user_mood', True):
mathieui's avatar
mathieui committed
99 100
            self.register_plugin('xep_0107')

101
        if config.get('enable_user_activity', True):
mathieui's avatar
mathieui committed
102 103
            self.register_plugin('xep_0108')

104
        if config.get('enable_user_gaming', True):
mathieui's avatar
mathieui committed
105 106
            self.register_plugin('xep_0196')

107
        if config.get('send_poezio_info', True):
108
            info = {'name':'poezio',
109
                    'version': options.version}
110
            if config.get('send_os_info', True):
111
                info['os'] = common.get_os_info()
mathieui's avatar
mathieui committed
112 113
            self.plugin['xep_0030'].set_identities(
                    identities=set([('client', 'pc', None, 'Poezio')]))
114 115
        else:
            info = {'name': '', 'version': ''}
mathieui's avatar
mathieui committed
116 117
            self.plugin['xep_0030'].set_identities(
                    identities=set([('client', 'pc', None, '')]))
118
        self.register_plugin('xep_0092', pconfig=info)
119
        if config.get('send_time', True):
120
            self.register_plugin('xep_0202')
121
        self.register_plugin('xep_0224')
122
        self.register_plugin('xep_0249')
mathieui's avatar
mathieui committed
123 124
        self.register_plugin('xep_0280')
        self.register_plugin('xep_0297')
Link Mauve's avatar
Link Mauve committed
125
        self.register_plugin('xep_0308')
126

127 128 129 130 131 132 133 134 135 136
    def set_keepalive_values(self, option=None, value=None):
        """
        Called at startup, or triggered when one of
        "connection_timeout_delay" and "connection_check_interval" options
        is changed.
        Unload and reload the ping plugin, with the new values.
        """
        ping_interval = config.get('connection_check_interval', 60)
        timeout_delay = config.get('connection_timeout_delay', 10)
        if timeout_delay <= 0:
137 138 139 140 141
            # We help the stupid user (with a delay of 0, poezio will try to
            # reconnect immediately because the timeout is immediately
            # passed)
            # 1 second is short, but, well
            timeout_delay = 1
142
        self.plugin['xep_0199'].disable_keepalive()
143 144
        # If the ping_interval is 0 or less, we just disable the keepalive
        if ping_interval > 0:
145 146
            self.plugin['xep_0199'].enable_keepalive(ping_interval,
                                                     timeout_delay)
147

148
    def start(self):
149 150 151 152 153
        """
        Connect and process events.

        TODO: try multiple servers with anon auth.
        """
154
        custom_host = config.get('custom_host', '')
155
        custom_port = config.get('custom_port', 5222)
156 157
        if custom_port == -1:
            custom_port = 5222
158
        if custom_host:
mathieui's avatar
mathieui committed
159
            res = self.connect((custom_host, custom_port), reattempt=True)
mathieui's avatar
mathieui committed
160
        elif custom_port != 5222 and custom_port != -1:
161 162
            res = self.connect((self.boundjid.host, custom_port),
                               reattempt=True)
163
        else:
mathieui's avatar
mathieui committed
164
            res = self.connect(reattempt=True)
165 166
        if not res:
            return False
167
        self.process(threaded=True)
168
        return True
169 170 171 172 173 174 175 176 177 178

    def send_raw(self, data, now=False, reconnect=None):
        """
        Overrides XMLStream.send_raw, with an event added
        """
        if self.core:
            self.core.outgoing_stanza(data)
        sleekxmpp.ClientXMPP.send_raw(self, data, now, reconnect)

class MatchAll(sleekxmpp.xmlstream.matcher.base.MatcherBase):
mathieui's avatar
mathieui committed
179 180 181
    """
    Callback to retrieve all the stanzas for the XML tab
    """
182
    def match(self, xml):
183
        "match everything"
184
        return True