comparison sat.tac @ 64:d46f849664aa

SàT: multi-profile, plugins updated - core: 2 new convenient methods: getJidNStream and getClient - new param in plugin info: "handler" to know if there is a handler to plug on profiles clients - plugins with handler now use an other class which is returned to profile client with the new method "getHandler" and pluged when connecting
author Goffi <goffi@goffi.org>
date Sat, 30 Jan 2010 16:17:33 +1100
parents 0db25931b60d
children d35c5edab53f
comparison
equal deleted inserted replaced
63:0db25931b60d 64:d46f849664aa
68 sat_id+=1 68 sat_id+=1
69 return "sat_id_"+str(sat_id) 69 return "sat_id_"+str(sat_id)
70 70
71 class SatXMPPClient(client.XMPPClient): 71 class SatXMPPClient(client.XMPPClient):
72 72
73 def __init__(self, bridge, profile, user_jid, password, host=None, port=5222): 73 def __init__(self, host_app, profile, user_jid, password, host=None, port=5222):
74 client.XMPPClient.__init__(self, user_jid, password, host, port) 74 client.XMPPClient.__init__(self, user_jid, password, host, port)
75 self.factory.clientConnectionLost = self.connectionLost 75 self.factory.clientConnectionLost = self.connectionLost
76 self.__connected=False 76 self.__connected=False
77 self.bridge = bridge
78 self.profile = profile 77 self.profile = profile
78 self.host_app = host_app
79 79
80 def _authd(self, xmlstream): 80 def _authd(self, xmlstream):
81 print "SatXMPPClient" 81 print "SatXMPPClient"
82 client.XMPPClient._authd(self, xmlstream) 82 client.XMPPClient._authd(self, xmlstream)
83 self.__connected=True 83 self.__connected=True
84 print "********** CONNECTED **********" 84 print "********** CONNECTED **********"
85 self.streamInitialized() 85 self.streamInitialized()
86 self.bridge.connected() #we send the signal to the clients 86 self.host_app.bridge.connected() #we send the signal to the clients
87 87
88 def streamInitialized(self): 88 def streamInitialized(self):
89 """Called after _authd""" 89 """Called after _authd"""
90 debug ("XML stream is initialized") 90 debug ("XML stream is initialized")
91 self.keep_alife = task.LoopingCall(self.xmlstream.send, " ") #Needed to avoid disconnection (specially with openfire) 91 self.keep_alife = task.LoopingCall(self.xmlstream.send, " ") #Needed to avoid disconnection (specially with openfire)
97 self.discoHandler.setHandlerParent(self) 97 self.discoHandler.setHandlerParent(self)
98 98
99 self.roster.requestRoster() 99 self.roster.requestRoster()
100 100
101 self.presence.available() 101 self.presence.available()
102 102
103 #self.disco.requestInfo(jid.JID(self.memory.getParamA("Server", "Connection"))).addCallback(self.host.serverDisco) #gof: FIXME 103 self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco) #FIXME: use these informations
104
105 104
106 def isConnected(self): 105 def isConnected(self):
107 return self.__connected 106 return self.__connected
108 107
109 def connectionLost(self, connector, unused_reason): 108 def connectionLost(self, connector, unused_reason):
111 print "********** DISCONNECTED **********" 110 print "********** DISCONNECTED **********"
112 try: 111 try:
113 self.keep_alife.stop() 112 self.keep_alife.stop()
114 except AttributeError: 113 except AttributeError:
115 debug("No keep_alife") 114 debug("No keep_alife")
116 self.bridge.disconnected() #we send the signal to the clients 115 self.host_app.bridge.disconnected() #we send the signal to the clients
117 116
118 117
119 class SatMessageProtocol(xmppim.MessageProtocol): 118 class SatMessageProtocol(xmppim.MessageProtocol):
120 119
121 def __init__(self, host): 120 def __init__(self, host):
349 self.bridge.register("isConnected", self.isConnected) 348 self.bridge.register("isConnected", self.isConnected)
350 self.bridge.register("launchAction", self.launchAction) 349 self.bridge.register("launchAction", self.launchAction)
351 self.bridge.register("confirmationAnswer", self.confirmationAnswer) 350 self.bridge.register("confirmationAnswer", self.confirmationAnswer)
352 self.bridge.register("getProgress", self.getProgress) 351 self.bridge.register("getProgress", self.getProgress)
353 352
354 #self._import_plugins() #gof: 353 self._import_plugins()
355 354
356 355
357 def _import_plugins(self): 356 def _import_plugins(self):
358 """Import all plugins found in plugins directory""" 357 """Import all plugins found in plugins directory"""
359 #TODO: manage dependencies 358 #TODO: manage dependencies
360 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob ("plugins/plugin*.py"))] 359 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob ("plugins/plugin*.py"))]
361 360
362 for plug in plug_lst: 361 for plug in plug_lst:
363 plug_path = 'plugins.'+plug 362 plug_path = 'plugins.'+plug
364 __import__(plug_path) 363 __import__(plug_path)
365 mod = sys.modules[plug_path] 364 mod = sys.modules[plug_path]
366 plug_info = mod.PLUGIN_INFO 365 plug_info = mod.PLUGIN_INFO
367 info ("importing plugin: %s", plug_info['name']) 366 info ("importing plugin: %s", plug_info['name'])
368 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self) 367 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self)
368 if plug_info.has_key('handler') and plug_info['handler'] == 'yes':
369 self.plugins[plug_info['import_name']].is_handler = True
370 else:
371 self.plugins[plug_info['import_name']].is_handler = False
369 #TODO: test xmppclient presence and register handler parent 372 #TODO: test xmppclient presence and register handler parent
370 373
371 def connect(self, profile_key = '@DEFAULT@'): 374 def connect(self, profile_key = '@DEFAULT@'):
372 """Connect to jabber server""" 375 """Connect to jabber server"""
373 376
379 382
380 if (self.isConnected()): 383 if (self.isConnected()):
381 info("already connected !") 384 info("already connected !")
382 return 385 return
383 print "connecting..." 386 print "connecting..."
384 current = self.profiles[profile] = SatXMPPClient(self.bridge, profile, 387 current = self.profiles[profile] = SatXMPPClient(self, profile,
385 jid.JID(self.memory.getParamA("JabberID", "Connection"), profile), 388 jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key = profile_key), profile),
386 self.memory.getParamA("Password", "Connection"), 389 self.memory.getParamA("Password", "Connection", profile_key = profile_key),
387 self.memory.getParamA("Server", "Connection"), 5222) 390 self.memory.getParamA("Server", "Connection", profile_key = profile_key), 5222)
388
389 #current.client.streamInitialized = self.streamInitialized #gof:
390 391
391 current.messageProt = SatMessageProtocol(self) 392 current.messageProt = SatMessageProtocol(self)
392 current.messageProt.setHandlerParent(current) 393 current.messageProt.setHandlerParent(current)
393 394
394 current.roster = SatRosterProtocol(self) 395 current.roster = SatRosterProtocol(self)
406 407
407 debug ("setting plugins parents") 408 debug ("setting plugins parents")
408 409
409 #FIXME: gof 410 #FIXME: gof
410 for plugin in self.plugins.iteritems(): 411 for plugin in self.plugins.iteritems():
411 if isinstance(plugin[1], XMPPHandler): 412 if plugin[1].is_handler:
412 plugin[1].setHandlerParent(current) 413 plugin[1].getHandler().setHandlerParent(current)
413 414
414 current.startService() 415 current.startService()
415 416
416 def disconnect(self, profile_key='@DEFAULT@'): 417 def disconnect(self, profile_key='@DEFAULT@'):
417 """disconnect from jabber server""" 418 """disconnect from jabber server"""
438 debug("stopping app") 439 debug("stopping app")
439 reactor.stop() 440 reactor.stop()
440 441
441 ## Misc methods ## 442 ## Misc methods ##
442 443
444 def getJidNStream(self, profile_key):
445 """Convenient method to get jid and stream from profile key
446 @return: tuple (jid, xmlstream) from profile, can be None"""
447 profile = self.memory.getProfileName(profile_key)
448 if not profile or not self.profiles[profile].isConnected():
449 return (None, None)
450 return (self.profiles[profile].jid, self.profiles[profile].xmlstream)
451
452 def getClient(self, profile_key):
453 """Convenient method to get client from profile key
454 @return: client or None if it doesn't exist"""
455 profile = self.memory.getProfileName(profile_key)
456 if not profile:
457 return None
458 return self.profiles[profile]
459
443 def registerNewAccount(self, login, password, server, port = 5222, id = None): 460 def registerNewAccount(self, login, password, server, port = 5222, id = None):
444 """Connect to a server and create a new account using in-band registration""" 461 """Connect to a server and create a new account using in-band registration"""
445 462
446 next_id = id or sat_next_id() #the id is used to send server's answer 463 next_id = id or sat_next_id() #the id is used to send server's answer
447 serverRegistrer = xmlstream.XmlStreamFactory(RegisteringAuthenticator(self, server, login, password, next_id)) 464 serverRegistrer = xmlstream.XmlStreamFactory(RegisteringAuthenticator(self, server, login, password, next_id))
449 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() 466 serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
450 467
451 return next_id 468 return next_id
452 469
453 def registerNewAccountCB(self, id, data): 470 def registerNewAccountCB(self, id, data):
471 #FIXME: gof: profile not managed here !
454 user = jid.parse(self.memory.getParamA("JabberID", "Connection"))[0] 472 user = jid.parse(self.memory.getParamA("JabberID", "Connection"))[0]
455 password = self.memory.getParamA("Password", "Connection") 473 password = self.memory.getParamA("Password", "Connection")
456 server = self.memory.getParamA("Server", "Connection") 474 server = self.memory.getParamA("Server", "Connection")
457 475
458 if not user or not password or not server: 476 if not user or not password or not server:
470 print ("===============+++++++++++ REGISTER NEW ACCOUNT++++++++++++++============") 488 print ("===============+++++++++++ REGISTER NEW ACCOUNT++++++++++++++============")
471 print "id=",id 489 print "id=",id
472 print "data=",data 490 print "data=",data
473 491
474 def regisConfirmCB(self, id, accepted, data): 492 def regisConfirmCB(self, id, accepted, data):
493 #FIXME: gof: profile not managed here !
475 print "register Confirmation CB ! (%s)" % str(accepted) 494 print "register Confirmation CB ! (%s)" % str(accepted)
476 action_id = self.__private_data[id] 495 action_id = self.__private_data[id]
477 del self.__private_data[id] 496 del self.__private_data[id]
478 user = jid.parse(self.memory.getParamA("JabberID", "Connection"))[0] 497 user = jid.parse(self.memory.getParamA("JabberID", "Connection"))[0]
479 password = self.memory.getParamA("Password", "Connection") 498 password = self.memory.getParamA("Password", "Connection")