Ignore:
Timestamp:
Oct 6, 2008, 10:56:52 PM (14 years ago)
Author:
Christopher Zorn <tofu@…>
Branch:
wokkel-muc-client-support-24
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/branches/wokkel-muc-client-support-24@76
Message:

all tests pass, needs comments, also need to get the entire xep in the tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r111 r112  
    121121    Configure MUC room request.
    122122
    123     @ivar namespace: Request namespace.
    124     @type namespace: C{str}
    125123    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    126124    @type method: C{str}
     
    144142    Register room request.
    145143
    146     @ivar namespace: Request namespace.
    147     @type namespace: C{str}
    148144    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    149145    @type method: C{str}
     146
    150147    """
    151148
     
    162159                # create a field
    163160                form.addField(f)
     161
     162
     163class AffiliationRequest(xmlstream.IQ):
     164    """
     165    Register room request.
     166
     167    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
     168    @type method: C{str}
     169
     170    @ivar affiliation: The affiliation type to send to room.
     171    @type affiliation: C{str}
     172
     173    """
     174
     175    def __init__(self, xs, method='get', affiliation='none', a_jid=None, reason=None):
     176        xmlstream.IQ.__init__(self, xs, method)
     177       
     178        q = self.addElement((NS_ADMIN, 'query'))
     179        i = q.addElement('item')
     180
     181        i['affiliation'] = affiliation
     182        if a_jid:
     183            i['jid'] = a_jid.full()
     184           
     185        if reason:
     186            i.addElement('reason', None, reason)
     187
     188           
     189       
    164190
    165191class GroupChat(domish.Element):
     
    178204        if subject:
    179205            self.addElement('subject',None, subject)
    180            
    181 
     206
     207
     208class PrivateChat(domish.Element):
     209    """
     210    """
     211    def __init__(self, to, body=None, frm=None):
     212        """To needs to be a string
     213        """
     214        domish.Element.__init__(self, (None, 'message'))
     215        self['type'] = 'chat'
     216        self['to']   = to
     217        if frm:
     218            self['from'] = frm
     219        if body:
     220            self.addElement('body',None, body)
     221           
     222class InviteMessage(PrivateChat):
     223    def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None):
     224        PrivateChat.__init__(self, to, body=body, frm=frm)
     225        del self['type'] # remove type
     226        x = self.addElement('x', NS_USER)
     227        invite = x.addElement('invite')
     228        if full_jid:
     229            invite['to'] = full_jid
     230        if reason:
     231            invite.addElement('reason', None, reason)
     232        if password:
     233            invite.addElement('password', None, password)
     234
     235class HistoryMessage(GroupChat):
     236    """
     237    """
     238    def __init__(self, to, stamp, body=None, subject=None, frm=None, h_frm=None):
     239        GroupChat.__init__(self, to, body=body, subject=subject, frm=frm)
     240        d = self.addElement('delay', NS_DELAY)
     241        d['stamp'] = stamp
     242        if h_frm:
     243            d['from'] = h_frm
    182244
    183245class Room(object):
     
    304366        """
    305367        """
    306         self.receivedGroupChat(msg)
    307 
     368        delay = getattr(msg, 'delay', None)
     369        if delay is None:
     370            self.receivedGroupChat(msg)
     371        else:
     372            self.receivedHistory(msg)
    308373
    309374
     
    372437        pass
    373438
     439
     440    def receivedHistory(self, msg):
     441        """
     442        """
     443        pass
     444
     445
    374446    def _cbDisco(self, iq):
    375447        # grab query
     
    425497        d = defer.Deferred()
    426498
    427         self._getRoom(room_jid)
     499        r = self._getRoom(room_jid)
    428500 
    429501        p = xmppim.UnavailablePresence(to=r.entity_id)
     
    432504
    433505        # add observer for joining the room
    434         self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and type='unavailable']" % (r.entity_id.full()),
     506        self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()),
    435507                                          self._leftRoom, 1, d)
    436508
     
    440512   
    441513
    442     def groupChat(self, to, message, children=None):
    443         """Send a groupchat message
    444         """
    445         msg = GroupChat(to, body=message)
     514    def _sendMessage(self, msg, children=None):
    446515
    447516        if children:
     
    451520        self.xmlstream.send(msg)
    452521
     522    def groupChat(self, to, message, children=None):
     523        """Send a groupchat message
     524        """
     525        msg = GroupChat(to, body=message)
     526       
     527        self._sendMessage(msg, children=children)
     528
     529    def chat(self, to, message, children=None):
     530        msg = PrivateChat(to, body=message)
     531
     532        self._sendMessage(msg, children=children)
     533       
     534    def invite(self, to, reason=None, full_jid=None):
     535        msg = InviteMessage(to, reason=reason, full_jid=full_jid)
     536        self._sendMessage(msg)
     537
     538
     539    def password(self, to, password):
     540        p = PasswordPresence(to, password)
     541
     542        self.xmlstream.send(p)
    453543   
    454544    def register(self, to, fields=[]):
     
    491581            self.xmlstream.send(m)
    492582
     583    def ban(self, to, ban_jid, frm, reason=None):
     584       
     585        iq = AffiliationRequest(self.xmlstream,
     586                                method='set',
     587                                affiliation='outcast',
     588                                a_jid=ban_jid,
     589                                reason=reason)
     590        iq['to'] = to.userhost() # this is a room jid, only send to room
     591        iq['from'] = frm.full()
     592        return iq.send()
     593
     594
     595    def kick(self, to, kick_jid, frm, reason=None):
     596       
     597        iq = AffiliationRequest(self.xmlstream,
     598                                method='set',
     599                                a_jid=kick_jid,
     600                                reason=reason)
     601        iq['to'] = to.userhost() # this is a room jid, only send to room
     602        iq['from'] = frm.full()
     603        return iq.send()
Note: See TracChangeset for help on using the changeset viewer.