connection.py 7.19 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
from sleekxmpp.plugins.xep_0184 import XEP_0184
19

20
import common
21
import fixes
22
from common import safeJID
23
from config import config, options
24

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

53
        force_encryption = config.get('force_encryption', True)
mathieui's avatar
mathieui committed
54
55
56
57
58
59
        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

60
        self.core = None
61
        self.auto_reconnect = config.get('auto_reconnect', False)
mathieui's avatar
mathieui committed
62
        self.reconnect_max_attempts = 0
louiz’'s avatar
louiz’ committed
63
        self.auto_authorize = None
64
65
        # prosody defaults, lowest is AES128-SHA, it should be a minimum
        # for anything that came out after 2002
66
67
68
        self.ciphers = config.get('ciphers',
                                  'HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK'
                                    ':!SRP:!3DES:!aNULL')
mathieui's avatar
mathieui committed
69
        self.ca_certs = config.get('ca_cert_path', '') or None
mathieui's avatar
mathieui committed
70
        interval = config.get('whitespace_interval', '300')
71
        if interval.isdecimal() and int(interval) > 0:
mathieui's avatar
mathieui committed
72
73
74
            self.whitespace_keepalive_interval = int(interval)
        else:
            self.whitespace_keepalive_interval = 300
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_0050')
81
        self.register_plugin('xep_0060')
82
        self.register_plugin('xep_0066')
83
        self.register_plugin('xep_0071')
84
85
        self.register_plugin('xep_0077')
        self.plugin['xep_0077'].create_account = False
86
        self.register_plugin('xep_0085')
mathieui's avatar
mathieui committed
87
        self.register_plugin('xep_0115')
88

89
90
91
        # monkey-patch xep_0184 to avoid requesting receipts for messages
        # without a body
        XEP_0184._filter_add_receipt_request = fixes._filter_add_receipt_request
92
93
94
95
96
97
        self.register_plugin('xep_0184')
        self.plugin['xep_0184'].auto_ack = config.get('ack_message_receipts',
                                                      True)
        self.plugin['xep_0184'].auto_request = config.get(
                'request_message_receipts', True)

98
        self.register_plugin('xep_0191')
99
100
        self.register_plugin('xep_0199')
        self.set_keepalive_values()
101

102
        if config.get('enable_user_tune', True):
103
            self.register_plugin('xep_0118')
mathieui's avatar
mathieui committed
104

105
        if config.get('enable_user_nick', True):
mathieui's avatar
mathieui committed
106
            self.register_plugin('xep_0172')
mathieui's avatar
mathieui committed
107

108
        if config.get('enable_user_mood', True):
mathieui's avatar
mathieui committed
109
110
            self.register_plugin('xep_0107')

111
        if config.get('enable_user_activity', True):
mathieui's avatar
mathieui committed
112
113
            self.register_plugin('xep_0108')

114
        if config.get('enable_user_gaming', True):
mathieui's avatar
mathieui committed
115
116
            self.register_plugin('xep_0196')

117
        if config.get('send_poezio_info', True):
118
            info = {'name':'poezio',
119
                    'version': options.version}
120
            if config.get('send_os_info', True):
121
                info['os'] = common.get_os_info()
mathieui's avatar
mathieui committed
122
123
            self.plugin['xep_0030'].set_identities(
                    identities=set([('client', 'pc', None, 'Poezio')]))
124
125
        else:
            info = {'name': '', 'version': ''}
mathieui's avatar
mathieui committed
126
127
            self.plugin['xep_0030'].set_identities(
                    identities=set([('client', 'pc', None, '')]))
128
        self.register_plugin('xep_0092', pconfig=info)
129
        if config.get('send_time', True):
130
            self.register_plugin('xep_0202')
131
        self.register_plugin('xep_0224')
132
        self.register_plugin('xep_0249')
mathieui's avatar
mathieui committed
133
134
        self.register_plugin('xep_0280')
        self.register_plugin('xep_0297')
Link Mauve's avatar
Link Mauve committed
135
        self.register_plugin('xep_0308')
136

137
138
139
140
141
142
143
144
145
146
    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:
147
148
149
150
151
            # 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
152
        self.plugin['xep_0199'].disable_keepalive()
153
154
        # If the ping_interval is 0 or less, we just disable the keepalive
        if ping_interval > 0:
155
156
            self.plugin['xep_0199'].enable_keepalive(ping_interval,
                                                     timeout_delay)
157

158
    def start(self):
159
160
161
162
163
        """
        Connect and process events.

        TODO: try multiple servers with anon auth.
        """
164
        custom_host = config.get('custom_host', '')
165
        custom_port = config.get('custom_port', 5222)
166
167
        if custom_port == -1:
            custom_port = 5222
168
        if custom_host:
mathieui's avatar
mathieui committed
169
            res = self.connect((custom_host, custom_port), reattempt=True)
mathieui's avatar
mathieui committed
170
        elif custom_port != 5222 and custom_port != -1:
171
172
            res = self.connect((self.boundjid.host, custom_port),
                               reattempt=True)
173
        else:
mathieui's avatar
mathieui committed
174
            res = self.connect(reattempt=True)
175
176
        if not res:
            return False
177
        self.process(threaded=True)
178
        return True
179
180
181
182
183
184
185
186
187
188

    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
189
190
191
    """
    Callback to retrieve all the stanzas for the XML tab
    """
192
    def match(self, xml):
193
        "match everything"
194
        return True