Changeset 151:fb6eaad60bb8 for wokkel


Ignore:
Timestamp:
Aug 15, 2011, 5:39:17 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Reimplement RegisterRequest? by generalizing the form handling for room config.

Location:
wokkel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/iwokkel.py

    r150 r151  
    916916
    917917
    918     def register(roomJID, fields=[]):
     918    def register(roomJID, options):
    919919        """
    920920        Send a request to register for a room.
     
    923923        @type roomJID: L{jid.JID}
    924924
    925         @param fields: The fields you need to register.
    926         @type fields: L{list} of L{dataform.Field}
    927         """
    928 
     925        @param options: A mapping of field names to values, or C{None} to
     926            cancel.
     927        @type options: C{dict}
     928        """
    929929
    930930
  • wokkel/muc.py

    r150 r151  
    3333NS_MUC_REGISTER = NS_MUC + '#register'
    3434
    35 NS_REQUEST = 'jabber:iq:register'
     35NS_REGISTER = 'jabber:iq:register'
    3636
    3737MESSAGE = '/message'
     
    4444
    4545
    46 class ConfigureRequest(generic.Request):
    47     """
    48     Configure MUC room request.
    49 
    50     http://xmpp.org/extensions/xep-0045.html#roomconfig
    51 
    52     @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    53     @type method: C{str}
    54     """
     46class _FormRequest(generic.Request):
     47    """
     48    Base class for form exchange requests.
     49    """
     50    requestNamespace = None
     51    formNamespace = None
    5552
    5653    def __init__(self, recipient, sender=None, options=None):
     
    6764        element = generic.Request.toElement(self)
    6865
    69         query = element.addElement((NS_MUC_OWNER, 'query'))
     66        query = element.addElement((self.requestNamespace, 'query'))
    7067        if self.options is None:
    7168            # This is a request for the configuration form.
     
    7471            form = data_form.Form(formType='cancel')
    7572        else:
    76             form = data_form.Form(formType='submit', formNamespace=NS_MUC_CONFIG)
     73            form = data_form.Form(formType='submit',
     74                                  formNamespace=self.formNamespace)
    7775            form.makeFields(self.options)
    7876
     
    8482
    8583
    86 class RegisterRequest(xmlstream.IQ):
    87     """
    88     Register room request.
    89 
    90     @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    91     @type method: C{str}
    92     """
    93 
    94     def __init__(self, xs, method='get', fields=[]):
    95         xmlstream.IQ.__init__(self, xs, method)
    96         q = self.addElement((NS_REQUEST, 'query'))
    97         if method == 'set':
    98             # build data form
    99             form_type = 'submit'
    100             form = data_form.Form(form_type, formNamespace=NS_MUC_REGISTER)
    101             q.addChild(form.toElement())
    102 
    103             for f in fields:
    104                 # create a field
    105                 form.addField(f)
     84class ConfigureRequest(_FormRequest):
     85    """
     86    Configure MUC room request.
     87
     88    http://xmpp.org/extensions/xep-0045.html#roomconfig
     89    """
     90
     91    requestNamespace = NS_MUC_OWNER
     92    formNamespace = NS_MUC_CONFIG
     93
     94
     95
     96class RegisterRequest(_FormRequest):
     97    """
     98    Register request.
     99
     100    http://xmpp.org/extensions/xep-0045.html#register
     101    """
     102
     103    requestNamespace = NS_REGISTER
     104    formNamespace = NS_MUC_REGISTER
    106105
    107106
     
    10871086
    10881087
    1089     def register(self, roomJID, fields=[]):
     1088    def register(self, roomJID, options):
    10901089        """
    10911090        Send a request to register for a room.
     
    10941093        @type roomJID: L{jid.JID}
    10951094
    1096         @param fields: The fields you need to register.
    1097         @type fields: L{list} of L{dataform.Field}
    1098         """
    1099         iq = RegisterRequest(self.xmlstream, method='set', fields=fields)
    1100         iq['to'] = roomJID.userhost()
    1101         return iq.send()
     1095        @param options: A mapping of field names to values, or C{None} to
     1096            cancel.
     1097        @type options: C{dict}
     1098        """
     1099        request = RegisterRequest(recipient=roomJID, options=options)
     1100        return self.request(request)
    11021101
    11031102
  • wokkel/test/test_muc.py

    r150 r151  
    559559        """
    560560
    561         # FIXME: this doesn't really test the registration
    562 
    563561        def cb(iq):
    564562            # check for a result
    565563            self.assertEquals('result', iq['type'], 'We did not get a result')
    566564
    567         d = self.protocol.register(self.roomJID)
    568         d.addCallback(cb)
    569 
    570         iq = self.stub.output[-1]
    571         query = "/iq/query[@xmlns='%s']" % muc.NS_REQUEST
    572         self.assertTrue(xpath.matches(query, iq), 'Invalid iq register request')
     565        d = self.protocol.register(self.roomJID,
     566                                   {'muc#register_roomnick': 'thirdwitch'})
     567        d.addCallback(cb)
     568
     569        iq = self.stub.output[-1]
     570
     571        query = "/iq/query[@xmlns='%s']" % muc.NS_REGISTER
     572        nodes = xpath.queryForNodes(query, iq)
     573        self.assertNotIdentical(None, nodes, 'Invalid registration request')
     574
     575        form = data_form.findForm(nodes[0], muc.NS_MUC_REGISTER)
     576        self.assertNotIdentical(None, form, 'Missing registration form')
     577        self.assertEquals('submit', form.formType)
     578        self.assertIn('muc#register_roomnick', form.fields)
     579
    573580
    574581        response = toResponse(iq, 'result')
Note: See TracChangeset for help on using the changeset viewer.