idsender.py 1.3 KB

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