Exemplo n.º 1
0
    def set_register(self, iq):
        to = iq.get_to()
        if to and to != self.jid:
            iq = iq.make_error_response("feature-not-implemented")
            self.stream.send(iq)
            return 1
        remove = iq.xpath_eval("r:query/r:remove", {"r": "jabber:iq:register"})
        if remove:
            m = Message(from_jid=iq.get_to(),
                        to_jid=iq.get_from(),
                        stanza_type="chat",
                        body=u"Unregistered")
            self.stream.send(m)
            p = Presence(from_jid=iq.get_to(),
                         to_jid=iq.get_from(),
                         stanza_type="unsubscribe")
            self.stream.send(p)
            p = Presence(from_jid=iq.get_to(),
                         to_jid=iq.get_from(),
                         stanza_type="unsubscribed")
            self.stream.send(p)
            return 1
        username = iq.xpath_eval("r:query/r:username",
                                 {"r": "jabber:iq:register"})
        if username:
            username = username[0].getContent()
        else:
            username = u""
        password = iq.xpath_eval("r:query/r:password",
                                 {"r": "jabber:iq:register"})
        if password:
            password = password[0].getContent()
        else:
            password = u""

        try:
            with open("regs/%s" % iq.get_from().bare(), "w") as f:
                f.write("%s\n%s" % (username, password))
        except Exception as ex:
            m = Message(from_jid=iq.get_to(),
                        to_jid=iq.get_from(),
                        stanza_type="chat",
                        body=u"Exception registering with username '%s': %s" %
                        (username, ex))
            self.stream.send(m)
            return 1

        m = Message(from_jid=iq.get_to(),
                    to_jid=iq.get_from(),
                    stanza_type="chat",
                    body=u"Registered with username '%s' and password '%s'" %
                    (username, password))
        self.stream.send(m)
        p = Presence(from_jid=iq.get_to(),
                     to_jid=iq.get_from(),
                     stanza_type="subscribe")
        self.stream.send(p)
        iq = iq.make_result_response()
        self.stream.send(iq)
        return 1
Exemplo n.º 2
0
 def test_presence_empty(self):
     p = Presence()
     self.check_presence_empty(p)
     node, doc = self.stanza_to_xml(p)
     self.check_presence_empty(Presence(node))
     node, doc = self.xml_to_xml(doc)
     self.check_presence_empty(Presence(node))
Exemplo n.º 3
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):
        """Initialize a `MucPresence` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `MucPresence` 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 `pyxmpp.stanza.Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `show`: `unicode`
            - `status`: `unicode`
            - `priority`: `unicode`
            - `error_cond`: `unicode`"""
        MucStanzaExt.__init__(self)
        Presence.__init__(self,xmlnode,from_jid=from_jid,to_jid=to_jid,
                stanza_type=stanza_type,stanza_id=stanza_id,
                show=show,status=status,priority=priority,
                error=error,error_cond=error_cond)
Exemplo n.º 4
0
def create_presence(jid):
    """ Creates a presence stanza (as described in XEP-0115)

    @param jid: bot's jabber ID
    """
    pres = Presence(from_jid=jid)

    c = pres.add_new_content('http://jabber.org/protocol/caps', 'c')
    c.setProp('node', NODE)

    ver = generate_ver(IDENT, FEAT)
    c.setProp('ver', ver)

    return pres
Exemplo n.º 5
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):
        """Initialize a `MucPresence` object.

        :Parameters:
            - `xmlnode`: XML node to_jid be wrapped into the `MucPresence` 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 `pyxmpp.stanza.Stanza`
            - `from_jid`: `JID`
            - `to_jid`: `JID`
            - `stanza_type`: `unicode`
            - `stanza_id`: `unicode`
            - `show`: `unicode`
            - `status`: `unicode`
            - `priority`: `unicode`
            - `error_cond`: `unicode`"""
        MucStanzaExt.__init__(self)
        Presence.__init__(self,
                          xmlnode,
                          from_jid=from_jid,
                          to_jid=to_jid,
                          stanza_type=stanza_type,
                          stanza_id=stanza_id,
                          show=show,
                          status=status,
                          priority=priority,
                          error=error,
                          error_cond=error_cond)
Exemplo n.º 6
0
def create_presence(jid):
    """ Creates a presence stanza (as described in XEP-0115)

    @param jid: bot's jabber ID
    """
    pres = Presence(from_jid=jid)

    c = pres.add_new_content('http://jabber.org/protocol/caps', 'c')
    c.setProp('node', NODE)

    ver = generate_ver(IDENT, FEAT)
    c.setProp('ver', ver)

    return pres
Exemplo n.º 7
0
 def join_raw_channel(self,stanza):
     self.raw_channel=1
     to=stanza.get_to()
     if to not in self.used_for:
         self.used_for.append(to)
     p=Presence(from_jid=to,to_jid=stanza.get_from())
     self.component.send(p)
Exemplo n.º 8
0
 def thread_run(self):
     clean_exit=1
     try:
         self.thread_loop()
     except:
         clean_exit=0
         self.__logger.exception("Exception cought:")
     self.lock.acquire()
     try:
         if not self.exited and self.socket:
             try:
                 if clean_exit and self.component.shutdown:
                     self._send("QUIT :JJIGW shutdown")
                 elif clean_exit and self.exit:
                     self._send("QUIT :%s" % (self.exit.encode(self.default_encoding,"replace")))
                 else:
                     self._send("QUIT :Internal JJIGW error")
             except socket.error:
                 pass
             self.exited=1
         if self.socket:
             try:
                 self.socket.close()
             except:
                 pass
             self.socket=None
         self.component.unregister_session(self)
     finally:
         self.lock.release()
     for j in self.used_for:
         p=Presence(from_jid=j,to_jid=self.jid,stanza_type="unavailable")
         self.component.send(p)
     self.used_for=[]
Exemplo n.º 9
0
    def subscription_requested(self, stanza):
        'A contact has requested to subscribe to your presence.'

        assert stanza.get_type() == 'subscribe'

        to_jid = stanza.get_from()
        if to_jid.domain in pref('digsby.guest.domains', ['guest.digsby.org']):
            from_jid = stanza.get_to()
            groups = jabber.jabber_util.xpath_eval(stanza.xmlnode, 'd:group',
                                                   {'d': "digsby:setgroup"})
            if groups:
                group = groups[0].getContent()
                item = RosterItem(node_or_jid=to_jid,
                                  subscription='none',
                                  name=None,
                                  groups=(group, ),
                                  ask=None)
                q = item.make_roster_push()
                self.send(q)
            pr2 = Presence(stanza_type='subscribe',
                           from_jid=from_jid,
                           to_jid=to_jid)
            self.send(pr2)
            self.send(stanza.make_accept_response())
            return True
        else:
            return jabber.protocol.subscription_requested(self, stanza)
Exemplo n.º 10
0
 def set_register(self, iq):
     to = iq.get_to()
     if to and to != self.jid:
         iq = iq.make_error_response("feature-not-implemented")
         self.stream.send(iq)
         return 1
     remove = iq.xpath_eval("r:query/r:remove", {"r": "jabber:iq:register"})
     if remove:
         m = Message(from_jid=iq.get_to(),
                     to_jid=iq.get_from(),
                     stanza_type="chat",
                     body=u"Unregistered")
         self.stream.send(m)
         p = Presence(from_jid=iq.get_to(),
                      to_jid=iq.get_from(),
                      stanza_type="unsubscribe")
         self.stream.send(p)
         p = Presence(from_jid=iq.get_to(),
                      to_jid=iq.get_from(),
                      stanza_type="unsubscribed")
         self.stream.send(p)
         return 1
     username = iq.xpath_eval("r:query/r:username",
                              {"r": "jabber:iq:register"})
     if username:
         username = username[0].getContent()
     else:
         username = u""
     password = iq.xpath_eval("r:query/r:password",
                              {"r": "jabber:iq:register"})
     if password:
         password = password[0].getContent()
     else:
         password = u""
     m = Message(from_jid=iq.get_to(),
                 to_jid=iq.get_from(),
                 stanza_type="chat",
                 body=u"Registered with username '%s' and password '%s'"
                 " (both ignored)" % (username, password))
     self.stream.send(m)
     p = Presence(from_jid=iq.get_to(),
                  to_jid=iq.get_from(),
                  stanza_type="subscribe")
     self.stream.send(p)
     iq = iq.make_result_response()
     self.stream.send(iq)
     return 1
Exemplo n.º 11
0
 def goaway():
     try:
         pres = Presence(stanza_type="unavailable",
                         status='Logged Out',
                         to_jid=self.jid)
         self.protocol.stream.send(pres)
     except (AttributeError, Exception):
         pass
Exemplo n.º 12
0
 def test_presence_full(self):
     p = Presence(from_jid=JID("[email protected]/res"),
                  to_jid=JID("*****@*****.**"),
                  stanza_type=None,
                  stanza_id=u"1",
                  show=u"away",
                  status=u"The Status",
                  priority="10")
     n = p.xmlnode.newChild(None, "payload", None)
     ns = n.newNs("http://pyxmpp.jajcus.net/xmlns/test", "t")
     n.setNs(ns)
     n.newChild(ns, "abc", None)
     self.check_presence_full(p)
     node, doc = self.stanza_to_xml(p)
     self.check_presence_full(Presence(node))
     xml = self.xml_to_xml(doc)
     self.check_presence_full(Presence(node))
Exemplo n.º 13
0
    def change_nick(self, new_nick):
        """
        Send a nick change request to the room.

        :Parameters:
            - `new_nick`: the new nickname requested.
        :Types:
            - `new_nick`: `unicode`
        """
        new_room_jid = JID(self.room_jid.node, self.room_jid.domain, new_nick)
        p = Presence(to_jid=new_room_jid)
        self.manager.stream.send(p)
Exemplo n.º 14
0
 def login(self,stanza):
     self.cond.acquire()
     try:
         if not self.ready:
             self.login_requests.append(stanza.copy())
             return
     finally:
         self.cond.release()
     to=stanza.get_to()
     if to not in self.used_for:
         self.used_for.append(to)
     fr=stanza.get_from()
     p=Presence(to_jid=fr,from_jid=to,status=stanza.get_status(),show=stanza.get_show())
     self.component.send(p)
Exemplo n.º 15
0
 def logout(self,stanza,send_response=1):
     to=stanza.get_to()
     if to not in self.used_for:
         self.__logger.debug("Unavailable presence sent with no matching available presence, ignoring it")
         return 0
     try:
         self.used_for.remove(to)
     except:
         pass
     if send_response:
         p=Presence(
             stanza_type="unavailable",
             to_jid=stanza.get_from(),
             from_jid=stanza.get_to()
             );
         self.component.send(p)
     if not self.used_for:
         self.disconnect(stanza.get_status())
         return 1
     else:
         return 0
Exemplo n.º 16
0
 def test_presence_full_from_xml(self):
     p = Presence(presence1_node)
     self.check_presence_full(p)
Exemplo n.º 17
0
 def free(self):
     """Free the data associated with this `MucPresence` object."""
     self.muc_free()
     Presence.free(self)
Exemplo n.º 18
0
 def test_presence_subscribe_from_xml(self):
     p = Presence(presence3_node)
     self.check_presence_subscribe(p)
Exemplo n.º 19
0
 def free(self):
     """Free the data associated with this `MucPresence` object."""
     self.muc_free()
     Presence.free(self)
Exemplo n.º 20
0
 def session_started(self):
     """Handle session started event. May be overriden in derived classes.
     This one requests the user's roster and sends the initial presence."""
     self.request_roster()
     p = Presence()
     self.stream.send(p)
Exemplo n.º 21
0
 def test_presence_empty_from_xml(self):
     p = Presence(presence2_node)
     self.check_presence_empty(p)