def test_replace_null_ns(self): doc = libxml2.newDoc("1.0") root = doc.newChild(None, "root", None) common_ns = root.newNs(xmlextra.COMMON_NS, None) root.setNs(common_ns) doc.setRootElement(root) n = input_doc2.getRootElement() try: input_ns = n.ns() except libxml2.treeError: input_ns = None n = n.children while n: n1 = n.docCopyNode(doc, 1) root.addChild(n1) if n1.type == 'element': try: n1_ns = n1.ns() except libxml2.treeError: n1_ns = None if n1_ns is None: xmlextra.replace_ns(n1, n1_ns, common_ns) n = n.next self.failUnless(xml_elements_equal(root, output_root))
def test_replace_null_ns(self): doc = libxml2.newDoc("1.0") root = doc.newChild(None, "root", None) common_ns = root.newNs(xmlextra.COMMON_NS, None) root.setNs(common_ns) doc.setRootElement(root) n = input_doc2.getRootElement() try: input_ns = n.ns() except libxml2.treeError: input_ns = None n = n.children while n: n1 = n.docCopyNode(doc, 1) root.addChild(n1) if n1.type == "element": try: n1_ns = n1.ns() except libxml2.treeError: n1_ns = None if n1_ns is None: xmlextra.replace_ns(n1, n1_ns, common_ns) n = n.next self.failUnless(xml_elements_equal(root, output_root))
def __init__(self, xmlnode=None, copy=True, parent=None): """ Copy MucXBase object or create a new one, possibly based on or wrapping an XML node. :Parameters: - `xmlnode`: is the object to copy or an XML node to wrap. - `copy`: when `True` a copy of the XML node provided will be included in `self`, the node will be copied otherwise. - `parent`: parent node for the created/copied XML element. :Types: - `xmlnode`: `MucXBase` or `libxml2.xmlNode` - `copy`: `bool` - `parent`: `libxml2.xmlNode` """ if self.ns==None: raise RuntimeError,"Pure virtual class called" self.xmlnode=None self.borrowed=False if isinstance(xmlnode,libxml2.xmlNode): if copy: self.xmlnode=xmlnode.docCopyNode(common_doc,1) common_root.addChild(self.xmlnode) else: self.xmlnode=xmlnode self.borrowed=True if copy: ns=xmlnode.ns() xmlextra.replace_ns(self.xmlnode, ns, common_ns) elif isinstance(xmlnode,MucXBase): if not copy: raise TypeError, "MucXBase may only be copied" self.xmlnode=xmlnode.xmlnode.docCopyNode(common_doc,1) common_root.addChild(self.xmlnode) elif xmlnode is not None: raise TypeError, "Bad MucX constructor argument" else: if parent: self.xmlnode=parent.newChild(None,self.element,None) self.borrowed=True else: self.xmlnode=common_root.newChild(None,self.element,None) ns=self.xmlnode.newNs(self.ns,None) self.xmlnode.setNs(ns)
def __init__(self, xmlnode=None, copy=True, parent=None): """ Copy MucXBase object or create a new one, possibly based on or wrapping an XML node. :Parameters: - `xmlnode`: is the object to copy or an XML node to wrap. - `copy`: when `True` a copy of the XML node provided will be included in `self`, the node will be copied otherwise. - `parent`: parent node for the created/copied XML element. :Types: - `xmlnode`: `MucXBase` or `libxml2.xmlNode` - `copy`: `bool` - `parent`: `libxml2.xmlNode` """ if self.ns == None: raise RuntimeError, "Pure virtual class called" self.xmlnode = None self.borrowed = False if isinstance(xmlnode, libxml2.xmlNode): if copy: self.xmlnode = xmlnode.docCopyNode(common_doc, 1) common_root.addChild(self.xmlnode) else: self.xmlnode = xmlnode self.borrowed = True if copy: ns = xmlnode.ns() xmlextra.replace_ns(self.xmlnode, ns, common_ns) elif isinstance(xmlnode, MucXBase): if not copy: raise TypeError, "MucXBase may only be copied" self.xmlnode = xmlnode.xmlnode.docCopyNode(common_doc, 1) common_root.addChild(self.xmlnode) elif xmlnode is not None: raise TypeError, "Bad MucX constructor argument" else: if parent: self.xmlnode = parent.newChild(None, self.element, None) self.borrowed = True else: self.xmlnode = common_root.newChild(None, self.element, None) ns = self.xmlnode.newNs(self.ns, None) self.xmlnode.setNs(ns)
def __from_xml(self, xmlnode, ns, copy, parent): """Initialize an ErrorNode object from an XML node. :Parameters: - `xmlnode`: XML node to be wrapped into this object. - `ns`: XML namespace URI of the error condition element (to be used when the provided node has no namespace). - `copy`: When `True` then the XML node will be copied, otherwise it is only borrowed. - `parent`: Parent node for the XML node to be copied or created. :Types: - `xmlnode`: `libxml2.xmlNode` - `ns`: `unicode` - `copy`: `bool` - `parent`: `libxml2.xmlNode`""" if not ns: ns = None c = xmlnode.children while c: ns = c.ns().getContent() if ns in (STREAM_ERROR_NS, STANZA_ERROR_NS): break ns = None c = c.next if ns == None: raise ProtocolError, "Bad error namespace" self.ns = from_utf8(ns) if copy: self.xmlnode = xmlnode.docCopyNode(common_doc, 1) if not parent: parent = common_root parent.addChild(self.xmlnode) else: self.xmlnode = xmlnode self.borrowed = 1 if copy: ns1 = xmlnode.ns() xmlextra.replace_ns(self.xmlnode, ns1, common_ns)
def __from_xml(self,xmlnode,ns,copy,parent): """Initialize an ErrorNode object from an XML node. :Parameters: - `xmlnode`: XML node to be wrapped into this object. - `ns`: XML namespace URI of the error condition element (to be used when the provided node has no namespace). - `copy`: When `True` then the XML node will be copied, otherwise it is only borrowed. - `parent`: Parent node for the XML node to be copied or created. :Types: - `xmlnode`: `libxml2.xmlNode` - `ns`: `unicode` - `copy`: `bool` - `parent`: `libxml2.xmlNode`""" if not ns: ns=None c=xmlnode.children while c: ns=c.ns().getContent() if ns in (STREAM_ERROR_NS,STANZA_ERROR_NS): break ns=None c=c.next if ns==None: raise ProtocolError, "Bad error namespace" self.ns=from_utf8(ns) if copy: self.xmlnode=xmlnode.docCopyNode(common_doc,1) if not parent: parent=common_root parent.addChild(self.xmlnode) else: self.xmlnode=xmlnode self.borrowed=1 if copy: ns1=xmlnode.ns() xmlextra.replace_ns(self.xmlnode, ns1, common_ns)
def message_received(self, user, stanza): if not hasattr(user, 'attacks'): print("invalid message, maybe history") return # check if we have dionaea entries in the message # provide a namespace ... # I love xml namespaces ... # dionaea r = stanza.xpath_eval("/default:message/default:body/dionaea:dionaea", namespaces = dionaea_ns) for d in r: # rename the namespace for the dionaea entries o = d.ns() n = d.newNs("http://dionaea.carnivore.it", "dionaea") d.setNs(n) replace_ns(d,o,n) # get the incident p = d.hasProp('incident') mname = p.content mname = mname.replace(".","_") # use the incidents name to get the appropriate handler method = getattr(self, "handle_incident_" + mname, None) # method = self.handle_incident_debug if method is not None: c = d.children while c is not None: # print("c: '%s'" % c) if c.isText(): c = c.next continue # call the handler with the object # print(mname) method(user, c) c = c.next # else: # print("method %s is not implemented" % mname) # self.handle_incident_not_implemented(user, stanza) # kippo r = stanza.xpath_eval("/default:message/default:body/kippo:kippo", namespaces = { "default" : "http://pyxmpp.jajcus.net/xmlns/common", "kippo" : "http://code.google.com/p/kippo/"}) for d in r: o = d.ns() n = d.newNs("http://code.google.com/p/kippo/", "kippo") d.setNs(n) replace_ns(d,o,n) # print(d) p = d.hasProp('type') mname = p.content method = getattr(self, "handle_kippo_" + mname, None) if method is not None: for c in d.children: if c.isText(): continue method(user, c) else: print("method %s is not implemented" % mname)
def __init__(self, name_or_xmlnode, from_jid=None, to_jid=None, stanza_type=None, stanza_id=None, error=None, error_cond=None, stream = None): """Initialize a Stanza object. :Parameters: - `name_or_xmlnode`: XML node to be wrapped into the Stanza object or other Presence object to be copied. If not given then new presence stanza is created using following parameters. - `from_jid`: sender JID. - `to_jid`: recipient JID. - `stanza_type`: staza type: one of: "get", "set", "result" or "error". - `stanza_id`: stanza id -- value of stanza's "id" attribute. If not given, then unique for the session value is generated. - `error`: error object. Ignored if `stanza_type` is not "error". - `error_cond`: error condition name. Ignored if `stanza_type` is not "error" or `error` is not None. :Types: - `name_or_xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza` - `from_jid`: `JID` - `to_jid`: `JID` - `stanza_type`: `unicode` - `stanza_id`: `unicode` - `error`: `pyxmpp.error.StanzaErrorNode` - `error_cond`: `unicode`""" self._error=None self.xmlnode=None if isinstance(name_or_xmlnode,Stanza): self.xmlnode=name_or_xmlnode.xmlnode.copyNode(1) common_doc.addChild(self.xmlnode) elif isinstance(name_or_xmlnode,libxml2.xmlNode): self.xmlnode=name_or_xmlnode.docCopyNode(common_doc,1) common_doc.addChild(self.xmlnode) try: ns = self.xmlnode.ns() except libxml2.treeError: ns = None if not ns or not ns.name: xmlextra.replace_ns(self.xmlnode, ns, common_ns) else: self.xmlnode=common_doc.newChild(common_ns,name_or_xmlnode,None) if from_jid is not None: if not isinstance(from_jid,JID): from_jid=JID(from_jid) self.xmlnode.setProp("from",from_jid.as_utf8()) if to_jid is not None: if not isinstance(to_jid,JID): to_jid=JID(to_jid) self.xmlnode.setProp("to",to_jid.as_utf8()) if stanza_type: self.xmlnode.setProp("type",stanza_type) if stanza_id: self.xmlnode.setProp("id",stanza_id) if self.get_type()=="error": from pyxmpp.error import StanzaErrorNode if error: self._error=StanzaErrorNode(error,parent=self.xmlnode,copy=1) elif error_cond: self._error=StanzaErrorNode(error_cond,parent=self.xmlnode) self.stream = stream