diff src/plugins/plugin_xep_0045.py @ 1989:757c512fe06c

plugin XEP-0045: various fixes/improvments: - use self.txt_cmds instead of self.host.plugins["XEP-0045"] - nick and options in join have now default values - directly raise ConflictError when room is already joined instead of using defer.fail - removed self.host.getClient where it's now useless because client is used instead of profile
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:23 +0200
parents fbd313cfd40b
children dfbe0bb056dc
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0045.py	Fri Jul 01 00:00:12 2016 +0200
+++ b/src/plugins/plugin_xep_0045.py	Fri Jul 01 00:00:23 2016 +0200
@@ -84,12 +84,12 @@
         self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True)
         host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM)
         try:
-            txt_cmds = self.host.plugins[C.TEXT_CMDS]
+            self.txt_cmds = self.host.plugins[C.TEXT_CMDS]
         except KeyError:
             log.info(_(u"Text commands not available"))
         else:
-            txt_cmds.registerTextCommands(self)
-            txt_cmds.addWhoIsCb(self._whois, 100)
+            self.txt_cmds.registerTextCommands(self)
+            self.txt_cmds.addWhoIsCb(self._whois, 100)
 
         host.trigger.add("presence_available", self.presenceTrigger)
         host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=1000000)
@@ -376,10 +376,8 @@
         @return: unicode (the room bare)
         """
         client = self.host.getClient(profile_key)
-        if options is None:
-            options = {}
         if room_jid_s:
-            muc_service = self.host.getClient(client.profile).muc_service
+            muc_service = client.muc_service
             try:
                 room_jid = jid.JID(room_jid_s)
             except (RuntimeError, jid.InvalidFormat, AttributeError):
@@ -392,7 +390,11 @@
         d = self.join(client, room_jid, nick, options)
         return d.addCallback(lambda room: room.roomJID.userhost())
 
-    def join(self, client, room_jid, nick, options):
+    def join(self, client, room_jid, nick=None, options=None):
+        if not nick:
+            nick = client.jid.user
+        if options is None:
+            options = {}
         def _errDeferred(exc_obj=Exception, txt=u'Error while joining room'):
             d = defer.Deferred()
             d.errback(exc_obj(txt))
@@ -400,7 +402,7 @@
 
         if room_jid in client._muc_client.joined_rooms:
             log.warning(_(u'{profile} is already in room {room_jid}').format(profile=client.profile, room_jid = room_jid.userhost()))
-            return defer.fail(exceptions.ConflictError(_(u"The room has already been joined")))
+            raise failure.Failure(exceptions.ConflictError(_(u"The room has already been joined")))
         log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick))
 
         password = options["password"] if "password" in options else None
@@ -496,9 +498,9 @@
             - JID: room to join (on the same service if full jid is not specified)
         """
         if mess_data["unparsed"].strip():
-            room_jid = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
+            room_jid = self.text_cmds.getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
             nick = (self.getRoomNick(client, room_jid) or
-                    self.host.getClient(client.profile).jid.user)
+                    client.jid.user)
             self.join(client, room_jid, nick, {})
 
         return False
@@ -510,7 +512,7 @@
             - ROOM_JID: jid of the room to live (current room if not specified)
         """
         if mess_data["unparsed"].strip():
-            room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
+            room = self.text_cmds.getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
         else:
             room = mess_data["to"]
 
@@ -538,7 +540,7 @@
             assert self.isNickInRoom(client, mess_data["to"], nick)
         except (IndexError, AssertionError):
             feedback = _(u"You must provide a member's nick to kick.")
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
+            self.text_cmds.feedBack(client, feedback, mess_data)
             return False
 
         d = self.kick(client, nick, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
@@ -547,7 +549,7 @@
             feedback_msg = _(u'You have kicked {}').format(nick)
             if len(options) > 1:
                 feedback_msg += _(u' for the following reason: {}').format(options[1])
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data)
+            self.text_cmds.feedBack(client, feedback_msg, mess_data)
             return True
         d.addCallback(cb)
         return d
@@ -567,7 +569,7 @@
             assert(entity_jid.host)
         except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError):
             feedback = _(u"You must provide a valid JID to ban, like in '/ban contact@example.net'")
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
+            self.text_cmds.feedBack(client, feedback, mess_data)
             return False
 
         d = self.ban(client, entity_jid, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
@@ -576,7 +578,7 @@
             feedback_msg = _(u'You have banned {}').format(entity_jid)
             if len(options) > 1:
                 feedback_msg += _(u' for the following reason: {}').format(options[1])
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data)
+            self.text_cmds.feedBack(client, feedback_msg, mess_data)
             return True
         d.addCallback(cb)
         return d
@@ -600,20 +602,20 @@
             assert(entity_jid.host)
         except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError):
             feedback = _(u"You must provide a valid JID to affiliate, like in '/affiliate contact@example.net member'")
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
+            self.text_cmds.feedBack(client, feedback, mess_data)
             return False
 
         affiliation = options[1] if len(options) > 1 else 'none'
         if affiliation not in AFFILIATIONS:
             feedback = _(u"You must provide a valid affiliation: %s") % ' '.join(AFFILIATIONS)
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
+            self.text_cmds.feedBack(client, feedback, mess_data)
             return False
 
         d = self.affiliate(client, entity_jid, mess_data["to"], {'affiliation': affiliation})
 
         def cb(dummy):
             feedback_msg = _(u'New affiliation for %(entity)s: %(affiliation)s').format(entity=entity_jid, affiliation=affiliation)
-            self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data)
+            self.text_cmds.feedBack(client, feedback_msg, mess_data)
             return True
         d.addCallback(cb)
         return d