comparison irclib.py @ 136:7dc6a1764c4f

handle irclib.ServerNotConnectedError Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 16 Jan 2010 14:09:53 +0100
parents 46af7f2744a9
children f3a9c891f7c0
comparison
equal deleted inserted replaced
135:922858915907 136:7dc6a1764c4f
207 if s == c._get_socket(): 207 if s == c._get_socket():
208 c.lock.acquire() 208 c.lock.acquire()
209 if hasattr(c, 'socket'): 209 if hasattr(c, 'socket'):
210 try: 210 try:
211 c.process_data() 211 c.process_data()
212 except ServerNotConnectedError:
213 self.bot.restart()
212 except: 214 except:
213 self.bot.error('[Error] Unkonwn exception on IRC thread:\n'+traceback.format_exc(), send_to_admins=True) 215 self.bot.error('[Error] Unkonwn exception on IRC thread:\n'+traceback.format_exc(), send_to_admins=True)
214 c.lock.release() 216 c.lock.release()
215 217
216 def process_timeout(self): 218 def process_timeout(self):
465 if self.really_connected == True: 467 if self.really_connected == True:
466 self._call_nick_callbacks(None) 468 self._call_nick_callbacks(None)
467 self.lock.release() 469 self.lock.release()
468 return self 470 return self
469 471
470 472 if self.socket != 'closed':
471 self.nick_callbacks = [] 473 self.nick_callbacks = []
472 self.irc_id = None 474 self.irc_id = None
473 self.previous_buffer = "" 475 self.previous_buffer = ""
474 self.handlers = {} 476 self.handlers = {}
475 self.real_server_name = "" 477 self.real_server_name = ""
476 self.real_nickname = self.nickname 478 self.real_nickname = self.nickname
477 self.username = username or self.nickname 479 self.username = username or self.nickname
478 self.ircname = ircname or self.nickname 480 self.ircname = ircname or self.nickname
479 self.password = password 481 self.password = password
480 self.localaddress = localaddress 482 self.localaddress = localaddress
481 self.localport = localport 483 self.localport = localport
482 self.localhost = socket.gethostname() 484 self.localhost = socket.gethostname()
483 485
484 self.irclibobj.bot.error('===> Debug: opening new IRC connection for '+self.__str__(), debug=True) 486 self.irclibobj.bot.error('===> Debug: opening new IRC connection for '+self.__str__(), debug=True)
487 else:
488 self.irclibobj.bot.error('===> Debug: reopening IRC connection for '+self.__str__(), debug=True)
489
485 self._ping() 490 self._ping()
486 491
487 if ipv6: 492 if ipv6:
488 self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) 493 self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
489 else: 494 else:
493 self.socket.connect((self.server, self.port)) 498 self.socket.connect((self.server, self.port))
494 if ssl: 499 if ssl:
495 self.ssl = socket.ssl(self.socket) 500 self.ssl = socket.ssl(self.socket)
496 except socket.error, x: 501 except socket.error, x:
497 self.socket.close() 502 self.socket.close()
498 self.socket = None 503 self.socket = 'closed'
499 raise ServerConnectionError, "Couldn't connect to socket: %s" % x 504 raise ServerConnectionError, "Couldn't connect to socket: %s" % x
500 self.connected = True 505 self.connected = True
501 if self.irclibobj.fn_to_add_socket: 506 if self.irclibobj.fn_to_add_socket:
502 self.irclibobj.fn_to_add_socket(self.socket) 507 self.irclibobj.fn_to_add_socket(self.socket)
503 508
752 757
753 try: 758 try:
754 self.socket.close() 759 self.socket.close()
755 except socket.error, x: 760 except socket.error, x:
756 pass 761 pass
757 self.socket = None 762 self.socket = 'closed'
758 self.lock.release() 763 self.lock.release()
759 764
760 if volontary == False: 765 if volontary == False:
761 self._handle_event(Event("disconnect", self.server, "", [message])) 766 self._handle_event(Event("disconnect", self.server, "", [message]))
762 767
899 """Send raw string to the server. 904 """Send raw string to the server.
900 905
901 The string will be padded with appropriate CR LF. 906 The string will be padded with appropriate CR LF.
902 """ 907 """
903 if self.socket is None: 908 if self.socket is None:
904 raise ServerNotConnectedError, "Not connected." 909 raise ServerNotConnectedError, self
905 try: 910 try:
906 if self.ssl: 911 if self.ssl:
907 self.ssl.write(string.encode('utf-8') + "\r\n") 912 self.ssl.write(string.encode('utf-8') + "\r\n")
908 else: 913 else:
909 self.socket.send(string.encode('utf-8') + "\r\n") 914 self.socket.send(string.encode('utf-8') + "\r\n")
1049 self.connected = False 1054 self.connected = False
1050 try: 1055 try:
1051 self.socket.close() 1056 self.socket.close()
1052 except socket.error, x: 1057 except socket.error, x:
1053 pass 1058 pass
1054 self.socket = None 1059 self.socket = 'closed'
1055 self.irclibobj._handle_event( 1060 self.irclibobj._handle_event(
1056 self, 1061 self,
1057 Event("dcc_disconnect", self.peeraddress, "", [message])) 1062 Event("dcc_disconnect", self.peeraddress, "", [message]))
1058 self.irclibobj._remove_connection(self) 1063 self.irclibobj._remove_connection(self)
1059 1064