Exemple #1
0
    def __init__(self,
                 xmlnode=None,
                 from_jid=None,
                 to_jid=None,
                 stanza_type=None,
                 stanza_id=None,
                 error=None,
                 error_cond=None,
                 stream=None):
        """Initialize an `Iq` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Iq` object
              or other Iq 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_cond`: error condition name. Ignored if `stanza_type` is not "error".
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Iq`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `error_cond`: `unicode`"""
        self.xmlnode = None
        if isinstance(xmlnode, Iq):
            pass
        elif isinstance(xmlnode, Stanza):
            raise TypeError, "Couldn't make Iq from other Stanza"
        elif isinstance(xmlnode, libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError, "Couldn't make Iq from %r" % (type(xmlnode), )
        elif not stanza_type:
            raise ValueError, "type is required for Iq"
        else:
            if not stanza_id and stanza_type in ("get", "set"):
                stanza_id = gen_id()

        if not xmlnode and stanza_type not in ("get", "set", "result",
                                               "error"):
            raise ValueError, "Invalid Iq type: %r" % (stanza_type, )

        if xmlnode is None:
            xmlnode = "iq"

        Stanza.__init__(self,
                        xmlnode,
                        from_jid=from_jid,
                        to_jid=to_jid,
                        stanza_type=stanza_type,
                        stanza_id=stanza_id,
                        error=error,
                        error_cond=error_cond,
                        stream=stream)
Exemple #2
0
    def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None, 
            stanza_id = None, show = None, status = None, priority = 0,
            error = None, error_cond = None, stream = None):
        """Initialize a `Presence` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Presence` 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: None, "available", "unavailable",
              "subscribe", "subscribed", "unsubscribe", "unsubscribed" or
              "error". "available" is automaticaly changed to_jid None.
            - `stanza_id`: stanza id -- value of stanza's "id" attribute
            - `show`: "show" field of presence stanza. One of: None, "away",
              "xa", "dnd", "chat".
            - `status`: descriptive text for the presence stanza.
            - `priority`: presence priority.
            - `error_cond`: error condition name. Ignored if `stanza_type` is not "error"
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `show`: `unicode`
            - `status`: `unicode`
            - `priority`: `unicode`
            - `error_cond`: `unicode`"""
        self.xmlnode=None
        if isinstance(xmlnode,Presence):
            pass
        elif isinstance(xmlnode,Stanza):
            raise TypeError,"Couldn't make Presence from other Stanza"
        elif isinstance(xmlnode,libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError,"Couldn't make Presence from %r" % (type(xmlnode),)

        if stanza_type and stanza_type not in presence_types:
            raise ValueError, "Invalid presence type: %r" % (type,)

        if stanza_type=="available":
            stanza_type=None

        if xmlnode is None:
            xmlnode="presence"

        Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type,
                stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)

        if show:
            self.xmlnode.newTextChild(common_ns,"show",to_utf8(show))
        if status:
            self.xmlnode.newTextChild(common_ns,"status",to_utf8(status))
        if priority and priority!=0:
            self.xmlnode.newTextChild(common_ns,"priority",to_utf8(unicode(priority)))
Exemple #3
0
    def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None, stanza_id = None,
            subject = None, body = None, thread = None, error = None, error_cond = None, stream = None):
        """Initialize a `Message` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Message` object
              or other Message 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.
            - `subject`: message subject,
            - `body`: message body.
            - `thread`: message thread id.
            - `error_cond`: error condition name. Ignored if `stanza_type` is not "error".
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `subject`: `unicode`
            - `body`: `unicode`
            - `thread`: `unicode`
            - `error_cond`: `unicode`"""

        self.xmlnode=None
        if isinstance(xmlnode,Message):
            pass
        elif isinstance(xmlnode,Stanza):
            raise TypeError, "Couldn't make Message from other Stanza"
        elif isinstance(xmlnode,libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError, "Couldn't make Message from %r" % (type(xmlnode),)

        if xmlnode is None:
            xmlnode="message"

        Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type,
                stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)

        if subject is not None:
            self.xmlnode.newTextChild(common_ns,"subject",to_utf8(subject))
        if body is not None:
            self.xmlnode.newTextChild(common_ns,"body",to_utf8(body))
        if thread is not None:
            self.xmlnode.newTextChild(common_ns,"thread",to_utf8(thread))
Exemple #4
0
    def safe_send(self, stanza):
        to = stanza.get_to()
        if not to.domain.endswith(self.config.domain): to = to.bare()

        stanza = Stanza(stanza, to_jid=to)
        dbg("tx:\n%s" % (fmt_evt(stanza), ))
        self.stream.send(stanza)
Exemple #5
0
    def safe_send(self, stanza):
        dbg("safe_send:\n%s" % (fmt_evt(stanza), ))
        to = stanza.get_to()
        if (to.node == self.config.dialback
                and to.domain.endswith(self.config.domain)):

            ## if this is to a dialback that is *not* online, swallow it rather
            ## than forwarding

            ## if it is an unavailable, that's always safe and may be a suicide
            ## note to a dialback that surprised us or didn't start properly so
            ## send it anyway

            if (get_presence(stanza) != "unavailable"
                    and not (to in St['dialback_online'] and
                             St['dialback_online'][to] == DIALBACK.online)):

                err("destination dialback not online!  dbo:%s stanza:\n%s" %
                    ((to in St['dialback_online'] and St['dialback_online'][to]
                      or "None"), fmt_evt(stanza)))
                return True

        ## if this is *not* an iq and *not* to a dialback, strip the destination
        ## jid
        if (not is_iq(stanza) and not to.domain.endswith(self.config.domain)):
            to = to.bare()

        stanza = Stanza(stanza, to_jid=to)
        dbg("tx:\n%s" % (fmt_evt(stanza), ))
        self.stream.send(stanza)
Exemple #6
0
    def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None,
            stanza_id = None, error = None, error_cond=None, stream = None):
        """Initialize an `Iq` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Iq` object
              or other Iq 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_cond`: error condition name. Ignored if `stanza_type` is not "error".
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Iq`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `error_cond`: `unicode`"""
        self.xmlnode=None
        if isinstance(xmlnode,Iq):
            pass
        elif isinstance(xmlnode,Stanza):
            raise TypeError,"Couldn't make Iq from other Stanza"
        elif isinstance(xmlnode,libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError,"Couldn't make Iq from %r" % (type(xmlnode),)
        elif not stanza_type:
            raise ValueError, "type is required for Iq"
        else:
            if not stanza_id and stanza_type in ("get", "set"):
                stanza_id=gen_id()

        if not xmlnode and stanza_type not in ("get","set","result","error"):
            raise ValueError, "Invalid Iq type: %r" % (stanza_type,)

        if xmlnode is None:
            xmlnode="iq"

        Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid,
            stanza_type = stanza_type, stanza_id = stanza_id, error = error,
            error_cond = error_cond, stream = stream)
Exemple #7
0
    def __init__(self, xmlnode = None, from_jid = None, to_jid = None,\
                stanza_type = None, stanza_id = None, error = None,\
                error_cond = None, stream = None):
        """Initialize a `PubSubMessage` object..........
            - `error_cond`: `unicode`"""

        self.xmlnode=None
        if isinstance(xmlnode,PubSubMessage):
            pass
        elif isinstance(xmlnode,Stanza):
            raise TypeError, "Couldn't make PubSubMessage from other Stanza"
        elif isinstance(xmlnode,libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError, "Couldn't make PubSubMessage from %r" % (type(xmlnode),)

        if xmlnode is None:
            xmlnode="message"

        Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid,\
                    stanza_type = stanza_type, stanza_id = stanza_id, \
                    error = error, error_cond = error_cond, stream = stream)
Exemple #8
0
    def subscribed(self, stanza):
        dbg("subscribed:\n%s" % (fmt_evt(stanza), ))
        StLock.acquire()
        try:
            to = stanza.get_to()
            if to.bare().as_utf8() == self.config.component:
                ## to master
                pass

            else:  ## to user @ skype
                frm = get_from(stanza)
                if frm in St['dialbacks']:
                    ## ...from skype user
                    ujid = St['dialbacks'][frm]
                    self.safe_send(
                        Stanza(stanza, to_jid=ujid, from_jid=stanza.get_to()))

                else:  ## ...from a dialback: forward on to user
                    ujid = frm.bare()
                    if ujid not in St['users']: return True
                    djid = St['users'][ujid]

                    iq = Iq(to_jid=djid,
                            from_jid=self.config.component,
                            stanza_type="set")
                    command = iq.new_query("http://vipadia.com/skype",
                                           "command")
                    add_child(command,
                              "item",
                              attrs={
                                  "from": "%s" % to.node,
                                  "command": "subscribed",
                              })
                    dbg("  subscribed:\n%s" % (fmt_evt(iq), ))
                    self.safe_send(iq)

            return True
        finally:
            StLock.release()
Exemple #9
0
    def __init__(self,
                 xmlnode=None,
                 from_jid=None,
                 to_jid=None,
                 stanza_type=None,
                 stanza_id=None,
                 show=None,
                 status=None,
                 priority=0,
                 error=None,
                 error_cond=None,
                 stream=None):
        """Initialize a `Presence` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Presence` 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: None, "available", "unavailable",
              "subscribe", "subscribed", "unsubscribe", "unsubscribed" or
              "error". "available" is automaticaly changed to_jid None.
            - `stanza_id`: stanza id -- value of stanza's "id" attribute
            - `show`: "show" field of presence stanza. One of: None, "away",
              "xa", "dnd", "chat".
            - `status`: descriptive text for the presence stanza.
            - `priority`: presence priority.
            - `error_cond`: error condition name. Ignored if `stanza_type` is not "error"
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `show`: `unicode`
            - `status`: `unicode`
            - `priority`: `unicode`
            - `error_cond`: `unicode`"""
        self.xmlnode = None
        if isinstance(xmlnode, Presence):
            pass
        elif isinstance(xmlnode, Stanza):
            raise TypeError, "Couldn't make Presence from other Stanza"
        elif isinstance(xmlnode, libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError, "Couldn't make Presence from %r" % (
                type(xmlnode), )

        if stanza_type and stanza_type not in presence_types:
            raise ValueError, "Invalid presence type: %r" % (type, )

        if stanza_type == "available":
            stanza_type = None

        if xmlnode is None:
            xmlnode = "presence"

        Stanza.__init__(self,
                        xmlnode,
                        from_jid=from_jid,
                        to_jid=to_jid,
                        stanza_type=stanza_type,
                        stanza_id=stanza_id,
                        error=error,
                        error_cond=error_cond,
                        stream=stream)

        if show:
            self.xmlnode.newTextChild(common_ns, "show", to_utf8(show))
        if status:
            self.xmlnode.newTextChild(common_ns, "status", to_utf8(status))
        if priority and priority != 0:
            self.xmlnode.newTextChild(common_ns, "priority",
                                      to_utf8(unicode(priority)))
Exemple #10
0
 def safe_send(self, stanza):
     to = stanza.get_to()
     if not to.domain.endswith(self.config.domain): to = to.bare()
         
     stanza = Stanza(stanza, to_jid=to)
     self.stream.send(stanza)
Exemple #11
0
 def make_stanza(m):
     return Stanza(parseDoc(m).children)
Exemple #12
0
    def default_handler(self, stanza):
        dbg("default_handler:\n%s" % (fmt_evt(stanza), ))
        StLock.acquire()
        try:
            if stanza.stanza_type == "presence":
                handled = self.default_presence_handler(stanza)
                if handled: return True

            frm = get_from(stanza)
            dbg("  frm:%s node:%s domain:%s resource:%s config:%s,%s" %
                (frm.as_utf8(), frm.node, frm.domain, frm.resource,
                 self.config.dialback, self.config.domain))

            if (frm.node, frm.domain) == (self.config.dialback,
                                          self.config.domain):
                ## ...from dialback
                djid = frm
                if djid not in St['dialbacks']: return True

                ujid = St['dialbacks'][djid]
                hsh = "%s" % (hash(ujid), )
                if not frm.resource.endswith(hsh):
                    err("*** SPOOFED MESSAGE DETECTED ***")
                    err("    ujid:%s hash(ujid):%s frm:%s" %
                        (ujid.as_utf8(), hsh, frm.as_utf8()))
                    err(fmt_evt(stanza))
                    err("*** DIE DIE DIE ***")
                    os._exit(os.EX_PROTOCOL)

                if stanza.stanza_type == "iq":
                    userjids = St['userjids'][ujid].keys()
                    forward = Stanza(stanza,
                                     to_jid=userjids[0],
                                     from_jid=stanza.get_to())
                else:
                    forward = Stanza(stanza,
                                     to_jid=ujid,
                                     from_jid=stanza.get_to())

            else:  ## ...from the user
                ujid = frm.bare()
                dbg("  frm:%s ujid:%s users:%s" %
                    (frm.as_utf8(), ujid.as_utf8(), St['users'].keys()))
                if ujid not in St['users']: return True

                djid = St['users'][ujid]
                dbg("  djid:%s to:%s" % (
                    djid.as_utf8(),
                    stanza.get_to().as_utf8(),
                ))
                forward = Stanza(stanza, to_jid=djid, from_jid=stanza.get_to())

            dbg("  forward:\n%s" % (fmt_evt(forward), ))
            if stanza.stanza_type == "message" and stanza.get_body() == None:
                dbg("  not forwarding blank message!")
                return True

            self.safe_send(forward)
            return True
        finally:
            StLock.release()
Exemple #13
0
    def __init__(self,
                 xmlnode=None,
                 from_jid=None,
                 to_jid=None,
                 stanza_type=None,
                 stanza_id=None,
                 subject=None,
                 body=None,
                 thread=None,
                 error=None,
                 error_cond=None,
                 stream=None):
        """Initialize a `Message` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `Message` object
              or other Message 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.
            - `subject`: message subject,
            - `body`: message body.
            - `thread`: message thread id.
            - `error_cond`: error condition name. Ignored if `stanza_type` is not "error".
        :Types:
            - `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `subject`: `unicode`
            - `body`: `unicode`
            - `thread`: `unicode`
            - `error_cond`: `unicode`"""

        self.xmlnode = None
        if isinstance(xmlnode, Message):
            pass
        elif isinstance(xmlnode, Stanza):
            raise TypeError, "Couldn't make Message from other Stanza"
        elif isinstance(xmlnode, libxml2.xmlNode):
            pass
        elif xmlnode is not None:
            raise TypeError, "Couldn't make Message from %r" % (
                type(xmlnode), )

        if xmlnode is None:
            xmlnode = "message"

        Stanza.__init__(self,
                        xmlnode,
                        from_jid=from_jid,
                        to_jid=to_jid,
                        stanza_type=stanza_type,
                        stanza_id=stanza_id,
                        error=error,
                        error_cond=error_cond,
                        stream=stream)

        if subject is not None:
            self.xmlnode.newTextChild(common_ns, "subject", to_utf8(subject))
        if body is not None:
            self.xmlnode.newTextChild(common_ns, "body", to_utf8(body))
        if thread is not None:
            self.xmlnode.newTextChild(common_ns, "thread", to_utf8(thread))