changeset 19:c1b84196c100

Changed format of non-debug error messages, fixed IRC namreply handling, prevented crash when receiving bad XMPP stanza. Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 20 Aug 2009 13:20:50 +0200
parents 3cdf7bb580da
children 08cde283621a
files bot.py bridge.py participant.py start_bots_from_xml_config.py
diffstat 4 files changed, 33 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -55,7 +55,7 @@ class bot(Thread):
 		try:
 			self.xmpp_c = self.get_xmpp_connection(self.jid.getResource())
 		except:
-			self.error('Error: XMPP Connection failed')
+			self.error('[Error] XMPP Connection failed')
 			raise
 		self.xmpp_thread = Thread(target=self._xmpp_loop)
 		self.xmpp_thread.start()
@@ -73,15 +73,19 @@ class bot(Thread):
 	def _xmpp_loop(self):
 		"""[Internal] XMPP infinite loop."""
 		while True:
-			self.xmpp_c.Process(0.5)
 			try:
-				for c in self.xmpp_connections.itervalues():
-					if hasattr(c, 'Process'):
-						c.Process(0.5)
-					else:
-						sleep(0.5)
-			except RuntimeError:
-				pass
+				self.xmpp_c.Process(0.5)
+				try:
+					for c in self.xmpp_connections.itervalues():
+						if hasattr(c, 'Process'):
+							c.Process(0.5)
+						else:
+							sleep(0.5)
+				except RuntimeError:
+					pass
+			except ExpatError:
+				self.error('=> Debug: received invalid stanza', debug=True)
+				continue
 	
 	
 	def _xmpp_presence_handler(self, xmpp_c, presence):
@@ -123,11 +127,11 @@ class bot(Thread):
 							# participant changed its nickname
 							item = x.getTag('item')
 							if not item:
-								self.error('Debug: bad stanza, no item element', debug=True)
+								self.error('=> Debug: bad stanza, no item element', debug=True)
 								return
 							new_nick = item.getAttr('nick')
 							if not new_nick:
-								self.error('Debug: bad stanza, new nick is not given', debug=True)
+								self.error('=> Debug: bad stanza, new nick is not given', debug=True)
 								return
 							p.changeNickname(new_nick, 'irc')
 						
@@ -327,7 +331,7 @@ class bot(Thread):
 			if event.eventtype() == 'namreply':
 				# TODO: lock self.bridges for thread safety
 				for bridge in self.getBridges(irc_room=event.arguments()[1], irc_server=connection.server):
-					for nickname in re.split('(?:^[@\+]?|(?: [@\+]?)*)', event.arguments()[2].strip()):
+					for nickname in re.split('(?:^[&@\+]?|(?: [&@\+]?)*)', event.arguments()[2].strip()):
 						if nickname == '' or nickname == self.nickname:
 							continue
 						bridge.addParticipant('irc', nickname)
--- a/bridge.py
+++ b/bridge.py
@@ -62,14 +62,14 @@ class bridge:
 			self.bot.error('===> Debug: successfully connected on IRC side of bridge "'+str(self)+'"', debug=True)
 			self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_xmpp=False)
 		if error == 'nicknameinuse':
-			self.bot.error('Error: "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"')
-			raise Exception('Error: "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"')
+			self.bot.error('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"')
+			raise Exception('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"')
 		elif error == 'erroneusnickname':
-			self.bot.error('Error: "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
-			raise Exception('Error: "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
+			self.bot.error('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
+			raise Exception('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
 		elif error == 'nicknametoolong':
-			self.bot.error('Error: "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
-			raise Exception('Error: "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
+			self.bot.error('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
+			raise Exception('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
 	
 	
 	def _xmpp_join_callback(self, errors):
@@ -81,8 +81,8 @@ class bridge:
 			try:
 				raise error
 			except xmpp.muc.NicknameConflict:
-				self.bot.error('Error: "'+self.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"')
-				raise Exception('Error: "'+self.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"')
+				self.bot.error('[Error] "'+self.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"')
+				raise Exception('[Error] "'+self.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"')
 	
 	
 	def addParticipant(self, protocol, nickname):
@@ -98,7 +98,7 @@ class bridge:
 				elif protocol == 'xmpp':
 					p.createDuplicateOnIRC()
 				else:
-					raise Exception('Internal Error: bad protocol')
+					raise Exception('[Internal Error] bad protocol')
 			return
 		except NoSuchParticipantException:
 			pass
@@ -156,7 +156,7 @@ class bridge:
 					was_on_both = False
 		
 		else:
-			raise Exception('Internal Error: bad protocol')
+			raise Exception('[Internal Error] bad protocol')
 		
 		if was_on_both == True:
 			self.bot.error('===> Debug: "'+nickname+'" was on both sides of bridge "'+str(self)+'" but left '+left_protocol, debug=True)
--- a/participant.py
+++ b/participant.py
@@ -37,7 +37,7 @@ class participant:
 		elif protocol == 'irc':
 			self.createDuplicateOnXMPP()
 		else:
-			raise Exception('Internal Error: bad protocol')
+			raise Exception('[Internal Error] bad protocol')
 	
 	
 	def createDuplicateOnXMPP(self):
@@ -121,7 +121,7 @@ class participant:
 	
 	def sayOnIRC(self, message):
 		if self.protocol == 'irc':
-			raise Exception('Internal Error: "'+self.nickname+'" comes from IRC')
+			raise Exception('[Internal Error] "'+self.nickname+'" comes from IRC')
 		
 		try:
 			if self.irc_connection == None:
@@ -134,7 +134,7 @@ class participant:
 	
 	def sayOnIRCTo(self, to, message):
 		if self.protocol == 'irc':
-			raise Exception('Internal Error: "'+self.nickname+'" comes from IRC')
+			raise Exception('[Internal Error] "'+self.nickname+'" comes from IRC')
 		
 		if self.irc_connection == None:
 			if self.bridge.mode != 'normal':
@@ -150,7 +150,7 @@ class participant:
 	
 	def sayOnXMPP(self, message):
 		if self.protocol == 'xmpp':
-			raise Exception('Internal Error: "'+self.nickname+'" comes from XMPP')
+			raise Exception('[Internal Error] "'+self.nickname+'" comes from XMPP')
 		
 		try:
 			if self.xmpp_c == None:
@@ -163,7 +163,7 @@ class participant:
 	
 	def sayOnXMPPTo(self, to, message):
 		if self.protocol == 'xmpp':
-			raise Exception('Internal Error: "'+self.nickname+'" comes from XMPP')
+			raise Exception('[Internal Error] "'+self.nickname+'" comes from XMPP')
 		
 		try:
 			self.muc.sayTo(to, auto_decode(message))
--- a/start_bots_from_xml_config.py
+++ b/start_bots_from_xml_config.py
@@ -31,13 +31,13 @@ try:
 	else:
 		config = parse('config.xml')
 except IOError:
-	print 'Error: configuration file is missing or cannot be read'
+	print '[Error] configuration file is missing or cannot be read'
 	quit(1)
 
 bots_jids = []
 for bot_el in config.getElementsByTagName('bot'):
 	if bot_el.getAttribute('jid') in bots_jids:
-		print 'Error: you cannot have two bots using the same JID'
+		print '[Error] you cannot have two bots using the same JID'
 		quit(2)
 	bots_jids.append(bot_el.getAttribute('jid'))