changeset 2175:75002ac33801

plugin XEP-0077: added changePassword and removeRegistration methods
author Goffi <goffi@goffi.org>
date Thu, 09 Mar 2017 00:06:13 +0100
parents 0c32ff2f64c4
children 61128d260eef
files src/plugins/plugin_misc_register_account.py src/plugins/plugin_xep_0077.py
diffstat 2 files changed, 28 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_register_account.py	Thu Mar 09 00:06:13 2017 +0100
+++ b/src/plugins/plugin_misc_register_account.py	Thu Mar 09 00:06:13 2017 +0100
@@ -80,7 +80,6 @@
 
     def _registerConfirmation(self, data, profile):
         """Save the related parameters and proceed the registration."""
-        client = self.host.getClient(profile)
         session_data = self._sessions.profileGet(data['session_id'], profile)
 
         self.host.memory.setParam("JabberID", session_data["JabberID"], "Connection", profile_key=profile)
@@ -88,7 +87,7 @@
         self.host.memory.setParam(C.FORCE_SERVER_PARAM, session_data[C.FORCE_SERVER_PARAM], "Connection", profile_key=profile)
         self.host.memory.setParam(C.FORCE_PORT_PARAM, session_data[C.FORCE_PORT_PARAM], "Connection", profile_key=profile)
 
-        d = self._registerNewAccount(client, jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server'])
+        d = self._registerNewAccount(jid.JID(session_data['JabberID']), session_data["Password"], None, session_data['server'])
         del self._sessions[data['session_id']]
         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 00:06:13 2017 +0100
@@ -62,16 +62,7 @@
         self.xmlstream.namespace = C.NS_CLIENT
         self.xmlstream.sendHeader()
 
-        iq = xmlstream.IQ(self.xmlstream, 'set')
-        iq["to"] = self.jid.host
-        query_elt = iq.addElement(('jabber:iq:register', 'query'))
-        username_elt = query_elt.addElement('username')
-        username_elt.addContent(self.jid.user)
-        password_elt = query_elt.addElement('password')
-        password_elt.addContent(self.password)
-        if self.email is not None:
-            email_elt = query_elt.addElement('email')
-            email_elt.addContent(self.email)
+        iq = XEP_0077.buildRegisterIQ(self.xmlstream, self.jid, self.password, self.email)
         d = iq.send(self.jid.host).addCallbacks(self.registrationCb, self.registrationEb)
         d.chainDeferred(self.registered)
 
@@ -94,6 +85,20 @@
                               method=self._inBandRegister,
                               async=True)
 
+    @staticmethod
+    def buildRegisterIQ(xmlstream_, jid_, password, email=None):
+        iq_elt = xmlstream.IQ(xmlstream_, 'set')
+        iq_elt["to"] = jid_.host
+        query_elt = iq_elt.addElement(('jabber:iq:register', 'query'))
+        username_elt = query_elt.addElement('username')
+        username_elt.addContent(jid_.user)
+        password_elt = query_elt.addElement('password')
+        password_elt.addContent(password)
+        if email is not None:
+            email_elt = query_elt.addElement('email')
+            email_elt.addContent(email)
+        return iq_elt
+
     def _regCb(self, answer, client, post_treat_cb):
         """Called after the first get IQ"""
         try:
@@ -156,7 +161,7 @@
         d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client])
         return d
 
-    def registerNewAccount(self, client, jid_, password, email=None, host="127.0.0.1", port=C.XMPP_C2S_PORT):
+    def registerNewAccount(self, jid_, password, email=None, host="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
@@ -171,3 +176,14 @@
         connector = reactor.connectTCP(host, port, serverRegistrer)
         serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
         return registered_d
+
+    def changePassword(self, client, new_password):
+        iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password)
+        return iq_elt.send(client.jid.host)
+
+    def removeRegistration(self, client, to_jid):
+        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()