fixes.py 2.35 KB
Newer Older
mathieui's avatar
mathieui committed
1
"""
louiz’'s avatar
louiz’ committed
2
Module used to provide fixes for slixmpp functions not yet fixed
mathieui's avatar
mathieui committed
3 4 5 6 7
upstream.

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

8 9
from typing import Callable, Any
from slixmpp import Message, Iq, ClientXMPP
louiz’'s avatar
louiz’ committed
10
from slixmpp.xmlstream import ET
11

12 13 14 15
import logging

log = logging.getLogger(__name__)

mathieui's avatar
mathieui committed
16

17 18
def has_identity(xmpp, jid, identity, on_true=None, on_false=None):
    def _cb(iq):
19
        ident = lambda x: x[0]
20 21 22 23 24
        res = identity in map(ident, iq['disco_info']['identities'])
        if res and on_true is not None:
            on_true()
        if not res and on_false is not None:
            on_false()
mathieui's avatar
mathieui committed
25

26
    xmpp.plugin['xep_0030'].get_info(jid=jid, callback=_cb)
27

mathieui's avatar
mathieui committed
28

29
def get_room_form(xmpp: ClientXMPP, room: str, callback: Callable[[Iq], Any]):
30 31
    def _cb(result):
        if result["type"] == "error":
32
            return callback(None)
mathieui's avatar
mathieui committed
33 34
        xform = result.xml.find(
            '{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x')
35
        if xform is None:
36
            return callback(None)
37
        form = xmpp.plugin['xep_0004'].build_form(xform)
38
        return callback(form)
39

mathieui's avatar
mathieui committed
40 41 42
    iq = xmpp.make_iq_get(ito=room)
    query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
    iq.append(query)
43
    iq.send(callback=_cb)
mathieui's avatar
mathieui committed
44

mathieui's avatar
mathieui committed
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
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

72
    if stanza['type'] not in self.ack_types:
73 74 75 76 77 78 79 80
        return stanza

    if stanza['receipt']:
        return stanza

    if not stanza['body']:
        return stanza

81 82 83
    # hack
    if stanza['to'].resource and not hasattr(stanza, '_add_receipt'):
        return stanza
mathieui's avatar
mathieui committed
84

85 86
    stanza['request_receipt'] = True
    return stanza