Mercurial > libervia-backend
diff sat.tac @ 16:0a024d5e0cd0
New account creation (in-band registration)
- new method "sendAnswer" in bridge for asyncronous result communication
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Nov 2009 00:45:03 +0100 |
parents | 218ec9984fa5 |
children | 74a39f40eb6d |
line wrap: on
line diff
--- a/sat.tac Sat Oct 31 00:18:35 2009 +0100 +++ b/sat.tac Mon Nov 02 00:45:03 2009 +0100 @@ -20,7 +20,7 @@ """ client_name = u'SàT (Salut à toi)' -client_version = '0.0.1' +client_version = '0.0.1D' #Please add 'D' at the end for dev versions from twisted.application import internet, service from twisted.internet import glib2reactor, protocol, task @@ -32,7 +32,7 @@ from twisted.internet import reactor import pdb -from wokkel import client, disco, xmppim, generic +from wokkel import client, disco, xmppim, generic, compat from sat_bridge.DBus import DBusBridge import logging @@ -56,6 +56,13 @@ ### +sat_id = 0 + +def sat_next_id(): + global sat_id + sat_id+=1 + return "sat_id_"+str(sat_id) + class SatXMPPClient(client.XMPPClient): def __init__(self, jid, password, host=None, port=5222): @@ -187,7 +194,50 @@ #pdb.set_trace() print "iqFallback: xml = [%s], handled=%s" % (iq.toXml(), "True" if iq.handled else "False") generic.FallbackHandler.iqFallback(self, iq) + +class RegisteringAuthenticator(xmlstream.ConnectAuthenticator): + + def __init__(self, host, jabber_host, user_login, user_pass, answer_id): + 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.answer_id = answer_id + def connectionMade(self): + print "connectionMade" + + self.xmlstream.namespace = "jabber:client" + self.xmlstream.sendHeader() + + iq = compat.IQ(self.xmlstream, 'set') + iq["to"] = self.jabber_host + query = iq.addElement(('jabber:iq:register', 'query')) + _user = query.addElement('username') + _user.addContent(self.user_login) + _pass = query.addElement('password') + _pass.addContent(self.user_pass) + reg = iq.send(self.jabber_host).addCallbacks(self.registrationAnswer, self.registrationFailure) + + def registrationAnswer(self, answer): + debug ("registration answer: %s" % answer.toXml()) + answer_type = "success" + answer_data={"human":"Registration successfull"} + self.host.bridge.sendAnswer(answer_type, self.answer_id, answer_data) + self.xmlstream.sendFooter() + + def registrationFailure(self, error): + info ("Registration failure: %s" %str(error)) + answer_type = "error" + answer_data={"human":"Registration failed"} + if error.value.condition == 'conflict': + answer_data['reason'] = 'conflict' + else: + answer_data['reason'] = 'unknown' + self.host.bridge.sendAnswer(answer_type, self.answer_id, answer_data) + self.xmlstream.sendFooter() + class SAT(service.Service): @@ -201,6 +251,7 @@ self.plugins = {} self.bridge=DBusBridge() + self.bridge.register("registerNewAccount", self.registerNewAccount) self.bridge.register("connect", self.connect) self.bridge.register("disconnect", self.disconnect) self.bridge.register("getContacts", self.memory.getContacts) @@ -219,7 +270,6 @@ self.bridge.register("getProgress", self.getProgress) self._import_plugins() - #self.connect() def _import_plugins(self): @@ -234,8 +284,11 @@ plug_info = mod.PLUGIN_INFO info ("importing plugin: %s", plug_info['name']) self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self) + #TODO: test xmppclient presence and register handler parent def connect(self): + """Connect to jabber server""" + if (self.isConnected()): info("already connected !") return @@ -264,10 +317,11 @@ for plugin in self.plugins.iteritems(): if isinstance(plugin[1], XMPPHandler): plugin[1].setHandlerParent(self.xmppclient) - + self.xmppclient.startService() def disconnect(self): + """disconnect from jabber server""" if (not self.isConnected()): info("not connected !") return @@ -307,19 +361,20 @@ self.presence.available() self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco) - + + ## Misc methods ## + + def registerNewAccount(self, login, password, server, port = 5222): + """Connect to a server and create a new account using in-band registration""" - def sendMessage(self,to,msg,type='chat'): - #FIXME: check validity of recipient - debug("Sending jabber message to %s...", to) - message = domish.Element(('jabber:client','message')) - message["to"] = jid.JID(to).full() - message["from"] = self.me.full() - message["type"] = type - message.addElement("body", "jabber:client", msg) - self.xmlstream.send(message) - self.memory.addToHistory(self.me, self.me, jid.JID(to), message["type"], unicode(msg)) - self.bridge.newMessage(message['from'], unicode(msg), to=message['to']) #We send back the message, so all clients are aware of it + next_id = sat_next_id() #the id is used to send server's answer + serverRegistrer = xmlstream.XmlStreamFactory(RegisteringAuthenticator(self, server, login, password, next_id)) + connector = reactor.connectTCP(server, port, serverRegistrer) + serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() + + return next_id + + ## Client management ## def setParam(self, name, value, namespace): """set wanted paramater and notice observers""" @@ -341,6 +396,19 @@ return False ## jabber methods ## + + def sendMessage(self,to,msg,type='chat'): + #FIXME: check validity of recipient + debug("Sending jabber message to %s...", to) + message = domish.Element(('jabber:client','message')) + message["to"] = jid.JID(to).full() + message["from"] = self.me.full() + message["type"] = type + message.addElement("body", "jabber:client", msg) + self.xmlstream.send(message) + self.memory.addToHistory(self.me, self.me, jid.JID(to), message["type"], unicode(msg)) + self.bridge.newMessage(message['from'], unicode(msg), to=message['to']) #We send back the message, so all clients are aware of it + def setPresence(self, to="", type="", show="", status="", priority=0): """Send our presence information"""