1# -*- coding: utf-8 -*-
2"""
3    slixmpp.xmlstream.matcher.id
4    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6    Part of Slixmpp: The Slick XMPP Library
7
8    :copyright: (c) 2011 Nathanael C. Fritz
9    :license: MIT, see LICENSE for more details
10"""
11
12from slixmpp.xmlstream.matcher.base import MatcherBase
13
14
15class MatchIDSender(MatcherBase):
16
17    """
18    The IDSender matcher selects stanzas that have the same stanza 'id'
19    interface value as the desired ID, and that the 'from' value is one
20    of a set of approved entities that can respond to a request.
21    """
22
23    def match(self, xml):
24        """Compare the given stanza's ``'id'`` attribute to the stored
25        ``id`` value, and verify the sender's JID.
26
27        :param xml: The :class:`~slixmpp.xmlstream.stanzabase.ElementBase`
28                    stanza to compare against.
29        """
30
31        selfjid = self._criteria['self']
32        peerjid = self._criteria['peer']
33
34        allowed = {}
35        allowed[''] = True
36        allowed[selfjid.bare] = True
37        allowed[selfjid.host] = True
38        allowed[peerjid.full] = True
39        allowed[peerjid.bare] = True
40        allowed[peerjid.host] = True
41
42        _from = xml['from']
43
44        try:
45            return xml['id'] == self._criteria['id'] and allowed[_from]
46        except KeyError:
47            return False
48