diff src/plugins/plugin_xep_0045.py @ 405:10b4f577d0c0

MUC update to follow wokkel's MUC branch update - bridge methods have been updated (API change) - plugin XEP-0045 has been updated
author Goffi <goffi@goffi.org>
date Sat, 08 Oct 2011 18:43:17 +0200
parents f964dcec1611
children b03b38b20c18
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0045.py	Sat Oct 08 11:19:25 2011 +0200
+++ b/src/plugins/plugin_xep_0045.py	Sat Oct 08 18:43:17 2011 +0200
@@ -58,14 +58,14 @@
         info(_("Plugin XEP_0045 initialization"))
         self.host = host
         self.clients={}
-        host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssss', out_sign='', method=self._join)
-        host.bridge.addMethod("getRoomJoined", ".plugin", in_sign='s', out_sign='a(ssass)', method=self.getRoomJoined)
-        host.bridge.addMethod("getRoomSubjects", ".plugin", in_sign='s', out_sign='a(sss)', method=self.getRoomSubjects)
+        host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._join)
+        host.bridge.addMethod("getRoomsJoined", ".plugin", in_sign='s', out_sign='a(sass)', method=self.getRoomsJoined)
+        host.bridge.addMethod("getRoomsSubjectss", ".plugin", in_sign='s', out_sign='a(ss)', method=self.getRoomsSubjectss)
         host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='s', out_sign='s', method=self.getUniqueName)
-        host.bridge.addSignal("roomJoined", ".plugin", signature='ssasss') #args: room_id, room_service, room_nicks, user_nick, profile
-        host.bridge.addSignal("roomUserJoined", ".plugin", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile
-        host.bridge.addSignal("roomUserLeft", ".plugin", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile
-        host.bridge.addSignal("roomNewSubject", ".plugin", signature='ssss') #args: room_id, room_service, subject, profile
+        host.bridge.addSignal("roomJoined", ".plugin", signature='sasss') #args: room_jid, room_nicks, user_nick, profile
+        host.bridge.addSignal("roomUserJoined", ".plugin", signature='ssa{ss}s') #args: room_jid, user_nick, user_data, profile
+        host.bridge.addSignal("roomUserLeft", ".plugin", signature='ssa{ss}s') #args: room_jid, user_nick, user_data, profile
+        host.bridge.addSignal("roomNewSubject", ".plugin", signature='sss') #args: room_jid, subject, profile
 
     def __check_profile(self, profile):
         """check if profile is used and connected
@@ -82,17 +82,15 @@
     def __room_joined(self, room, profile):
         """Called when the user is in the requested room"""
         def _sendBridgeSignal(ignore=None):
-            self.host.bridge.roomJoined(room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick, profile)
+            self.host.bridge.roomJoined(room.roomJID.userhost(), [user.nick for user in room.roster.values()], room.nick, profile)
 
-        room_jid = room.roomIdentifier+'@'+room.service
-        self.clients[profile].joined_rooms[room_jid] = room
+        room_jid_s = room.roomJID.userhost()
+        self.clients[profile].joined_rooms[room_jid_s] = room
         if room.status == '201':
             #FIXME: the current behaviour is to create an instant room
             #and send the signal only when the room is unlocked
             #a proper configuration management should be done
-            #TODO: wokkel's muc currently doesn't manage correctly message from the room
-            #      service (without resource) in room.getUser 
-            self.clients[profile].configure(room_jid).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room')))
+            self.clients[profile].configure(room_jid_s).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room')))
         else:
             _sendBridgeSignal()
         return room
@@ -104,14 +102,14 @@
         error (mess)
         self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile)
 
-    def getRoomJoined(self, profile_key='@DEFAULT@'):
+    def getRoomsJoined(self, profile_key='@DEFAULT@'):
         """Return room where user is"""
         profile = self.host.memory.getProfileName(profile_key)
         result = []
         if not self.__check_profile(profile):
             return result
         for room in self.clients[profile].joined_rooms.values():
-            result.append((room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick))
+            result.append((room.roomJID.userhost(), [user.nick for user in room.roster.values()], room.nick))
         return result
 
     def getRoomNick(self, room_jid, profile_key='@DEFAULT@'):
@@ -125,7 +123,7 @@
         return self.clients[profile].joined_rooms[room_jid].nick
 
 
-    def getRoomSubjects(self, profile_key='@DEFAULT@'):
+    def getRoomsSubjectss(self, profile_key='@DEFAULT@'):
         """Return received subjects of rooms"""
         profile = self.host.memory.getProfileName(profile_key)
         if not self.__check_profile(profile):
@@ -138,7 +136,7 @@
         #TODO: we should be able to select the MUC service here
         return uuid.uuid1() 
 
-    def join(self, service, roomId, nick, profile_key='@DEFAULT@'):
+    def join(self, room_jid, nick, options, profile_key='@DEFAULT@'):
         def _errDeferred(exc_obj = Exception, txt='Error while joining room'):
             d = defer.Deferred()
             d.errback(exc_obj(txt))
@@ -146,23 +144,19 @@
         profile = self.host.memory.getProfileName(profile_key)
         if not self.__check_profile(profile):
             return _errDeferred() 
-        room_jid = roomId+'@'+service
-        if self.clients[profile].joined_rooms.has_key(room_jid):
-            warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid})
+        if self.clients[profile].joined_rooms.has_key(room_jid.userhost()):
+            warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid.userhost()})
             return _errDeferred() 
-        info (_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile':profile,'room':roomId+'@'+service, 'nick':nick})
-        try:
-            return self.clients[profile].join(service, roomId, nick).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackKeywords={'profile':profile})
-        except:
-            #XXX: this is a ugly workaround as MUCClient thrown an error if there is invalid chars in the room jid (like with the default string)
-            #FIXME: must be removed when MUCClient manage this better
-            d = _errDeferred(txt="ugly workaround")
-            d.addErrback(self.__err_joining_room, profile)
-            return d
+        info (_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile':profile,'room':room_jid.userhost(), 'nick':nick})
 
-    def _join(self, service, roomId, nick, profile_key='@DEFAULT@'):
+        history_options = options["history"] == "True" if options.has_key("history") else None
+        password = options["password"] if options.has_key("password") else None
+        
+        return self.clients[profile].join(room_jid, nick, history_options, password).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackKeywords={'profile':profile})
+
+    def _join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'):
         """join method used by bridge: use the _join method, but doesn't return any deferred"""
-        d = self.join(service, roomId, nick, profile_key)
+        d = self.join(jid.JID(room_jid), nick, options, profile_key)
         d.addErrback(lambda x: warning(_('Error while joining room'))) #TODO: error management + signal in bridge
     
     def getHandler(self, profile):
@@ -189,24 +183,22 @@
         debug (_("user %(nick)s has joined room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()})
         if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile):
             return
-        user_data={'entity':user.entity or '', 'affiliation':user.affiliation, 'role':user.role} 
-        self.host.bridge.roomUserJoined(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile)
+        user_data={'entity':user.entity.full() if user.entity else  '', 'affiliation':user.affiliation, 'role':user.role} 
+        self.host.bridge.roomUserJoined(room.roomJID.userhost(), user.nick, user_data, self.parent.profile)
     
     def userLeftRoom(self, room, user):
         debug (_("user %(nick)s left room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()})
-        user_data={'entity':user.entity or '', 'affiliation':user.affiliation, 'role':user.role} 
-        self.host.bridge.roomUserLeft(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile)
+        user_data={'entity':user.entity.full() if user.entity else  '', 'affiliation':user.affiliation, 'role':user.role} 
+        self.host.bridge.roomUserLeft(room.rooJID.userhost(), user.nick, user_data, self.parent.profile)
 
     def userUpdatedStatus(self, room, user, show, status):
         print("FIXME: MUC status not managed yet")
         #FIXME: gof
 
-    def receivedSubject(self, occupantJID, subject):
-        room = self._getRoom(occupantJID)
-        debug (_("New subject for room (%(room_id)s): %(subject)s") % {'room_id':room.occupantJID.userhost(),'subject':subject})
-        room_jid = room.roomIdentifier+'@'+room.service
-        self.rec_subjects[room_jid] = (room.roomIdentifier, room.service, subject)
-        self.host.bridge.roomNewSubject(room.roomIdentifier, room.service, subject, self.parent.profile)
+    def receivedSubject(self, room, user, subject):
+        debug (_("New subject for room (%(room_id)s): %(subject)s") % {'room_id':room.roomJID.full(),'subject':subject})
+        self.rec_subjects[room.roomJID.userhost()] = (room.roomJID.userhost(), subject)
+        self.host.bridge.roomNewSubject(room.roomJID.userhost(), subject, self.parent.profile)
 
     #def connectionInitialized(self):
         #pass