Ignore:
Timestamp:
Oct 7, 2008, 11:47:13 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@83
Message:

some documentation and additions to the interface, also some more abstraction and tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/muc.py

    r112 r113  
    6666
    6767GROUPCHAT     = MESSAGE +'[@type="groupchat"]/body'
    68 SUBJECT       = MESSAGE +'[@type="groupchat"]/body'
     68SUBJECT       = MESSAGE +'[@type="groupchat"]/subject'
    6969MESSAGE_ERROR = MESSAGE +'[@type="error"]'
    7070
     
    121121    Configure MUC room request.
    122122
     123    http://xmpp.org/extensions/xep-0045.html#roomconfig
     124
    123125    @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'}
    124126    @type method: C{str}
     
    243245            d['from'] = h_frm
    244246
     247class User(object):
     248    """
     249    A user/entity in a multi-user chat room.
     250    """
     251   
     252    def __init__(self, nick, user_jid=None):
     253        self.nick = nick
     254        self.user_jid = user_jid
     255        self.affiliation = 'none'
     256        self.role = 'none'
     257       
     258        self.status = None
     259        self.show   = None
     260
     261
    245262class Room(object):
    246263    """
     
    261278               
    262279        self.roster = {}
     280
     281       
     282
     283    def addUser(self, user):
     284        """
     285        """
     286        self.roster[user.nick.lower()] = user
     287
     288    def inRoster(self, user):
     289        """
     290        """
     291
     292        return self.roster.has_key(user.nick.lower())
     293
     294    def getUser(self, nick):
     295        """
     296        """
     297        return self.roster.get(nick.lower())
    263298
    264299       
     
    341376
    342377    def connectionInitialized(self):
    343         self.xmlstream.addObserver(PRESENCE+"/x", self._onXPresence)
     378        self.xmlstream.addObserver(PRESENCE+"[not(@type) or @type='available']/x", self._onXPresence)
     379        self.xmlstream.addObserver(PRESENCE+"[@type='unavailable']", self._onUnavailablePresence)
     380        self.xmlstream.addObserver(PRESENCE+"[@type='error']", self._onPresenceError)
    344381        self.xmlstream.addObserver(GROUPCHAT, self._onGroupChat)
    345382        self.xmlstream.addObserver(SUBJECT, self._onSubject)
     
    356393            del self.rooms[room_jid.full().lower()]
    357394
     395
     396    def _onUnavailablePresence(self, prs):
     397        """
     398        """
     399        if not prs.hasAttribute('from'):
     400            return
     401        room_jid = jid.internJID(prs.getAttribute('from', ''))
     402
     403
     404    def _onPresenceError(self, prs):
     405        """
     406        """
     407        if not prs.hasAttribute('from'):
     408            return
     409        room_jid = jid.internJID(prs.getAttribute('from', ''))
     410
     411
    358412    def _onXPresence(self, prs):
    359413        """
    360414        """
    361         if prs.x.uri == NS_USER:
    362             self.receivedUserPresence(prs)
     415        if not prs.hasAttribute('from'):
     416            return
     417        room_jid = jid.internJID(prs.getAttribute('from', ''))
     418           
     419        status = getattr(prs, 'status', None)
     420        show   = getattr(prs, 'show', None)
     421       
     422        # grab room
     423        room = self._getRoom(room_jid)
     424        if room is None:
     425            # not in the room yet
     426            return
     427
     428        # check if user is in roster
     429        user = room.getUser(room_jid.resource)
     430        if user is None: # create a user that does not exist
     431            user = User(room_jid.resource)
     432           
     433       
     434        if room.inRoster(user):
     435            # we changed status or nick
     436            muc_status = getattr(prs.x, 'status', None)
     437            if muc_status:
     438                code = muc_status.getAttribute('code', 0)
     439            else:
     440                self.userUpdatedStatus(room, user, show, status)
     441        else:           
     442            room.addUser(user)
     443            self.userJoinedRoom(room, user)
    363444           
    364445
     
    366447        """
    367448        """
     449        if not msg.hasAttribute('from'):
     450            # need to return an error here
     451            return
     452        room_jid = jid.internJID(msg.getAttribute('from', ''))
     453
     454        room = self._getRoom(room_jid)
     455        if room is None:
     456            # not in the room yet
     457            return
     458        user = room.getUser(room_jid.resource)
     459
    368460        delay = getattr(msg, 'delay', None)
     461        body  = unicode(msg.body)
     462        # grab room
    369463        if delay is None:
    370             self.receivedGroupChat(msg)
     464            self.receivedGroupChat(room, user, body)
    371465        else:
    372             self.receivedHistory(msg)
     466            self.receivedHistory(room, user, body, delay['stamp'], frm=delay.getAttribute('from',None))
    373467
    374468
     
    376470        """
    377471        """
    378         self.receivedSubject(msg)
     472        if not msg.hasAttribute('from'):
     473            return
     474        room_jid = jid.internJID(msg['from'])
     475
     476        # grab room
     477        room = self._getRoom(room_jid)
     478        if room is None:
     479            # not in the room yet
     480            return
     481
     482        self.receivedSubject(room_jid, unicode(msg.subject))
    379483
    380484
     
    426530            d.callback(True)
    427531
    428     def receivedUserPresence(self, prs):
     532    def userJoinedRoom(self, room, user):
     533        """User has joined a room
     534        """
     535        pass
     536
     537
     538    def userUserUpdatedStatus(self, room, user, show, status):
    429539        """User Presence has been received
    430540        """
     
    432542       
    433543
    434     def receivedSubject(self, msg):
     544    def receivedSubject(self, room, subject):
    435545        """
    436546        """
     
    438548
    439549
    440     def receivedHistory(self, msg):
     550    def receivedHistory(self, room, user, message, history, frm=None):
    441551        """
    442552        """
     
    533643       
    534644    def invite(self, to, reason=None, full_jid=None):
     645        """
     646        """
    535647        msg = InviteMessage(to, reason=reason, full_jid=full_jid)
    536648        self._sendMessage(msg)
     
    547659        return iq.send()
    548660
    549     def getRegisterForm(self, to):
     661    def getRegisterForm(self, room):
     662        """
     663        """
    550664        iq = RegisterRequest(self.xmlstream)
    551         iq['to'] = to
     665        iq['to'] = room.userhost()
    552666        return iq.send()
    553667
Note: See TracChangeset for help on using the changeset viewer.