Changeset 112:be0b126081f2


Ignore:
Timestamp:
Oct 6, 2008, 10:56:52 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@76
Message:

all tests pass, needs comments, also need to get the entire xep in the tests

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r111 r112  
    121121    Configure MUC room request.
    122122
    123     @ivar namespace: Request namespace.
    124     @type namespace: C{str}
    125123    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    126124    @type method: C{str}
     
    144142    Register room request.
    145143
    146     @ivar namespace: Request namespace.
    147     @type namespace: C{str}
    148144    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    149145    @type method: C{str}
     146
    150147    """
    151148
     
    162159                # create a field
    163160                form.addField(f)
     161
     162
     163class AffiliationRequest(xmlstream.IQ):
     164    """
     165    Register room request.
     166
     167    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     168    @type method: C{str}
     169
     170    @ivar affiliation: The affiliation type to send to room.
     171    @type affiliation: C{str}
     172
     173    """
     174
     175    def __init__(self, xs, method='get', affiliation='none', a_jid=None, reason=None):
     176        xmlstream.IQ.__init__(self, xs, method)
     177       
     178        q = self.addElement((NS_ADMIN, 'query'))
     179        i = q.addElement('item')
     180
     181        i['affiliation'] = affiliation
     182        if a_jid:
     183            i['jid'] = a_jid.full()
     184           
     185        if reason:
     186            i.addElement('reason', None, reason)
     187
     188           
     189       
    164190
    165191class GroupChat(domish.Element):
     
    178204        if subject:
    179205            self.addElement('subject',None, subject)
    180            
    181 
     206
     207
     208class PrivateChat(domish.Element):
     209    """
     210    """
     211    def __init__(self, to, body=None, frm=None):
     212        """To needs to be a string
     213        """
     214        domish.Element.__init__(self, (None, 'message'))
     215        self['type'] = 'chat'
     216        self['to']   = to
     217        if frm:
     218            self['from'] = frm
     219        if body:
     220            self.addElement('body',None, body)
     221           
     222class InviteMessage(PrivateChat):
     223    def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None):
     224        PrivateChat.__init__(self, to, body=body, frm=frm)
     225        del self['type'] # remove type
     226        x = self.addElement('x', NS_USER)
     227        invite = x.addElement('invite')
     228        if full_jid:
     229            invite['to'] = full_jid
     230        if reason:
     231            invite.addElement('reason', None, reason)
     232        if password:
     233            invite.addElement('password', None, password)
     234
     235class HistoryMessage(GroupChat):
     236    """
     237    """
     238    def __init__(self, to, stamp, body=None, subject=None, frm=None, h_frm=None):
     239        GroupChat.__init__(self, to, body=body, subject=subject, frm=frm)
     240        d = self.addElement('delay', NS_DELAY)
     241        d['stamp'] = stamp
     242        if h_frm:
     243            d['from'] = h_frm
    182244
    183245class Room(object):
     
    304366        """
    305367        """
    306         self.receivedGroupChat(msg)
    307 
     368        delay = getattr(msg, 'delay', None)
     369        if delay is None:
     370            self.receivedGroupChat(msg)
     371        else:
     372            self.receivedHistory(msg)
    308373
    309374
     
    372437        pass
    373438
     439
     440    def receivedHistory(self, msg):
     441        """
     442        """
     443        pass
     444
     445
    374446    def _cbDisco(self, iq):
    375447        # grab query
     
    425497        d = defer.Deferred()
    426498
    427         self._getRoom(room_jid)
     499        r = self._getRoom(room_jid)
    428500 
    429501        p = xmppim.UnavailablePresence(to=r.entity_id)
     
    432504
    433505        # add observer for joining the room
    434         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and type='unavailable']" % (r.entity_id.full()),
     506        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()),
    435507                                          self._leftRoom, 1, d)
    436508
     
    440512   
    441513
    442     def groupChat(self, to, message, children=None):
    443         """Send a groupchat message
    444         """
    445         msg = GroupChat(to, body=message)
     514    def _sendMessage(self, msg, children=None):
    446515
    447516        if children:
     
    451520        self.xmlstream.send(msg)
    452521
     522    def groupChat(self, to, message, children=None):
     523        """Send a groupchat message
     524        """
     525        msg = GroupChat(to, body=message)
     526       
     527        self._sendMessage(msg, children=children)
     528
     529    def chat(self, to, message, children=None):
     530        msg = PrivateChat(to, body=message)
     531
     532        self._sendMessage(msg, children=children)
     533       
     534    def invite(self, to, reason=None, full_jid=None):
     535        msg = InviteMessage(to, reason=reason, full_jid=full_jid)
     536        self._sendMessage(msg)
     537
     538
     539    def password(self, to, password):
     540        p = PasswordPresence(to, password)
     541
     542        self.xmlstream.send(p)
    453543   
    454544    def register(self, to, fields=[]):
     
    491581            self.xmlstream.send(m)
    492582
     583    def ban(self, to, ban_jid, frm, reason=None):
     584       
     585        iq = AffiliationRequest(self.xmlstream,
     586                                method='set',
     587                                affiliation='outcast',
     588                                a_jid=ban_jid,
     589                                reason=reason)
     590        iq['to'] = to.userhost() # this is a room jid, only send to room
     591        iq['from'] = frm.full()
     592        return iq.send()
     593
     594
     595    def kick(self, to, kick_jid, frm, reason=None):
     596       
     597        iq = AffiliationRequest(self.xmlstream,
     598                                method='set',
     599                                a_jid=kick_jid,
     600                                reason=reason)
     601        iq['to'] = to.userhost() # this is a room jid, only send to room
     602        iq['from'] = frm.full()
     603        return iq.send()
  • wokkel/test/test_muc.py

    r111 r112  
    5555        self.room_jid = JID(self.test_room+'@'+self.test_srv+'/'+self.test_nick)
    5656
     57        self.user_jid = JID('test@jabber.org/Testing')
     58
    5759    def test_interface(self):
    5860        """
     
    167169
    168170    def test_partRoom(self):
    169         self.fail('Not Implemented')
     171       
     172        def cb(left):
     173            self.failUnless(left, 'did not leave room')
     174
     175
     176        d = self.protocol.leave(self.room_jid)
     177        d.addCallback(cb)
     178
     179        prs = self.stub.output[-1]
     180       
     181        self.failUnless(prs['type']=='unavailable', 'Unavailable is not being sent')
     182       
     183        response = prs
     184        response['from'] = response['to']
     185        response['to'] = 'test@jabber.org'
     186
     187        self.stub.send(response)
     188        return d
    170189       
    171190
    172191    def test_ban(self):
    173192       
    174         self.fail('Not Implemented')
     193        banned = JID('ban@jabber.org/TroubleMakger')
     194        def cb(banned):
     195            self.failUnless(banned, 'Did not ban user')
     196
     197           
     198        d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam')
     199        d.addCallback(cb)
     200
     201        iq = self.stub.output[-1]
     202       
     203        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza')
     204
     205        response = toResponse(iq, 'result')
     206
     207        self.stub.send(response)
     208
     209        return d
     210
    175211
    176212    def test_kick(self):
     213
     214        kicked = JID('kick@jabber.org/TroubleMakger')
     215        def cb(kicked):
     216            self.failUnless(kicked, 'Did not kick user')
     217
     218           
     219        d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam')
     220        d.addCallback(cb)
     221
     222        iq = self.stub.output[-1]
     223       
     224        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza')
     225
     226        response = toResponse(iq, 'result')
     227
     228        self.stub.send(response)
     229
     230        return d
     231
    177232        self.fail('Not Implemented')
    178233       
     
    182237        """
    183238       
    184        
    185         self.fail('Not Implemented')
     239        self.protocol.password(self.room_jid, 'secret')
     240       
     241        prs = self.stub.output[-1]
     242       
     243        self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza')
     244
    186245
    187246    def test_history(self):
    188        
    189         self.fail('Not Implemented')
     247        """Test receiving history on room join.
     248        """
     249        m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test')
     250       
     251        def roomHistory(h):
     252            self.failUnless(getattr(h,'delay',None), 'No delay element')
     253                   
     254
     255        d, self.protocol.receivedHistory = calledAsync(roomHistory)
     256        self.stub.send(m)
     257        return d
    190258
    191259
     
    223291
    224292    def test_invite(self):
    225         self.fail('Not Implemented')
     293        other_jid = 'test@jabber.org'
     294
     295        self.protocol.invite(other_jid, 'This is a test')
     296
     297        msg = self.stub.output[-1]
     298
     299        self.failUnless(xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid,), msg), 'Wrong message type')
     300
    226301
    227302       
    228303    def test_privateMessage(self):
    229        
    230         self.fail('Not Implemented')
     304        """Test sending private messages to muc entities.
     305        """
     306        other_nick = self.room_jid.userhost()+'/OtherNick'
     307
     308        self.protocol.chat(other_nick, 'This is a test')
     309
     310        msg = self.stub.output[-1]
     311
     312        self.failUnless(xpath.matches("/message[@type='chat' and @to='%s']/body" % (other_nick,), msg), 'Wrong message type')
     313
    231314
    232315    def test_register(self):
Note: See TracChangeset for help on using the changeset viewer.