Exemple #1
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <presence /> stanza with an optional 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self['id'] == '':
            if self.stream is not None and self.stream.use_presence_ids:
                self['id'] = self.stream.new_id()
Exemple #2
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <presence /> stanza with an optional 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self['id'] == '':
            if self.stream is not None and self.stream.use_presence_ids:
                self['id'] = self.stream.new_id()
Exemple #3
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <message /> stanza with an optional 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self["id"] == "":
            if self.stream is not None and self.stream.use_message_ids:
                self["id"] = self.stream.new_id()
Exemple #4
0
    def set_payload(self, value):
        """
        Set the XML contents of the <iq> stanza.

        :param value: An XML object or a list of XML objects to use as the <iq>
                      stanza's contents
        :type value: list or XML object
        """
        self.clear()
        StanzaBase.set_payload(self, value)
        return self
Exemple #5
0
    def set_payload(self, value):
        """
        Set the XML contents of the <iq> stanza.

        :param value: An XML object or a list of XML objects to use as the <iq>
                      stanza's contents
        :type value: list or XML object
        """
        self.clear()
        StanzaBase.set_payload(self, value)
        return self
Exemple #6
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <message /> stanza with an optional 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self['id'] == '':
            if self.stream is not None and self.stream.use_message_ids:
                self['id'] = self.stream.new_id()
            else:
                del self['origin_id']
Exemple #7
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <iq> stanza with an 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self['id'] == '':
            if self.stream is not None:
                self['id'] = self.stream.new_id()
            else:
                self['id'] = '0'
Exemple #8
0
    def __init__(self, *args, **kwargs):
        """
        Initialize a new <iq> stanza with an 'id' value.

        Overrides StanzaBase.__init__.
        """
        StanzaBase.__init__(self, *args, **kwargs)
        if self["id"] == "":
            if self.stream is not None:
                self["id"] = self.stream.new_id()
            else:
                self["id"] = "0"
Exemple #9
0
    def reply(self, body=None, clear=True):
        """
        Create a message reply.

        Overrides StanzaBase.reply.

        Sets proper 'to' attribute if the message is from a MUC, and
        adds a message body if one is given.

        :param str body:  Optional text content for the message.
        :param bool clear: Indicates if existing content should be removed
                           before replying. Defaults to True.

        :rtype: :class:`~.Message`
        """
        new_message = StanzaBase.reply(self, clear)

        if self['type'] == 'groupchat':
            new_message['to'] = new_message['to'].bare

        new_message['thread'] = self['thread']
        new_message['parent_thread'] = self['parent_thread']

        del new_message['id']
        if self.stream is not None and self.stream.use_message_ids:
            new_message['id'] = self.stream.new_id()

        if body is not None:
            new_message['body'] = body
        return new_message
Exemple #10
0
    def reply(self, body=None, clear=True):
        """
        Create a message reply.

        Overrides StanzaBase.reply.

        Sets proper 'to' attribute if the message is from a MUC, and
        adds a message body if one is given.

        :param str body:  Optional text content for the message.
        :param bool clear: Indicates if existing content should be removed
                           before replying. Defaults to True.

        :rtype: :class:`~.Message`
        """
        new_message = StanzaBase.reply(self, clear)

        if self['type'] == 'groupchat':
            new_message['to'] = new_message['to'].bare

        new_message['thread'] = self['thread']
        new_message['parent_thread'] = self['parent_thread']

        del new_message['id']

        if body is not None:
            new_message['body'] = body
        return new_message
Exemple #11
0
    def reply(self, clear=True):
        """
        Create a new reply <presence/> stanza from ``self``.

        Overrides StanzaBase.reply.

        :param bool clear: Indicates if the stanza contents should be removed
                           before replying. Defaults to True.
        """
        new_presence = StanzaBase.reply(self, clear)
        if self['type'] == 'unsubscribe':
            new_presence['type'] = 'unsubscribed'
        elif self['type'] == 'subscribe':
            new_presence['type'] = 'subscribed'
        return new_presence
Exemple #12
0
    def reply(self, clear=True):
        """
        Create a new reply <presence/> stanza from ``self``.

        Overrides StanzaBase.reply.

        :param bool clear: Indicates if the stanza contents should be removed
                           before replying. Defaults to True.
        """
        new_presence = StanzaBase.reply(self, clear)
        if self['type'] == 'unsubscribe':
            new_presence['type'] = 'unsubscribed'
        elif self['type'] == 'subscribe':
            new_presence['type'] = 'subscribed'
        return new_presence
Exemple #13
0
    def reply(self, clear=True):
        """
        Create a new <iq> stanza replying to ``self``.

        Overrides StanzaBase.reply

        Sets the 'type' to 'result' in addition to the default
        StanzaBase.reply behavior.

        :param bool clear: Indicates if existing content should be
                           removed before replying. Defaults to True.
        """
        new_iq = StanzaBase.reply(self, clear=clear)
        new_iq['type'] = 'result'
        return new_iq
Exemple #14
0
    def _set_stanza_values(self, values):
        """
        Set multiple stanza interface values using a dictionary.

        Stanza plugin values may be set usind nested dictionaries.

        If the interface 'query' is given, then it will be set
        last to avoid duplication of the <query /> element.

        Overrides ElementBase._set_stanza_values.

        Arguments:
            values -- A dictionary mapping stanza interface with values.
                      Plugin interfaces may accept a nested dictionary that
                      will be used recursively.
        """
        query = values.get("query", "")
        if query:
            del values["query"]
            StanzaBase._set_stanza_values(self, values)
            self["query"] = query
        else:
            StanzaBase._set_stanza_values(self, values)
        return self
Exemple #15
0
    def reply(self, clear=True):
        """
        Create a new <iq> stanza replying to ``self``.

        Overrides StanzaBase.reply

        Sets the 'type' to 'result' in addition to the default
        StanzaBase.reply behavior.

        :param bool clear: Indicates if existing content should be
                           removed before replying. Defaults to True.
        """
        new_iq = StanzaBase.reply(self, clear=clear)
        new_iq["type"] = "result"
        return new_iq
Exemple #16
0
    def _set_stanza_values(self, values):
        """
        Set multiple stanza interface values using a dictionary.

        Stanza plugin values may be set usind nested dictionaries.

        If the interface 'query' is given, then it will be set
        last to avoid duplication of the <query /> element.

        Overrides ElementBase._set_stanza_values.

        Arguments:
            values -- A dictionary mapping stanza interface with values.
                      Plugin interfaces may accept a nested dictionary that
                      will be used recursively.
        """
        query = values.get('query', '')
        if query:
            del values['query']
            StanzaBase._set_stanza_values(self, values)
            self['query'] = query
        else:
            StanzaBase._set_stanza_values(self, values)
        return self
Exemple #17
0
    def setup(self, xml=None):
        """
        Populate the stanza object using an optional XML object.

        Overrides ElementBase.setup.

        Sets a default error type and condition, and changes the
        parent stanza's type to 'error'.

        Arguments:
            xml -- Use an existing XML object for the stanza's values.
        """
        # StanzaBase overrides self.namespace
        self.namespace = Failure.namespace

        if StanzaBase.setup(self, xml):
            #If we had to generate XML then set default values.
            self['condition'] = 'not-authorized'

        self.xml.tag = self.tag_name()
Exemple #18
0
 def setup(self, xml):
     StanzaBase.setup(self, xml)
     self.xml.tag = self.tag_name()
Exemple #19
0
    def send(self, callback=None, timeout=None, timeout_callback=None):
        """Send an <iq> stanza over the XML stream.

        A callback handler can be provided that will be executed when the Iq
        stanza's result reply is received.

        Returns a future which result will be set to the result Iq if it is of type 'get' or 'set'
        (when it is received), or a future with the result set to None if it has another type.

        Overrides StanzaBase.send

        :param function callback: Optional reference to a stream handler
                                  function. Will be executed when a reply
                                  stanza is received.
        :param int timeout: The length of time (in seconds) to wait for a
                            response before the timeout_callback is called,
                            instead of the regular callback
        :param function timeout_callback: Optional reference to a stream handler
                                          function.  Will be executed when the
                                          timeout expires before a response has
                                          been received for the originally-sent
                                          IQ stanza.
        :rtype: asyncio.Future
        """
        if self.stream.session_bind_event.is_set():
            matcher = MatchIDSender({
                'id': self['id'],
                'self': self.stream.boundjid,
                'peer': self['to']
            })
        else:
            matcher = MatcherId(self['id'])

        future = asyncio.Future()

        # Prevents handlers from existing forever.
        if timeout is None:
            timeout = 120

        def callback_success(result):
            type_ = result['type']
            if type_ == 'result':
                future.set_result(result)
            elif type_ == 'error':
                future.set_exception(IqError(result))
            else:
                # Most likely an iq addressed to ourself, rearm the callback.
                handler = constr(handler_name,
                                 matcher,
                                 callback_success,
                                 once=True)
                self.stream.register_handler(handler)
                return

            if timeout is not None:
                self.stream.cancel_schedule('IqTimeout_%s' % self['id'])
            if callback is not None:
                callback(result)

        def callback_timeout():
            future.set_exception(IqTimeout(self))
            self.stream.remove_handler('IqCallback_%s' % self['id'])
            if timeout_callback is not None:
                timeout_callback(self)

        if self['type'] in ('get', 'set'):
            handler_name = 'IqCallback_%s' % self['id']
            if asyncio.iscoroutinefunction(callback):
                constr = CoroutineCallback
            else:
                constr = Callback
            if timeout is not None:
                self.stream.schedule('IqTimeout_%s' % self['id'],
                                     timeout,
                                     callback_timeout,
                                     repeat=False)
            handler = constr(handler_name,
                             matcher,
                             callback_success,
                             once=True)
            self.stream.register_handler(handler)
        else:
            future.set_result(None)
        StanzaBase.send(self)
        return future
Exemple #20
0
 def setup(self, xml):
     StanzaBase.setup(self, xml)
     self.values = self.values
Exemple #21
0
    def send(self, callback=None, timeout=None, timeout_callback=None):
        """Send an <iq> stanza over the XML stream.

        A callback handler can be provided that will be executed when the Iq
        stanza's result reply is received.

        Returns a future which result will be set to the result Iq if it is of type 'get' or 'set'
        (when it is received), or a future with the result set to None if it has another type.

        Overrides StanzaBase.send

        :param function callback: Optional reference to a stream handler
                                  function. Will be executed when a reply
                                  stanza is received.
        :param int timeout: The length of time (in seconds) to wait for a
                            response before the timeout_callback is called,
                            instead of the regular callback
        :param function timeout_callback: Optional reference to a stream handler
                                          function.  Will be executed when the
                                          timeout expires before a response has
                                          been received for the originally-sent
                                          IQ stanza.
        :rtype: asyncio.Future
        """
        if self.stream.session_bind_event.is_set():
            matcher = MatchIDSender({"id": self["id"], "self": self.stream.boundjid, "peer": self["to"]})
        else:
            matcher = MatcherId(self["id"])

        future = asyncio.Future()

        def callback_success(result):
            if result["type"] == "error":
                future.set_exception(IqError(result))
            else:
                future.set_result(result)

            if timeout is not None:
                self.stream.cancel_schedule("IqTimeout_%s" % self["id"])
            if callback is not None:
                callback(result)

        def callback_timeout():
            future.set_exception(IqTimeout(self))
            self.stream.remove_handler("IqCallback_%s" % self["id"])
            if timeout_callback is not None:
                timeout_callback(self)

        if self["type"] in ("get", "set"):
            handler_name = "IqCallback_%s" % self["id"]
            if asyncio.iscoroutinefunction(callback):
                constr = CoroutineCallback
            else:
                constr = Callback
            if timeout is not None:
                self.stream.schedule("IqTimeout_%s" % self["id"], timeout, callback_timeout, repeat=False)
            handler = constr(handler_name, matcher, callback_success, once=True)
            self.stream.register_handler(handler)
        else:
            future.set_result(None)
        StanzaBase.send(self)
        return future
Exemple #22
0
 def setup(self, xml):
     StanzaBase.setup(self, xml)
     self.xml.tag = self.tag_name()
Exemple #23
0
 def setup(self, xml):
     StanzaBase.setup(self, xml)
     self.values = self.values
Exemple #24
0
    def send(self, callback=None, timeout=None, timeout_callback=None):
        """Send an <iq> stanza over the XML stream.

        A callback handler can be provided that will be executed when the Iq
        stanza's result reply is received.

        Returns a future which result will be set to the result Iq if it is of type 'get' or 'set'
        (when it is received), or a future with the result set to None if it has another type.

        Overrides StanzaBase.send

        :param function callback: Optional reference to a stream handler
                                  function. Will be executed when a reply
                                  stanza is received.
        :param int timeout: The length of time (in seconds) to wait for a
                            response before the timeout_callback is called,
                            instead of the regular callback
        :param function timeout_callback: Optional reference to a stream handler
                                          function.  Will be executed when the
                                          timeout expires before a response has
                                          been received for the originally-sent
                                          IQ stanza.
        :rtype: asyncio.Future
        """
        if self.stream.session_bind_event.is_set():
            matcher = MatchIDSender({
                'id': self['id'],
                'self': self.stream.boundjid,
                'peer': self['to']
            })
        else:
            matcher = MatcherId(self['id'])

        future = asyncio.Future()

        def callback_success(result):
            if result['type'] == 'error':
                future.set_exception(IqError(result))
            else:
                future.set_result(result)

            if timeout is not None:
                self.stream.cancel_schedule('IqTimeout_%s' % self['id'])
            if callback is not None:
                callback(result)

        def callback_timeout():
            future.set_exception(IqTimeout(self))
            self.stream.remove_handler('IqCallback_%s' % self['id'])
            if timeout_callback is not None:
                timeout_callback(self)

        if self['type'] in ('get', 'set'):
            handler_name = 'IqCallback_%s' % self['id']
            if asyncio.iscoroutinefunction(callback):
                constr = CoroutineCallback
            else:
                constr = Callback
            if timeout is not None:
                self.stream.schedule('IqTimeout_%s' % self['id'],
                                     timeout,
                                     callback_timeout,
                                     repeat=False)
            handler = constr(handler_name,
                             matcher,
                             callback_success,
                             once=True)
            self.stream.register_handler(handler)
        else:
            future.set_result(None)
        StanzaBase.send(self)
        return future