Decoding IRC message: use encoding from configuration file

This commit is contained in:
nemunaire 2014-10-04 07:39:08 +02:00
parent 49bfcdcae5
commit 020759fdab

View File

@ -45,6 +45,11 @@ class IRCServer(SocketServer):
#Keep a list of connected channels #Keep a list of connected channels
self.channels = dict() self.channels = dict()
if node.hasAttribute("encoding"):
self.encoding = node["encoding"]
else:
self.encoding = "utf-8"
if node.hasAttribute("caps"): if node.hasAttribute("caps"):
if node["caps"].lower() == "no": if node["caps"].lower() == "no":
self.capabilities = None self.capabilities = None
@ -196,13 +201,15 @@ class IRCServer(SocketServer):
return True return True
return False return False
def _close(self): def _close(self):
if self.connected: self.write("QUIT") if self.connected: self.write("QUIT")
return SocketServer._close(self) return SocketServer._close(self)
def read(self): def read(self):
for line in SocketServer.read(self): for line in SocketServer.read(self):
msg = IRCMessage(line, datetime.now(timezone.utc)) msg = IRCMessage(line, self.encoding)
if msg.cmd in self.hookscmd: if msg.cmd in self.hookscmd:
self.hookscmd[msg.cmd](msg) self.hookscmd[msg.cmd](msg)
@ -246,9 +253,11 @@ mgx = re.compile(b'''^(?:@(?P<tags>[^ ]+)\ )?
class IRCMessage: class IRCMessage:
def __init__(self, raw, timestamp): """Class responsible for parsing IRC messages"""
self.raw = raw
self.tags = { 'time': timestamp } def __init__(self, raw, encoding="utf-8"):
self.encoding = encoding
self.tags = { 'time': datetime.now(timezone.utc) }
self.params = list() self.params = list()
p = mgx.match(raw.rstrip()) p = mgx.match(raw.rstrip())
@ -299,8 +308,7 @@ class IRCMessage:
try: try:
s = s.decode() s = s.decode()
except UnicodeDecodeError: except UnicodeDecodeError:
#TODO: use encoding from config file s = s.decode(self.encoding, 'replace')
s = s.decode('utf-8', 'replace')
return s return s