--- a/irc.py Mon Aug 03 19:45:57 2015 +0300 +++ b/irc.py Tue Aug 04 22:39:22 2015 +0300 @@ -26,6 +26,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' +from __future__ import print_function import asyncore import socket import sys @@ -60,7 +61,8 @@ # Prints a line to log channel(s) # def broadcast (line): - print line + print (line) + for client in all_clients: if not client.flags & CLIF_CONNECTED: continue @@ -104,7 +106,8 @@ all_clients.append (self) asyncore.dispatcher.__init__ (self) self.create_socket (socket.AF_INET, socket.SOCK_STREAM) - print "[%s] Connecting to [%s] %s:%d..." % (get_timestamp(), self.name, self.host, self.port) + print ("[%s] Connecting to [%s] %s:%d..." % \ + (get_timestamp(), self.name, self.host, self.port)) self.connect ((self.host, self.port)) ClientsByName[self.name] = self @@ -116,17 +119,21 @@ self.write ("NICK %s" % self.mynick) def handle_connect (self): - print "[%s] Connected to [%s] %s:%d" % (get_timestamp(), self.name, self.host, self.port) + print ("[%s] Connected to [%s] %s:%d" % (get_timestamp(), self.name, self.host, self.port)) self.register_to_irc() def write (self, utfdata): try: - self.send_buffer.append ("%s" % utfdata.decode("utf-8","ignore").encode("ascii","ignore")) + if sys.version_info < (3, 0): + self.send_buffer.append (utfdata.decode("utf-8","ignore").encode("ascii","ignore")) + else: + self.send_buffer.append (utfdata) except UnicodeEncodeError: pass def handle_close (self): - print "[%s] Connection to [%s] %s:%d terminated." % (get_timestamp(), self.name, self.host, self.port) + print ("[%s] Connection to [%s] %s:%d terminated." % + (get_timestamp(), self.name, self.host, self.port)) self.close() def handle_write (self): @@ -141,24 +148,31 @@ def send_all_now (self): for line in self.send_buffer: if self.verbose: - print "[%s] [%s] <- %s" % (get_timestamp(), self.name, line) - self.send ("%s\n" % line) + print ("[%s] [%s] <- %s" % (get_timestamp(), self.name, line)) + + if sys.version_info >= (3, 0): + self.send (bytes (line + '\n', 'UTF-8')) + else: + self.send (line + '\n') time.sleep (0.25) self.send_buffer = [] def handle_read (self): lines = self.recv (4096).splitlines() - for utfline in lines: + for line in lines: try: - line = utfline.decode("utf-8","ignore").encode("ascii","ignore") + line = line.decode ('utf-8', 'ignore') + + if sys.version_info < (3, 0): + line = line.encode ('ascii', 'ignore') except UnicodeDecodeError: continue if self.verbose: - print "[%s] [%s] -> %s" % (get_timestamp(), self.name, line) + print ("[%s] [%s] -> %s" % (get_timestamp(), self.name, line)) - if line.startswith ("PING :"): - self.write ("PONG :%s" % line[6:]) + if line[:len('PING :')] == ('PING :'): + self.write ("PONG :%s" % line[len('PING :'):]) self.send_all_now() # pings must be responded to immediately! else: words = line.split(" ") @@ -172,7 +186,9 @@ self.write ('MODE %s %s' % (self.mynick, self.cfg.get_value ('umode', ''))) for channel in self.channels: - self.write ("JOIN %s %s" % (channel.get_value ('name'), channel.get_value ('password', default=''))) + self.write ("JOIN %s %s" % + (channel.get_value ('name'), + channel.get_value ('password', default=''))) elif words[1] == "PRIVMSG": self.handle_privmsg (line) elif words[1] == 'QUIT': @@ -271,7 +287,7 @@ return def handle_error(self): - raise RestartError (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) + raise RestartError (traceback.format_exception (*sys.exc_info())) def restart(self): raise RestartError('')