Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0077.py @ 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 | 75002ac33801 |
children | 1b42bd8c10fb |
comparison
equal
deleted
inserted
replaced
2176:61128d260eef | 2177:09cfec4d8d19 |
---|---|
79 class XEP_0077(object): | 79 class XEP_0077(object): |
80 | 80 |
81 def __init__(self, host): | 81 def __init__(self, host): |
82 log.info(_("Plugin XEP_0077 initialization")) | 82 log.info(_("Plugin XEP_0077 initialization")) |
83 self.host = host | 83 self.host = host |
84 host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='s', | 84 host.bridge.addMethod("inBandRegister", ".plugin", in_sign='ss', out_sign='', |
85 method=self._inBandRegister, | 85 method=self._inBandRegister, |
86 async=True) | |
87 host.bridge.addMethod("inBandAccountNew", ".plugin", in_sign='ssssi', out_sign='', | |
88 method=self._registerNewAccount, | |
89 async=True) | |
90 host.bridge.addMethod("inBandUnregister", ".plugin", in_sign='ss', out_sign='', | |
91 method=self._unregister, | |
92 async=True) | |
93 host.bridge.addMethod("inBandPasswordChange", ".plugin", in_sign='ss', out_sign='', | |
94 method=self._changePassword, | |
86 async=True) | 95 async=True) |
87 | 96 |
88 @staticmethod | 97 @staticmethod |
89 def buildRegisterIQ(xmlstream_, jid_, password, email=None): | 98 def buildRegisterIQ(xmlstream_, jid_, password, email=None): |
90 iq_elt = xmlstream.IQ(xmlstream_, 'set') | 99 iq_elt = xmlstream.IQ(xmlstream_, 'set') |
149 | 158 |
150 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): | 159 def _inBandRegister(self, to_jid_s, profile_key=C.PROF_KEY_NONE): |
151 return self.inBandRegister, jid.JID(to_jid_s, profile_key) | 160 return self.inBandRegister, jid.JID(to_jid_s, profile_key) |
152 | 161 |
153 def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): | 162 def inBandRegister(self, to_jid, post_treat_cb=None, profile_key=C.PROF_KEY_NONE): |
154 """register to a target JID""" | 163 """register to a service |
164 | |
165 @param to_jid(jid.JID): jid of the service to register to | |
166 """ | |
167 # FIXME: this post_treat_cb arguments seems wrong, check it | |
155 client = self.host.getClient(profile_key) | 168 client = self.host.getClient(profile_key) |
156 log.debug(_(u"Asking registration for {}").format(to_jid.full())) | 169 log.debug(_(u"Asking registration for {}").format(to_jid.full())) |
157 reg_request = client.IQ(u'get') | 170 reg_request = client.IQ(u'get') |
158 reg_request["from"] = client.jid.full() | 171 reg_request["from"] = client.jid.full() |
159 reg_request["to"] = to_jid.full() | 172 reg_request["to"] = to_jid.full() |
160 reg_request.addElement('query', NS_REG) | 173 reg_request.addElement('query', NS_REG) |
161 d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) | 174 d = reg_request.send(to_jid.full()).addCallbacks(self._regCb, self._regEb, callbackArgs=[client, post_treat_cb], errbackArgs=[client]) |
162 return d | 175 return d |
163 | 176 |
164 def registerNewAccount(self, jid_, password, email=None, host="127.0.0.1", port=C.XMPP_C2S_PORT): | 177 def _registerNewAccount(self, jid_, password, email, host, port): |
178 kwargs = {} | |
179 if email: | |
180 kwargs['email'] = email | |
181 if host: | |
182 kwargs['host'] = host | |
183 if port: | |
184 kwargs['port'] = port | |
185 return self.registerNewAccount(jid.JID(jid_), password, **kwargs) | |
186 | |
187 def registerNewAccount(self, jid_, password, email=None, host=u"127.0.0.1", port=C.XMPP_C2S_PORT): | |
165 """register a new account on a XMPP server | 188 """register a new account on a XMPP server |
166 | 189 |
167 @param jid_(jid.JID): request jid to register | 190 @param jid_(jid.JID): request jid to register |
168 @param password(unicode): password of the account | 191 @param password(unicode): password of the account |
169 @param email(unicode): email of the account | 192 @param email(unicode): email of the account |
175 serverRegistrer = xmlstream.XmlStreamFactory(authenticator) | 198 serverRegistrer = xmlstream.XmlStreamFactory(authenticator) |
176 connector = reactor.connectTCP(host, port, serverRegistrer) | 199 connector = reactor.connectTCP(host, port, serverRegistrer) |
177 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() | 200 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() |
178 return registered_d | 201 return registered_d |
179 | 202 |
203 def _changePassword(self, new_password, profile_key): | |
204 client = self.host.getClient(profile_key) | |
205 return self.changePassword(client, new_password) | |
206 | |
180 def changePassword(self, client, new_password): | 207 def changePassword(self, client, new_password): |
181 iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password) | 208 iq_elt = self.buildRegisterIQ(client.xmlstream, client.jid, new_password) |
182 return iq_elt.send(client.jid.host) | 209 d = iq_elt.send(client.jid.host) |
183 | 210 d.addCallback(lambda dummy: self.host.memory.setParam("Password", new_password, "Connection", profile_key=client.profile)) |
184 def removeRegistration(self, client, to_jid): | 211 return d |
212 | |
213 def _unregister(self, to_jid_s, profile_key): | |
214 client = self.host.getClient(profile_key) | |
215 return self.unregister(client, jid.JID(to_jid_s)) | |
216 | |
217 def unregister(self, client, to_jid): | |
218 """remove registration from a server/service | |
219 | |
220 BEWARE! if you remove registration from profile own server, this will | |
221 DELETE THE XMPP ACCOUNT WITHOUT WARNING | |
222 @param to_jid(jid.JID): jid of the service or server | |
223 """ | |
185 iq_elt = client.IQ() | 224 iq_elt = client.IQ() |
186 iq_elt['to'] = to_jid.full() | 225 iq_elt['to'] = to_jid.full() |
187 query_elt = iq_elt.addElement((NS_REG, u'query')) | 226 query_elt = iq_elt.addElement((NS_REG, u'query')) |
188 query_elt.addElement(u'remove') | 227 query_elt.addElement(u'remove') |
189 iq_elt.send() | 228 return iq_elt.send() |