comparison irclib.py @ 172:64a0e9636ae6

removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Tue, 02 Feb 2010 20:19:50 +0100
parents f4997d50e93b
children c158ad24ef3c
comparison
equal deleted inserted replaced
171:489c157d9e82 172:64a0e9636ae6
161 161
162 self.fn_to_add_timeout = fn_to_add_timeout 162 self.fn_to_add_timeout = fn_to_add_timeout
163 self.connections = [] 163 self.connections = []
164 self.handlers = {} 164 self.handlers = {}
165 self.delayed_commands = [] # list of tuples in the format (time, function, arguments) 165 self.delayed_commands = [] # list of tuples in the format (time, function, arguments)
166 self.charsets = {'': ['utf-8']}
166 167
167 self.add_global_handler("ping", _ping_ponger, -42) 168 self.add_global_handler("ping", _ping_ponger, -42)
168 169
169 def get_connection(self, server, port, nickname): 170 def get_connection(self, server, port, nickname):
170 for c in self.connections: 171 for c in self.connections:
422 self.lock = threading.RLock() 423 self.lock = threading.RLock()
423 self.left_channels = [] 424 self.left_channels = []
424 425
425 426
426 def __str__(self): 427 def __str__(self):
427 return self.real_nickname+' at '+self.server+':'+str(self.port) 428 return self.real_nickname+' at '+self._server_str()
429
430
431 def _decode(self, bytes):
432 charsets = self.irclibobj.charsets[self._server_str()] or self.irclibobj.charsets['']
433 for codec in charsets:
434 try:
435 return bytes.decode(codec)
436 except (UnicodeEncodeError, UnicodeDecodeError):
437 pass
438 raise Exception, 'no suitable codec found for: '+repr(bytes)+'\ntried: '+' '.join(charsets)
428 439
429 440
430 def _ping(self): 441 def _ping(self):
431 self.irclibobj.execute_delayed(60, self._ping) 442 self.irclibobj.execute_delayed(60, self._ping)
432 if self.connected == False: 443 if self.connected == False:
433 return 444 return
434 self.irclibobj.bot.error('=> Debug: sending IRC ping', debug=True) 445 self.irclibobj.bot.error('=> Debug: sending IRC ping', debug=True)
435 self.ping(self.get_server_name()) 446 self.ping(self.get_server_name())
436 447
437 448
449 def _server_str(self):
450 return self.server+':'+str(self.port)
451
452
438 def connect(self, password=None, username=None, 453 def connect(self, password=None, username=None,
439 ircname=None, localaddress="", localport=0, ssl=False, ipv6=False, nick_callback=None): 454 ircname=None, localaddress="", localport=0, ssl=False, ipv6=False, nick_callback=None, charsets=None):
440 """Connect to the server. 455 """Connect to the server.
441 456
442 Arguments: 457 Arguments:
443 458
444 password -- Password (if any). 459 password -- Password (if any).
471 self._call_nick_callbacks(None) 486 self._call_nick_callbacks(None)
472 self.lock.release() 487 self.lock.release()
473 return self 488 return self
474 489
475 if self.socket != 'closed': 490 if self.socket != 'closed':
491 if charsets or not self.irclibobj.charsets.has_key(self._server_str()):
492 self.irclibobj.charsets[self._server_str()] = charsets
476 self.nick_callbacks = [] 493 self.nick_callbacks = []
477 self.irc_id = None 494 self.irc_id = None
478 self.previous_buffer = "" 495 self.previous_buffer = ""
479 self.handlers = {} 496 self.handlers = {}
480 self.real_server_name = "" 497 self.real_server_name = ""
597 if DEBUG: 614 if DEBUG:
598 print "FROM SERVER:", line 615 print "FROM SERVER:", line
599 616
600 if not line: 617 if not line:
601 continue 618 continue
619
620 line = self._decode(line)
602 621
603 prefix = None 622 prefix = None
604 command = None 623 command = None
605 arguments = None 624 arguments = None
606 self._handle_event(Event("all_raw_messages", 625 self._handle_event(Event("all_raw_messages",