Mercurial > libervia-backend
changeset 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 |
files | sat.tac sat_bridge/DBus.py |
diffstat | 2 files changed, 123 insertions(+), 41 deletions(-) [+] |
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"""
--- a/sat_bridge/DBus.py Sat Oct 31 00:18:35 2009 +0100 +++ b/sat_bridge/DBus.py Mon Nov 02 00:45:03 2009 +0100 @@ -28,6 +28,8 @@ from logging import debug, info, error const_INT_PREFIX = "org.goffi.SAT" #Interface prefix +const_COMM_SUFFIX = ".communication" +const_REQ_SUFFIX = ".request" class DbusObject(dbus.service.Object): @@ -41,132 +43,141 @@ ### signals ### - @dbus.service.signal(const_INT_PREFIX+".communication", + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='sa{ss}as') def newContact(self, contact, attributes, groups): debug("new contact signal (%s) sended", contact) - @dbus.service.signal(const_INT_PREFIX+".communication", + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='ssss') def newMessage(self, from_jid, msg, type='chat', to=''): debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) - @dbus.service.signal(const_INT_PREFIX+".communication", + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='ssssi') def presenceUpdate(self, jid, type, show, status, priority): debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) - @dbus.service.signal(const_INT_PREFIX+".communication", + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='sss') def paramUpdate(self, name, value, namespace): debug("param update signal: %s=%s in namespace %s", name, value, namespace) - @dbus.service.signal(const_INT_PREFIX+".communication", + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='s') def contactDeleted(self, jid): debug("contact deleted signal: %s", jid) - @dbus.service.signal(const_INT_PREFIX+".request", + @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, signature='ssa{ss}') def askConfirmation(self, type, id, data): debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) - + @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, + signature='ssa{ss}') + def sendAnswer(self, type, id, data): + debug("sending answer: id = [%s] type = %s data = %s", id, type, data) ### methods ### - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, + in_signature='sssi', out_signature='s') + def registerNewAccount(self, login, password, host, port=5222): + info ("New account registration asked") + return self.cb["registerNewAccount"](login, password, host, port) + + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='') def connect(self): info ("Connection asked") return self.cb["connect"]() - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='') def disconnect(self): info ("Disconnection asked") return self.cb["disconnect"]() - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='a(sa{ss}as)') def getContacts(self): debug("getContacts...") return self.cb["getContacts"]() - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='a(ssssi)') def getPresenceStatus(self): debug("getPresenceStatus...") return self.cb["getPresenceStatus"]() - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ss', out_signature='') def sendMessage(self, to, message): debug("sendMessage...") self.cb["sendMessage"](to, message) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ssssi', out_signature='') def setPresence(self, to="", type="", show="", status="", priority=0): self.cb["setPresence"](to, type, show, status, priority) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='sss', out_signature='') def setParam(self, name, value, namespace="default"): self.cb["setParam"](name, str(value), namespace) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ss', out_signature='(ss)') def getParam(self, name, namespace="default"): return self.cb["getParam"](name, namespace) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='a(sss)') def getParams(self, namespace): return self.cb["getParams"](namespace) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='as') def getParamsCategories(self): return self.cb["getParamsCategories"]() - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ssi', out_signature='a{i(ss)}') def getHistory(self, from_jid, to_jid, size): debug("History asked for %s", to_jid) return self.cb["getHistory"](from_jid, to_jid, size) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='') def addContact(self, jid): debug("Subscription asked for %s", jid) return self.cb["addContact"](jid) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='') def delContact(self, jid): debug("Unsubscription asked for %s", jid) return self.cb["delContact"](jid) - @dbus.service.method(const_INT_PREFIX+".communication", + @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='b') def isConnected(self): debug("Connection status requested") return self.cb["isConnected"]() - @dbus.service.method(const_INT_PREFIX+".request", + @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, in_signature='sba{ss}', out_signature='') def confirmationAnswer(self, id, accepted, data): debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") return self.cb["confirmationAnswer"](id, accepted, data) - @dbus.service.method(const_INT_PREFIX+".request", + @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, in_signature='s', out_signature='a{ss}') def getProgress(self, id): #debug("Progress asked for %s", id) return self.cb["getProgress"](id) - def _attribute_string(self, in_sign): + def __attribute_string(self, in_sign): i=0 idx=0 attr_string="" @@ -192,7 +203,7 @@ def addMethod(self, name, int_suffix, in_sign, out_sign): """Dynamically add a method to Dbus Bridge""" #FIXME: Better way ??? - attributes = self._attribute_string(in_sign) + attributes = self.__attribute_string(in_sign) code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') exec (code) @@ -231,6 +242,9 @@ def askConfirmation(self, type, id, data): self.dbus_bridge.askConfirmation(type, id, data) + def sendAnswer(self, type, id, data): + self.dbus_bridge.sendAnswer(type, id, data) + def register(self, name, callback): debug("registering DBus bridge method [%s]",name) self.dbus_bridge.register(name, callback)