Ignore:
Timestamp:
Jan 8, 2012, 9:26:02 AM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Use symbolic constants instead of integers MUC status code.

Instead of using normal constant values for representing MUC status codes,
UserPresence now uses twisted.python.constants to define these. This
makes code better to understand and helps debugging.

If a user presence includes one or more status codes, they are stored in the
mucStatuses attribute, as an instance of Statuses. This replaces the
former statusCodes attribute.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r156 r160  
    88
    99This protocol is specified in
    10 U{XEP-0045<http://www.xmpp.org/extensions/xep-0045.html>}.
     10U{XEP-0045<http://xmpp.org/extensions/xep-0045.html>}.
    1111"""
    1212from dateutil.tz import tzutc
     
    1818from twisted.words.xish import domish
    1919
    20 from wokkel import data_form, generic, xmppim
     20from wokkel import data_form, generic, iwokkel, xmppim
     21from wokkel.compat import Values, ValueConstant
    2122from wokkel.delay import Delay, DelayMixin
    2223from wokkel.subprotocols import XMPPHandler
     
    4142
    4243DEFER_TIMEOUT = 30 # basic timeout is 30 seconds
     44
     45class STATUS_CODE(Values):
     46    REALJID_PUBLIC = ValueConstant(100)
     47    AFFILIATION_CHANGED = ValueConstant(101)
     48    UNAVAILABLE_SHOWN = ValueConstant(102)
     49    UNAVAILABLE_NOT_SHOWN = ValueConstant(103)
     50    CONFIGURATION_CHANGED = ValueConstant(104)
     51    SELF_PRESENCE = ValueConstant(110)
     52    LOGGING_ENABLED = ValueConstant(170)
     53    LOGGING_DISABLED = ValueConstant(171)
     54    NON_ANONYMOUS = ValueConstant(172)
     55    SEMI_ANONYMOUS = ValueConstant(173)
     56    FULLY_ANONYMOUS = ValueConstant(174)
     57    ROOM_CREATED = ValueConstant(201)
     58    NICK_ASSIGNED = ValueConstant(210)
     59    BANNED = ValueConstant(301)
     60    NEW_NICK = ValueConstant(303)
     61    KICKED = ValueConstant(307)
     62    REMOVED_AFFILIATION = ValueConstant(321)
     63    REMOVED_MEMBERSHIP = ValueConstant(322)
     64    REMOVED_SHUTDOWN = ValueConstant(332)
     65
     66
     67class Statuses(set):
     68    """
     69    Container of MUC status conditions.
     70
     71    This is currently implemented as a set of constant values from
     72    L{STATUS_CODE}. Instances of this class provide L{IMUCStatuses}, that
     73    defines the supported operations. Even though this class currently derives
     74    from C{set}, future versions might not. This provides an upgrade path to
     75    cater for extensible status conditions, as defined in
     76    U{XEP-0306<http://xmpp.org/extensions/xep-0306.html>}.
     77    """
     78    implements(iwokkel.IMUCStatuses)
    4379
    4480
     
    354390    """
    355391    Availability presence sent from MUC service to client.
     392
     393    @ivar affiliation: Affiliation of the entity to the room.
     394    @type affiliation: C{unicode}
     395
     396    @ivar role: Role of the entity in the room.
     397    @type role: C{unicode}
     398
     399    @ivar entity: The real JID of the entity this presence is from.
     400    @type entity: L{jid.JID}
     401
     402    @ivar mucStatuses: Set of one or more status codes from L{STATUS_CODE}.
     403        See L{Statuses} for usage notes.
     404    @type mucStatuses: L{Statuses}
     405
     406    @ivar nick: The nick name of the entity in the room.
     407    @type nick: C{unicode}
    356408    """
    357409
     
    361413    nick = None
    362414
    363     statusCodes = None
     415    mucStatuses = None
    364416
    365417    childParsers = {(NS_MUC_USER, 'x'): '_childParser_mucUser'}
    366418
     419    def __init__(self, *args, **kwargs):
     420        self.mucStatuses = Statuses()
     421        xmppim.AvailabilityPresence.__init__(self, *args, **kwargs)
     422
     423
    367424    def _childParser_mucUser(self, element):
    368         statusCodes = set()
    369 
     425        """
     426        Parse the MUC user extension element.
     427        """
    370428        for child in element.elements():
    371429            if child.uri != NS_MUC_USER:
     
    374432            elif child.name == 'status':
    375433                try:
    376                     statusCode = int(child.getAttribute('code'))
     434                    value = int(child.getAttribute('code'))
     435                    statusCode = STATUS_CODE.lookupByValue(value)
    377436                except (TypeError, ValueError):
    378437                    continue
    379438
    380                 statusCodes.add(statusCode)
     439                self.mucStatuses.add(statusCode)
    381440
    382441            elif child.name == 'item':
     
    393452            # TODO: destroy
    394453
    395         if statusCodes:
    396             self.statusCodes = statusCodes
    397454
    398455
Note: See TracChangeset for help on using the changeset viewer.