changeset 219:63289aa1dea7

moved "nicknametoolong" and ServerConnection.really_connected handling to irclib, renamed ServerConnection.really_connected to .logged_in Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 07 Mar 2010 12:08:29 +0100
parents ca22fdea1c17
children 5726a0833537
files bot.py bridge.py irclib.py participant.py
diffstat 4 files changed, 35 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -459,7 +459,7 @@ class Bot(threading.Thread):
 		# Events we always want to ignore
 		if 'all' in event.eventtype() or 'motd' in event.eventtype() or event.eventtype() in ['nicknameinuse', 'nickcollision', 'erroneusnickname']:
 			return
-		if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo', '328', 'pubnotice', '042']:
+		if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo', '328', 'pubnotice', '042', 'umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns']:
 			self.error(1, 'ignoring IRC '+event.eventtype(), debug=True)
 			return
 		
@@ -470,19 +470,6 @@ class Bot(threading.Thread):
 				nickname = event.source().split('!')[0]
 		
 		
-		# Events that we want to ignore only in some cases
-		if event.eventtype() in ['umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns']:
-			if connection.really_connected == False:
-				if event.target() == connection.nickname:
-					connection.really_connected = True
-					connection._call_nick_callbacks(None)
-				elif len(connection.nick_callbacks) > 0:
-					self.error(3, 'event target ('+event.target()+') and connection nickname ('+connection.nickname+') don\'t match', debug=True)
-					connection._call_nick_callbacks('nicknametoolong', arguments=[len(event.target())])
-			self.error(1, 'ignoring '+event.eventtype(), debug=True)
-			return
-		
-		
 		# A string representation of the event
 		event_str = '\nconnection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments())
 		debug_str = 'Received IRC event.'+event_str
@@ -499,7 +486,7 @@ class Bot(threading.Thread):
 				return
 			
 			if event.eventtype() in ['quit', 'part', 'nick', 'kick']:
-				if connection.get_nickname() != self.nickname:
+				if connection.real_nickname != self.nickname:
 					self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True)
 					return
 				else:
@@ -511,7 +498,7 @@ class Bot(threading.Thread):
 				return
 			
 			if event.eventtype() in ['pubmsg', 'action']:
-				if connection.get_nickname() != self.nickname:
+				if connection.real_nickname != self.nickname:
 					self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True)
 					return
 				if nickname == self.nickname:
@@ -654,17 +641,17 @@ class Bot(threading.Thread):
 			if len(bridges) > 1:
 				raise Exception, 'more than one bridge for one irc chan, WTF ?'
 			bridge = bridges[0]
-			if connection.get_nickname() == self.nickname:
+			if connection.real_nickname == self.nickname:
 				bridge._join_irc_failed(event.eventtype())
 			else:
-				p = bridge.get_participant(connection.get_nickname())
+				p = bridge.get_participant(connection.real_nickname)
 				p._close_irc_connection('')
 				p.irc_connection = event.eventtype()
 			return
 		
 		
 		# Ignore events not received on bot connection
-		if connection.get_nickname() != self.nickname:
+		if connection.real_nickname != self.nickname:
 			self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bridge connection', debug=True)
 			return
 		
--- a/bridge.py
+++ b/bridge.py
@@ -75,7 +75,7 @@ class Bridge:
 		self.stop(message='Failed to connect to the IRC chan, stopping bridge', log=False)
 	
 	
-	def _irc_nick_callback(self, error, arguments=[]):
+	def _irc_nick_callback(self, error):
 		if error == None:
 			if self.mode == None:
 				return
@@ -92,7 +92,7 @@ class Bridge:
 			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])
+				reason = '"'+self.bot.nickname+'" got "nicknametoolong", limit seems to be '+str(len(self.irc_connection.real_nickname))
 			else:
 				reason = error
 			self._join_irc_failed(reason)
@@ -135,7 +135,7 @@ class Bridge:
 		try:
 			p = self.get_participant(nickname)
 			if p.protocol != from_protocol:
-				if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.really_connected == True and p.irc_connection.real_nickname == nickname or from_protocol == 'xmpp' and isinstance(p.xmpp_c, xmpp.client.Client) and isinstance(p.muc, xmpp.muc) and p.xmpp_c.nickname == nickname:
+				if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.logged_in and p.irc_connection.real_nickname == nickname or from_protocol == 'xmpp' and isinstance(p.xmpp_c, xmpp.client.Client) and isinstance(p.muc, xmpp.muc) and p.xmpp_c.nickname == nickname:
 					if irc_id:
 						p.irc_connection.irc_id = irc_id
 					return p
@@ -380,7 +380,7 @@ class Bridge:
 	
 	
 	def show_participants_list_on(self, protocols=[]):
-		if 'irc' in protocols and self.irc_connection.really_connected:
+		if 'irc' in protocols and self.irc_connection.logged_in:
 			xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp'])
 			self.say(say_levels.info, 'Participants on XMPP: '+'  '.join(xmpp_participants_nicknames), on_xmpp=False)
 		if 'xmpp' in protocols:
--- a/irclib.py
+++ b/irclib.py
@@ -196,7 +196,7 @@ class IRC:
 
     def get_connection(self, server, port, nickname):
         for c in self.connections:
-            if c.server == server and c.port == port and c.real_nickname == nickname:
+            if c.server == server and c.port == port and nickname in [c.nickname, c.real_nickname]:
                 return c
         return None
 
@@ -454,7 +454,7 @@ class ServerConnection(Connection):
     def __init__(self, irclibobj, server, port, nickname):
         Connection.__init__(self, irclibobj)
         self.connected = False  # Not connected yet.
-        self.really_connected = False
+        self.logged_in = False
         self.used_by = 0
         self.socket = None
         self.ssl = None
@@ -525,7 +525,7 @@ class ServerConnection(Connection):
         if self.used_by > 0:
             self.used_by += 1
             self.irclibobj.bot.error(3, 'using existing IRC connection for '+self.__str__()+', this connection is now used by '+str(self.used_by)+' bridges', debug=True)
-            if self.really_connected:
+            if self.logged_in:
                 self._call_nick_callbacks(None)
             self.lock.release()
             return self
@@ -541,6 +541,7 @@ class ServerConnection(Connection):
             self.handlers = {}
             self.real_server_name = ""
             self.real_nickname = self.nickname
+            self.new_nickname = None
             self.username = username or self.nickname
             self.ircname = ircname or self.nickname
             self.password = password
@@ -603,11 +604,11 @@ class ServerConnection(Connection):
         return self
 
 
-    def _call_nick_callbacks(self, error, arguments=[]):
+    def _call_nick_callbacks(self, error):
         i = 0
         for f in self.nick_callbacks:
             i += 1
-            f(error, arguments=arguments)
+            f(error)
         self.nick_callbacks = []
         if i == 0:
             self.irclibobj.bot.error(1, 'no nick callback for "'+self.__str__()+'"', debug=True)
@@ -645,15 +646,6 @@ class ServerConnection(Connection):
         else:
             return ""
 
-    def get_nickname(self):
-        """Get the (real) nick name.
-
-        This method returns the (real) nickname.  The library keeps
-        track of nick changes, so it might not be the nick name that
-        was passed to the connect() method.  """
-
-        return self.real_nickname
-
     def process_data(self):
         """[Internal]"""
 
@@ -714,13 +706,17 @@ class ServerConnection(Connection):
             if command in numeric_events:
                 command = numeric_events[command]
 
-            if command == "nick":
-                if nm_to_n(prefix) == self.real_nickname:
-                    self.real_nickname = arguments[0]
-            elif command == "welcome":
-                # Record the nickname in case the client changed nick
-                # in a nicknameinuse callback.
+            if command in ["nick", "welcome"]:
+                self.logged_in = True
                 self.real_nickname = arguments[0]
+                if self.new_nickname != arguments[0]:
+                    if len(self.new_nickname) > len(arguments[0]):
+                        self._handle_event(Event('nicknametoolong', None, None, None))
+                    else:
+                        self._handle_event(Event('erroneusnickname', None, None, None))
+                else:
+                    self._call_nick_callbacks(None)
+                self.new_nickname = None
 
             if command in ["privmsg", "notice"]:
                 target, message = arguments[0], arguments[1]
@@ -787,8 +783,8 @@ class ServerConnection(Connection):
     def _handle_event(self, event):
         """[Internal]"""
         self.irclibobj._handle_event(self, event)
-        if event.eventtype() in ['disconnect', 'nicknameinuse', 'nickcollision', 'erroneusnickname']:
-            self._call_nick_callbacks(event.eventtype(), arguments=[event])
+        if event.eventtype() in ['disconnect', 'nicknameinuse', 'nickcollision', 'erroneusnickname', 'nicknametoolong']:
+            self._call_nick_callbacks(event.eventtype())
         if event.eventtype() in self.handlers:
             for fn in self.handlers[event.eventtype()]:
                 fn(self, event)
@@ -843,8 +839,8 @@ class ServerConnection(Connection):
 
         if self.connected:
             self.connected = False
-        if self.really_connected:
-            self.really_connected = False
+        if self.logged_in:
+            self.logged_in = False
 
         if self.socket and self.socket != 'closed':
             if message and message != 'Connection reset by peer':
@@ -942,6 +938,7 @@ class ServerConnection(Connection):
         except:
             self._call_nick_callbacks('erroneusnickname')
             return False
+        self.new_nickname = newnick
         self.send_raw("NICK " + newnick)
         return True
 
--- a/participant.py
+++ b/participant.py
@@ -114,7 +114,7 @@ class Participant:
 		self.irc_connection.connect(nick_callback=self._irc_nick_callback)
 	
 	
-	def _irc_nick_callback(self, error, arguments=[]):
+	def _irc_nick_callback(self, error):
 		if error == None:
 			self.irc_connection.join(self.bridge.irc_room)
 			m = '"'+self.nickname+'" duplicate succesfully created on IRC side of bridge "'+str(self.bridge)+'"'
@@ -157,7 +157,7 @@ class Participant:
 					self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" contains unauthorized characters and cannot be used in the IRC channel', log=True)
 			
 			elif error == 'nicknametoolong':
-				self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is too long (limit seems to be '+str(arguments[0])+') and cannot be used in the IRC channel', log=True)
+				self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is too long (limit seems to be '+str(len(self.irc_connection.real_nickname))+') and cannot be used in the IRC channel', log=True)
 			
 			else:
 				self.bridge.say(say_levels.warning, 'unknown error while adding "'+self.nickname+'" to IRC side of bridge', log=True)
@@ -340,7 +340,7 @@ class Participant:
 	
 	def _close_irc_connection(self, message):
 		if isinstance(self.irc_connection, ServerConnection):
-			if self.irc_connection.really_connected == True:
+			if self.irc_connection.logged_in:
 				self.irc_connection.part(self.bridge.irc_room, message=message)
 			self.irc_connection.used_by -= 1
 			if self.irc_connection.used_by < 1:
@@ -351,7 +351,7 @@ class Participant:
 	def __str__(self):
 		r = 'self.protocol='+str(self.protocol)+'\n'+'self.nickname='+str(self.nickname)
 		if isinstance(self.irc_connection, ServerConnection):
-			r += '\nself.irc_connection='+str(self.irc_connection)+'\n'+'self.irc_connection.really_connected='+str(self.irc_connection.really_connected)
+			r += '\nself.irc_connection='+str(self.irc_connection)+'\n'+'self.irc_connection.logged_in='+str(self.irc_connection.logged_in)
 		if isinstance(self.xmpp_c, xmpp.client.Client):
 			r += '\nself.muc.connected='+str(self.muc.connected)
 		return r