changeset 2177:09cfec4d8d19

plugin XEP-0077: added inBandAccountNew, inBandUnregister and inBandPasswordChange bridge methods
author Goffi <goffi@goffi.org>
date Thu, 09 Mar 2017 23:07:38 +0100
parents 61128d260eef
children b9bfc45cea22
files src/plugins/plugin_misc_account.py src/plugins/plugin_xep_0077.py
diffstat 2 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_account.py	Thu Mar 09 00:06:13 2017 +0100
+++ b/src/plugins/plugin_misc_account.py	Thu Mar 09 23:07:38 2017 +0100
@@ -482,7 +482,7 @@
             error_ui.addText(D_("Your XMPP account could not be deleted: %s") % failure.getErrorMessage())
             return defer.succeed({'xmlui': error_ui.toXml()})
 
-        d = self.host.plugins['XEP-0077'].removeRegistration(client, jid.JID(client.jid.host))
+        d = self.host.plugins['XEP-0077'].unregister(client, jid.JID(client.jid.host))
         d.addCallbacks(userDeleted, errback)
         return d
 
--- a/src/plugins/plugin_xep_0077.py	Thu Mar 09 00:06:13 2017 +0100
+++ b/src/plugins/plugin_xep_0077.py	Thu Mar 09 23:07:38 2017 +0100
@@ -81,9 +81,18 @@
     def __init__(self, host):
         log.info(_("Plugin XEP_0077 initialization"))
         self.host = host
-        host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='s',
+        host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='',
                               method=self._inBandRegister,
                               async=True)
+        host.bridge.addMethod("inBandAccountNew", ".plugin", in_sign='ssssi', out_sign='',
+                              method=self._registerNewAccount,
+                              async=True)
+        host.bridge.addMethod("inBandUnregister", ".plugin", in_sign='ss', out_sign='',
+                              method=self._unregister,
+                              async=True)
+        host.bridge.addMethod("inBandPasswordChange", ".plugin", in_sign='ss', out_sign='',
+                              method=self._changePassword,
+                              async=True)
 
     @staticmethod
     def buildRegisterIQ(xmlstream_, jid_, password, email=None):
@@ -151,7 +160,11 @@
         return self.inBandRegister, jid.JID(to_jid_s, profile_key)
 
     def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE):
-        """register to a target JID"""
+        """register to a service
+
+        @param to_jid(jid.JID): jid of the service to register to
+        """
+        # FIXME: this post_treat_cb arguments seems wrong, check it
         client = self.host.getClient(profile_key)
         log.debug(_(u"Asking registration for {}").format(to_jid.full()))
         reg_request = client.IQ(u'get')
@@ -161,7 +174,17 @@
         d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client])
         return d
 
-    def registerNewAccount(self, jid_, password, email=None, host="127.0.0.1", port=C.XMPP_C2S_PORT):
+    def _registerNewAccount(self, jid_, password, email, host, port):
+        kwargs = {}
+        if email:
+            kwargs['email'] = email
+        if host:
+            kwargs['host'] = host
+        if port:
+            kwargs['port'] = port
+        return self.registerNewAccount(jid.JID(jid_), password, **kwargs)
+
+    def registerNewAccount(self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT):
         """register a new account on a XMPP server
 
         @param jid_(jid.JID): request jid to register
@@ -177,13 +200,29 @@
         serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
         return registered_d
 
+    def _changePassword(self, new_password, profile_key):
+        client = self.host.getClient(profile_key)
+        return self.changePassword(client, new_password)
+
     def changePassword(self, client, new_password):
         iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password)
-        return iq_elt.send(client.jid.host)
+        d = iq_elt.send(client.jid.host)
+        d.addCallback(lambda dummy: self.host.memory.setParam("Password", new_password, "Connection", profile_key=client.profile))
+        return d
+
+    def _unregister(self, to_jid_s, profile_key):
+        client = self.host.getClient(profile_key)
+        return self.unregister(client, jid.JID(to_jid_s))
 
-    def removeRegistration(self, client, to_jid):
+    def unregister(self, client, to_jid):
+        """remove registration from a server/service
+
+        BEWARE! if you remove registration from profile own server, this will
+        DELETE THE XMPP ACCOUNT WITHOUT WARNING
+        @param to_jid(jid.JID): jid of the service or server
+        """
         iq_elt = client.IQ()
         iq_elt['to'] = to_jid.full()
         query_elt = iq_elt.addElement((NS_REG, u'query'))
         query_elt.addElement(u'remove')
-        iq_elt.send()
+        return iq_elt.send()