2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-09-01 18:28:43 +00:00
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
import logging
|
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
from xml.parsers.expat import ExpatError
|
|
|
|
|
2010-09-01 18:28:43 +00:00
|
|
|
from sleekxmpp.xmlstream.stanzabase import ET
|
|
|
|
from sleekxmpp.xmlstream.matcher.base import MatcherBase
|
|
|
|
|
|
|
|
|
|
|
|
# Flag indicating if the builtin XPath matcher should be used, which
|
|
|
|
# uses namespaces, or a custom matcher that ignores namespaces.
|
|
|
|
# Changing this will affect ALL XMLMask matchers.
|
|
|
|
IGNORE_NS = False
|
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-09-01 18:28:43 +00:00
|
|
|
class MatchXMLMask(MatcherBase):
|
|
|
|
|
|
|
|
"""
|
|
|
|
The XMLMask matcher selects stanzas whose XML matches a given
|
|
|
|
XML pattern, or mask. For example, message stanzas with body elements
|
|
|
|
could be matched using the mask:
|
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
.. code-block:: xml
|
|
|
|
|
2010-09-01 18:28:43 +00:00
|
|
|
<message xmlns="jabber:client"><body /></message>
|
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
Use of XMLMask is discouraged, and
|
|
|
|
:class:`~sleekxmpp.xmlstream.matcher.xpath.MatchXPath` or
|
|
|
|
:class:`~sleekxmpp.xmlstream.matcher.stanzapath.StanzaPath`
|
|
|
|
should be used instead.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
|
|
|
The use of namespaces in the mask comparison is controlled by
|
2011-12-05 04:35:17 +00:00
|
|
|
``IGNORE_NS``. Setting ``IGNORE_NS`` to ``True`` will disable namespace
|
|
|
|
based matching for ALL XMLMask matchers.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
:param criteria: Either an :class:`~xml.etree.ElementTree.Element` XML
|
|
|
|
object or XML string to use as a mask.
|
2010-09-01 18:28:43 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, criteria):
|
|
|
|
MatcherBase.__init__(self, criteria)
|
|
|
|
if isinstance(criteria, str):
|
|
|
|
self._criteria = ET.fromstring(self._criteria)
|
|
|
|
self.default_ns = 'jabber:client'
|
|
|
|
|
|
|
|
def setDefaultNS(self, ns):
|
2011-12-05 04:35:17 +00:00
|
|
|
"""Set the default namespace to use during comparisons.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
:param ns: The new namespace to use as the default.
|
2010-09-01 18:28:43 +00:00
|
|
|
"""
|
|
|
|
self.default_ns = ns
|
|
|
|
|
|
|
|
def match(self, xml):
|
2011-12-05 04:35:17 +00:00
|
|
|
"""Compare a stanza object or XML object against the stored XML mask.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
|
|
|
Overrides MatcherBase.match.
|
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
:param xml: The stanza object or XML object to compare against.
|
2010-09-01 18:28:43 +00:00
|
|
|
"""
|
|
|
|
if hasattr(xml, 'xml'):
|
|
|
|
xml = xml.xml
|
|
|
|
return self._mask_cmp(xml, self._criteria, True)
|
|
|
|
|
|
|
|
def _mask_cmp(self, source, mask, use_ns=False, default_ns='__no_ns__'):
|
2011-12-05 04:35:17 +00:00
|
|
|
"""Compare an XML object against an XML mask.
|
|
|
|
|
|
|
|
:param source: The :class:`~xml.etree.ElementTree.Element` XML object
|
|
|
|
to compare against the mask.
|
|
|
|
:param mask: The :class:`~xml.etree.ElementTree.Element` XML object
|
|
|
|
serving as the mask.
|
|
|
|
:param use_ns: Indicates if namespaces should be respected during
|
|
|
|
the comparison.
|
|
|
|
:default_ns: The default namespace to apply to elements that
|
|
|
|
do not have a specified namespace.
|
|
|
|
Defaults to ``"__no_ns__"``.
|
2010-09-01 18:28:43 +00:00
|
|
|
"""
|
|
|
|
use_ns = not IGNORE_NS
|
|
|
|
|
|
|
|
if source is None:
|
|
|
|
# If the element was not found. May happend during recursive calls.
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Convert the mask to an XML object if it is a string.
|
|
|
|
if not hasattr(mask, 'attrib'):
|
|
|
|
try:
|
|
|
|
mask = ET.fromstring(mask)
|
|
|
|
except ExpatError:
|
2011-11-19 20:07:57 +00:00
|
|
|
log.warning("Expat error: %s\nIn parsing: %s", '', mask)
|
2010-09-01 18:28:43 +00:00
|
|
|
if not use_ns:
|
|
|
|
# Compare the element without using namespaces.
|
|
|
|
source_tag = source.tag.split('}', 1)[-1]
|
|
|
|
mask_tag = mask.tag.split('}', 1)[-1]
|
|
|
|
if source_tag != mask_tag:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Compare the element using namespaces
|
|
|
|
mask_ns_tag = "{%s}%s" % (self.default_ns, mask.tag)
|
|
|
|
if source.tag not in [mask.tag, mask_ns_tag]:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If the mask includes text, compare it.
|
2011-02-14 18:49:43 +00:00
|
|
|
if mask.text and source.text and \
|
|
|
|
source.text.strip() != mask.text.strip():
|
2010-09-01 18:28:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Compare attributes. The stanza must include the attributes
|
|
|
|
# defined by the mask, but may include others.
|
|
|
|
for name, value in mask.attrib.items():
|
|
|
|
if source.attrib.get(name, "__None__") != value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Recursively check subelements.
|
2010-11-17 18:37:03 +00:00
|
|
|
matched_elements = {}
|
2010-09-01 18:28:43 +00:00
|
|
|
for subelement in mask:
|
|
|
|
if use_ns:
|
2010-11-17 18:37:03 +00:00
|
|
|
matched = False
|
|
|
|
for other in source.findall(subelement.tag):
|
|
|
|
matched_elements[other] = False
|
|
|
|
if self._mask_cmp(other, subelement, use_ns):
|
|
|
|
if not matched_elements.get(other, False):
|
|
|
|
matched_elements[other] = True
|
|
|
|
matched = True
|
|
|
|
if not matched:
|
2010-09-01 18:28:43 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not self._mask_cmp(self._get_child(source, subelement.tag),
|
|
|
|
subelement, use_ns):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Everything matches.
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _get_child(self, xml, tag):
|
2011-12-05 04:35:17 +00:00
|
|
|
"""Return a child element given its tag, ignoring namespace values.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
Returns ``None`` if the child was not found.
|
2010-09-01 18:28:43 +00:00
|
|
|
|
2011-12-05 04:35:17 +00:00
|
|
|
:param xml: The :class:`~xml.etree.ElementTree.Element` XML object
|
|
|
|
to search for the given child tag.
|
|
|
|
:param tag: The name of the subelement to find.
|
2010-09-01 18:28:43 +00:00
|
|
|
"""
|
|
|
|
tag = tag.split('}')[-1]
|
|
|
|
try:
|
|
|
|
children = [c.tag.split('}')[-1] for c in xml.getchildren()]
|
|
|
|
index = children.index(tag)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
return xml.getchildren()[index]
|