changeset 1083:e8731b02f5ea

plugin XEP-0045: small refactorization + clean on profile's disconnection
author souliane <souliane@mailoo.org>
date Mon, 23 Jun 2014 16:07:13 +0200
parents 246712d2e7bc
children 03dcb6ca7e49
files src/plugins/plugin_xep_0045.py
diffstat 1 files changed, 40 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0045.py	Mon Jun 23 15:42:56 2014 +0200
+++ b/src/plugins/plugin_xep_0045.py	Mon Jun 23 16:07:13 2014 +0200
@@ -90,12 +90,12 @@
             client.muc_service = service
         return self.getMUCService(profile_key=profile).addCallback(assign_service)
 
-    def __check_profile(self, profile):
-        """check if profile is used and connected
+    def checkClient(self, profile):
+        """Check if the profile is connected and has used the MUC feature.
 
-        if profile known but disconnected, remove it from known profiles
+        If profile was using MUC feature but is now disconnected, remove it from the client list.
         @param profile: profile to check
-        @return: True if the profile is known and connected, else False"""
+        @return: True if the profile is connected and has used the MUC feature, else False"""
         if not profile or profile not in self.clients or not self.host.isConnected(profile):
             log.error(_('Unknown or disconnected profile (%s)') % profile)
             if profile in self.clients:
@@ -103,6 +103,20 @@
             return False
         return True
 
+    def getProfileAssertInRoom(self, room_jid, profile_key):
+        """Retrieve the profile name, assert that it's connected and participating in the given room.
+
+        @param room_jid (JID): room JID
+        @param profile_key (str): %(doc_profile_key)
+        @return: the profile name
+        """
+        profile = self.host.memory.getProfileName(profile_key)
+        if not self.checkClient(profile):
+            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
+        if room_jid.userhost() not in self.clients[profile].joined_rooms:
+            raise UnknownRoom("This room has not been joined")
+        return profile
+
     def __room_joined(self, room, profile):
         """Called when the user is in the requested room"""
 
@@ -141,7 +155,7 @@
         """Return room where user is"""
         profile = self.host.memory.getProfileName(profile_key)
         result = []
-        if not self.__check_profile(profile):
+        if not self.checkClient(profile):
             return result
         for room in self.clients[profile].joined_rooms.values():
             result.append((room.roomJID.userhost(), [user.nick for user in room.roster.values()], room.nick))
@@ -154,7 +168,7 @@
         @profile_key: profile
         @return: nick or empty string in case of error"""
         profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile) or room_jid_s not in self.clients[profile].joined_rooms:
+        if not self.checkClient(profile) or room_jid_s not in self.clients[profile].joined_rooms:
             return ''
         return self.clients[profile].joined_rooms[room_jid_s].nick
 
@@ -210,11 +224,7 @@
         @param profile_key: %(doc_profile_key)s
         @return: configuration form as XMLUI
         """
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom(D_("This room has not been joined"))
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
 
         def config2XMLUI(result):
             if not result:
@@ -248,17 +258,13 @@
 
     def isNickInRoom(self, room_jid, nick, profile):
         """Tell if a nick is currently present in a room"""
-        profile = self.host.memory.getProfileName(profile)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile)
         return self.clients[profile].joined_rooms[room_jid.userhost()].inRoster(muc.User(nick))
 
     def getRoomsSubjects(self, profile_key=C.PROF_KEY_NONE):
         """Return received subjects of rooms"""
         profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
+        if not self.checkClient(profile):
             return []
         return self.clients[profile].rec_subjects.values()
 
@@ -311,7 +317,7 @@
             return d
 
         profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
+        if not self.checkClient(profile):
             return _errDeferred()
         if room_jid.userhost() in self.clients[profile].joined_rooms:
             log.warning(_('%(profile)s is already in room %(room_jid)s') % {'profile': profile, 'room_jid': room_jid.userhost()})
@@ -332,7 +338,7 @@
         @return the room userhost (given value or unique generated name)
         """
         profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
+        if not self.checkClient(profile):
             return
         if room_jid_s == "":
             room_jid_s = self.getUniqueName(profile_key=profile_key)
@@ -348,27 +354,15 @@
         return room_jid_s
 
     def nick(self, room_jid, nick, profile_key):
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         return self.clients[profile].nick(room_jid, nick)
 
     def leave(self, room_jid, profile_key):
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         return self.clients[profile].leave(room_jid)
 
     def subject(self, room_jid, subject, profile_key):
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         return self.clients[profile].subject(room_jid, subject)
 
     def mucNick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE):
@@ -383,6 +377,12 @@
         self.clients[profile] = SatMUCClient(self)
         return self.clients[profile]
 
+    def profileDisconnected(self, profile):
+        try:
+            del self.clients[profile]
+        except KeyError:
+            pass
+
     def kick(self, nick, room_jid, options={}, profile_key=C.PROF_KEY_NONE):
         """
         Kick a participant from the room
@@ -391,11 +391,7 @@
         @param options (dict): attribute with extra info (reason, password) as in #XEP-0045
         @param profile_key (str): %(doc_profile_key)s
         """
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         return self.clients[profile].kick(room_jid, nick, reason=options.get('reason', None))
 
     def ban(self, entity_jid, room_jid, options={}, profile_key=C.PROF_KEY_NONE):
@@ -408,11 +404,7 @@
         """
         assert(not entity_jid.resource)
         assert(not room_jid.resource)
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         return self.clients[profile].ban(room_jid, entity_jid, reason=options.get('reason', None))
 
     def affiliate(self, entity_jid, room_jid, options=None, profile_key=C.PROF_KEY_NONE):
@@ -426,11 +418,7 @@
         assert(not entity_jid.resource)
         assert(not room_jid.resource)
         assert('affiliation' in options)
-        profile = self.host.memory.getProfileName(profile_key)
-        if not self.__check_profile(profile):
-            raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
-        if room_jid.userhost() not in self.clients[profile].joined_rooms:
-            raise UnknownRoom("This room has not been joined")
+        profile = self.getProfileAssertInRoom(room_jid, profile_key)
         # TODO: handles reason and nick
         return self.clients[profile].modifyAffiliationList(room_jid, [entity_jid], options['affiliation'])
 
@@ -626,9 +614,9 @@
         whois_msg.append(_("Nickname: %s") % user.nick)
         if user.entity:
             whois_msg.append(_("Entity: %s") % user.entity)
-        if user.affiliation!='none':
+        if user.affiliation != 'none':
             whois_msg.append(_("Affiliation: %s") % user.affiliation)
-        if user.role!='none':
+        if user.role != 'none':
             whois_msg.append(_("Role: %s") % user.role)
         if user.status:
             whois_msg.append(_("Status: %s") % user.status)
@@ -646,7 +634,7 @@
         self.joined_rooms = {}  # FIXME: seem to do the same thing as MUCClient's _rooms attribute, must be removed
         self.rec_subjects = {}
         self.__changing_nicks = set()  # used to keep trace of who is changing nick,
-                                      # and to discard userJoinedRoom signal in this case
+                                       # and to discard userJoinedRoom signal in this case
         print "init SatMUCClient OK"
 
     def subject(self, room, subject):