plugin XEP-0045: small refactorization + clean on profile's disconnection

This commit is contained in:
souliane 2014-06-23 16:07:13 +02:00
commit 7d849b11ca

View file

@ -90,12 +90,12 @@ class XEP_0045(object):
client.muc_service = service client.muc_service = service
return self.getMUCService(profile_key=profile).addCallback(assign_service) return self.getMUCService(profile_key=profile).addCallback(assign_service)
def __check_profile(self, profile): def checkClient(self, profile):
"""check if profile is used and connected """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 @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): if not profile or profile not in self.clients or not self.host.isConnected(profile):
log.error(_('Unknown or disconnected profile (%s)') % profile) log.error(_('Unknown or disconnected profile (%s)') % profile)
if profile in self.clients: if profile in self.clients:
@ -103,6 +103,20 @@ class XEP_0045(object):
return False return False
return True 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): def __room_joined(self, room, profile):
"""Called when the user is in the requested room""" """Called when the user is in the requested room"""
@ -141,7 +155,7 @@ class XEP_0045(object):
"""Return room where user is""" """Return room where user is"""
profile = self.host.memory.getProfileName(profile_key) profile = self.host.memory.getProfileName(profile_key)
result = [] result = []
if not self.__check_profile(profile): if not self.checkClient(profile):
return result return result
for room in self.clients[profile].joined_rooms.values(): for room in self.clients[profile].joined_rooms.values():
result.append((room.roomJID.userhost(), [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))
@ -154,7 +168,7 @@ class XEP_0045(object):
@profile_key: profile @profile_key: profile
@return: nick or empty string in case of error""" @return: nick or empty string in case of error"""
profile = self.host.memory.getProfileName(profile_key) 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 ''
return self.clients[profile].joined_rooms[room_jid_s].nick return self.clients[profile].joined_rooms[room_jid_s].nick
@ -210,11 +224,7 @@ class XEP_0045(object):
@param profile_key: %(doc_profile_key)s @param profile_key: %(doc_profile_key)s
@return: configuration form as XMLUI @return: configuration form as XMLUI
""" """
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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"))
def config2XMLUI(result): def config2XMLUI(result):
if not result: if not result:
@ -248,17 +258,13 @@ class XEP_0045(object):
def isNickInRoom(self, room_jid, nick, profile): def isNickInRoom(self, room_jid, nick, profile):
"""Tell if a nick is currently present in a room""" """Tell if a nick is currently present in a room"""
profile = self.host.memory.getProfileName(profile) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].joined_rooms[room_jid.userhost()].inRoster(muc.User(nick)) return self.clients[profile].joined_rooms[room_jid.userhost()].inRoster(muc.User(nick))
def getRoomsSubjects(self, profile_key=C.PROF_KEY_NONE): def getRoomsSubjects(self, profile_key=C.PROF_KEY_NONE):
"""Return received subjects of rooms""" """Return received subjects of rooms"""
profile = self.host.memory.getProfileName(profile_key) profile = self.host.memory.getProfileName(profile_key)
if not self.__check_profile(profile): if not self.checkClient(profile):
return [] return []
return self.clients[profile].rec_subjects.values() return self.clients[profile].rec_subjects.values()
@ -311,7 +317,7 @@ class XEP_0045(object):
return d return d
profile = self.host.memory.getProfileName(profile_key) profile = self.host.memory.getProfileName(profile_key)
if not self.__check_profile(profile): if not self.checkClient(profile):
return _errDeferred() return _errDeferred()
if room_jid.userhost() in self.clients[profile].joined_rooms: 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()}) log.warning(_('%(profile)s is already in room %(room_jid)s') % {'profile': profile, 'room_jid': room_jid.userhost()})
@ -332,7 +338,7 @@ class XEP_0045(object):
@return the room userhost (given value or unique generated name) @return the room userhost (given value or unique generated name)
""" """
profile = self.host.memory.getProfileName(profile_key) profile = self.host.memory.getProfileName(profile_key)
if not self.__check_profile(profile): if not self.checkClient(profile):
return return
if room_jid_s == "": if room_jid_s == "":
room_jid_s = self.getUniqueName(profile_key=profile_key) room_jid_s = self.getUniqueName(profile_key=profile_key)
@ -348,27 +354,15 @@ class XEP_0045(object):
return room_jid_s return room_jid_s
def nick(self, room_jid, nick, profile_key): def nick(self, room_jid, nick, profile_key):
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].nick(room_jid, nick) return self.clients[profile].nick(room_jid, nick)
def leave(self, room_jid, profile_key): def leave(self, room_jid, profile_key):
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].leave(room_jid) return self.clients[profile].leave(room_jid)
def subject(self, room_jid, subject, profile_key): def subject(self, room_jid, subject, profile_key):
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].subject(room_jid, subject) return self.clients[profile].subject(room_jid, subject)
def mucNick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE): def mucNick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE):
@ -383,6 +377,12 @@ class XEP_0045(object):
self.clients[profile] = SatMUCClient(self) self.clients[profile] = SatMUCClient(self)
return self.clients[profile] 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): def kick(self, nick, room_jid, options={}, profile_key=C.PROF_KEY_NONE):
""" """
Kick a participant from the room Kick a participant from the room
@ -391,11 +391,7 @@ class XEP_0045(object):
@param options (dict): attribute with extra info (reason, password) as in #XEP-0045 @param options (dict): attribute with extra info (reason, password) as in #XEP-0045
@param profile_key (str): %(doc_profile_key)s @param profile_key (str): %(doc_profile_key)s
""" """
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].kick(room_jid, nick, reason=options.get('reason', None)) 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): def ban(self, entity_jid, room_jid, options={}, profile_key=C.PROF_KEY_NONE):
@ -408,11 +404,7 @@ class XEP_0045(object):
""" """
assert(not entity_jid.resource) assert(not entity_jid.resource)
assert(not room_jid.resource) assert(not room_jid.resource)
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
return self.clients[profile].ban(room_jid, entity_jid, reason=options.get('reason', None)) 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): def affiliate(self, entity_jid, room_jid, options=None, profile_key=C.PROF_KEY_NONE):
@ -426,11 +418,7 @@ class XEP_0045(object):
assert(not entity_jid.resource) assert(not entity_jid.resource)
assert(not room_jid.resource) assert(not room_jid.resource)
assert('affiliation' in options) assert('affiliation' in options)
profile = self.host.memory.getProfileName(profile_key) profile = self.getProfileAssertInRoom(room_jid, 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")
# TODO: handles reason and nick # TODO: handles reason and nick
return self.clients[profile].modifyAffiliationList(room_jid, [entity_jid], options['affiliation']) return self.clients[profile].modifyAffiliationList(room_jid, [entity_jid], options['affiliation'])
@ -626,9 +614,9 @@ class XEP_0045(object):
whois_msg.append(_("Nickname: %s") % user.nick) whois_msg.append(_("Nickname: %s") % user.nick)
if user.entity: if user.entity:
whois_msg.append(_("Entity: %s") % user.entity) whois_msg.append(_("Entity: %s") % user.entity)
if user.affiliation!='none': if user.affiliation != 'none':
whois_msg.append(_("Affiliation: %s") % user.affiliation) whois_msg.append(_("Affiliation: %s") % user.affiliation)
if user.role!='none': if user.role != 'none':
whois_msg.append(_("Role: %s") % user.role) whois_msg.append(_("Role: %s") % user.role)
if user.status: if user.status:
whois_msg.append(_("Status: %s") % user.status) whois_msg.append(_("Status: %s") % user.status)
@ -646,7 +634,7 @@ class SatMUCClient (muc.MUCClient):
self.joined_rooms = {} # FIXME: seem to do the same thing as MUCClient's _rooms attribute, must be removed self.joined_rooms = {} # FIXME: seem to do the same thing as MUCClient's _rooms attribute, must be removed
self.rec_subjects = {} self.rec_subjects = {}
self.__changing_nicks = set() # used to keep trace of who is changing nick, 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" print "init SatMUCClient OK"
def subject(self, room, subject): def subject(self, room, subject):