diff src/plugins/plugin_xep_0249.py @ 2045:48536a22b599

plugin XEP-0249: some modernisation, don't use anymore deprecated methods, invite bridge method use a single param for room jid
author Goffi <goffi@goffi.org>
date Sun, 28 Aug 2016 18:18:10 +0200
parents a5eb6f6b8db2
children 6a66c8c5a567
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0249.py	Sun Aug 28 18:16:03 2016 +0200
+++ b/src/plugins/plugin_xep_0249.py	Sun Aug 28 18:18:10 2016 +0200
@@ -17,11 +17,12 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from sat.core.i18n import _
+from sat.core.i18n import _, D_
 from sat.core.constants import Const as C
 from sat.core import exceptions
 from sat.core.log import getLogger
 log = getLogger(__name__)
+from sat.tools import xml_tools
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
 
@@ -81,7 +82,7 @@
         log.info(_("Plugin XEP_0249 initialization"))
         self.host = host
         host.memory.updateParams(self.params)
-        host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite)
+        host.bridge.addMethod("inviteMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._invite)
         try:
             self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
         except KeyError:
@@ -90,36 +91,35 @@
     def getHandler(self, profile):
         return XEP_0249_handler(self)
 
-    def invite(self, target, room, options={}, profile_key=C.PROF_KEY_NONE):
-        """
-        Invite a user to a room
-        @param target: jid of the user to invite
-        @param room: jid of the room where the user is invited
-        @options: attribute with extra info (reason, password) as in #XEP-0249
-        @profile_key: %(doc_profile_key)s
-        """
-        profile = self.host.memory.getProfileName(profile_key)
-        if not profile:
-            log.error(_("Profile doesn't exists !"))
-            return
-        message = domish.Element((None, 'message'))
-        message["to"] = target.full()
-        x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x'))
-        x_elt['jid'] = room.userhost()
-        for opt in options:
-            x_elt[opt] = options[opt]
-        self.host.profiles[profile].xmlstream.send(message)
+    def _invite(self, guest_jid_s, room_jid_s, options, profile_key):
+        """Invite an user to a room
 
-    def _invite(self, target, service, roomId, options={}, profile_key=C.PROF_KEY_NONE):
-        """
-        Invite an user to a room
-        @param target: jid of the user to invite
+        @param guest_jid_s: jid of the user to invite
         @param service: jid of the MUC service
         @param roomId: name of the room
         @param profile_key: %(doc_profile_key)s
         """
         #TODO: check parameters validity
-        self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key)
+        client = self.host.getClient(profile_key)
+        self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options))
+
+    def invite(self, client, guest, room, options={}):
+        """Invite a user to a room
+
+        @param guest(jid.JID): jid of the user to invite
+        @param room(jid.JID): jid of the room where the user is invited
+        @param options(dict): attribute with extra info (reason, password) as in #XEP-0249
+        """
+        message = domish.Element((None, 'message'))
+        message["to"] = guest.full()
+        x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x'))
+        x_elt['jid'] = room.userhost()
+        for key, value in options.iteritems():
+            if key not in ('password', 'reason', 'thread'):
+                log.warning(u"Ignoring invalid invite option: {}".format(key))
+                continue
+            x_elt[key] = value
+        client.xmlstream.send(message)
 
     def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE):
         """Accept the invitation to join a MUC.
@@ -156,17 +156,21 @@
 
         autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile)
 
-        def accept_cb(conf_id, accepted, data, profile):
-            if conf_id == room_jid_s and accepted:
-                self._accept(room_jid, profile)
-
         if autojoin == "always":
             self._accept(room_jid, profile)
         elif autojoin == "never":
-            self.host.bridge.newAlert(_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_jid_s, 'room': room_jid_s}, _("MUC invitation"), "INFO", profile)
+            msg = D_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_jid_s, 'room': room_jid_s}
+            title = D_("MUC invitation")
+            xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO)
         else:  # leave the default value here
-            data = {"message": _("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_jid_s, 'room': room_jid_s}, "title": _("MUC invitation")}
-            self.host.askConfirmation(room_jid_s, "YES/NO", data, accept_cb, profile)
+            confirm_msg = D_("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_jid_s, 'room': room_jid_s}
+            confirm_title = D_("MUC invitation")
+            d = xml_tools.deferConfirm(self.host, confirm_msg, confirm_title, profile=profile)
+            def accept_cb(accepted):
+                if accepted:
+                    self._accept(room_jid, profile)
+
+            d.addCallback(accept_cb)
 
     def cmd_invite(self, client, mess_data):
         """invite someone in the room
@@ -184,7 +188,7 @@
             return False
         if not contact_jid.user:
             contact_jid.user, contact_jid.host = contact_jid.host, my_host
-        self.invite(contact_jid, mess_data["to"], {}, client.profile)
+        self.invite(client, contact_jid, mess_data["to"])
         return False