Changeset 129:29e517800b96 for wokkel


Ignore:
Timestamp:
Jun 4, 2009, 5:44:50 PM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Whitespace and pyflakes changes.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r127 r129  
    11# -*- test-case-name: wokkel.test.test_muc -*-
    22#
    3 # Copyright (c) 2003-2008 Ralph Meijer
     3# Copyright (c) 2003-2009 Ralph Meijer
    44# See LICENSE for details.
    55
     
    1818from twisted.words.xish import domish
    1919
    20 from wokkel import disco, data_form, shim, xmppim
    21 from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
     20from wokkel import disco, data_form, xmppim
     21from wokkel.subprotocols import XMPPHandler
    2222from wokkel.iwokkel import IMUCClient
    2323
     
    3737NS_REQUEST  = 'jabber:iq:register'
    3838
    39 # ad hoc commands
    40 NS_AD_HOC       = "http://jabber.org/protocol/commands"
    41 
    42 
    4339# Iq get and set XPath queries
    4440IQ     = '/iq'
     
    7571        {'name':'fulljid',
    7672         'stanza':'presence',
    77          
     73
    7874         },
    79     201: 
    80         {'name':'created', 
     75    201:
     76        {'name':'created',
    8177         'stanza': 'presence',
    8278         'context':'Entering a room',
    8379         'purpose':'Inform user that a new room has been created'
    84          },   
     80         },
    8581}
    8682
     
    9389    A jid malformed error from the server.
    9490
    95    
     91
    9692    """
    9793    condition    = 'modify'
    9894    mucCondition = 'jid-malformed'
    99    
     95
     96
     97
    10098class NotAuthorized(Exception):
    10199    """
     
    104102    mucCondition = 'not-authorized'
    105103
     104
     105
    106106class RegistrationRequired(Exception):
    107107    """
     
    111111
    112112
     113
    113114class Forbidden(Exception):
    114115    """
     
    117118    mucCondition = 'forbidden'
    118119
     120
     121
    119122class Conflict(Exception):
    120123    """
     
    123126    mucCondition = 'conflict'
    124127
     128
     129
    125130class NotFound(Exception):
    126131    """
     
    128133    condition    = 'cancel'
    129134    mucCondition = 'not-found'
     135
     136
    130137
    131138class ServiceUnavailable(Exception):
     
    147154    }
    148155
     156
     157
    149158class MUCError(error.StanzaError):
    150159    """
     
    161170
    162171
     172
    163173class BadRequest(MUCError):
    164174    """
     
    196206            form = data_form.Form('submit', formNamespace=NS_MUC_CONFIG)
    197207            q.addChild(form.toElement())
    198            
     208
    199209            for f in fields:
    200210                # create a field
    201211                form.addField(f)
     212
    202213
    203214
     
    216227        if method == 'set':
    217228            # build data form
    218             form_type = 'submit'       
     229            form_type = 'submit'
    219230            form = data_form.Form(form_type, formNamespace=NS_MUC_REGISTER)
    220             q.addChild(form.toElement())       
    221            
     231            q.addChild(form.toElement())
     232
    222233            for f in fields:
    223234                # create a field
     
    225236
    226237
     238
    227239class AdminRequest(xmlstream.IQ):
    228240    """
    229     A basic admin iq request 
     241    A basic admin iq request
    230242
    231243    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     
    239251
    240252
     253
    241254class OwnerRequest(xmlstream.IQ):
    242255    """
    243     A basic owner iq request 
     256    A basic owner iq request
    244257
    245258    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     
    252265        q = self.addElement((NS_MUC_OWNER, 'query'))
    253266
    254    
     267
    255268
    256269class AffiliationRequest(AdminRequest):
     
    283296            i.addElement('reason', None, reason)
    284297
     298
    285299    def items(self, jid_list=None):
    286         """ Set or Get the items list for this affiliation request.
     300        """
     301        Set or Get the items list for this affiliation request.
    287302        """
    288303        if jid_list:
     
    296311
    297312        return self.query.elements()
    298            
    299            
     313
     314
    300315
    301316class RoleRequest(AdminRequest):
     
    312327        if reason:
    313328            i.addElement('reason', None, reason)
    314        
     329
     330
     331
    315332class GroupChat(domish.Element):
    316     """A message stanza of type groupchat. Used to send a message to a MUC room that will be
     333    """
     334        A message stanza of type groupchat. Used to send a message to a MUC room that will be
    317335    broadcasted to people in the room.
    318336
     
    335353
    336354
     355
    337356class PrivateChat(domish.Element):
    338     """A chat message.
     357    """
     358        A chat message.
    339359    """
    340360    def __init__(self, to, body=None, frm=None):
    341         """ Initialize the L{PrivateChat}
     361        """
     362        Initialize the L{PrivateChat}
    342363
    343364        @param to: The xmpp entity you are sending this chat to.
     
    361382        if body:
    362383            self.addElement('body',None, body)
    363            
     384
     385
     386
    364387class InviteMessage(PrivateChat):
    365388    def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None):
     
    375398            invite.addElement('password', None, password)
    376399
     400
     401
    377402class HistoryMessage(GroupChat):
    378     """ A history message for MUC groupchat history.
     403    """
     404        A history message for MUC groupchat history.
    379405    """
    380406    def __init__(self, to, stamp, body=None, subject=None, frm=None, h_frm=None):
     
    385411            d['from'] = h_frm
    386412
     413
     414
    387415class HistoryOptions(object):
    388     """A history configuration object.
    389 
    390     @ivar maxchars: Limit the total number of characters in the history to "X"
     416    """
     417        A history configuration object.
     418
     419    @ivar maxchars: Limit the total number of characters in the history to "X"
    391420                    (where the character count is the characters of the complete XML stanzas, not only their XML character data).
    392421    @type maxchars: L{int}
    393                
     422
    394423    @ivar  maxstanzas:  Limit the total number of messages in the history to "X".
    395424    @type mazstanzas: L{int}
     
    410439        self.since      = since
    411440
     441
    412442    def toElement(self):
    413         """Returns a L{domish.Element} representing the xml for the history options.
     443        """
     444        Returns a L{domish.Element} representing the xml for the history options.
    414445        """
    415446        h = domish.Element((None, 'history'))
    416447        for key in self.attributes:
    417             a = getattr(self, key, a)
     448            a = getattr(self, key, None)
    418449            if a is not None:
    419450                if key == 'since':
     
    421452                else:
    422453                    h[key] = str(a)
    423        
     454
    424455        return h
    425456
     457
     458
    426459class User(object):
    427460    """
    428461    A user/entity in a multi-user chat room.
    429462    """
    430    
     463
    431464    def __init__(self, nick, user_jid=None):
    432465        self.nick = nick
     
    434467        self.affiliation = 'none'
    435468        self.role = 'none'
    436        
     469
    437470        self.status = None
    438471        self.show   = None
    439472
    440473
     474
    441475class Room(object):
    442476    """
     
    444478    """
    445479
    446    
     480
    447481    def __init__(self, name, server, nick, state=None):
    448         """ Initialize the room.
    449        
     482        """
     483        Initialize the room.
     484
    450485        @ivar name: The name of the MUC room.
    451486        @type name: L{unicode}
     
    471506
    472507        self.entity_id = self.entityId()
    473                
     508
    474509        self.roster = {}
    475510
     511
    476512    def entityId(self):
    477         """ Creates the L{jid.JID} for the room. If name, server, or nick change then this method
     513        """
     514        Creates the L{jid.JID} for the room. If name, server, or nick change then this method
    478515        should be called.
    479516
     
    481518        self.entity_id = jid.internJID(self.name+'@'+self.server+'/'+self.nick)
    482519
    483         return self.entity_id
     520        return self.entity_id
     521
    484522
    485523    def addUser(self, user):
    486         """Add a user to the room roster.
     524        """
     525        Add a user to the room roster.
    487526
    488527        @param user: The user object that is being added to the room.
     
    492531        self.roster[user.nick.lower()] = user
    493532
     533
    494534    def inRoster(self, user):
    495         """ Check if a user is in the MUC room.
     535        """
     536        Check if a user is in the MUC room.
    496537
    497538        @param user: The user object to check.
     
    502543        return self.roster.has_key(user.nick.lower())
    503544
     545
    504546    def getUser(self, nick):
    505         """ Get a user from the room's roster.
     547        """
     548        Get a user from the room's roster.
    506549
    507550        @param nick: The nick for the user in the MUC room.
     
    511554        return self.roster.get(nick.lower())
    512555
     556
    513557    def removeUser(self, user):
    514         """ Remove a user from the MUC room's roster.
    515        
     558        """
     559        Remove a user from the MUC room's roster.
     560
    516561        @param user: The user object to check.
    517562        @type  user: L{User}
    518        
     563
    519564        """
    520565        if self.inRoster(user):
    521566            del self.roster[user.nick.lower()]
    522        
     567
     568
    523569
    524570class BasicPresence(xmppim.AvailablePresence):
     
    532578        # add muc elements
    533579        x = self.addElement('x', NS_MUC)
     580
    534581
    535582
     
    551598            x['role'] = role
    552599
     600
     601
    553602class UnavailableUserPresence(xmppim.UnavailablePresence):
    554603    """
     
    569618
    570619
     620
    571621class PasswordPresence(BasicPresence):
    572     """ This behaves like an object providing L{domish.IElement}.
     622    """
     623        This behaves like an object providing L{domish.IElement}.
    573624    """
    574625    def __init__(self, to, password):
    575626        BasicPresence.__init__(self, to)
    576        
     627
    577628        self.x.addElement('password', None, password)
    578629
    579630
     631
    580632class MessageVoice(GroupChat):
    581     """  This behaves like an object providing L{domish.IElement}.
     633    """
     634        This behaves like an object providing L{domish.IElement}.
    582635    """
    583636    def __init__(self, to=None, frm=None):
     
    586639        form = data_form.Form('submit', formNamespace=NS_MUC_REQUEST)
    587640        form.addField(data_form.Field(var='muc#role',
    588                                       value='participant', 
     641                                      value='participant',
    589642                                      label='Requested role'))
    590         self.addChild(form.toElement())           
     643        self.addChild(form.toElement())
     644
     645
    591646
    592647class PresenceError(xmppim.Presence):
     
    602657        # add muc elements
    603658        x = self.addElement('x', NS_MUC)
    604        
    605         # add error 
     659
     660        # add error
    606661        e = error.getElement()
    607662        self.addChild(e)
    608        
     663
     664
    609665
    610666class MUCClient(XMPPHandler):
     
    623679
    624680    def connectionInitialized(self):
    625         """ This method is called when the client has successfully authenticated.
     681        """
     682        This method is called when the client has successfully authenticated.
    626683        It initializes several xpath events to handle MUC stanzas that come in.
    627         After those are initialized then the method initialized is called to signal that we have finished. 
     684        After those are initialized then the method initialized is called to signal that we have finished.
    628685        """
    629686        self.xmlstream.addObserver(PRESENCE+"[not(@type) or @type='available']/x", self._onXPresence)
     
    636693        self.initialized()
    637694
     695
    638696    def _setRoom(self, room):
    639         """Add a room to the room collection.
     697        """
     698        Add a room to the room collection.
    640699        """
    641700        self.rooms[room.entity_id.userhost().lower()] = room
    642701
     702
    643703    def _getRoom(self, room_jid):
    644         """Grab a room from the room collection.
     704        """
     705        Grab a room from the room collection.
    645706        """
    646707        return self.rooms.get(room_jid.userhost().lower())
    647708
     709
    648710    def _removeRoom(self, room_jid):
    649         """Delete a room from the room collection.
     711        """
     712        Delete a room from the room collection.
    650713        """
    651714        if self.rooms.has_key(room_jid.userhost().lower()):
     
    654717
    655718    def _onUnavailablePresence(self, prs):
    656         """ This method is called when the stanza matches the xpath observer.
    657         The client has received a presence stanza with the 'type' attribute of unavailable.
     719        """
     720        This method is called when the stanza matches the xpath observer.
     721        The client has received a presence stanza with the 'type' attribute of unavailable.
    658722        It means a user has exited a MUC room.
    659723        """
     
    664728        self._userLeavesRoom(room_jid)
    665729
     730
    666731    def _onPresenceError(self, prs):
    667         """This method is called when a presence stanza with the 'type' attribute of error.
     732        """
     733        This method is called when a presence stanza with the 'type' attribute of error.
    668734        There are various reasons for receiving a presence error and it means that the user has left the room.
    669735        """
     
    673739        # add an error hook here?
    674740        self._userLeavesRoom(room_jid)
    675        
     741
     742
    676743    def _getExceptionFromElement(self, stanza):
    677744        # find an exception based on the error stanza
     
    684751
    685752        return MUC_EXCEPTIONS[muc_condition]
     753
    686754
    687755    def _userLeavesRoom(self, room_jid):
     
    698766            room.removeUser(user)
    699767            self.userLeftRoom(room, user)
    700        
     768
     769
    701770    def _onXPresence(self, prs):
    702         """ A muc presence has been received.
     771        """
     772        A muc presence has been received.
    703773        """
    704774        if not prs.hasAttribute('from'):
    705775            return
    706776        room_jid = jid.internJID(prs.getAttribute('from', ''))
    707            
     777
    708778        status = getattr(prs, 'status', None)
    709779        show   = getattr(prs, 'show', None)
    710        
     780
    711781        # grab room
    712782        room = self._getRoom(room_jid)
     
    717787
    718788        if room.inRoster(user):
    719             # we changed status or nick 
     789            # we changed status or nick
    720790            muc_status = getattr(prs.x, 'status', None)
    721791            if muc_status:
     
    723793            else:
    724794                self.userUpdatedStatus(room, user, show, status)
    725         else:           
     795        else:
    726796            room.addUser(user)
    727797            self.userJoinedRoom(room, user)
    728            
     798
    729799
    730800    def _onGroupChat(self, msg):
    731         """ A group chat message has been received from a MUC room.
    732        
     801        """
     802        A group chat message has been received from a MUC room.
     803
    733804        There are a few event methods that may get called here. receviedGroupChat and receivedHistory
    734805        """
     
    757828
    758829    def _onSubject(self, msg):
    759         """A subject has been sent from a MUC room.
     830        """
     831        A subject has been sent from a MUC room.
    760832        """
    761833        if not msg.hasAttribute('from'):
     
    776848        if stamp is None:
    777849            stamp = datetime.datetime.now()
    778            
     850
    779851        return stamp.strftime('%Y%m%dT%H:%M:%S')
    780852
    781853
    782854    def _joinedRoom(self, d, prs):
    783         """We have presence that says we joined a room.
     855        """
     856        We have presence that says we joined a room.
    784857        """
    785858        room_jid = jid.internJID(prs['from'])
    786        
     859
    787860        # check for errors
    788         if prs.hasAttribute('type') and prs['type'] == 'error':           
     861        if prs.hasAttribute('type') and prs['type'] == 'error':
    789862            d.errback(self._getExceptionFromElement(prs))
    790         else:   
     863        else:
    791864            # change the state of the room
    792865            r = self._getRoom(room_jid)
     
    794867                raise NotFound
    795868            r.state = 'joined'
    796            
     869
    797870            # grab status
    798871            status = getattr(prs.x,'status',None)
     
    804877
    805878    def _leftRoom(self, d, prs):
    806         """We have presence that says we joined a room.
     879        """
     880        We have presence that says we joined a room.
    807881        """
    808882        room_jid = jid.internJID(prs['from'])
    809        
     883
    810884        # check for errors
    811         if prs.hasAttribute('type') and prs['type'] == 'error':           
     885        if prs.hasAttribute('type') and prs['type'] == 'error':
    812886            d.errback(self._getExceptionFromElement(prs))
    813         else:   
     887        else:
    814888            # change the state of the room
    815889            r = self._getRoom(room_jid)
     
    817891                raise NotFound
    818892            self._removeRoom(room_jid)
    819            
     893
    820894            d.callback(True)
    821895
     896
    822897    def initialized(self):
    823         """Client is initialized and ready!
     898        """
     899        Client is initialized and ready!
    824900        """
    825901        pass
    826902
     903
    827904    def userJoinedRoom(self, room, user):
    828         """User has joined a MUC room.
     905        """
     906        User has joined a MUC room.
    829907
    830908        This method will need to be modified inorder for clients to
     
    840918        pass
    841919
     920
    842921    def userLeftRoom(self, room, user):
    843         """User has left a room.
    844        
     922        """
     923        User has left a room.
     924
    845925        This method will need to be modified inorder for clients to
    846926        do something when this event occurs.
     
    857937
    858938    def userUpdatedStatus(self, room, user, show, status):
    859         """User Presence has been received
    860        
     939        """
     940        User Presence has been received
     941
    861942        This method will need to be modified inorder for clients to
    862943        do something when this event occurs.
    863        
     944
    864945        """
    865946        pass
    866        
     947
    867948
    868949    def receivedSubject(self, room, subject):
     
    885966    def _cbDisco(self, iq):
    886967        # grab query
    887        
     968
    888969        return getattr(iq,'query', None)
    889        
     970
    890971
    891972    def sendDeferred(self,  obj, timeout):
    892         """ Send data or a domish element, adding a deferred with a timeout.
    893        
     973        """
     974        Send data or a domish element, adding a deferred with a timeout.
     975
    894976        @param obj: The object to send over the wire.
    895977        @type  obj: L{domish.Element} or L{unicode}
     
    913995
    914996        call = reactor.callLater(timeout, onTimeout)
    915        
     997
    916998        def cancelTimeout(result):
    917999            if call.active():
     
    9251007        return d
    9261008
     1009
    9271010    def disco(self, entity, type='info'):
    928         """Send disco queries to a XMPP entity.
     1011        """
     1012        Send disco queries to a XMPP entity.
    9291013
    9301014        @param entity: The server or entity where we want discovery information from.
     
    9401024
    9411025        return iq.send().addBoth(self._cbDisco)
    942        
     1026
    9431027
    9441028    def configure(self, room_jid, fields=[]):
    945         """Configure a room
     1029        """
     1030        Configure a room
    9461031
    9471032        @param room_jid: The room jabber/xmpp entity id for the requested configuration form.
     
    9541039        request = ConfigureRequest(self.xmlstream, method='set', fields=fields)
    9551040        request['to'] = room_jid
    956        
     1041
    9571042        return request.send()
    9581043
     1044
    9591045    def getConfigureForm(self, room_jid):
    960         """Grab the configuration form from the room. This sends an iq request to the room.
     1046        """
     1047        Grab the configuration form from the room. This sends an iq request to the room.
    9611048
    9621049        @param room_jid: The room jabber/xmpp entity id for the requested configuration form.
     
    9701057
    9711058    def join(self, server, room, nick, history = None):
    972         """ Join a MUC room by sending presence to it. Returns a defered that is called when
    973         the entity is in the room or an error has occurred.
    974        
     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.
     1062
    9751063        @param server: The server where the room is located.
    9761064        @type  server: L{unicode}
     
    9811069        @param nick: The nick name for the entitity joining the room.
    9821070        @type  nick: L{unicode}
    983        
     1071
    9841072        @param history: The maximum number of history stanzas you would like.
    9851073
     
    9871075        r = Room(room, server, nick, state='joining')
    9881076        self._setRoom(r)
    989  
     1077
    9901078        p = BasicPresence(to=r.entity_id)
    9911079        if history is not None:
     
    9951083
    9961084        # add observer for joining the room
    997         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()), 
     1085        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    9981086                                          self._joinedRoom, 1, d)
    9991087
    10001088        return d
    1001    
     1089
     1090
    10021091    def _changeUserStatus(self, r, room_jid, status, show):
    10031092        # change the user status in a room.
     
    10151104        return user
    10161105
     1106
    10171107    def _changed(self, d, room_jid, prs):
    1018         """Callback for changing the nick and status.
     1108        """
     1109        Callback for changing the nick and status.
    10191110        """
    10201111
     
    10301121
    10311122    def nick(self, room_jid, new_nick):
    1032         """ Change an entities nick name in a MUC room.
    1033        
     1123        """
     1124        Change an entities nick name in a MUC room.
     1125
    10341126        See: http://xmpp.org/extensions/xep-0045.html#changenick
    10351127
     
    10391131        @param new_nick: The nick name for the entitity joining the room.
    10401132        @type  new_nick: L{unicode}
    1041        
    1042         """
    1043 
    1044        
     1133
     1134        """
     1135
     1136
    10451137        r = self._getRoom(room_jid)
    10461138        if r is None:
    10471139            raise NotFound
    10481140        r.nick = new_nick # change the nick
    1049         # create presence 
     1141        # create presence
    10501142        # make sure we call the method to generate the new entity xmpp id
    1051         p = BasicPresence(to=r.entityId()) 
     1143        p = BasicPresence(to=r.entityId())
    10521144        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    10531145
    10541146        # add observer for joining the room
    1055         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()), 
     1147        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    10561148                                          self._changed, 1, d, room_jid)
    10571149
    10581150        return d
    1059        
    1060 
    1061    
     1151
     1152
    10621153    def leave(self, room_jid):
    1063         """Leave a MUC room.
     1154        """
     1155        Leave a MUC room.
    10641156
    10651157        See: http://xmpp.org/extensions/xep-0045.html#exit
     
    10701162        """
    10711163        r = self._getRoom(room_jid)
    1072  
     1164
    10731165        p = xmppim.UnavailablePresence(to=r.entity_id)
    10741166
    10751167        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    10761168        # add observer for joining the room
    1077         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()), 
     1169        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()),
    10781170                                          self._leftRoom, 1, d)
    10791171
    10801172        return d
    1081    
     1173
    10821174
    10831175    def status(self, room_jid, show=None, status=None):
    1084         """Change user status.
     1176        """
     1177        Change user status.
    10851178
    10861179        See: http://xmpp.org/extensions/xep-0045.html#changepres
     
    10921185        @type  show: L{unicode}
    10931186
    1094         @param show: The current status of the entity. 
     1187        @param show: The current status of the entity.
    10951188        @type  show: L{unicode}
    10961189
     
    11001193            raise NotFound
    11011194
    1102         p = BasicPresence(to=r.entityId()) 
     1195        p = BasicPresence(to=r.entityId())
    11031196        if status is not None:
    11041197            p.addElement('status', None, status)
    1105            
     1198
    11061199        if show is not None:
    11071200            p.addElement('show', None, show)
    1108            
     1201
    11091202        d = self.sendDeferred(p, timeout=DEFER_TIMEOUT)
    11101203
    11111204        # add observer for joining the room
    1112         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()), 
     1205        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()),
    11131206                                          self._changed, 1, d, room_jid)
    11141207
    11151208        return d
     1209
    11161210
    11171211    def _sendMessage(self, msg, children=None):
     
    11201214            for c in children:
    11211215                msg.addChild(c)
    1122        
     1216
    11231217        self.xmlstream.send(msg)
    11241218
     1219
    11251220    def groupChat(self, to, message, children=None):
    1126         """Send a groupchat message
     1221        """
     1222        Send a groupchat message
    11271223        """
    11281224        msg = GroupChat(to, body=message)
    1129        
     1225
    11301226        self._sendMessage(msg, children=children)
    11311227
     1228
    11321229    def chat(self, room_jid, message, children=None):
    1133         """Send a private chat message to a user in a MUC room.
    1134        
     1230        """
     1231        Send a private chat message to a user in a MUC room.
     1232
    11351233        See: http://xmpp.org/extensions/xep-0045.html#privatemessage
    11361234
     
    11431241
    11441242        self._sendMessage(msg, children=children)
    1145        
     1243
     1244
    11461245    def invite(self, room_jid, reason=None, full_jid=None):
    1147         """Invite a xmpp entity to a MUC room.
     1246        """
     1247        Invite a xmpp entity to a MUC room.
    11481248
    11491249        See: http://xmpp.org/extensions/xep-0045.html#invite
     
    11511251        @param room_jid: The room entity id.
    11521252        @type  room_jid: L{jid.JID}
    1153        
     1253
    11541254        @param reason: The reason for the invite.
    11551255        @type  reason: L{unicode}
     
    11641264
    11651265    def password(self, room_jid, password):
    1166         """Send a password to a room so the entity can join.
    1167        
     1266        """
     1267        Send a password to a room so the entity can join.
     1268
    11681269        See: http://xmpp.org/extensions/xep-0045.html#enter-pw
    11691270
    11701271        @param room_jid: The room entity id.
    11711272        @type  room_jid: L{jid.JID}
    1172        
     1273
    11731274        @param password: The MUC room password.
    11741275        @type  password: L{unicode}
    1175        
     1276
    11761277        """
    11771278        p = PasswordPresence(room_jid, password)
    11781279
    11791280        self.xmlstream.send(p)
    1180    
     1281
     1282
    11811283    def register(self, room_jid, fields=[]):
    1182         """ Send a request to register for a room.
     1284        """
     1285        Send a request to register for a room.
    11831286
    11841287        @param room_jid: The room entity id.
     
    11981301        iq = AffiliationRequest(self.xmlstream,
    11991302                                method='get',
    1200                                 affiliation=affiliation, 
     1303                                affiliation=affiliation,
    12011304                                )
    12021305        iq['to'] = room_jid.full()
    1203         return iq.send()       
     1306        return iq.send()
    12041307
    12051308
     
    12111314                                )
    12121315        iq['to'] = room_jid.full()
    1213         return iq.send()       
     1316        return iq.send()
    12141317
    12151318
     
    12201323            affiliation_list = []
    12211324            setattr(r, affiliation, [])
    1222            
     1325
    12231326            for item in iq.query.elements():
    12241327                nick   = item.getAttribute('nick', None)
     
    12281331                    raise Exception, 'bad attributes in item list'
    12291332                if nick is not None:
    1230                     u = room.getUser(nick)
     1333                    u = r.getUser(nick)
    12311334                if u is None:
    12321335                    u = User(nick, user_jid=jid.internJID(entity))
    12331336                    u.affiliation = 'member'
    1234                    
     1337
    12351338                affiliation_list.append(u)
    12361339
     
    12381341        return r
    12391342
     1343
    12401344    def getMemberList(self, room_jid):
    1241         """ Get a member list from a room.
     1345        """
     1346        Get a member list from a room.
    12421347
    12431348        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     
    12491354        return d
    12501355
     1356
    12511357    def getAdminList(self, room_jid):
    1252         """ Get an admin list from a room.
     1358        """
     1359        Get an admin list from a room.
    12531360
    12541361        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     
    12601367        return d
    12611368
     1369
    12621370    def getBanList(self, room_jid):
    1263         """ Get an outcast list from a room.
     1371        """
     1372        Get an outcast list from a room.
    12641373
    12651374        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     
    12711380        return d
    12721381
     1382
    12731383    def getOwnerList(self, room_jid):
    1274         """ Get an owner list from a room.
     1384        """
     1385        Get an owner list from a room.
    12751386
    12761387        @param room_jid: The room jabber/xmpp entity id for the requested member list.
     
    12821393        return d
    12831394
     1395
    12841396    def getRegisterForm(self, room):
    1285         """ Grab the registration form for a MUC room.
     1397        """
     1398        Grab the registration form for a MUC room.
    12861399
    12871400        @param room: The room jabber/xmpp entity id for the requested registration form.
     
    12931406        return iq.send()
    12941407
     1408
    12951409    def destroy(self, room_jid, reason=None):
    1296         """ Destroy a room.
    1297        
     1410        """
     1411        Destroy a room.
     1412
    12981413        @param room_jid: The room jabber/xmpp entity id.
    12991414        @type  room_jid: L{jid.JID}
    1300        
     1415
    13011416        @ivar reason: The reason we are destroying the room.
    13021417        @type reason: L{unicode}
     
    13151430        return iq.send().addCallback(destroyed)
    13161431
     1432
    13171433    def subject(self, room_jid, subject):
    1318         """ Change the subject of a MUC room.
     1434        """
     1435        Change the subject of a MUC room.
    13191436
    13201437        See: http://xmpp.org/extensions/xep-0045.html#subject-mod
     
    13301447        self.xmlstream.send(msg)
    13311448
     1449
    13321450    def voice(self, room_jid):
    1333         """ Request voice for a moderated room.
     1451        """
     1452        Request voice for a moderated room.
    13341453
    13351454        @param room_jid: The room jabber/xmpp entity id.
     
    13411460
    13421461
    1343 
    13441462    def history(self, room_jid, message_list):
    1345         """ Send history to create a MUC based on a one on one chat.
    1346        
     1463        """
     1464        Send history to create a MUC based on a one on one chat.
     1465
    13471466        See: http://xmpp.org/extensions/xep-0045.html#continue
    13481467
     
    13541473
    13551474        """
    1356        
     1475
    13571476        for m in message_list:
    13581477            m['type'] = 'groupchat'
     
    13631482            d = m.addElement('delay', NS_DELAY)
    13641483            d['stamp'] = self._makeTimeStamp()
    1365             d['from'] = mto 
     1484            d['from'] = mto
    13661485
    13671486            self.xmlstream.send(m)
     1487
    13681488
    13691489    def _setAffiliation(self, frm, room_jid, affiliation, reason=None, a_jid=None, nick=None):
     
    13721492                                method='set',
    13731493                                affiliation=affiliation,
    1374                                 a_jid=a_jid, 
     1494                                a_jid=a_jid,
    13751495                                nick=nick,
    13761496                                reason=reason)
     
    13781498        iq['from'] = frm.full()
    13791499        return iq.send()
     1500
    13801501
    13811502    def _setRole(self, frm, room_jid, a_jid=None, reason=None, role='none', nick=None):
     
    13871508                         nick=nick,
    13881509                         reason=reason)
    1389        
     1510
    13901511        iq['to'] = room_jid.userhost() # this is a room jid, only send to room
    13911512        iq['from'] = frm.full()
    13921513        return iq.send()
     1514
    13931515
    13941516    def _cbRequest(self, room_jid, iq):
     
    13991521        return r
    14001522
     1523
    14011524    def modifyAffiliationList(self, frm, room_jid, jid_list, affiliation):
    1402         """Modify an affiliation list.
     1525        """
     1526        Modify an affiliation list.
    14031527
    14041528        @param frm: The entity sending the request.
     
    14251549        return iq.send()
    14261550
     1551
    14271552    def grantVoice(self, frm, room_jid, voice_jid=None, reason=None, nick=None):
    1428         """ Grant voice to an entity.
    1429        
     1553        """
     1554        Grant voice to an entity.
     1555
    14301556        @param frm: The entity sending the request.
    14311557        @type  frm: L{jid.JID}
     
    14331559        @param room_jid: The room jabber/xmpp entity id.
    14341560        @type  room_jid: L{jid.JID}
    1435        
     1561
    14361562        @param reason: The reason for granting voice to the entity.
    14371563        @type  reason: L{unicode}
     
    14431569        return self._setRole(frm, room_jid, role='participant', a_jid=voice_jid, nick=nick, reason=reason)
    14441570
     1571
    14451572    def grantVisitor(self, frm, room_jid, reason=None, nick=None):
    1446         """ Change a participant to a visitor. This will disallow the entity to send messages to a moderated room.
     1573        """
     1574        Change a participant to a visitor. This will disallow the entity to send messages to a moderated room.
    14471575        @param frm: The entity sending the request.
    14481576        @type  frm: L{jid.JID}
     
    14501578        @param room_jid: The room jabber/xmpp entity id.
    14511579        @type  room_jid: L{jid.JID}
    1452        
     1580
    14531581        @param reason: The reason for granting voice to the entity.
    14541582        @type  reason: L{unicode}
     
    14601588        return self._setRole(frm, room_jid, role='visitor', reason=reason, nick=nick)
    14611589
     1590
    14621591    def grantModerator(self, frm, room_jid, reason=None, nick=None):
    1463         """Grant moderator priviledges to a MUC room.
     1592        """
     1593        Grant moderator priviledges to a MUC room.
    14641594
    14651595        @param frm: The entity sending the request.
     
    14721602        return self._setRole(frm, room_jid, role='moderator', reason=reason, nick=nick)
    14731603
     1604
    14741605    def ban(self, room_jid, ban_jid, frm, reason=None, nick=None):
    1475         """Ban a user from a MUC room.
     1606        """
     1607        Ban a user from a MUC room.
    14761608
    14771609        @param room_jid: The room jabber/xmpp entity id.
     
    14931625        return self._setAffiliation(frm, room_jid, 'outcast', nick=nick, a_jid=ban_jid, reason=reason)
    14941626
    1495     def kick(self, room_jid, kick_jid, frm, reason=None, nick=None):       
    1496         """Kick a user from a MUC room.
     1627
     1628    def kick(self, room_jid, kick_jid, frm, reason=None, nick=None):
     1629        """
     1630        Kick a user from a MUC room.
    14971631
    14981632        @param room_jid: The room jabber/xmpp entity id.
     
    15131647        """
    15141648        return self._setAffiliation(frm, room_jid, 'none', a_jid=kick_jid, nick=nick, reason=reason)
    1515        
  • wokkel/test/test_muc.py

    r127 r129  
    1 # Copyright (c) 2003-2008 Ralph Meijer
     1# Copyright (c) 2003-2009 Ralph Meijer
    22# See LICENSE for details.
    33
     
    1111from twisted.internet import defer
    1212from twisted.words.xish import domish, xpath
    13 from twisted.words.protocols.jabber import error
    1413from twisted.words.protocols.jabber.jid import JID
    1514
    16 from wokkel import data_form, iwokkel, muc, shim, disco
    17 from wokkel.generic import parseXml
     15from wokkel import data_form, iwokkel, muc, disco
    1816from wokkel.test.helpers import XmlStreamStub
    1917
    20 try:
    21     from twisted.words.protocols.jabber.xmlstream import toResponse
    22 except ImportError:
    23     from wokkel.compat import toResponse
     18from twisted.words.protocols.jabber.xmlstream import toResponse
    2419
    2520
     
    3934
    4035    return d, func
     36
    4137
    4238
     
    5753        self.user_jid = JID('test@jabber.org/Testing')
    5854
     55
    5956    def _createRoom(self):
    6057        """A helper method to create a test room.
     
    8178        """
    8279        p = muc.UserPresence()
    83         p['to'] = self.user_jid.full()
     80        p['to'] = self.user_jid.full()
    8481        p['from'] = self.room_jid.full()
    8582
     
    9087            self.failUnless(room.name==self.test_room, 'Wrong room name')
    9188            self.failUnless(room.inRoster(user), 'User not in roster')
    92                            
    93        
     89
     90
    9491        d, self.protocol.userJoinedRoom = calledAsync(userPresence)
    9592        self.stub.send(p)
     
    10198        """
    10299        m = muc.GroupChat('test@test.com',body='test')
    103         m['from'] = self.room_jid.full()
     100        m['from'] = self.room_jid.full()
    104101
    105102        self._createRoom()
     
    108105            self.failUnless(message=='test', "Wrong group chat message")
    109106            self.failUnless(room.name==self.test_room, 'Wrong room name')
    110                            
    111        
     107
     108
    112109        d, self.protocol.receivedGroupChat = calledAsync(groupChat)
    113110        self.stub.send(m)
     
    123120            # check namespace
    124121            self.failUnless(query.uri==disco.NS_INFO, 'Wrong namespace')
    125            
     122
    126123
    127124        d = self.protocol.disco(test_srv)
     
    129126
    130127        iq = self.stub.output[-1]
    131        
     128
    132129        # send back a response
    133130        response = toResponse(iq, 'result')
     
    138135                                                    name='Macbeth Chat Service',
    139136                                                    type='text'))
    140        
    141         self.stub.send(response)
    142         return d
    143        
    144 
    145        
     137
     138        self.stub.send(response)
     139        return d
     140
     141
    146142    def test_joinRoom(self):
    147143        """Joining a room
    148144        """
    149        
     145
    150146        def cb(room):
    151147            self.assertEquals(self.test_room, room.name)
     
    158154        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    159155
    160         # send back user presence, they joined       
     156        # send back user presence, they joined
    161157        response = muc.UserPresence(frm=self.test_room+'@'+self.test_srv+'/'+self.test_nick)
    162158        self.stub.send(response)
    163159        return d
    164160
    165    
    166161
    167162    def test_joinRoomForbidden(self):
     
    170165
    171166        def cb(error):
    172            
     167
    173168            self.failUnless(error.value.mucCondition=='forbidden','Wrong muc condition')
    174169
    175            
    176            
     170
     171
    177172        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
    178173        d.addBoth(cb)
     
    182177        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    183178        # send back user presence, they joined
    184        
     179
    185180        response = muc.PresenceError(error=muc.MUCError('auth',
    186181                                                        'forbidden'
     
    188183                                     frm=self.room_jid.full())
    189184        self.stub.send(response)
    190         return d       
     185        return d
    191186
    192187
     
    196191
    197192        def cb(error):
    198            
     193
    199194            self.failUnless(error.value.mucCondition=='jid-malformed','Wrong muc condition')
    200195
    201            
    202            
     196
     197
    203198        d = self.protocol.join(self.test_srv, self.test_room, self.test_nick)
    204199        d.addBoth(cb)
     
    208203        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    209204        # send back user presence, they joined
    210        
     205
    211206        response = muc.PresenceError(error=muc.MUCError('modify',
    212207                                                        'jid-malformed'
     
    214209                                     frm=self.room_jid.full())
    215210        self.stub.send(response)
    216         return d       
    217 
     211        return d
    218212
    219213
     
    229223
    230224        prs = self.stub.output[-1]
    231        
     225
    232226        self.failUnless(prs['type']=='unavailable', 'Unavailable is not being sent')
    233        
     227
    234228        response = prs
    235229        response['from'] = response['to']
     
    238232        self.stub.send(response)
    239233        return d
    240        
     234
    241235
    242236    def test_userPartsRoom(self):
     
    245239
    246240        p = muc.UnavailableUserPresence()
    247         p['to'] = self.user_jid.full()
     241        p['to'] = self.user_jid.full()
    248242        p['from'] = self.room_jid.full()
    249243
     
    259253            self.failUnless(room.name==self.test_room, 'Wrong room name')
    260254            self.failUnless(room.inRoster(user)==False, 'User in roster')
    261                            
     255
    262256        d, self.protocol.userLeftRoom = calledAsync(userPresence)
    263257        self.stub.send(p)
    264258        return d
    265        
     259
    266260
    267261    def test_ban(self):
     
    272266            self.failUnless(banned, 'Did not ban user')
    273267
    274            
     268
    275269        d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam')
    276270        d.addCallback(cb)
    277271
    278272        iq = self.stub.output[-1]
    279        
     273
    280274        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza')
    281275
     
    294288            self.failUnless(kicked, 'Did not kick user')
    295289
    296            
     290
    297291        d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam')
    298292        d.addCallback(cb)
    299293
    300294        iq = self.stub.output[-1]
    301        
     295
    302296        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza')
    303297
     
    308302        return d
    309303
    310        
    311304
    312305    def test_password(self):
    313306        """Sending a password via presence to a password protected room.
    314307        """
    315        
     308
    316309        self.protocol.password(self.room_jid, 'secret')
    317        
    318         prs = self.stub.output[-1]
    319        
     310
     311        prs = self.stub.output[-1]
     312
    320313        self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza')
    321314
     
    325318        """
    326319        m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test')
    327         m['from'] = self.room_jid.full()
    328        
     320        m['from'] = self.room_jid.full()
     321
    329322        self._createRoom()
    330323
     
    332325            self.failUnless(body=='test', "wrong message body")
    333326            self.failUnless(stamp, 'Does not have a history stamp')
    334                    
     327
    335328
    336329        d, self.protocol.receivedHistory = calledAsync(roomHistory)
     
    369362            self.failUnless(m.name=='message', 'Wrong stanza')
    370363            self.failUnless(xpath.matches("/message/delay", m), 'Invalid history stanza')
    371        
     364
    372365
    373366    def test_invite(self):
     
    383376
    384377
    385        
    386378    def test_privateMessage(self):
    387379        """Send private messages to muc entities.
     
    400392
    401393        """
    402        
     394
    403395        def cb(iq):
    404396            # check for a result
    405397            self.failUnless(iq['type']=='result', 'We did not get a result')
    406        
     398
    407399        d = self.protocol.register(self.room_jid)
    408400        d.addCallback(cb)
     
    410402        iq = self.stub.output[-1]
    411403        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request')
    412        
    413         response = toResponse(iq, 'result')
    414        
    415         self.stub.send(response)
    416         return d
     404
     405        response = toResponse(iq, 'result')
     406
     407        self.stub.send(response)
     408        return d
     409
    417410
    418411    def test_voice(self):
     
    422415
    423416        m = self.stub.output[-1]
    424        
     417
    425418        self.failUnless(xpath.matches("/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_MUC_REQUEST,), m), 'Invalid voice message stanza')
    426419
     
    432425        def cb(iq):
    433426            self.failUnless(iq['type']=='result', 'Not a result')
    434            
     427
    435428
    436429        fields = []
     
    439432                                      var='muc#roomconfig_roomname',
    440433                                      value=self.test_room))
    441        
     434
    442435        d = self.protocol.configure(self.room_jid.userhost(), fields)
    443436        d.addCallback(cb)
     
    445438        iq = self.stub.output[-1]
    446439        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/x"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request')
    447        
     440
    448441        response = toResponse(iq, 'result')
    449442        self.stub.send(response)
     
    457450        def cb(destroyed):
    458451            self.failUnless(destroyed==True, 'Room not destroyed.')
    459                    
     452
    460453        d = self.protocol.destroy(self.room_jid)
    461454        d.addCallback(cb)
     
    463456        iq = self.stub.output[-1]
    464457        self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/destroy"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request')
    465        
     458
    466459        response = toResponse(iq, 'result')
    467460        self.stub.send(response)
     
    473466        """
    474467        test_nick = 'newNick'
    475        
     468
    476469        self._createRoom()
    477470
     
    487480        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    488481
    489         # send back user presence, they joined       
     482        # send back user presence, they joined
    490483        response = muc.UserPresence(frm=self.test_room+'@'+self.test_srv+'/'+test_nick)
    491        
    492         self.stub.send(response)
    493         return d
     484
     485        self.stub.send(response)
     486        return d
     487
    494488
    495489    def test_grantVoice(self):
     
    501495            self.failUnless(give_voice, 'Did not give voice user')
    502496
    503            
     497
    504498        d = self.protocol.grantVoice(self.user_jid, self.room_jid, give_voice)
    505499        d.addCallback(cb)
    506500
    507501        iq = self.stub.output[-1]
    508        
     502
    509503        self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@role='participant']" % (self.room_jid.userhost(),), iq), 'Wrong voice stanza')
    510504
     
    530524            self.failUnless(u.status == 'testing MUC', 'Wrong status')
    531525            self.failUnless(u.show == 'xa', 'Wrong show')
    532            
     526
    533527        d = self.protocol.status(self.room_jid, 'xa', 'testing MUC')
    534528        d.addCallback(cb)
     
    539533        self.failUnless(getattr(prs, 'x', None), 'No muc x element')
    540534
    541         # send back user presence, they joined       
     535        # send back user presence, they joined
    542536        response = muc.UserPresence(frm=self.room_jid.full())
    543537        response.addElement('show', None, 'xa')
    544538        response.addElement('status', None, 'testing MUC')
    545539        self.stub.send(response)
    546         return d       
     540        return d
Note: See TracChangeset for help on using the changeset viewer.