Esempio n. 1
0
def toResponse(stanza, stanzaType=None):
    """
	Create a response stanza from another stanza.

	This takes the addressing and id attributes from a stanza to create a (new,
	empty) response stanza. The addressing attributes are swapped and the id
	copied. Optionally, the stanza type of the response can be specified.

	@param stanza: the original stanza
	@type stanza: L{domish.Element}
	@param stanzaType: optional response stanza type
	@type stanzaType: C{str}
	@return: the response stanza.
	@rtype: L{domish.Element}
	"""

    toAddr = stanza.getAttribute('from')
    fromAddr = stanza.getAttribute('to')
    stanzaID = stanza.getAttribute('id')

    response = domish.Element((None, stanza.name))
    if toAddr:
        response['to'] = toAddr
    if fromAddr:
        response['from'] = fromAddr
    if stanzaID:
        response['id'] = stanzaID
    if stanzaType:
        response['type'] = stanzaType

    return response
Esempio n. 2
0
    def sendHeader(self):
        """
		Send stream header.
		"""
        # set up optional extra namespaces
        localPrefixes = {}
        for uri, prefix in self.prefixes.iteritems():
            if uri != NS_STREAMS:
                localPrefixes[prefix] = uri

        rootElement = domish.Element((NS_STREAMS, 'stream'),
                                     self.namespace,
                                     localPrefixes=localPrefixes)

        if self.otherEntity:
            rootElement['to'] = self.otherEntity.userhost()

        if self.thisEntity:
            rootElement['from'] = self.thisEntity.userhost()

        if not self.initiating and self.sid:
            rootElement['id'] = self.sid

        if self.version >= (1, 0):
            rootElement['version'] = "%d.%d" % self.version

        self.send(rootElement.toXml(prefixes=self.prefixes, closeElement=0))
        self._headerSent = True
Esempio n. 3
0
    def start(self):
        """
		Start TLS negotiation.

		This checks if the receiving entity requires TLS, the SSL library is
		available and uses the C{required} and C{wanted} instance variables to
		determine what to do in the various different cases.

		For example, if the SSL library is not available, and wanted and
		required by the user, it raises an exception. However if it is not
		required by both parties, initialization silently succeeds, moving
		on to the next step.
		"""
        if self.wanted:
            if ssl is None:
                if self.required:
                    return defer.fail(TLSNotSupported())
                else:
                    return defer.succeed(None)
            else:
                pass
        elif self.xmlstream.features[self.feature].required:
            return defer.fail(TLSRequired())
        else:
            return defer.succeed(None)

        self._deferred = defer.Deferred()
        self.xmlstream.addOnetimeObserver("/proceed", self.onProceed)
        self.xmlstream.addOnetimeObserver("/failure", self.onFailure)
        self.xmlstream.send(domish.Element((NS_XMPP_TLS, "starttls")))
        return self._deferred
Esempio n. 4
0
    def initialize(self):
        xs = self.xmlstream
        hs = domish.Element((self.xmlstream.namespace, "handshake"))
        hs.addContent(
            xmlstream.hashPassword(xs.sid, unicode(xs.authenticator.password)))

        # Setup observer to watch for handshake result
        xs.addOnetimeObserver("/handshake", self._cbHandshake)
        xs.send(hs)
        self._deferred = defer.Deferred()
        return self._deferred
Esempio n. 5
0
    def sendResponse(self, data=''):
        """
		Send response to a challenge.

		@param data: client response.
		@type data: L{str}.
		"""

        response = domish.Element((NS_XMPP_SASL, 'response'))
        if data:
            response.addContent(b64encode(data))
        self.xmlstream.send(response)
Esempio n. 6
0
    def sendAuth(self, data=None):
        """
		Initiate authentication protocol exchange.

		If an initial client response is given in C{data}, it will be
		sent along.

		@param data: initial client response.
		@type data: L{str} or L{None}.
		"""

        auth = domish.Element((NS_XMPP_SASL, 'auth'))
        auth['mechanism'] = self.mechanism.name
        if data is not None:
            auth.addContent(b64encode(data) or '=')
        self.xmlstream.send(auth)
Esempio n. 7
0
	def getElement(self):
		"""
		Get XML representation from self.

		The method creates an L{domish} representation of the
		error data contained in this exception.

		@rtype: L{domish.Element}
		"""
		error = domish.Element((None, 'error'))
		error.addElement((self.namespace, self.condition))
		if self.text:
			text = error.addElement((self.namespace, 'text'),
									content=self.text)
			if self.textLang:
				text[(NS_XML, 'lang')] = self.textLang
		if self.appCondition:
			error.addChild(self.appCondition)
		return error