def _process_node(self, node): """Process first level element of the stream. Handle component handshake (authentication) element, and treat elements in "jabber:component:accept", "jabber:client" and "jabber:server" equally (pass to `self.process_stanza`). All other elements are passed to `Stream._process_node`. :Parameters: - `node`: XML node describing the element """ ns = node.ns() if ns: ns_uri = node.ns().getContent() if (not ns or ns_uri == "jabber:component:accept") and node.name == "handshake": if self.initiator and not self.authenticated: self.authenticated = 1 self.state_change("authenticated", self.me) self._post_auth() return elif not self.authenticated and node.getContent() == self._compute_handshake(): self.peer = self.me n = common_doc.newChild(None, "handshake", None) self._write_node(n) n.unlinkNode() n.freeNode() self.peer_authenticated = 1 self.state_change("authenticated", self.peer) self._post_auth() return else: self._send_stream_error("not-authorized") raise FatalComponentStreamError, "Hanshake error." if ns_uri in ("jabber:component:accept", "jabber:client", "jabber:server"): stanza = stanza_factory(node) self.lock.release() try: self.process_stanza(stanza) finally: self.lock.acquire() stanza.free() return return Stream._process_node(self, node)
def _process_node(self,node): """Process first level element of the stream. Handle component handshake (authentication) element, and treat elements in "jabber:component:accept", "jabber:client" and "jabber:server" equally (pass to `self.process_stanza`). All other elements are passed to `Stream._process_node`. :Parameters: - `node`: XML node describing the element """ ns=node.ns() if ns: ns_uri=node.ns().getContent() if (not ns or ns_uri=="jabber:component:accept") and node.name=="handshake": if self.initiator and not self.authenticated: self.authenticated=1 self.state_change("authenticated",self.me) self._post_auth() return elif not self.authenticated and node.getContent()==self._compute_handshake(): self.peer=self.me n=common_doc.newChild(None,"handshake",None) self._write_node(n) n.unlinkNode() n.freeNode() self.peer_authenticated=1 self.state_change("authenticated",self.peer) self._post_auth() return else: self._send_stream_error("not-authorized") raise FatalComponentStreamError,"Hanshake error." if ns_uri in ("jabber:component:accept","jabber:client","jabber:server"): stanza=stanza_factory(node) self.lock.release() try: self.process_stanza(stanza) finally: self.lock.acquire() stanza.free() return return Stream._process_node(self,node)
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