Mercurial > libervia-backend
view plugins/plugin_xep_0077.py @ 37:a61beb21d16d
Gateway registration, unregistration & edition
- default values added in form
- DBus bridge: fixed array of struct management when adding dynamically a method
- fixed doc for some methods
- ugly fix for presence status
- added dependency for XEP-0077 in XEP-0100 plugin
- Wix: added unregister button in gateways manager
- Wix: added privacy warning in gateways manager
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 15 Dec 2009 01:27:32 +1100 |
parents | 6491b7956c80 |
children | 2e3411a6baad |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- """ SAT plugin for managing xep-0077 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from logging import debug, info, error from twisted.words.protocols.jabber import client, jid, xmlstream, error from twisted.words.protocols.jabber.xmlstream import IQ from twisted.internet import reactor from tools.xml_tools import XMLTools import pdb from wokkel import data_form NS_REG = 'jabber:iq:register' PLUGIN_INFO = { "name": "XEP 0077 Plugin", "import_name": "XEP_0077", "type": "XEP", "dependencies": [], "main": "XEP_0077", "description": """Implementation of in-band registration""" } class XEP_0077(): def __init__(self, host): info("Plugin XEP_0077 initialization") self.host = host self.triggers = {} #used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid host.bridge.addMethod("in_band_register", ".communication", in_sign='s', out_sign='s', method=self.in_band_register) host.bridge.addMethod("in_band_submit", ".request", in_sign='sa(ss)', out_sign='s', method=self.in_band_submit) def addTrigger(self, target, cb): """Add a callback which is called when registration to target is successful""" self.triggers[target] = cb def reg_ok(self, answer): """Called after the first get IQ""" form = data_form.Form.fromElement(answer.firstChildElement().firstChildElement()) xml_data = XMLTools.dataForm2xml(form) self.host.bridge.actionResult("FORM", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}) def reg_err(self, failure): """Called when something is wrong with registration""" info ("Registration failure: %s" % str(failure.value)) answer_data = {} answer_data['reason'] = 'unknown' answer_data={"message":"%s [code: %s]" % (failure.value.condition, failure.value.code)} answer_type = "ERROR" self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) def unregistrationAnswer(self, answer): debug ("registration answer: %s" % answer.toXml()) answer_type = "SUCCESS" answer_data={"message":"Your are now unregistred"} self.host.bridge.actionResult(answer_type, answer['id'], answer_data) def unregistrationFailure(self, failure): info ("Unregistration failure: %s" % str(failure.value)) answer_type = "ERROR" answer_data = {} answer_data['reason'] = 'unknown' answer_data={"message":"Unregistration failed: %s" % failure.value.condition} self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) def registrationAnswer(self, answer): debug ("registration answer: %s" % answer.toXml()) answer_type = "SUCCESS" answer_data={"message":"Registration successfull"} self.host.bridge.actionResult(answer_type, answer['id'], answer_data) if self.triggers.has_key(answer["from"]): self.triggers[answer["from"]](answer["from"]) del self.triggers[answer["from"]] def registrationFailure(self, failure): info ("Registration failure: %s" % str(failure.value)) print failure.value.stanza.toXml() answer_type = "ERROR" answer_data = {} if failure.value.condition == 'conflict': answer_data['reason'] = 'conflict' answer_data={"message":"Username already exists, please choose an other one"} else: answer_data['reason'] = 'unknown' answer_data={"message":"Registration failed"} self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) if self.triggers.has_key(answer["from"]): del self.triggers[answer["from"]] def in_band_submit(self, action, target, fields): """Submit a form for registration, using data_form""" id, deferred = self.host.submitForm(action, target, fields) if action == 'CANCEL': deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure) else: deferred.addCallbacks(self.registrationAnswer, self.registrationFailure) return id def in_band_register(self, target): """register to a target JID""" to_jid = jid.JID(target) debug("Asking registration for [%s]" % target) reg_request=IQ(self.host.xmlstream,'get') reg_request["from"]=self.host.me.full() reg_request["to"] = to_jid.full() query=reg_request.addElement('query', NS_REG) reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err) return reg_request["id"]