From 9d4cdc4931aa58919fe3db013466d662abdc69a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9munaire?= Date: Tue, 17 Jul 2012 00:56:27 +0200 Subject: [PATCH] Better messages decoding --- message.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/message.py b/message.py index 954f1a5..5151af7 100644 --- a/message.py +++ b/message.py @@ -54,10 +54,12 @@ class Message: if self.cmd == b'PRIVMSG': #Check for CTCP request - self.ctcp = (words[3][0] == 0x01 or words[3][1] == 0x01) - self.content = words[3].decode() + self.ctcp = len(words[3]) > 1 and (words[3][0] == 0x01 or words[3][1] == 0x01) + self.content = words[3] + self.decode() if self.content[0] == ':': - self.content = ' '.join(words[3:])[1:] + self.content = (b' '.join(words[3:])[1:]) + self.decode() elif self.cmd == '353' and len(words) > 3: for i in range(2, len(words)): if words[i][0] == ":": @@ -86,6 +88,12 @@ class Message: if self.content[0] == ':': self.content = ' '.join(words[3:])[1:] + def decode(self): + try: + self.content = self.content.decode() + except UnicodeDecodeError: + self.content = self.content.decode('utf-8', 'replace') + @property def is_owner(self): return self.sender == self.srv.owner @@ -166,8 +174,8 @@ class Message: self.srv.send_ctcp(self.sender, "USERINFO %s" % (self.srv.realname)) elif self.content == '\x01VERSION\x01': self.srv.send_ctcp(self.sender, "VERSION nemubot v3") - else: - print (self.content) + elif self.content[:7] != '\x01ACTION': + print (self.content[:7]) self.srv.send_ctcp(self.sender, "ERRMSG Unknown or unimplemented CTCP request") def reparsemsg(self): @@ -232,7 +240,7 @@ class Message: print (CREDITS[c].to_string()) #Messages stating with ! - elif self.content[0] == '!': + elif self.content[0] == '!' and len(self.content[0]) > 1: self.mods = mods try: self.cmd = shlex.split(self.content[1:])