diff src/plugins/plugin_xep_0249.py @ 689:78bf4ed37574

plugin XEP-249: added parameter Misc / Auto-join MUC on invitation
author souliane <souliane@mailoo.org>
date Tue, 05 Nov 2013 21:08:31 +0100
parents 84a6e83157c2
children 3344f1d8a232
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0249.py	Tue Oct 29 16:26:55 2013 +0100
+++ b/src/plugins/plugin_xep_0249.py	Tue Nov 05 21:08:31 2013 +0100
@@ -35,6 +35,9 @@
 MESSAGE = '/message'
 NS_DIRECT_MUC_INVITATION = 'jabber:x:conference'
 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]'
+AUTOJOIN_KEY = "Misc"
+AUTOJOIN_NAME = "Auto-join MUC on invitation"
+AUTOJOIN_VALUES = ["ask", "always", "never"]
 
 PLUGIN_INFO = {
     "name": "XEP 0249 Plugin",
@@ -50,9 +53,29 @@
 
 class XEP_0249(object):
 
+    params = """
+    <params>
+    <individual>
+    <category name="%(category_name)s" label="%(category_label)s">
+        <param name="%(param_name)s" label="%(param_label)s" value="%(param_default)s" type="list" security="0">
+            %(param_options)s
+        </param>
+     </category>
+    </individual>
+    </params>
+    """ % {
+        'category_name': AUTOJOIN_KEY,
+        'category_label': _("Misc"),
+        'param_name': AUTOJOIN_NAME,
+        'param_label': _("Auto-join MUC on invitation"),
+        'param_default': AUTOJOIN_VALUES[0],
+        'param_options': ['<option value="%s"/>' % value for value in AUTOJOIN_VALUES]
+    }
+
     def __init__(self, host):
         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)
 
     def getHandler(self, profile):
@@ -89,21 +112,45 @@
         #TODO: check parameters validity
         self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key)
 
+    def _accept(self, room, profile_key='@DEFAULT@'):
+        """
+        Accept the invitation to join a MUC
+        @param room: room jid as string
+        """
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error(_("Profile doesn't exists !"))
+            return
+        info(_('Invitation accepted for room %(room)s [%(profile)s]') % {'room': room, 'profile': profile})
+        _jid, xmlstream = self.host.getJidNStream(profile)
+        d = self.host.plugins["XEP-0045"].join(jid.JID(room), _jid.user, {}, profile)
+
     def onInvitation(self, message, profile):
         """
         called when an invitation is received
         @param message: message element
         @profile: %(doc_profile)s
         """
-        info(_('Invitation received for room %(room)s [%(profile)s]') % {'room': '', 'profile': profile})
         try:
-            room = jid.JID(message.firstChildElement()['jid'])
+            room = message.firstChildElement()['jid']
+            info(_('Invitation received for room %(room)s [%(profile)s]') % {'room': room, 'profile': profile})
         except:
             error(_('Error while parsing invitation'))
             return
-        _jid, xmlstream = self.host.getJidNStream(profile)
-        #TODO: we always autojoin so far, we need to add a parameter to autojoin/ignore invitations or let user choose to follow it
-        d = self.host.plugins["XEP-0045"].join(room, _jid.user, {}, profile)
+        autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile)
+        from_ = message["from"]
+
+        def accept_cb(conf_id, accepted, data, profile):
+            if conf_id == room and accepted:
+                self._accept(room, profile)
+
+        if autojoin == "always":
+            self._accept(room, profile)
+        elif autojoin == "ask":
+            data = {"message": _("You have been invited by %s to join the room %s. Do you accept?") % (from_, room), "title": _("MUC invitation")}
+            self.host.askConfirmation(room, "YES/NO", data, accept_cb, profile)
+        else:
+            self.host.bridge.newAlert(_("An invitation from %s to join the room %s has been declined according to your personal settings.") % (from_, room), _("MUC invitation"), "INFO", profile)
 
 
 class XEP_0249_handler(XMPPHandler):