gunnbr / gthx

An IRC bot that tracks the status of another bot and takes over when the primary bot is down
GNU General Public License v2.0
6 stars 4 forks source link

Properly handle invalid ASCII characters in quit message #27

Closed gunnbr closed 7 years ago

gunnbr commented 7 years ago

The following quit message:

Feb 20 17:18:48 * Guest44 has quit (Quit: My MacBook has gone to sleep. ZZZzzz…)

Caused an exception:

2017-02-20 17:18:48-0800 [Gthx,client] Unhandled Error
        Traceback (most recent call last):
          File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 215, in _dataReceived
            rval = self.protocol.dataReceived(data)
          File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2622, in dataReceived
            basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
          File "/usr/lib/python2.7/dist-packages/twisted/protocols/basic.py", line 571, in dataReceived
            why = self.lineReceived(line)
          File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2630, in lineReceived
            self.handleCommand(command, prefix, params)
        --- <exception caught here> ---
          File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2685, in handleCommand
            method(prefix, params)
          File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 1988, in irc_QUIT
            self.userQuit(nick, params[0])
          File "/usr/sbin/gthx/gthx.py", line 181, in userQuit
            print "%s disconnected : %s" % (user, quitMessage)
          File "/usr/lib/python2.7/dist-packages/twisted/logger/_io.py", line 163, in write
            string = string.decode(self._encoding)
        exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal not in range(128)

gthx did not crash, but it would be nice to handle this more gracefully and properly.

gunnbr commented 7 years ago

After running into another similar exception in another place, I realize that this exception comes from attempting to log a message that contains unicode characters. The problem is that logger._encoding is set to ascii, but ascii can't handle emoji.

gunnbr commented 7 years ago

I couldn't find a way to set the default encoding of the logger, so I ended up just calling decode("utf-8") on each user entered string before logging it. Fixed it for factoids, info, and quit messages.