Changeset 121:e74ad91e5c6f for wokkel


Ignore:
Timestamp:
Oct 20, 2008, 10:28:22 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@117
Message:

add some more exeptions and implement history options

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r120 r121  
    117117    mucCondition = 'forbidden'
    118118
     119class Conflict(Exception):
     120    """
     121    """
     122    condition    = 'cancel'
     123    mucCondition = 'conflict'
     124
     125class NotFound(Exception):
     126    """
     127    """
     128    condition    = 'cancel'
     129    mucCondition = 'not-found'
     130
     131class ServiceUnavailable(Exception):
     132    """
     133    """
     134    condition    = 'wait'
     135    mucCondition = 'service-unavailable'
     136
     137
    119138
    120139MUC_EXCEPTIONS = {
     
    123142    'not-authorized': NotAuthorized,
    124143    'exception': Exception,
     144    'conflict': Conflict,
     145    'service-unavailable': ServiceUnavailable,
     146    'not-found': NotFound,
    125147    }
    126148
     
    316338            d['from'] = h_frm
    317339
     340class HistoryOptions(object):
     341    """A history configuration object.
     342
     343    @ivar maxchars: Limit the total number of characters in the history to "X"
     344                    (where the character count is the characters of the complete XML stanzas, not only their XML character data).
     345    @itype maxchars: L{int}
     346               
     347    @ivar  maxstanzas:  Limit the total number of messages in the history to "X".
     348    @itype mazstanzas: L{int}
     349
     350    @ivar  seconds: Send only the messages received in the last "X" seconds.
     351    @itype seconds: L{int}
     352
     353    @ivar  since: Send only the messages received since the datetime specified (which MUST conform to the DateTime profile specified in XMPP Date and Time Profiles [14]).
     354    @itype since: L{datetime.datetime}
     355
     356    """
     357    attributes = ['maxchars', 'maxstanzas', 'seconds', 'since']
     358
     359    def __init__(self, maxchars=None, maxstanzas=None, seconds=None, since=None):
     360        self.maxchars   = maxchars
     361        self.maxstanzas = maxstanzas
     362        self.seconds    = seconds
     363        self.since      = since
     364
     365    def toElement(self):
     366        h = domish.Element((None, 'history'))
     367        for key in self.attributes:
     368            a = getattr(self, key, a)
     369            if a is not None:
     370                h[key] = str(a)
     371       
     372        return h
     373
    318374class User(object):
    319375    """
     
    516572        self._userLeavesRoom(room_jid)
    517573       
    518     def _getExceptionFromPresence(self, prs):
     574    def _getExceptionFromElement(self, stanza):
    519575        muc_condition = 'exception'
    520576
    521         error = getattr(prs, 'error', None)
     577        error = getattr(stanza, 'error', None)
    522578        if error is not None:
    523579            for e in error.elements():
     
    629685        # check for errors
    630686        if prs.hasAttribute('type') and prs['type'] == 'error':           
    631             d.errback(self._getExceptionFromPresence(prs))
     687            d.errback(self._getExceptionFromElement(prs))
    632688        else:   
    633689            # change the state of the room
    634690            r = self._getRoom(room_jid)
    635691            if r is None:
    636                 raise Exception, 'Room Not Found'
     692                raise NotFound
    637693            r.state = 'joined'
    638694           
     
    652708        # check for errors
    653709        if prs.hasAttribute('type') and prs['type'] == 'error':           
    654             d.errback(prs)
     710            d.errback(self._getExceptionFromElement(prs))
    655711        else:   
    656712            # change the state of the room
    657713            r = self._getRoom(room_jid)
    658714            if r is None:
    659                 raise Exception, 'Room Not Found'
     715                raise NotFound
    660716            self._removeRoom(room_jid)
    661717           
     
    764820
    765821
    766     def join(self, server, room, nick):
     822    def join(self, server, room, nick, history = None):
    767823        """ Join a MUC room by sending presence to it. Returns a defered that is called when
    768824        the entity is in the room or an error has occurred.
     
    777833        @type  nick: L{unicode}
    778834       
     835        @param history: The maximum number of history stanzas you would like.
     836
    779837        """
    780838        r = Room(room, server, nick, state='joining')
     
    782840 
    783841        p = BasicPresence(to=r.entity_id)
     842        if history is not None:
     843            p.x.addChild(history.toElement())
     844
    784845        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    785846
     
    815876        r = self._getRoom(room_jid)
    816877        if r is None:
    817             raise Exception, 'Room not found'
     878            raise NotFound
    818879        r.nick = new_nick # change the nick
    819880        # create presence
     
    862923        r = self._getRoom(room_jid)
    863924        if r is None:
    864             raise Exception, 'Room not found'
     925            raise NotFound
    865926
    866927        p = BasicPresence(to=r.entityId())
Note: See TracChangeset for help on using the changeset viewer.