Changeset 111:a51e5d87ded8


Ignore:
Timestamp:
Oct 3, 2008, 8:50:37 PM (13 years ago)
Author:
Christopher Zorn <tofu@…>
Branch:
wokkel-muc-client-support-24
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/branches/wokkel-muc-client-support-24@72
Message:

add leave room, register for a room and other things for re #24, still need admin and owner stuff, more tests and more docs

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r110 r111  
    3030NS_CONFIG   = NS + '#roomconfig'
    3131NS_REQUEST  = NS + '#request'
     32NS_REGISTER = NS + '#register'
    3233
    3334NS_DELAY    = 'urn:xmpp:delay'
     35NS_REQUEST  = 'jabber:iq:register'
    3436
    3537# ad hoc commands
     
    138140
    139141
     142class RegisterRequest(xmlstream.IQ):
     143    """
     144    Register room request.
     145
     146    @ivar namespace: Request namespace.
     147    @type namespace: C{str}
     148    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     149    @type method: C{str}
     150    """
     151
     152    def __init__(self, xs, method='get', fields=[]):
     153        xmlstream.IQ.__init__(self, xs, method)
     154        q = self.addElement((NS_REQUEST, 'query'))
     155        if method == 'set':
     156            # build data form
     157            form_type = 'submit'       
     158            form = data_form.Form(form_type, formNamespace=NS_REGISTER)
     159            q.addChild(form.toElement())       
     160           
     161            for f in fields:
     162                # create a field
     163                form.addField(f)
     164
    140165class GroupChat(domish.Element):
    141166    """
     
    177202       
    178203
    179 class BasicPresence(xmppim.Presence):
     204class BasicPresence(xmppim.AvailablePresence):
    180205    """
    181206    This behaves like an object providing L{domish.IElement}.
     
    183208    """
    184209
    185     def __init__(self, to=None, type=None):
    186         xmppim.Presence.__init__(self, to, type)
    187        
     210    def __init__(self, to=None, show=None, statuses=None):
     211        xmppim.AvailablePresence.__init__(self, to=to, show=show, statuses=statuses)
    188212        # add muc elements
    189213        x = self.addElement('x', NS)
     
    229253        self.addChild(form.toElement())           
    230254
    231 class PresenceError(BasicPresence):
     255class PresenceError(xmppim.Presence):
    232256    """
    233257    This behaves like an object providing L{domish.IElement}.
     
    236260
    237261    def __init__(self, error, to=None, frm=None):
    238         BasicPresence.__init__(self, to, type='error')
     262        xmppim.Presence.__init__(self, to, type='error')
    239263        if frm:
    240264            self['from'] = frm
     
    266290        return self.rooms.get(room_jid.full().lower())
    267291
     292    def _removeRoom(self, room_jid):
     293        if self.rooms.has_key(room_jid.full().lower()):
     294            del self.rooms[room_jid.full().lower()]
    268295
    269296    def _onXPresence(self, prs):
     
    306333            r = self._getRoom(room_jid)
    307334            if r is None:
    308                 raise
     335                raise Exception, 'Room Not Found'
    309336            r.state = 'joined'
    310337           
     
    316343            d.callback(r)
    317344
     345
     346    def _leftRoom(self, d, prs):
     347        """We have presence that says we joined a room.
     348        """
     349        room_jid = jid.internJID(prs['from'])
     350       
     351        # check for errors
     352        if prs.hasAttribute('type') and prs['type'] == 'error':           
     353            d.errback(prs)
     354        else:   
     355            # change the state of the room
     356            r = self._getRoom(room_jid)
     357            if r is None:
     358                raise Exception, 'Room Not Found'
     359            self._removeRoom(room_jid)
     360           
     361            d.callback(True)
     362
    318363    def receivedUserPresence(self, prs):
    319364        """User Presence has been received
     
    342387       
    343388
    344     def configure(self, room_jid, **kwargs):
     389    def configure(self, room_jid, fields=[]):
    345390        """Configure a room
    346391        """
    347         request = ConfigureRequest(self.xmlstream, method='set', **kwargs)
     392        request = ConfigureRequest(self.xmlstream, method='set', fields=fields)
    348393        request['to'] = room_jid
    349394       
     
    374419   
    375420
     421   
     422    def leave(self, room_jid):
     423        """
     424        """
     425        d = defer.Deferred()
     426
     427        self._getRoom(room_jid)
     428 
     429        p = xmppim.UnavailablePresence(to=r.entity_id)
     430        # p['from'] = self.jid.full()
     431        self.xmlstream.send(p)
     432
     433        # add observer for joining the room
     434        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and type='unavailable']" % (r.entity_id.full()),
     435                                          self._leftRoom, 1, d)
     436
     437        return d
     438   
     439
     440   
     441
    376442    def groupChat(self, to, message, children=None):
    377443        """Send a groupchat message
     
    386452
    387453   
     454    def register(self, to, fields=[]):
     455        iq = RegisterRequest(self.xmlstream, method='set', fields=fields)
     456        iq['to'] = to
     457        return iq.send()
     458
     459    def getRegisterForm(self, to):
     460        iq = RegisterRequest(self.xmlstream)
     461        iq['to'] = to
     462        return iq.send()
     463
    388464    def subject(self, to, subject):
    389465        """
  • wokkel/test/test_muc.py

    r110 r111  
    227227       
    228228    def test_privateMessage(self):
     229       
    229230        self.fail('Not Implemented')
    230231
    231232    def test_register(self):
    232         self.fail('Not Implemented')
     233        """Test client registering with a room. http://xmpp.org/extensions/xep-0045.html#register
     234
     235        """
     236       
     237        def cb(iq):
     238            # check for a result
     239            self.failUnless(iq['type']=='result', 'We did not get a result')
     240       
     241        d = self.protocol.register(self.room_jid.userhost())
     242        d.addCallback(cb)
     243
     244        iq = self.stub.output[-1]
     245       
     246        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request')
     247       
     248        response = toResponse(iq, 'result')
     249        self.stub.send(response)
     250        return d
    233251
    234252    def test_voice(self):
    235        
     253        """
     254        """
    236255        self.protocol.voice(self.room_jid.userhost())
    237256
    238257        m = self.stub.output[-1]
    239         self.failUnless(m.name=='message', "Need to be message stanza")
    240         self.failUnless(getattr(m, 'x', None), 'No muc x element')
    241 
     258       
    242259        self.failUnless(xpath.matches("/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_REQUEST,), m), 'Invalid voice message stanza')
    243260
    244261
    245262    def test_roomConfigure(self):
    246 
    247         self.fail('Not Implemented')
    248        
    249 
     263        """
     264        """
     265
     266        def cb(iq):
     267            self.failUnless(iq['type']=='result', 'Not a result')
     268           
     269
     270        fields = []
     271
     272        fields.append(data_form.Field(label='Natural-Language Room Name',
     273                                      var='muc#roomconfig_roomname',
     274                                      value=self.test_room))
     275       
     276        d = self.protocol.configure(self.room_jid.userhost(), fields)
     277        d.addCallback(cb)
     278
     279        iq = self.stub.output[-1]
     280        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/x"% (muc.NS_OWNER,), iq), 'Bad configure request')
     281       
     282        response = toResponse(iq, 'result')
     283        self.stub.send(response)
     284        return d
     285
     286
Note: See TracChangeset for help on using the changeset viewer.