changeset 193:c2a8586e64b5

handle IRC inviteonlychan event, improved error messages Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 21 Feb 2010 15:38:42 +0100
parents 12fa9bb73b1d
children 5af6c828c606
files bot.py bridge.py
diffstat 2 files changed, 30 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -167,7 +167,7 @@ class Bot(threading.Thread):
 							
 							# room has been destroyed, stop the bridge
 							self.error(say_levels.error, 'The MUC room of the bridge '+str(bridge)+' has been destroyed with reason "'+r+'", stopping the bridge', send_to_admins=True)
-							bridge.stop(message='The MUC room of the bridge has been destroyed with reason "'+r+'", stopping the bridge')
+							bridge.stop(message='The MUC room has been destroyed with reason "'+r+'", stopping the bridge')
 				
 				else:
 					# presence comes from a participant of the muc
@@ -417,7 +417,7 @@ class Bot(threading.Thread):
 										# we don't have the permission to speak
 										# let's remove the bridge and tell admins
 										self.error(say_levels.error, 'Not allowed to speak on the XMPP MUC of bridge '+str(b)+', stopping it', send_to_admins=True)
-										b.stop(message='Not allowed to speak on the XMPP MUC, stopping bridge.')
+										b.stop(message='Not allowed to speak on the XMPP MUC, stopping the bridge')
 									else:
 										self.error(2, 'recevied unknown error message\n'+message.__str__(fancy=1), debug=True)
 					return
@@ -627,14 +627,14 @@ class Bot(threading.Thread):
 			return
 		
 		
-		if event.eventtype() in ['cannotsendtochan', 'notonchannel']:
+		if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']:
 			self.error(2, debug_str, debug=True)
 			bridges = self.getBridges(irc_room=event.arguments()[0], irc_server=connection.server)
 			if len(bridges) > 1:
 				raise Exception, 'more than one bridge for one irc chan, WTF ?'
 			bridge = bridges[0]
 			if connection.get_nickname() == self.nickname:
-				bridge._join_irc_failed()
+				bridge._join_irc_failed(event.eventtype())
 			else:
 				p = bridge.getParticipant(connection.get_nickname())
 				p._close_irc_connection('')
@@ -832,9 +832,9 @@ class Bot(threading.Thread):
 			self.error(3, 'XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True)
 	
 	
-	def removeBridge(self, bridge, message='Removing bridge'):
+	def removeBridge(self, bridge, message='Removing bridge', log=True):
 		self.bridges.remove(bridge)
-		bridge.stop(message)
+		bridge.stop(message=message, log=log)
 	
 	
 	def respond(self, message, participant=None, bot_admin=False):
@@ -851,7 +851,7 @@ class Bot(threading.Thread):
 	def restart(self):
 		# Stop the bridges
 		for b in self.bridges:
-			b.stop(message='Restarting bot')
+			b.stop(message='Restarting bot', log=False)
 		
 		# Reopen the bot's XMPP connection
 		self.reopen_xmpp_connection(self.xmpp_c)
@@ -879,17 +879,18 @@ class Bot(threading.Thread):
 					b.reconnecting = True
 					self.irc.execute_delayed(delay, b.init2)
 				
-				b.stop(message=leave_message)
+				b.stop(message=leave_message, log=False)
 		
 		self.error(error[0], error[1], send_to_admins=True)
 	
 	
 	def stop(self, message='Stopping bot'):
+		self.error(-1, message, send_to_admins=True)
 		for bridge in self.bridges:
-			bridge.stop(message=message)
+			bridge.stop(message=message, log=False)
 	
 	
 	def __del__(self):
 		for bridge in self.bridges:
-			self.removeBridge(bridge, message='Stopping bot')
+			self.removeBridge(bridge, message='Stopping bot', log=False)
 		self.halt = True
--- a/bridge.py
+++ b/bridge.py
@@ -68,9 +68,9 @@ class Bridge:
 		self.bot.error(say_levels.notice, 'bridge "'+str(self)+'" is running in '+self.mode+' mode and a say_level of "'+str(self.say_level)+'"')
 	
 	
-	def _join_irc_failed(self):
-		self.bot.error(say_levels.error, 'failed to connect to the IRC chan of bridge "'+str(self)+'", stopping bridge', send_to_admins=True)
-		self.stop(message='failed to connect to the IRC chan')
+	def _join_irc_failed(self, reason):
+		self.bot.error(say_levels.error, 'failed to connect to the IRC chan of bridge '+str(self)+'\nreason: '+reason, send_to_admins=True)
+		self.stop(message='Failed to connect to the IRC chan, stopping bridge', log=False)
 	
 	
 	def _irc_nick_callback(self, error, arguments=[]):
@@ -85,20 +85,15 @@ class Bridge:
 		else:
 			self.mode = None
 			self.say(say_levels.error, 'failed to connect to the IRC chan, leaving ...', on_irc=False)
-			try:
-				if error == 'nicknameinuse':
-					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 == 'nickcollision':
-					raise Exception('[Error] "'+self.bot.nickname+'" is already used or reserved on the IRC server of bridge "'+str(self)+'"')
-				elif error == 'erroneusnickname':
-					raise Exception('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
-				elif error == 'nicknametoolong':
-					raise Exception('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
-				else:
-					raise Exception('[Error] unknown error for "'+self.bot.nickname+'" on bridge "'+str(self)+'"')
-			except:
-				trace = traceback.format_exc()
-			self._join_irc_failed()
+			if error in ['nicknameinuse', 'nickcollision']:
+				reason = '"'+self.bot.nickname+'" is already used or reserved on the IRC server'
+			elif error == 'erroneusnickname':
+				reason = '"'+self.bot.nickname+'" got "erroneusnickname"'
+			elif error == 'nicknametoolong':
+				reason = '"'+self.bot.nickname+'" got "nicknametoolong", limit seems to be '+str(arguments[0])
+			else:
+				reason = error
+			self._join_irc_failed(reason)
 	
 	
 	def _RemoteServerNotFound_handler(self):
@@ -128,7 +123,7 @@ class Bridge:
 				except:
 					trace = traceback.format_exc()
 			self.bot.error(say_levels.error, 'failed to connect to the XMPP room of bridge "'+str(self)+'", stopping bridge\n'+trace, send_to_admins=True)
-			self.stop(message='failed to connect to the XMPP room')
+			self.stop(message='Failed to connect to the XMPP room, stopping bridge')
 	
 	
 	def addParticipant(self, from_protocol, nickname, real_jid=None, irc_id=None):
@@ -340,14 +335,11 @@ class Bridge:
 			self.bot.error(1, 'Bad decision tree,  p.protocol='+p.protocol+'  left_protocol='+left_protocol+'\np.xmpp_c='+str(p.xmpp_c)+'\np.irc_connection='+str(p.irc_connection), debug=True)
 	
 	
-	def restart(self):
+	def restart(self, log=True):
 		"""Restart the bridge"""
 		
-		# Tell admins
-		self.bot.error(-1, 'Restarting bridge '+str(self), send_to_admins=True)
-		
 		# Stop the bridge
-		self.stop(message='Restarting bridge')
+		self.stop(message='Restarting bridge', log=log)
 		
 		# Recreate the bridge
 		self.init2()
@@ -375,7 +367,7 @@ class Bridge:
 			self.say(say_levels.info, 'Participants on IRC: '+'  '.join(irc_participants_nicknames), on_irc=False)
 	
 	
-	def stop(self, message='Stopping bridge'):
+	def stop(self, message='Stopping bridge', log=True):
 		"""Stop the bridge"""
 		
 		# Close IRC connection if not used by an other bridge, just leave the room otherwise
@@ -397,6 +389,9 @@ class Bridge:
 		for p in self.participants:
 			p.leave(message)
 		self.participants = []
+		
+		if log:
+			self.bot.error(-1, message+' '+str(self), send_to_admins=True)
 	
 	
 	def __str__(self):