Changeset 108:50e3c368f2f3


Ignore:
Timestamp:
Sep 30, 2008, 7:08:45 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@69
Message:

add basic errors and disco stuff, still need iq and message parts re #24

Location:
wokkel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/disco.py

    r31 r108  
    1212
    1313from twisted.internet import defer
    14 from twisted.words.protocols.jabber import error, jid
     14from twisted.words.protocols.jabber import error, jid, xmlstream
    1515from twisted.words.xish import domish
    1616
     
    6262        if name:
    6363            self['name'] = name
     64
     65
     66class DiscoRequest(xmlstream.IQ):
     67    """
     68    Disco request.
     69
     70    @ivar namespace: Request namespace.
     71    @type namespace: C{str}
     72    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     73    @type method: C{str}
     74    """
     75
     76    def __init__(self, xs, namespace=NS, method='get'):
     77        xmlstream.IQ.__init__(self, xs, method)
     78        self.addElement((namespace, 'query'))
     79
    6480
    6581
  • wokkel/muc.py

    r107 r108  
    1919from wokkel import disco, data_form, shim, xmppim
    2020from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
    21 from wokkel.iwokkel import IMUCClient, IMUCService
     21from wokkel.iwokkel import IMUCClient
    2222
    2323# Multi User Chat namespaces
    24 NS_MUC          = 'http://jabber.org/protocol/muc'
    25 NS_MUC_USER     = NS_MUC + '#user'
    26 NS_MUC_ADMIN    = NS_MUC + '#admin'
    27 NS_MUC_OWNER    = NS_MUC + '#owner'
    28 NS_MUC_ROOMINFO = NS_MUC + '#roominfo'
    29 NS_MUC_CONFIG   = NS_MUC + '#roomconfig'
     24NS          = 'http://jabber.org/protocol/muc'
     25NS_USER     = NS + '#user'
     26NS_ADMIN    = NS + '#admin'
     27NS_OWNER    = NS + '#owner'
     28NS_ROOMINFO = NS + '#roominfo'
     29NS_CONFIG   = NS + '#roomconfig'
    3030
    3131# ad hoc commands
     
    4747IQ_COMMAND   = IQ+'/command'
    4848
    49 MUC_ADMIN = IQ_QUERY+'[@xmlns="' + NS_MUC_ADMIN + '"]'
    50 MUC_OWNER = IQ_QUERY+'[@xmlns="' + NS_MUC_OWNER + '"]'
     49MUC_ADMIN = IQ_QUERY+'[@xmlns="' + NS_ADMIN + '"]'
     50MUC_OWNER = IQ_QUERY+'[@xmlns="' + NS_OWNER + '"]'
    5151
    5252MUC_AO = MUC_ADMIN + '|' + MUC_OWNER
     
    6969    """
    7070    def __init__(self, condition, mucCondition, feature=None, text=None):
    71         appCondition = domish.Element((NS_MUC_ERRORS, mucCondition))
     71        appCondition = domish.Element((NS, mucCondition))
    7272        if feature:
    7373            appCondition['feature'] = feature
     
    9494
    9595
     96
    9697class Room(object):
    9798    """
     
    124125       
    125126        # add muc elements
    126         x = self.addElement('x', NS_MUC)
     127        x = self.addElement('x', NS)
    127128
    128129
     
    138139            self['from'] = frm
    139140        # add muc elements
    140         x = self.addElement('x', NS_MUC_USER)
     141        x = self.addElement('x', NS_USER)
    141142        if affiliation:
    142143            x['affiliation'] = affiliation
     
    151152    """
    152153
    153     def __init__(self, error, to=None):
     154    def __init__(self, error, to=None, frm=None):
    154155        BasicPresence.__init__(self, to, type='error')
    155        
     156        if frm:
     157            self['from'] = frm
    156158        # add muc elements
    157         x = self.addElement('x', NS_MUC)
     159        x = self.addElement('x', NS)
    158160        # add error
    159161        self.addChild(error)
     
    170172    def connectionInitialized(self):
    171173        self.rooms = {}
    172         self.xmlstream.addObserver(PRESENCE, self._onPresence)
    173         self.xmlstream.addObserver(GROUPCHAT, self._onGroupChat)
     174       
    174175
    175176    def _setRoom(self, room):
     
    179180        return self.rooms.get(room_jid.full().lower())
    180181
    181     def _onGroupChat(self, msg):
    182         """handle groupchat message stanzas
    183         """
    184 
    185 
    186     def _onPresence(self, prs):
    187         """handle groupchat presence
    188         """
    189         x = getattr(prs, 'x', None)
    190         if x.uri == NS_MUC_USER:
    191             self.userPresence(prs)
    192182
    193183
     
    196186        """
    197187        room_jid = jid.internJID(prs['from'])
    198         print type(prs)
    199         print type(d)
     188       
    200189        # check for errors
    201190        if prs.hasAttribute('type') and prs['type'] == 'error':           
    202             print room_jid.full()
    203         # change the state of the room
    204         r = self._getRoom(room_jid)
    205         r.state = 'joined'
    206         d.callback(room_jid, r)
     191            d.errback(prs)
     192        else:   
     193            # change the state of the room
     194            r = self._getRoom(room_jid)
     195            r.state = 'joined'
     196            d.callback(r)
    207197
    208198    def userPresence(self, prs):
     
    211201        pass
    212202       
    213     def joinRoom(self, server, room, nick):
     203
     204    def _cbDisco(self, iq):
     205        # grab query
     206       
     207        return iq.query
     208       
     209    def disco(self, entity, type='info'):
     210        """Send disco queries to a XMPP entity
     211        """
     212
     213        iq = disco.DiscoRequest(self.xmlstream, disco.NS_INFO, 'get')
     214        iq['to'] = entity
     215
     216        return iq.send().addCallback(self._cbDisco)
     217       
     218
     219    def join(self, server, room, nick):
    214220        """
    215221        """
     
    224230        # add observer for joining the room
    225231        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    226                                           self._joinedRoom, d)
     232                                          self._joinedRoom, 1, d)
    227233
    228234        return d
  • wokkel/test/test_muc.py

    r107 r108  
    1414from twisted.words.protocols.jabber.jid import JID
    1515
    16 from wokkel import data_form, iwokkel, muc, shim
     16from wokkel import data_form, iwokkel, muc, shim, disco
    1717from wokkel.generic import parseXml
    1818from wokkel.test.helpers import XmlStreamStub
     
    5858
    5959
    60     def test_presence(self):
    61         """Test receiving room presence
     60
     61    def test_discoServerSupport(self):
     62        """Test for disco support from a server.
    6263        """
    63         p = muc.UserPresence()
     64        test_srv = 'shakespeare.lit'
    6465
    65         def userPresence(prs):
    66             self.failUnless(len(prs.children)==1, 'Not enough children')
    67             self.failUnless(getattr(prs,'x',None), 'No x element')
     66        def cb(query):
     67            # check namespace
     68            self.failUnless(query.uri==disco.NS_INFO, 'Wrong namespace')
    6869           
    6970
    70         d, self.protocol.userPresence = calledAsync(userPresence)
    71         self.stub.send(p)
     71        d = self.protocol.disco(test_srv)
     72        d.addCallback(cb)
     73
     74        iq = self.stub.output[-1]
     75       
     76        # send back a response
     77        response = toResponse(iq, 'result')
     78        response.addElement('query', disco.NS_INFO)
     79        # need to add information to response
     80        response.query.addChild(disco.DiscoFeature(muc.NS))
     81        response.query.addChild(disco.DiscoIdentity(category='conference',
     82                                                    name='Macbeth Chat Service',
     83                                                    type='text'))
     84       
     85        self.stub.send(response)
    7286        return d
     87       
    7388
     89       
    7490    def test_joinRoom(self):
    7591        """Test joining a room
     
    7995        test_nick = 'Nick'
    8096
    81         # p = muc.BasicPresenc(to=)
    82 
    8397        def cb(room):
    8498            self.assertEquals(test_room, room.name)
    8599
    86         d = self.protocol.joinRoom(test_srv, test_room, test_nick)
     100        d = self.protocol.join(test_srv, test_room, test_nick)
    87101        d.addCallback(cb)
     102
     103        prs = self.stub.output[-1]
     104        self.failUnless(prs.name=='presence', "Need to be presence")
     105        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
     106
     107        # send back user presence, they joined       
     108        response = muc.UserPresence(frm=test_room+'@'+test_srv+'/'+test_nick)
     109        self.stub.send(response)
     110        return d
     111
     112
     113    def test_joinRoomForbidden(self):
     114        """Test joining a room and getting an error
     115        """
     116        test_room = 'test'
     117        test_srv  = 'conference.example.org'
     118        test_nick = 'Nick'
     119
     120        # p = muc.BasicPresenc(to=)
     121
     122        def cb(error):
     123            self.failUnless(isinstance(error.value,muc.PresenceError), 'Wrong type')
     124            self.failUnless(error.value['type']=='error', 'Not an error returned')
     125           
     126           
     127        d = self.protocol.join(test_srv, test_room, test_nick)
     128        d.addBoth(cb)
    88129
    89130        prs = self.stub.output[-1]
     
    92133        # send back user presence, they joined
    93134       
    94         response = muc.UserPresence()
    95         print response.toXml()
     135        response = muc.PresenceError(error=muc.MUCError('auth',
     136                                                        'forbidden'
     137                                                        ),
     138                                     frm=test_room+'@'+test_srv+'/'+test_nick)
    96139        self.stub.send(response)
    97         return d
    98 
    99 class MucServiceTest(unittest.TestCase):
    100     """
    101     Tests for L{muc.MUCService}.
    102     """
    103 
    104     def setUp(self):
    105         self.service = muc.MUCService()
    106 
    107     def handleRequest(self, xml):
    108         """
    109         Find a handler and call it directly
    110         """
    111         handler = None
    112         iq = parseXml(xml)
    113         for queryString, method in self.service.iqHandlers.iteritems():
    114             if xpath.internQuery(queryString).matches(iq):
    115                 handler = getattr(self.service, method)
    116 
    117         if handler:
    118             d = defer.maybeDeferred(handler, iq)
    119         else:
    120             d = defer.fail(NotImplementedError())
    121 
    122         return d
    123 
    124 
    125     def test_interface(self):
    126         """
    127         Do instances of L{muc.MucService} provide L{iwokkel.IMucService}?
    128         """
    129         verify.verifyObject(iwokkel.IMUCService, self.service)
    130 
    131 
     140        return d       
Note: See TracChangeset for help on using the changeset viewer.