Ignore:
Timestamp:
Aug 12, 2011, 3:57:20 PM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Redo room configuration.

  • The underlying request object now derives from generic.Request.
  • MUCClient.getConfiguration now returns a wokkel.data_form.Form.
  • MUCClient.configure now takes a simple dictionary of values.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r147 r148  
    4444
    4545
    46 class ConfigureRequest(xmlstream.IQ):
     46class ConfigureRequest(generic.Request):
    4747    """
    4848    Configure MUC room request.
     
    5454    """
    5555
    56     def __init__(self, xs, method='get', fields=[]):
    57         xmlstream.IQ.__init__(self, xs, method)
    58         q = self.addElement((NS_MUC_OWNER, 'query'))
    59         if method == 'set':
    60             # build data form
    61             form = data_form.Form('submit', formNamespace=NS_MUC_CONFIG)
    62             q.addChild(form.toElement())
    63 
    64             for f in fields:
    65                 # create a field
    66                 form.addField(f)
     56    def __init__(self, recipient, sender=None, options=None):
     57        if options is None:
     58            stanzaType = 'get'
     59        else:
     60            stanzaType = 'set'
     61
     62        generic.Request.__init__(self, recipient, sender, stanzaType)
     63        self.options = options
     64
     65
     66    def toElement(self):
     67        element = generic.Request.toElement(self)
     68
     69        query = element.addElement((NS_MUC_OWNER, 'query'))
     70        if self.options is None:
     71            # This is a request for the configuration form.
     72            form = None
     73        elif not self.options:
     74            form = data_form.Form(formType='cancel')
     75        else:
     76            form = data_form.Form(formType='submit', formNamespace=NS_MUC_CONFIG)
     77            form.makeFields(self.options)
     78
     79        if form:
     80            query.addChild(form.toElement())
     81
     82        return element
    6783
    6884
     
    826842
    827843
    828     def configure(self, roomJID, fields=[]):
     844    def configure(self, roomJID, options):
    829845        """
    830846        Configure a room.
    831847
     848        @param roomJID: The room to configure.
     849        @type roomJID: L{jid.JID}
     850
     851        @param options: A mapping of field names to values, or C{None} to cancel.
     852        @type options: C{dict}
     853        """
     854        if not options:
     855            options = False
     856        request = ConfigureRequest(recipient=roomJID, options=options)
     857        return self.request(request)
     858
     859
     860    def getConfiguration(self, roomJID):
     861        """
     862        Grab the configuration from the room.
     863
     864        This sends an iq request to the room.
     865
    832866        @param roomJID: The bare JID of the room.
    833867        @type roomJID: L{jid.JID}
    834868
    835         @param fields: The fields we want to modify.
    836         @type fields: A L{list} or L{dataform.Field}
    837         """
    838         request = ConfigureRequest(self.xmlstream, method='set', fields=fields)
    839         request['to'] = roomJID
    840 
    841         return request.send()
    842 
    843 
    844     def getConfigureForm(self, roomJID):
    845         """
    846         Grab the configuration form from the room.
    847 
    848         This sends an iq request to the room.
    849 
    850         @param roomJID: The bare JID of the room.
    851         @type roomJID: L{jid.JID}
    852         """
    853         request = ConfigureRequest(self.xmlstream)
    854         request['to'] = roomJID
    855         return request.send()
     869        @return: A deferred that fires with the room's configuration form as
     870            a L{data_form.Form} or C{None} if there are no configuration
     871            options available.
     872        """
     873        def cb(response):
     874            form = data_form.findForm(response.query, NS_MUC_CONFIG)
     875            return form
     876
     877        request = ConfigureRequest(recipient=roomJID, options=None)
     878        d = self.request(request)
     879        d.addCallback(cb)
     880        return d
    856881
    857882
Note: See TracChangeset for help on using the changeset viewer.