idsender.py 1.48 KB
Newer Older
1

2 3 4 5 6
# slixmpp.xmlstream.matcher.id
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Part of Slixmpp: The Slick XMPP Library
# :copyright: (c) 2011 Nathanael C. Fritz
# :license: MIT, see LICENSE for more details
mathieui's avatar
mathieui committed
7

louiz’'s avatar
louiz’ committed
8
from slixmpp.xmlstream.matcher.base import MatcherBase
mathieui's avatar
mathieui committed
9 10 11 12 13 14 15 16 17
from slixmpp.xmlstream.stanzabase import StanzaBase
from slixmpp.jid import JID
from slixmpp.types import TypedDict


class CriteriaType(TypedDict):
    self: JID
    peer: JID
    id: str
18 19 20 21 22 23 24 25 26


class MatchIDSender(MatcherBase):

    """
    The IDSender matcher selects stanzas that have the same stanza 'id'
    interface value as the desired ID, and that the 'from' value is one
    of a set of approved entities that can respond to a request.
    """
mathieui's avatar
mathieui committed
27
    _criteria: CriteriaType
28

mathieui's avatar
mathieui committed
29
    def match(self, xml: StanzaBase) -> bool:
30 31 32
        """Compare the given stanza's ``'id'`` attribute to the stored
        ``id`` value, and verify the sender's JID.

mathieui's avatar
mathieui committed
33
        :param xml: The :class:`~slixmpp.xmlstream.stanzabase.StanzaBase`
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
                    stanza to compare against.
        """

        selfjid = self._criteria['self']
        peerjid = self._criteria['peer']

        allowed = {}
        allowed[''] = True
        allowed[selfjid.bare] = True
        allowed[selfjid.host] = True
        allowed[peerjid.full] = True
        allowed[peerjid.bare] = True
        allowed[peerjid.host] = True

        _from = xml['from']

        try:
            return xml['id'] == self._criteria['id'] and allowed[_from]
        except KeyError:
            return False