Changeset 130:45c3ce4fe4eb for wokkel


Ignore:
Timestamp:
Jun 9, 2009, 7:33:41 PM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Another round of cleanups, renames. Also removed disco support.

  • Whitespace and other PEP-8 cleanups
  • Naming changes to clarify and adhere to Twisted style guide.
  • Removed disco method, should use DiscoClientProtocol.requestInfo
  • Redid invite code, some of the parameters were actually not optional, and passwords should be sent by the service, not the client.
Location:
wokkel
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • wokkel/disco.py

    r128 r130  
    409409
    410410
    411 class DiscoRequest(xmlstream.IQ):
    412     """
    413     Disco request.
    414 
    415     @ivar namespace: Request namespace.
    416     @type namespace: C{str}
    417     @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    418     @type method: C{str}
    419     """
    420 
    421     def __init__(self, xs, namespace=NS, method='get'):
    422         xmlstream.IQ.__init__(self, xs, method)
    423         self.addElement((namespace, 'query'))
    424 
    425 
    426 
    427411class DiscoHandler(XMPPHandler, IQHandlerMixin):
    428412    """
  • wokkel/iwokkel.py

    r128 r130  
    799799        """
    800800
    801    
    802     def disco(entity, type='info'):
    803         """Send disco queries to a XMPP entity
    804 
    805         @param entity: A XMPP entity to send the disco request to.
    806         @type entity: C{unicode}
    807 
    808         @param type: Optional argument to determine the type of discovery being done.
    809         @type type: C{str}
    810 
    811         """
    812 
    813        
    814801
    815802    def configure(room_jid, fields=[]):
  • wokkel/muc.py

    r129 r130  
    1818from twisted.words.xish import domish
    1919
    20 from wokkel import disco, data_form, xmppim
     20from wokkel import data_form, xmppim
    2121from wokkel.subprotocols import XMPPHandler
    2222from wokkel.iwokkel import IMUCClient
    2323
    2424# Multi User Chat namespaces
    25 NS_MUC          = 'http://jabber.org/protocol/muc'
    26 NS_MUC_USER     = NS_MUC + '#user'
    27 NS_MUC_ADMIN    = NS_MUC + '#admin'
    28 NS_MUC_OWNER    = NS_MUC + '#owner'
     25NS_MUC = 'http://jabber.org/protocol/muc'
     26NS_MUC_USER = NS_MUC + '#user'
     27NS_MUC_ADMIN = NS_MUC + '#admin'
     28NS_MUC_OWNER = NS_MUC + '#owner'
    2929NS_MUC_ROOMINFO = NS_MUC + '#roominfo'
    30 NS_MUC_CONFIG   = NS_MUC + '#roomconfig'
    31 NS_MUC_REQUEST  = NS_MUC + '#request'
     30NS_MUC_CONFIG = NS_MUC + '#roomconfig'
     31NS_MUC_REQUEST = NS_MUC + '#request'
    3232NS_MUC_REGISTER = NS_MUC + '#register'
    3333
    34 NS_DELAY        = 'urn:xmpp:delay'
     34NS_DELAY = 'urn:xmpp:delay'
    3535NS_JABBER_DELAY = 'jabber:x:delay'
    3636
    37 NS_REQUEST  = 'jabber:iq:register'
     37NS_REQUEST = 'jabber:iq:register'
    3838
    3939# Iq get and set XPath queries
    40 IQ     = '/iq'
     40IQ = '/iq'
    4141IQ_GET = IQ+'[@type="get"]'
    4242IQ_SET = IQ+'[@type="set"]'
    4343
    4444IQ_RESULT = IQ+'[@type="result"]'
    45 IQ_ERROR  = IQ+'[@type="error"]'
    46 
    47 IQ_QUERY     = IQ+'/query'
     45IQ_ERROR = IQ+'[@type="error"]'
     46
     47IQ_QUERY = IQ+'/query'
    4848IQ_GET_QUERY = IQ_GET + '/query'
    4949IQ_SET_QUERY = IQ_SET + '/query'
    5050
    51 IQ_COMMAND   = IQ+'/command'
     51IQ_COMMAND = IQ+'/command'
    5252
    5353MUC_ADMIN = IQ_QUERY+'[@xmlns="' + NS_MUC_ADMIN + '"]'
     
    5757
    5858
    59 MESSAGE  = '/message'
     59MESSAGE = '/message'
    6060PRESENCE = '/presence'
    6161
    6262CHAT_BODY = MESSAGE +'[@type="chat"]/body'
    63 CHAT      = MESSAGE +'[@type="chat"]'
    64 
    65 GROUPCHAT     = MESSAGE +'[@type="groupchat"]/body'
    66 SUBJECT       = MESSAGE +'[@type="groupchat"]/subject'
     63CHAT = MESSAGE +'[@type="chat"]'
     64
     65GROUPCHAT = MESSAGE +'[@type="groupchat"]/body'
     66SUBJECT = MESSAGE +'[@type="groupchat"]/subject'
    6767MESSAGE_ERROR = MESSAGE +'[@type="error"]'
    6868
     
    9191
    9292    """
    93     condition    = 'modify'
     93    condition = 'modify'
    9494    mucCondition = 'jid-malformed'
    9595
     
    9999    """
    100100    """
    101     condition    = 'auth'
     101    condition = 'auth'
    102102    mucCondition = 'not-authorized'
    103103
     
    107107    """
    108108    """
    109     condition    = 'auth'
     109    condition = 'auth'
    110110    mucCondition = 'registration-required'
    111111
     
    115115    """
    116116    """
    117     condition    = 'auth'
     117    condition = 'auth'
    118118    mucCondition = 'forbidden'
    119119
     
    123123    """
    124124    """
    125     condition    = 'cancel'
     125    condition = 'cancel'
    126126    mucCondition = 'conflict'
    127127
     
    131131    """
    132132    """
    133     condition    = 'cancel'
     133    condition = 'cancel'
    134134    mucCondition = 'not-found'
    135135
     
    139139    """
    140140    """
    141     condition    = 'wait'
     141    condition = 'wait'
    142142    mucCondition = 'service-unavailable'
    143143
     
    385385
    386386
    387 class InviteMessage(PrivateChat):
    388     def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None):
    389         PrivateChat.__init__(self, to, body=body, frm=frm)
    390         del self['type'] # remove type
     387class InviteMessage(domish.Element):
     388    def __init__(self, bareRoomJID, invitee, reason=None):
     389        domish.Element.__init__(self, (None, 'message'))
     390        self['to'] = unicode(bareRoomJID)
    391391        x = self.addElement('x', NS_MUC_USER)
    392392        invite = x.addElement('invite')
    393         if full_jid:
    394             invite['to'] = full_jid
     393        invite['to'] = invitee
    395394        if reason:
    396395            invite.addElement('reason', None, reason)
    397         if password:
    398             invite.addElement('password', None, password)
    399396
    400397
     
    434431
    435432    def __init__(self, maxchars=None, maxstanzas=None, seconds=None, since=None):
    436         self.maxchars   = maxchars
     433        self.maxchars = maxchars
    437434        self.maxstanzas = maxstanzas
    438         self.seconds    = seconds
    439         self.since      = since
     435        self.seconds = seconds
     436        self.since = since
    440437
    441438
     
    462459    """
    463460
    464     def __init__(self, nick, user_jid=None):
     461    def __init__(self, nick, entity=None):
    465462        self.nick = nick
    466         self.user_jid = user_jid
     463        self.entity = entity
    467464        self.affiliation = 'none'
    468465        self.role = 'none'
    469466
    470467        self.status = None
    471         self.show   = None
     468        self.show = None
    472469
    473470
     
    479476
    480477
    481     def __init__(self, name, server, nick, state=None):
     478    def __init__(self, roomIdentifier, service, nick, state=None):
    482479        """
    483480        Initialize the room.
    484481
    485         @ivar name: The name of the MUC room.
    486         @type name: L{unicode}
    487 
    488         @ivar server: The server where the MUC room is located.
    489         @type server: L{unicode}
     482        @ivar roomIdentifier: The Room ID of the MUC room.
     483        @type roomIdentifier: L{unicode}
     484
     485        @ivar service: The server where the MUC room is located.
     486        @type service: L{unicode}
    490487
    491488        @ivar nick: The nick name for the client in this room.
     
    495492        @type state: L{int}
    496493
    497         @ivar entity_id: The jid of the room. Generated from name, server, and nick.
    498         @type entity_id: L{jid.JID}
    499 
    500         """
    501         self.state  = state
    502         self.name   = name
    503         self.server = server
    504         self.nick   = nick
     494        @ivar roomJID: The JID of the occupant in the room. Generated from roomIdentifier, service, and nick.
     495        @type roomJID: L{jid.JID}
     496
     497        """
     498        self.state = state
     499        self.roomIdentifier = roomIdentifier
     500        self.service = service
     501        self.nick = nick
    505502        self.status = 0
    506503
    507         self.entity_id = self.entityId()
     504        self.roomJID = self.refreshRoomJID()
    508505
    509506        self.roster = {}
    510507
    511508
    512     def entityId(self):
    513         """
    514         Creates the L{jid.JID} for the room. If name, server, or nick change then this method
    515         should be called.
    516 
    517         """
    518         self.entity_id = jid.internJID(self.name+'@'+self.server+'/'+self.nick)
    519 
    520         return self.entity_id
     509    def refreshRoomJID(self):
     510        """
     511        Refreshes the Room JID.
     512
     513        This method should be called whenever roomIdentifier, service, or nick
     514        change.
     515        """
     516        self.roomJID = jid.internJID(u"%s@%s/%s" % (self.roomIdentifier,
     517                                                    self.service,
     518                                                    self.nick))
     519        return self.roomJID
    521520
    522521
     
    666665class MUCClient(XMPPHandler):
    667666    """
    668     Multi-User chat client protocol. This is a subclass of L{XMPPHandler} and implements L{IMUCCLient}.
    669 
     667    Multi-User Chat client protocol.
     668
     669    This is a subclass of L{XMPPHandler} and implements L{IMUCCLient}.
     670
     671    @ivar _rooms: Collection of occupied rooms, keyed by the bare JID of the
     672                  room. Note that a particular entity can only join a room once
     673                  at a time.
     674    @type _rooms: C{dict}
    670675    """
    671676
    672677    implements(IMUCClient)
    673678
    674     rooms = {}
    675 
    676679    timeout = None
    677680
    678     _deferreds = []
     681    def __init__(self):
     682        XMPPHandler.__init__(self)
     683
     684        self._rooms = {}
     685        self._deferreds = []
     686
    679687
    680688    def connectionInitialized(self):
     
    694702
    695703
    696     def _setRoom(self, room):
     704    def _addRoom(self, room):
    697705        """
    698706        Add a room to the room collection.
    699         """
    700         self.rooms[room.entity_id.userhost().lower()] = room
    701 
    702 
    703     def _getRoom(self, room_jid):
     707
     708        Rooms are stored by the JID of the room itself. I.e. it uses the Room
     709        ID and service parts of the Room JID.
     710
     711        @note: An entity can only join a particular room once.
     712        """
     713        bareRoomJID = room.roomJID.userhostJID()
     714        self._rooms[bareRoomJID] = room
     715
     716
     717    def _getRoom(self, roomJID):
    704718        """
    705719        Grab a room from the room collection.
    706         """
    707         return self.rooms.get(room_jid.userhost().lower())
    708 
    709 
    710     def _removeRoom(self, room_jid):
     720
     721        This uses the Room ID and service parts of the given JID to look up
     722        the L{Room} instance associated with it.
     723        """
     724        bareRoomJID = roomJID.userhostJID()
     725        return self._rooms.get(bareRoomJID)
     726
     727
     728    def _removeRoom(self, roomJID):
    711729        """
    712730        Delete a room from the room collection.
    713731        """
    714         if self.rooms.has_key(room_jid.userhost().lower()):
    715             del self.rooms[room_jid.userhost().lower()]
     732        bareRoomJID = roomJID.userhostJID()
     733        if bareRoomJID in self._rooms:
     734            del self._rooms[bareRoomJID]
    716735
    717736
     
    777796
    778797        status = getattr(prs, 'status', None)
    779         show   = getattr(prs, 'show', None)
     798        show = getattr(prs, 'show', None)
    780799
    781800        # grab room
     
    819838            if e.uri == NS_DELAY or e.uri == NS_JABBER_DELAY:
    820839                delay = e
    821         body  = unicode(msg.body)
     840        body = unicode(msg.body)
    822841        # grab room
    823842        if delay is None:
     
    856875        We have presence that says we joined a room.
    857876        """
    858         room_jid = jid.internJID(prs['from'])
     877        roomJID = jid.internJID(prs['from'])
    859878
    860879        # check for errors
     
    863882        else:
    864883            # change the state of the room
    865             r = self._getRoom(room_jid)
    866             if r is None:
     884            room = self._getRoom(roomJID)
     885            if room is None:
    867886                raise NotFound
    868             r.state = 'joined'
     887            room.state = 'joined'
    869888
    870889            # grab status
    871             status = getattr(prs.x,'status',None)
     890            status = getattr(prs.x, 'status', None)
    872891            if status:
    873                 r.status = status.getAttribute('code', None)
    874 
    875             d.callback(r)
     892                room.status = status.getAttribute('code', None)
     893
     894            d.callback(room)
    876895
    877896
    878897    def _leftRoom(self, d, prs):
    879898        """
    880         We have presence that says we joined a room.
    881         """
    882         room_jid = jid.internJID(prs['from'])
     899        We have presence that says we left a room.
     900        """
     901        roomJID = jid.internJID(prs['from'])
    883902
    884903        # check for errors
     
    887906        else:
    888907            # change the state of the room
    889             r = self._getRoom(room_jid)
    890             if r is None:
     908            room = self._getRoom(roomJID)
     909            if room is None:
    891910                raise NotFound
    892             self._removeRoom(room_jid)
     911            self._removeRoom(roomJID)
    893912
    894913            d.callback(True)
     
    10081027
    10091028
    1010     def disco(self, entity, type='info'):
    1011         """
    1012         Send disco queries to a XMPP entity.
    1013 
    1014         @param entity: The server or entity where we want discovery information from.
    1015         @type  entity: L{jid.JID}
    1016 
    1017         @param type: The optional disco type.
    1018         @type  type: L{unicode}
    1019 
    1020         """
    1021 
    1022         iq = disco.DiscoRequest(self.xmlstream, disco.NS_INFO, 'get')
    1023         iq['to'] = entity
    1024 
    1025         return iq.send().addBoth(self._cbDisco)
    1026 
    1027 
    10281029    def configure(self, room_jid, fields=[]):
    10291030        """
     
    10561057
    10571058
    1058     def join(self, server, room, nick, history = None):
    1059         """
    1060         Join a MUC room by sending presence to it. Returns a defered that is called when
    1061         the entity is in the room or an error has occurred.
     1059    def join(self, service, roomIdentifier, nick, history=None):
     1060        """
     1061        Join a MUC room by sending presence to it.
    10621062
    10631063        @param server: The server where the room is located.
    10641064        @type  server: L{unicode}
    1065 
    10661065        @param room: The room name the entity is joining.
    10671066        @type  room: L{unicode}
    1068 
    10691067        @param nick: The nick name for the entitity joining the room.
    10701068        @type  nick: L{unicode}
    1071 
    10721069        @param history: The maximum number of history stanzas you would like.
    10731070
    1074         """
    1075         r = Room(room, server, nick, state='joining')
    1076         self._setRoom(r)
    1077 
    1078         p = BasicPresence(to=r.entity_id)
     1071        @return: A deferred that fires when the entity is in the room or an
     1072                 error has occurred.
     1073        """
     1074        room = Room(roomIdentifier, service, nick, state='joining')
     1075        self._addRoom(room)
     1076
     1077        p = BasicPresence(to=room.roomJID)
    10791078        if history is not None:
    10801079            p.x.addChild(history.toElement())
     
    10831082
    10841083        # add observer for joining the room
    1085         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    1086                                           self._joinedRoom, 1, d)
     1084        query = PRESENCE + u"[@from='%s']" % room.roomJID
     1085        self.xmlstream.addOnetimeObserver(query, self._joinedRoom, 1, d)
    10871086
    10881087        return d
    10891088
    10901089
    1091     def _changeUserStatus(self, r, room_jid, status, show):
    1092         # change the user status in a room.
     1090    def _changeUserStatus(self, room, roomJID, status, show):
     1091        """
     1092        Change the user status in a room.
     1093        """
    10931094
    10941095        # check if user is in roster
    1095         user = r.getUser(room_jid.resource)
     1096        user = room.getUser(roomJID.resource)
    10961097        if user is None: # create a user that does not exist
    1097             user = User(room_jid.resource)
     1098            user = User(roomJID.resource)
    10981099
    10991100        if status is not None:
    1100             user.status = str(status)
     1101            user.status = unicode(status)
    11011102        if show is not None:
    1102             user.show   = str(show)
     1103            user.show = unicode(show)
    11031104
    11041105        return user
    11051106
    11061107
    1107     def _changed(self, d, room_jid, prs):
     1108    def _changed(self, d, roomJID, prs):
    11081109        """
    11091110        Callback for changing the nick and status.
     
    11111112
    11121113        status = getattr(prs, 'status', None)
    1113         show   = getattr(prs, 'show', None)
    1114 
    1115         r = self._getRoom(room_jid)
    1116 
    1117         user = self._changeUserStatus(r, room_jid, status, show)
    1118 
    1119         d.callback(r)
    1120 
    1121 
    1122     def nick(self, room_jid, new_nick):
     1114        show = getattr(prs, 'show', None)
     1115
     1116        room = self._getRoom(roomJID)
     1117
     1118        user = self._changeUserStatus(room, roomJID, status, show)
     1119
     1120        d.callback(room)
     1121
     1122
     1123    def nick(self, bareRoomJID, new_nick):
    11231124        """
    11241125        Change an entities nick name in a MUC room.
     
    11261127        See: http://xmpp.org/extensions/xep-0045.html#changenick
    11271128
    1128         @param room_jid: The room jabber/xmpp entity id for the requested configuration form.
    1129         @type  room_jid: L{jid.JID}
     1129        @param bareRoomJID: The JID of the room, i.e. without a resource.
     1130        @type bareRoomJID: L{jid.JID}
    11301131
    11311132        @param new_nick: The nick name for the entitity joining the room.
    1132         @type  new_nick: L{unicode}
    1133 
    1134         """
    1135 
    1136 
    1137         r = self._getRoom(room_jid)
    1138         if r is None:
     1133        @type new_nick: L{unicode}
     1134
     1135        """
     1136
     1137
     1138        room = self._getRoom(bareRoomJID)
     1139        if room is None:
    11391140            raise NotFound
    1140         r.nick = new_nick # change the nick
    1141         # create presence
    1142         # make sure we call the method to generate the new entity xmpp id
    1143         p = BasicPresence(to=r.entityId())
     1141
     1142        # Change the nickname
     1143        room.nick = new_nick
     1144        room.refreshRoomJID()
     1145
     1146        # Create presence
     1147        p = BasicPresence(to=room.roomJID)
    11441148        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    11451149
    1146         # add observer for joining the room
    1147         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    1148                                           self._changed, 1, d, room_jid)
     1150        # Add observer for joining the room
     1151        query = PRESENCE+"[@from='%s']" % (room.roomJID.full())
     1152        self.xmlstream.addOnetimeObserver(query, self._changed, 1, d, bareRoomJID)
    11491153
    11501154        return d
    11511155
    11521156
    1153     def leave(self, room_jid):
     1157    def leave(self, roomJID):
    11541158        """
    11551159        Leave a MUC room.
     
    11571161        See: http://xmpp.org/extensions/xep-0045.html#exit
    11581162
    1159         @param room_jid: The room entity id you want to exit.
    1160         @type  room_jid: L{jid.JID}
    1161 
    1162         """
    1163         r = self._getRoom(room_jid)
    1164 
    1165         p = xmppim.UnavailablePresence(to=r.entity_id)
     1163        @param roomJID: The Room JID of the room to leave.
     1164        @type roomJID: L{jid.JID}
     1165
     1166        """
     1167        room = self._getRoom(roomJID)
     1168
     1169        p = xmppim.UnavailablePresence(to=room.roomJID)
    11661170
    11671171        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    11681172        # add observer for joining the room
    1169         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()),
    1170                                           self._leftRoom, 1, d)
     1173        query = PRESENCE + u"[@from='%s' and @type='unavailable']" % (room.roomJID)
     1174        self.xmlstream.addOnetimeObserver(query, self._leftRoom, 1, d)
    11711175
    11721176        return d
    11731177
    11741178
    1175     def status(self, room_jid, show=None, status=None):
     1179    def status(self, roomJID, show=None, status=None):
    11761180        """
    11771181        Change user status.
     
    11791183        See: http://xmpp.org/extensions/xep-0045.html#changepres
    11801184
    1181         @param room_jid: The room jabber/xmpp entity id for the requested configuration form.
    1182         @type  room_jid: L{jid.JID}
     1185        @param roomJID: The room jabber/xmpp entity id for the requested configuration form.
     1186        @type  roomJID: L{jid.JID}
    11831187
    11841188        @param show: The availability of the entity. Common values are xa, available, etc
     
    11891193
    11901194        """
    1191         r = self._getRoom(room_jid)
    1192         if r is None:
     1195        room = self._getRoom(roomJID)
     1196        if room is None:
    11931197            raise NotFound
    11941198
    1195         p = BasicPresence(to=r.entityId())
     1199        p = BasicPresence(to=room.roomJID)
    11961200        if status is not None:
    11971201            p.addElement('status', None, status)
     
    12031207
    12041208        # add observer for joining the room
    1205         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    1206                                           self._changed, 1, d, room_jid)
     1209        query = PRESENCE + u"[@from='%s']" % room.roomJID
     1210        self.xmlstream.addOnetimeObserver(query, self._changed, 1, d, roomJID)
    12071211
    12081212        return d
     
    12101214
    12111215    def _sendMessage(self, msg, children=None):
    1212         # send a message
     1216        """
     1217        Send a message.
     1218        """
    12131219        if children:
    1214             for c in children:
    1215                 msg.addChild(c)
     1220            for child in children:
     1221                msg.addChild(child)
    12161222
    12171223        self.xmlstream.send(msg)
     
    12431249
    12441250
    1245     def invite(self, room_jid, reason=None, full_jid=None):
     1251    def invite(self, bareRoomJID, reason=None, invitee=None):
    12461252        """
    12471253        Invite a xmpp entity to a MUC room.
     
    12491255        See: http://xmpp.org/extensions/xep-0045.html#invite
    12501256
    1251         @param room_jid: The room entity id.
    1252         @type  room_jid: L{jid.JID}
    1253 
     1257        @param bareRoomJID: The bare JID of the room.
     1258        @type bareRoomJID: L{jid.JID}
    12541259        @param reason: The reason for the invite.
    1255         @type  reason: L{unicode}
    1256 
    1257         @param full_jid: The xmpp user's entity id.
    1258         @type  full_jid: L{jid.JID}
    1259 
    1260         """
    1261         msg = InviteMessage(room_jid, reason=reason, full_jid=full_jid)
     1260        @type reason: L{unicode}
     1261        @param invitee: The entity that is being invited.
     1262        @type invitee: L{jid.JID}
     1263
     1264        """
     1265        msg = InviteMessage(bareRoomJID, reason=reason, invitee=invitee)
    12621266        self._sendMessage(msg)
    12631267
     
    13171321
    13181322
    1319     def _setAffiliationList(self, affiliation, room_jid, iq):
     1323    def _setAffiliationList(self, iq, affiliation, roomJID):
    13201324        # set a rooms affiliation list
    1321         r = self._getRoom(room_jid)
    1322         if r is not None:
     1325        room = self._getRoom(roomJID)
     1326        if room is not None:
    13231327            affiliation_list = []
    1324             setattr(r, affiliation, [])
     1328            setattr(room, affiliation, [])
    13251329
    13261330            for item in iq.query.elements():
    1327                 nick   = item.getAttribute('nick', None)
     1331                nick = item.getAttribute('nick', None)
    13281332                entity = item.getAttribute('jid', None)
    1329                 u      = None
     1333                role = item.getAttribute('role', None)
     1334                user = None
    13301335                if nick is None and entity is None:
    13311336                    raise Exception, 'bad attributes in item list'
    13321337                if nick is not None:
    1333                     u = r.getUser(nick)
    1334                 if u is None:
    1335                     u = User(nick, user_jid=jid.internJID(entity))
    1336                     u.affiliation = 'member'
    1337 
    1338                 affiliation_list.append(u)
    1339 
    1340             setattr(r, affiliation, affiliation_list)
    1341         return r
    1342 
    1343 
    1344     def getMemberList(self, room_jid):
    1345         """
    1346         Get a member list from a room.
    1347 
    1348         @param room_jid: The room jabber/xmpp entity id for the requested member list.
    1349         @type  room_jid: L{jid.JID}
    1350 
    1351         """
    1352         d = self._getAffiliationList(room_jid, 'member')
    1353         d.addCallback(self._setAffiliationList, 'members', room_jid)
     1338                    user = room.getUser(nick)
     1339                if user is None:
     1340                    user = User(nick, entity=jid.internJID(entity))
     1341                    user.affiliation = 'member'
     1342                if role is not None:
     1343                    user.role = role
     1344
     1345                affiliation_list.append(user)
     1346
     1347            setattr(room, affiliation, affiliation_list)
     1348        return room
     1349
     1350
     1351    def getMemberList(self, bareRoomJID):
     1352        """
     1353        Get the member list of a room.
     1354
     1355        @param bareRoomJID: The bare JID of the room.
     1356        @type  bareRoomJID: L{jid.JID}
     1357
     1358        """
     1359        d = self._getAffiliationList(bareRoomJID, 'member')
     1360        d.addCallback(self._setAffiliationList, 'members', bareRoomJID)
    13541361        return d
    13551362
    13561363
    1357     def getAdminList(self, room_jid):
    1358         """
    1359         Get an admin list from a room.
    1360 
    1361         @param room_jid: The room jabber/xmpp entity id for the requested member list.
    1362         @type  room_jid: L{jid.JID}
    1363 
    1364         """
    1365         d = self._getAffiliationList(room_jid, 'admin')
    1366         d.addCallback(self._setAffiliationList, 'members', room_jid)
     1364    def getAdminList(self, bareRoomJID):
     1365        """
     1366        Get the admin list of a room.
     1367
     1368        @param bareRoomJID: The bare JID of the room.
     1369        @type  bareRoomJID: L{jid.JID}
     1370
     1371        """
     1372        d = self._getAffiliationList(bareRoomJID, 'admin')
     1373        d.addCallback(self._setAffiliationList, 'admin', bareRoomJID)
    13671374        return d
    13681375
    13691376
    1370     def getBanList(self, room_jid):
     1377    def getBanList(self, bareRoomJID):
    13711378        """
    13721379        Get an outcast list from a room.
    13731380
    1374         @param room_jid: The room jabber/xmpp entity id for the requested member list.
    1375         @type  room_jid: L{jid.JID}
    1376 
    1377         """
    1378         d = self._getAffiliationList(room_jid, 'outcast')
    1379         d.addCallback(self._setAffiliationList, 'members', room_jid)
     1381        @param bareRoomJID: The bare JID of the room.
     1382        @type  bareRoomJID: L{jid.JID}
     1383
     1384        """
     1385        d = self._getAffiliationList(bareRoomJID, 'outcast')
     1386        d.addCallback(self._setAffiliationList, 'outcast', bareRoomJID)
    13801387        return d
    13811388
    13821389
    1383     def getOwnerList(self, room_jid):
     1390    def getOwnerList(self, bareRoomJID):
    13841391        """
    13851392        Get an owner list from a room.
    13861393
    1387         @param room_jid: The room jabber/xmpp entity id for the requested member list.
    1388         @type  room_jid: L{jid.JID}
    1389 
    1390         """
    1391         d = self._getAffiliationList(room_jid, 'owner')
    1392         d.addCallback(self._setAffiliationList, 'members', room_jid)
     1394        @param bareRoomJID: The room jabber/xmpp entity id for the requested member list.
     1395        @type  bareRoomJID: L{jid.JID}
     1396
     1397        """
     1398        d = self._getAffiliationList(bareRoomJID, 'owner')
     1399        d.addCallback(self._setAffiliationList, 'owner', bareRoomJID)
    13931400        return d
    13941401
     
    14231430
    14241431        iq = OwnerRequest(self.xmlstream, method='set')
    1425         d  = iq.query.addElement('destroy')
     1432        d = iq.query.addElement('destroy')
    14261433        d['jid'] = room_jid.userhost()
    14271434        if reason is not None:
  • wokkel/test/test_muc.py

    r129 r130  
    1818from twisted.words.protocols.jabber.xmlstream import toResponse
    1919
     20NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
    2021
    2122def calledAsync(fn):
     
    5556
    5657    def _createRoom(self):
    57         """A helper method to create a test room.
     58        """
     59        A helper method to create a test room.
    5860        """
    5961        # create a room
    6062        self.current_room = muc.Room(self.test_room, self.test_srv, self.test_nick)
    61         self.protocol._setRoom(self.current_room)
     63        self.protocol._addRoom(self.current_room)
    6264
    6365
     
    7072
    7173    def test_userJoinedRoom(self):
    72         """The client receives presence from an entity joining the room.
     74        """
     75        The client receives presence from an entity joining the room.
    7376
    7477        This tests the class L{muc.UserPresence} and the userJoinedRoom event method.
     
    8588
    8689        def userPresence(room, user):
    87             self.failUnless(room.name==self.test_room, 'Wrong room name')
     90            self.failUnless(room.roomIdentifier==self.test_room, 'Wrong room name')
    8891            self.failUnless(room.inRoster(user), 'User not in roster')
    8992
     
    9598
    9699    def test_groupChat(self):
    97         """The client receives a groupchat message from an entity in the room.
     100        """
     101        The client receives a groupchat message from an entity in the room.
    98102        """
    99103        m = muc.GroupChat('test@test.com',body='test')
     
    104108        def groupChat(room, user, message):
    105109            self.failUnless(message=='test', "Wrong group chat message")
    106             self.failUnless(room.name==self.test_room, 'Wrong room name')
     110            self.failUnless(room.roomIdentifier==self.test_room, 'Wrong room name')
    107111
    108112
     
    112116
    113117
    114     def test_discoServerSupport(self):
    115         """Disco support from client to server.
    116         """
    117         test_srv = 'shakespeare.lit'
    118 
    119         def cb(query):
    120             # check namespace
    121             self.failUnless(query.uri==disco.NS_INFO, 'Wrong namespace')
    122 
    123 
    124         d = self.protocol.disco(test_srv)
    125         d.addCallback(cb)
    126 
    127         iq = self.stub.output[-1]
    128 
    129         # send back a response
    130         response = toResponse(iq, 'result')
    131         response.addElement('query', disco.NS_INFO)
    132         # need to add information to response
    133         response.query.addChild(disco.DiscoFeature(muc.NS_MUC))
    134         response.query.addChild(disco.DiscoIdentity(category='conference',
    135                                                     name='Macbeth Chat Service',
    136                                                     type='text'))
    137 
    138         self.stub.send(response)
    139         return d
    140 
    141 
    142118    def test_joinRoom(self):
    143         """Joining a room
     119        """
     120        Joining a room
    144121        """
    145122
    146123        def cb(room):
    147             self.assertEquals(self.test_room, room.name)
     124            self.assertEquals(self.test_room, room.roomIdentifier)
    148125
    149126        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
     
    161138
    162139    def test_joinRoomForbidden(self):
    163         """Client joining a room and getting a forbidden error.
     140        """
     141        Client joining a room and getting a forbidden error.
    164142        """
    165143
     
    187165
    188166    def test_joinRoomBadJid(self):
    189         """Client joining a room and getting a forbidden error.
     167        """
     168        Client joining a room and getting a forbidden error.
    190169        """
    191170
     
    213192
    214193    def test_partRoom(self):
    215         """Client leaves a room
     194        """
     195        Client leaves a room
    216196        """
    217197        def cb(left):
     
    219199
    220200
     201        self._createRoom()
    221202        d = self.protocol.leave(self.room_jid)
    222203        d.addCallback(cb)
     
    235216
    236217    def test_userPartsRoom(self):
    237         """An entity leaves the room, a presence of type unavailable is received by the client.
     218        """
     219        An entity leaves the room, a presence of type unavailable is received by the client.
    238220        """
    239221
     
    251233
    252234        def userPresence(room, user):
    253             self.failUnless(room.name==self.test_room, 'Wrong room name')
     235            self.failUnless(room.roomIdentifier==self.test_room, 'Wrong room name')
    254236            self.failUnless(room.inRoster(user)==False, 'User in roster')
    255237
     
    260242
    261243    def test_ban(self):
    262         """Ban an entity in a room.
     244        """
     245        Ban an entity in a room.
    263246        """
    264247        banned = JID('ban@jabber.org/TroubleMakger')
     
    282265
    283266    def test_kick(self):
    284         """Kick an entity from a room.
     267        """
     268        Kick an entity from a room.
    285269        """
    286270        kicked = JID('kick@jabber.org/TroubleMakger')
     
    304288
    305289    def test_password(self):
    306         """Sending a password via presence to a password protected room.
     290        """
     291        Sending a password via presence to a password protected room.
    307292        """
    308293
     
    315300
    316301    def test_history(self):
    317         """Receiving history on room join.
     302        """
     303        Receiving history on room join.
    318304        """
    319305        m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test')
     
    333319
    334320    def test_oneToOneChat(self):
    335         """Converting a one to one chat to a multi-user chat.
     321        """
     322        Converting a one to one chat to a multi-user chat.
    336323        """
    337324        archive = []
     
    365352
    366353    def test_invite(self):
    367         """Invite a user to a room
    368         """
    369         other_jid = 'test@jabber.org'
    370 
    371         self.protocol.invite(other_jid, 'This is a test')
     354        """
     355        Invite a user to a room
     356        """
     357        bareRoomJID = self.room_jid.userhostJID()
     358        invitee = JID('test@jabber.org')
     359
     360        self.protocol.invite(bareRoomJID, 'This is a test', invitee)
    372361
    373362        msg = self.stub.output[-1]
    374363
    375         self.failUnless(xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid,), msg), 'Wrong message type')
     364        query = u"/message[@to='%s']/x/invite/reason" % bareRoomJID
     365        self.failUnless(xpath.matches(query, msg), 'Wrong message type')
    376366
    377367
    378368    def test_privateMessage(self):
    379         """Send private messages to muc entities.
     369        """
     370        Send private messages to muc entities.
    380371        """
    381372        other_nick = self.room_jid.userhost()+'/OtherNick'
     
    389380
    390381    def test_register(self):
    391         """Client registering with a room. http://xmpp.org/extensions/xep-0045.html#register
     382        """
     383        Client registering with a room. http://xmpp.org/extensions/xep-0045.html#register
    392384
    393385        """
     
    410402
    411403    def test_voice(self):
    412         """ Client requesting voice for a room.
     404        """
     405        Client requesting voice for a room.
    413406        """
    414407        self.protocol.voice(self.room_jid.userhost())
     
    420413
    421414    def test_roomConfigure(self):
    422         """ Default configure and changing the room name.
     415        """
     416        Default configure and changing the room name.
    423417        """
    424418
     
    445439
    446440    def test_roomDestroy(self):
    447         """ Destroy a room.
     441        """
     442        Destroy a room.
    448443        """
    449444
     
    463458
    464459    def test_nickChange(self):
    465         """Send a nick change to the server.
     460        """
     461        Send a nick change to the server.
    466462        """
    467463        test_nick = 'newNick'
     
    470466
    471467        def cb(room):
    472             self.assertEquals(self.test_room, room.name)
     468            self.assertEquals(self.test_room, room.roomIdentifier)
    473469            self.assertEquals(test_nick, room.nick)
    474470
     
    488484
    489485    def test_grantVoice(self):
    490         """Test granting voice to a user.
     486        """
     487        Test granting voice to a user.
    491488
    492489        """
     
    511508
    512509    def test_changeStatus(self):
    513         """Change status
     510        """
     511        Change status
    514512        """
    515513        self._createRoom()
     
    519517
    520518        def cb(room):
    521             self.assertEquals(self.test_room, room.name)
     519            self.assertEquals(self.test_room, room.roomIdentifier)
    522520            u = room.getUser(self.room_jid.resource)
    523521            self.failUnless(u is not None, 'User not found')
     
    539537        self.stub.send(response)
    540538        return d
     539
     540
     541    def test_getMemberList(self):
     542        def cb(room):
     543            members = room.members
     544            self.assertEqual(1, len(members))
     545            user = members[0]
     546            self.assertEquals(JID(u'hag66@shakespeare.lit'), user.entity)
     547            self.assertEquals(u'thirdwitch', user.nick)
     548            self.assertEquals(u'participant', user.role)
     549
     550        self._createRoom()
     551        bareRoomJID = self.room_jid.userhostJID()
     552        d = self.protocol.getMemberList(bareRoomJID)
     553        d.addCallback(cb)
     554
     555        iq = self.stub.output[-1]
     556        query = iq.query
     557        self.assertNotIdentical(None, query)
     558        self.assertEquals(NS_MUC_ADMIN, query.uri)
     559
     560        response = toResponse(iq, 'result')
     561        query = response.addElement((NS_MUC_ADMIN, 'query'))
     562        item = query.addElement('item')
     563        item['affiliation'] ='member'
     564        item['jid'] = 'hag66@shakespeare.lit'
     565        item['nick'] = 'thirdwitch'
     566        item['role'] = 'participant'
     567        self.stub.send(response)
     568
     569        return d
     570
     571
Note: See TracChangeset for help on using the changeset viewer.