core, memory, bridge, plugin misc_register_account: move registerNewAccount to a new plugin:

- the bridge method has been removed, now a plugin takes care of it with XMLUI callback system
    - TODO: xmpp.RegisteringAuthenticator still needs to be fixed
This commit is contained in:
souliane 2014-05-16 00:58:20 +02:00
commit dd597a2ead
2 changed files with 131 additions and 95 deletions

View file

@ -20,7 +20,7 @@
from sat.core.i18n import _, languageSwitch
from twisted.application import service
from twisted.internet import defer
from twisted.words.protocols.jabber import jid, xmlstream
from twisted.words.protocols.jabber import jid
from twisted.words.xish import domish
from twisted.internet import reactor
from wokkel.xmppim import RosterItem
@ -102,7 +102,6 @@ class SAT(service.Service):
self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile))
self.bridge.register("asyncCreateProfile", self.memory.asyncCreateProfile)
self.bridge.register("asyncDeleteProfile", self.memory.asyncDeleteProfile)
self.bridge.register("registerNewAccount", self.registerNewAccount)
self.bridge.register("connect", self.connect)
self.bridge.register("asyncConnect", self.asyncConnect)
self.bridge.register("disconnect", self.disconnect)
@ -413,52 +412,6 @@ class SAT(service.Service):
raise exceptions.ProfileKeyUnknownError
return [self.profiles[profile]]
def registerNewAccount(self, login, password, email, server, port=5222, id_=None, profile_key=C.PROF_KEY_NONE):
"""Connect to a server and create a new account using in-band registration"""
profile = self.memory.getProfileName(profile_key)
assert(profile)
next_id = id_ or self.get_next_id() # the id is used to send server's answer
serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, server, login, password, email, next_id, profile))
connector = reactor.connectTCP(server, port, serverRegistrer)
serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
return next_id
def registerNewAccountCB(self, data, profile):
# FIXME: to be removed/redone elsewhere
user = jid.parse(self.memory.getParamA("JabberID", "Connection", profile_key=profile))[0]
server = self.memory.getParamA("Server", "Connection", profile_key=profile)
d = self.memory.asyncGetParamA("Password", "Connection", profile_key=profile)
def gotPassword(password):
if not user or not password or not server:
raise exceptions.DataError(_("No user, password or server given, can't register new account."))
# FIXME: to be fixed with XMLUI dialogs once their implemented
confirm_id = sat_next_id()
self.__private_data[confirm_id] = (id, profile)
self.askConfirmation(
confirm_id, "YES/NO",
{"message": _("Are you sure to register new account [%(user)s] to server %(server)s ?") % {'user': user, 'server': server, 'profile': profile}},
self.regisConfirmCB, profile)
print "===============+++++++++++ REGISTER NEW ACCOUNT++++++++++++++============"
d.addCallback(gotPassword)
def regisConfirmCB(self, id, accepted, data, profile):
print _("register Confirmation CB ! (%s)") % str(accepted)
action_id, profile = self.__private_data[id]
del self.__private_data[id]
if accepted:
user = jid.parse(self.memory.getParamA("JabberID", "Connection", profile_key=profile))[0]
server = self.memory.getParamA("Server", "Connection", profile_key=profile)
d = self.memory.asyncGetParamA("Password", "Connection", profile_key=profile)
d.addCallback(lambda password: self.registerNewAccount(user, password, None, server, id=action_id))
else:
self.actionResult(action_id, "SUPPRESS", {}, profile)
## Client management ##
def setParam(self, name, value, category, security_limit, profile_key):

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from twisted.internet import task, defer
from twisted.words.protocols.jabber import jid, xmlstream
@ -412,20 +412,19 @@ class SatFallbackHandler(generic.FallbackHandler):
class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
def __init__(self, host, jabber_host, user_login, user_pass, email, answer_id, profile):
def __init__(self, host, jabber_host, user_login, user_pass, email, deferred, profile):
xmlstream.ConnectAuthenticator.__init__(self, jabber_host)
self.host = host
self.jabber_host = jabber_host
self.user_login = user_login
self.user_pass = user_pass
self.user_email = email
self.answer_id = answer_id
self.deferred = deferred
self.profile = profile
print _("Registration asked for"), user_login, user_pass, jabber_host
log.debug(_("Registration asked for %(user)s@%(host)s") % {'user': user_login, 'host': jabber_host})
def connectionMade(self):
print "connectionMade"
log.debug(_("Connection made with %s" % self.jabber_host))
self.xmlstream.namespace = "jabber:client"
self.xmlstream.sendHeader()
@ -439,27 +438,17 @@ class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
if self.user_email:
_email = query.addElement('email')
_email.addContent(self.user_email)
iq.send(self.jabber_host).addCallbacks(self.registrationAnswer, self.registrationFailure)
d = iq.send(self.jabber_host).addCallbacks(self.registrationAnswer, self.registrationFailure)
d.chainDeferred(self.deferred)
def registrationAnswer(self, answer):
log.debug(_("registration answer: %s") % answer.toXml())
answer_type = "SUCCESS"
answer_data = {"message": _("Registration successfull")}
self.host.bridge.actionResult(answer_type, self.answer_id, answer_data, self.profile)
log.debug(_("Registration answer: %s") % answer.toXml())
self.xmlstream.sendFooter()
def registrationFailure(self, failure):
log.info(_("Registration failure: %s") % str(failure.value))
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 (%s)") % str(failure.value.condition)}
self.host.bridge.actionResult(answer_type, self.answer_id, answer_data, self.profile)
self.xmlstream.sendFooter()
raise failure.value
class SatVersionHandler(generic.VersionHandler):