changeset 155:63db565438bd

fixed the halt command Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 17 Jan 2010 23:24:00 +0100
parents 25f6778b8841
children 3f299ad4d452
files bot.py bridge.py commands.py irclib.py start_bots_from_xml_config.py
diffstat 5 files changed, 37 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -37,6 +37,7 @@ class Bot(threading.Thread):
 	
 	def __init__(self, jid, password, nickname, admins_jid=[], error_fd=sys.stderr, debug=False):
 		threading.Thread.__init__(self)
+		self.halt = False
 		self.bare_jid = xmpp.protocol.JID(jid=jid)
 		self.bare_jid.setResource('')
 		self.nickname = nickname
@@ -76,6 +77,11 @@ class Bot(threading.Thread):
 		"""[Internal] XMPP infinite loop."""
 		i = 1
 		while True:
+			if self.halt:
+				s = len(self.xmpp_connections)
+				for i in range(s):
+					self.close_xmpp_connection(self.xmpp_connections.keys()[s-i-1], force=True)
+				break
 			unlock = False
 			try:
 				if len(self.xmpp_connections) == 1:
@@ -780,13 +786,13 @@ class Bot(threading.Thread):
 		return c
 	
 	
-	def close_xmpp_connection(self, nickname):
+	def close_xmpp_connection(self, nickname, force=False):
 		if not self.xmpp_connections.has_key(nickname):
 			return
 		c = self.xmpp_connections[nickname]
 		c.lock.acquire()
 		c.used_by -= 1
-		if c.used_by < 1:
+		if c.used_by < 1 or force:
 			self.error('===> Debug: closing XMPP connection for "'+nickname+'"', debug=True)
 			self.xmpp_connections.pop(nickname)
 			c.send(xmpp.protocol.Presence(typ='unavailable'))
@@ -846,8 +852,10 @@ class Bot(threading.Thread):
 	
 	def stop(self, message='Stopping bot'):
 		for bridge in self.bridges:
-			self.removeBridge(bridge, message=message)
+			bridge.stop(message=message)
 	
 	
 	def __del__(self):
-		self.stop()
+		for bridge in self.bridges:
+			self.removeBridge(bridge, message=message)
+		self.halt = True
--- a/bridge.py
+++ b/bridge.py
@@ -414,7 +414,6 @@ class Bridge:
 		# Delete participants objects
 		for p in self.participants:
 			p.leave(message)
-			del p
 		self.participants = []
 	
 	
--- a/commands.py
+++ b/commands.py
@@ -180,7 +180,7 @@ def debug(bot, command, args_array, bot_
 
 
 def halt(bot, command, args_array, bot_admin, bridge):
-	bot.stop()
+	bot.__del__()
 	return
 
 
--- a/irclib.py
+++ b/irclib.py
@@ -254,6 +254,9 @@ class IRC:
             timeout -- Parameter to pass to process_once.
         """
         while 1:
+            if self.bot.halt:
+                self.disconnect_all(message='Stopping bot')
+                break
             try:
                 self.process_once(timeout)
             except ServerNotConnectedError:
--- a/start_bots_from_xml_config.py
+++ b/start_bots_from_xml_config.py
@@ -58,26 +58,45 @@ try:
 		for bridge_el in bot_el.getElementsByTagName('bridge'):
 			xmpp_room = bridge_el.getElementsByTagName('xmpp-room')[0]
 			irc = bridge_el.getElementsByTagName('irc')[0]
+			
 			irc_connection_interval = 1
 			if irc.hasAttribute('connection_interval'):
 				try:
 					irc_connection_interval = float(irc.getAttribute('connection_interval'))
 				except ValueError:
 					print '[Error] the value of connection_interval must be a number'
-			say_level = 'all'
+			
 			if bridge_el.hasAttribute('say_level'):
 				say_level = bridge_el.getAttribute('say_level')
+			else:
+				say_level = 'all'
+			
 			if bridge_el.hasAttribute('mode'):
 				mode = bridge_el.getAttribute('mode')
 			else:
 				mode = 'normal'
+			
 			bot.new_bridge(xmpp_room.getAttribute('jid'), irc.getAttribute('chan'), irc.getAttribute('server'), mode, say_level, irc_connection_interval=irc_connection_interval)
 	
 	
+	if len(bots) == 0:
+		print 'No bots in the configuration file, exiting ...'
+		exit(0)
+	
 	while True:
-		sleep(1)
+		for bot in bots:
+			if len(bot.xmpp_connections) == 0:
+				bots.remove(bot)
+		if len(bots) == 0:
+			raise Exception()
+		sleep(10)
 except:
+	if len(bots) == 0:
+		print 'All bots have been shut down, exiting ...'
+		exit(0)
+	
 	for bot in bots:
+		bots.remove(bot)
 		del bot
 	traceback.print_exc()
 	quit(3)
\ No newline at end of file