comparison sat.tac @ 15:218ec9984fa5

wokkel integration part III + memory saved again - disco handler (plugins handled)
author Goffi <goffi@goffi.org>
date Sat, 31 Oct 2009 00:18:35 +0100
parents a62d7d453f22
children 0a024d5e0cd0
comparison
equal deleted inserted replaced
14:a62d7d453f22 15:218ec9984fa5
41 import signal, sys 41 import signal, sys
42 import os.path 42 import os.path
43 43
44 from tools.memory import Memory 44 from tools.memory import Memory
45 from glob import glob 45 from glob import glob
46
47 try:
48 from twisted.words.protocols.xmlstream import XMPPHandler
49 except ImportError:
50 from wokkel.subprotocols import XMPPHandler
46 51
47 52
48 ### logging configuration FIXME: put this elsewhere ### 53 ### logging configuration FIXME: put this elsewhere ###
49 logging.basicConfig(level=logging.DEBUG, 54 logging.basicConfig(level=logging.DEBUG,
50 format='%(message)s') 55 format='%(message)s')
172 177
173 class SatDiscoProtocol(disco.DiscoClientProtocol): 178 class SatDiscoProtocol(disco.DiscoClientProtocol):
174 def __init__(self, host): 179 def __init__(self, host):
175 disco.DiscoClientProtocol.__init__(self) 180 disco.DiscoClientProtocol.__init__(self)
176 181
182 class SatFallbackHandler(generic.FallbackHandler):
183 def __init__(self, host):
184 generic.FallbackHandler.__init__(self)
185
186 def iqFallback(self, iq):
187 #pdb.set_trace()
188 print "iqFallback: xml = [%s], handled=%s" % (iq.toXml(), "True" if iq.handled else "False")
189 generic.FallbackHandler.iqFallback(self, iq)
190
191
177 class SAT(service.Service): 192 class SAT(service.Service):
178 193
179 def __init__(self): 194 def __init__(self):
180 #self.reactor=reactor 195 #self.reactor=reactor
181 self.memory=Memory() 196 self.memory=Memory()
182 self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future 197 self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future
183 198
184 self._iq_cb_map = {} #callback called when ns is found on IQ
185 self._waiting_conf = {} #callback called when a confirmation is received 199 self._waiting_conf = {} #callback called when a confirmation is received
186 self._progress_cb_map = {} #callback called when a progress is requested (key = progress id) 200 self._progress_cb_map = {} #callback called when a progress is requested (key = progress id)
187 self.plugins = {} 201 self.plugins = {}
188 202
189 self.bridge=DBusBridge() 203 self.bridge=DBusBridge()
238 self.roster.setHandlerParent(self.xmppclient) 252 self.roster.setHandlerParent(self.xmppclient)
239 253
240 self.presence = SatPresenceProtocol(self) 254 self.presence = SatPresenceProtocol(self)
241 self.presence.setHandlerParent(self.xmppclient) 255 self.presence.setHandlerParent(self.xmppclient)
242 256
243 self.fallBack = generic.FallbackHandler() 257 self.fallBack = SatFallbackHandler(self)
244 self.fallBack.setHandlerParent(self.xmppclient) 258 self.fallBack.setHandlerParent(self.xmppclient)
245 259
246 self.versionHandler = generic.VersionHandler(unicode(client_name), client_version) 260 self.versionHandler = generic.VersionHandler(unicode(client_name), client_version)
247 self.versionHandler.setHandlerParent(self.xmppclient) 261 self.versionHandler.setHandlerParent(self.xmppclient)
248 262
263 debug ("setting plugins parents")
264 for plugin in self.plugins.iteritems():
265 if isinstance(plugin[1], XMPPHandler):
266 plugin[1].setHandlerParent(self.xmppclient)
267
249 self.xmppclient.startService() 268 self.xmppclient.startService()
250 269
251 def disconnect(self): 270 def disconnect(self):
252 if (not self.isConnected()): 271 if (not self.isConnected()):
253 info("not connected !") 272 info("not connected !")
258 def startService(self): 277 def startService(self):
259 info("Salut à toi ô mon frère !") 278 info("Salut à toi ô mon frère !")
260 self.connect() 279 self.connect()
261 280
262 def stopService(self): 281 def stopService(self):
282 self.memory.save()
263 info("Salut aussi à Rantanplan") 283 info("Salut aussi à Rantanplan")
264 284
265 def run(self): 285 def run(self):
266 debug("running app") 286 debug("running app")
267 reactor.run() 287 reactor.run()
275 SatXMPPClient.streamInitialized(self.xmppclient) 295 SatXMPPClient.streamInitialized(self.xmppclient)
276 debug ("XML stream is initialized") 296 debug ("XML stream is initialized")
277 self.xmlstream = self.xmppclient.xmlstream 297 self.xmlstream = self.xmppclient.xmlstream
278 self.me = self.xmppclient.jid #in case of the ressource has changed 298 self.me = self.xmppclient.jid #in case of the ressource has changed
279 299
280 self.roster.requestRoster()
281
282 self.presence.available()
283
284 #FIXME:tmp
285 self.xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
286 ###FIXME: tmp disco ###
287 self.disco = SatDiscoProtocol(self) 300 self.disco = SatDiscoProtocol(self)
288 self.disco.setHandlerParent(self.xmppclient) 301 self.disco.setHandlerParent(self.xmppclient)
302 self.discoHandler = disco.DiscoHandler()
303 self.discoHandler.setHandlerParent(self.xmppclient)
304
305 self.roster.requestRoster()
306
307 self.presence.available()
308
289 self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco) 309 self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco)
290 #we now send our presence status
291
292 # add a callback for the messages
293
294 #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
295 310
296 311
297 def sendMessage(self,to,msg,type='chat'): 312 def sendMessage(self,to,msg,type='chat'):
298 #FIXME: check validity of recipient 313 #FIXME: check validity of recipient
299 debug("Sending jabber message to %s...", to) 314 debug("Sending jabber message to %s...", to)
361 self.bridge.contactDeleted(to) 376 self.bridge.contactDeleted(to)
362 377
363 378
364 ## callbacks ## 379 ## callbacks ##
365 380
366 def add_IQ_cb(self, ns, cb):
367 """Add an IQ callback on namespace ns"""
368 debug ("Registered callback for namespace %s", ns)
369 self._iq_cb_map[ns]=cb
370
371 def iqCb(self, stanza):
372 info ("iqCb")
373 debug ("="*20)
374 debug ("DEBUG:\n")
375 debug (stanza.toXml().encode('utf-8'))
376 debug ("="*20)
377 #FIXME: temporary ugly code
378 uri = stanza.firstChildElement().uri
379 if self._iq_cb_map.has_key(uri):
380 self._iq_cb_map[uri](stanza)
381 #TODO: manage errors stanza
382
383
384
385 def serverDisco(self, disco): 381 def serverDisco(self, disco):
386 """xep-0030 Discovery Protocol.""" 382 """xep-0030 Discovery Protocol."""
387 for feature in disco.features: 383 for feature in disco.features:
388 debug ("Feature found: %s",feature) 384 debug ("Feature found: %s",feature)
389 self.server_features.append(feature) 385 self.server_features.append(feature)
437 433
438 434
439 application = service.Application('SàT') 435 application = service.Application('SàT')
440 service = SAT() 436 service = SAT()
441 service.setServiceParent(application) 437 service.setServiceParent(application)
442
443
444 #app.memory.save() #FIXME: not the best place
445 #debug("Good Bye")