Changeset 120:9548851ca5ac for wokkel


Ignore:
Timestamp:
Oct 20, 2008, 9:54:28 PM (11 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@116
Message:

implement error conditions

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r119 r120  
    8989DEFER_TIMEOUT = 30 # basic timeout is 30 seconds
    9090
     91class JidMalformed(Exception):
     92    """
     93    A jid malformed error from the server.
     94
     95   
     96    """
     97    condition    = 'modify'
     98    mucCondition = 'jid-malformed'
     99   
     100class NotAuthorized(Exception):
     101    """
     102    """
     103    condition    = 'auth'
     104    mucCondition = 'not-authorized'
     105
     106class RegistrationRequired(Exception):
     107    """
     108    """
     109    condition    = 'auth'
     110    mucCondition = 'registration-required'
     111
     112
     113class Forbidden(Exception):
     114    """
     115    """
     116    condition    = 'auth'
     117    mucCondition = 'forbidden'
     118
     119
     120MUC_EXCEPTIONS = {
     121    'jid-malformed': JidMalformed,
     122    'forbidden': Forbidden,
     123    'not-authorized': NotAuthorized,
     124    'exception': Exception,
     125    }
     126
    91127class MUCError(error.StanzaError):
    92128    """
    93129    Exception with muc specific condition.
    94130    """
    95     def __init__(self, condition, mucCondition, feature=None, text=None):
     131    def __init__(self, condition, mucCondition, type='error', feature=None, text=None):
    96132        appCondition = domish.Element((NS_MUC, mucCondition))
    97133        if feature:
    98134            appCondition['feature'] = feature
    99135        error.StanzaError.__init__(self, condition,
     136                                         type=type,
    100137                                         text=text,
    101138                                         appCondition=appCondition)
     
    421458        # add muc elements
    422459        x = self.addElement('x', NS_MUC)
     460       
    423461        # add error
    424         self.addChild(error)
     462        e = error.getElement()
     463        self.addChild(e)
    425464       
    426465
     
    476515        # add an error hook here?
    477516        self._userLeavesRoom(room_jid)
     517       
     518    def _getExceptionFromPresence(self, prs):
     519        muc_condition = 'exception'
     520
     521        error = getattr(prs, 'error', None)
     522        if error is not None:
     523            for e in error.elements():
     524                muc_condition = e.name
     525
     526        return MUC_EXCEPTIONS[muc_condition]
    478527
    479528    def _userLeavesRoom(self, room_jid):
     
    580629        # check for errors
    581630        if prs.hasAttribute('type') and prs['type'] == 'error':           
    582             d.errback(prs)
     631            d.errback(self._getExceptionFromPresence(prs))
    583632        else:   
    584633            # change the state of the room
     
    867916        return iq.send()
    868917
    869     def getMemberList(self, room_jid):
    870         """ Get a member list from a room.
    871 
    872         @param room_jid: The room jabber/xmpp entity id for the requested member list.
    873         @type  room_jid: L{jid.JID}
    874 
    875         """
     918
     919    def _getAffiliationList(self, room_jid, affiliation):
    876920        iq = AffiliationRequest(self.xmlstream,
    877921                                method='get',
    878                                 affiliation='member',
     922                                affiliation=affiliation,
    879923                                )
    880924        iq['to'] = room_jid.full()
    881925        return iq.send()       
    882        
     926
     927
     928
     929    def getMemberList(self, room_jid):
     930        """ Get a member list from a room.
     931
     932        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     933        @type  room_jid: L{jid.JID}
     934
     935        """
     936        return self._getAffiliationList(room_jid, 'member')
     937
     938
     939    def getAdminList(self, room_jid):
     940        """ Get an admin list from a room.
     941
     942        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     943        @type  room_jid: L{jid.JID}
     944
     945        """
     946        return self._getAffiliationList(room_jid, 'admin')
     947
     948    def getBanList(self, room_jid):
     949        """ Get an outcast list from a room.
     950
     951        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     952        @type  room_jid: L{jid.JID}
     953
     954        """
     955        return self._getAffiliationList(room_jid, 'outcast')
     956
     957    def getOwnerList(self, room_jid):
     958        """ Get an owner list from a room.
     959
     960        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     961        @type  room_jid: L{jid.JID}
     962
     963        """
     964        return self._getAffiliationList(room_jid, 'owner')
     965
    883966
    884967    def getRegisterForm(self, room):
     
    892975        iq['to'] = room.userhost()
    893976        return iq.send()
     977
     978    def destroy(self, room_jid, reason=None):
     979        """ Destroy a room.
     980       
     981        @param room_jid: The room jabber/xmpp entity id.
     982        @type  room_jid: L{jid.JID}
     983       
     984        """
     985        def destroyed(iq):
     986            self._removeRoom(room_jid)
     987            return True
     988
     989        iq = OwnerRequest(self.xmlstream, method='set')
     990        d  = iq.query.addElement('destroy')
     991        d['jid'] = room_jid.userhost()
     992        if reason is not None:
     993            d.addElement('reason', None, reason)
     994
     995        return iq.send().addCallback(destroyed)
    894996
    895997    def subject(self, to, subject):
  • wokkel/test/test_muc.py

    r118 r120  
    158158        return d
    159159
     160   
    160161
    161162    def test_joinRoomForbidden(self):
     
    164165
    165166        def cb(error):
    166             self.failUnless(isinstance(error.value,muc.PresenceError), 'Wrong type')
    167             self.failUnless(error.value['type']=='error', 'Not an error returned')
     167           
     168            self.failUnless(error.value.mucCondition=='forbidden','Wrong muc condition')
     169
    168170           
    169171           
Note: See TracChangeset for help on using the changeset viewer.