summaryrefslogtreecommitdiff
path: root/src/fixes.py
blob: da00c48e0aa869d3e5989138d81b2cc34f3350fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Module used to provide fixes for slixmpp functions not yet fixed
upstream.

TODO: Check that they are fixed and remove those hacks
"""

from slixmpp.stanza import Message
from slixmpp.xmlstream import ET

import logging

log = logging.getLogger(__name__)

def has_identity(xmpp, jid, identity):
    try:
        iq = xmpp.plugin['xep_0030'].get_info(jid=jid, block=True, timeout=1)
        ident = lambda x: x[0]
        return identity in map(ident, iq['disco_info']['identities'])
    except:
        log.debug('Traceback while retrieving identity', exc_info=True)
    return False

def get_version(xmpp, jid, callback=None, **kwargs):
    def handle_result(res):
        if res and res['type'] != 'error':
            ret = res['software_version'].values
        else:
            ret = False
        if callback:
            callback(ret)
        return ret
    iq = xmpp.make_iq_get(ito=jid)
    iq['query'] = 'jabber:iq:version'
    result = iq.send(callback=handle_result if callback else None)
    if not callback:
        return handle_result(result)


def get_room_form(xmpp, room, callback):
    def _cb(result):
        if result["type"] == "error":
            callback(None)
        xform = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x')
        if xform is None:
            callback(None)
        form = xmpp.plugin['xep_0004'].buildForm(xform)
        callback(form)

    iq = xmpp.make_iq_get(ito=room)
    query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
    iq.append(query)
    iq.send(callback=_cb)

def _filter_add_receipt_request(self, stanza):
    """
    Auto add receipt requests to outgoing messages, if:

        - ``self.auto_request`` is set to ``True``
        - The message is not for groupchat
        - The message does not contain a receipt acknowledgment
        - The recipient is a bare JID or, if a full JID, one
          that has the ``urn:xmpp:receipts`` feature enabled
        - The message has a body

    The disco cache is checked if a full JID is specified in
    the outgoing message, which may mean a round-trip disco#info
    delay for the first message sent to the JID if entity caps
    are not used.
    """

    if not self.auto_request:
        return stanza

    if not isinstance(stanza, Message):
        return stanza

    if stanza['request_receipt']:
        return stanza

    if not stanza['type'] in self.ack_types:
        return stanza

    if stanza['receipt']:
        return stanza

    if not stanza['body']:
        return stanza

    # hack
    if stanza['to'].resource and not hasattr(stanza, '_add_receipt'):
        return stanza

    stanza['request_receipt'] = True
    return stanza