view plugins/ @ 69:86f1f7f6d332

i18n first draft - gettext support added in SàT - first draft of french translation - added README with a HOWTO for translators
author Goffi <>
date Wed, 03 Mar 2010 17:12:23 +1100
parents d46f849664aa
children 783e9d6980ec
line wrap: on
line source

# -*- coding: utf-8 -*-

SAT plugin for managing xep-0077
Copyright (C) 2009, 2010  Jérôme Poisson (

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
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 <>.

from logging import debug, info, error
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_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'

"name": "XEP 0077 Plugin",
"import_name": "XEP_0077",
"type": "XEP",
"protocols": ["XEP-0077"],
"dependencies": [],
"main": "XEP_0077",
"description": _("""Implementation of in-band registration""")

class XEP_0077():
    def __init__(self, host):
        info(_("Plugin XEP_0077 initialization")) = 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='ss', 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"""
            x_elem = filter (lambda == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form)
        except IndexError:
            info(_("No data form found"))
            #TODO: manage registration without data form
            answer_data = {}
            answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")}
            answer_type = "ERROR"
  , answer['id'], answer_data)
        form = data_form.Form.fromElement(x_elem)
        xml_data = XMLTools.dataForm2xml(form)"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", 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")}, 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}, failure.value.stanza['id'], answer_data)
    def registrationAnswer(self, answer):
        debug (_("registration answer: %s") % answer.toXml())
        answer_type = "SUCCESS"
        answer_data={"message":_("Registration successfull")}, answer['id'], answer_data)
        if self.triggers.has_key(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")}
            answer_data['reason'] = 'unknown'
            answer_data={"message":_("Registration failed")}, 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 =, target, fields)
        if action == 'CANCEL':
            deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure)
            deferred.addCallbacks(self.registrationAnswer, self.registrationFailure)
        return id
    def in_band_register(self, target, profile_key='@DEFAULT@'):
        """register to a target JID"""
        current_jid, xmlstream =
        if not xmlstream:
            error (_('Asking for an non-existant or not connected profile'))
            return ""
        to_jid = jid.JID(target)
        debug(_("Asking registration for [%s]") % to_jid.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"]