UTF8 to ISO-8859-1 xchat converter

Today I’m a little short in time, so I decided to implement one of the bit-sized tasks in my backlog. These UTF8 raw strings appearing in my xchat when someone else is using that encoding is bothering for a long time, and the fix is now straightforward, with the latest update I’ve done in the xchat python plugin (the emit_print() API function was implemented). It’s just a matter of creating a decode() function which tries to decode the string from UTF8, and reencode it as ISO-8859-1. If it succeeds, it blocks xchat, and sends the message by itself. Otherwise it just lets xchat do its work. After that, I’ve hooked this method in the many print events, including information about which of the parameters are the strings to decode. Here is an excerpt (the full version is in the examples section of the xchat-python page):


def decode(word, word_eol, userdata):
    event, pos = userdata
    if type(pos) is int:
        pos = (pos,)
    changed = False
    for i in pos:
            reencoded = word[i].decode('utf8') 
        except UnicodeError:
        if reencoded != word[i]:
            word[i] = reencoded
            changed = True
    if changed:
        xchat.emit_print(event, *word)
        return xchat.EAT_XCHAT
        return xchat.EAT_NONE

  ("Channel Action", 1),
  ("Channel Action Hilight", 1),

for event in EVENTS:
    xchat.hook_print(event[0], decode, event)


Notice that you need the latest version of the xchat-python plugin, which will be available in the next version of xchat (coming after 2.0.5). You may also download xchat 2.0.5, and update the plugin with the file available in the Download section of the plugin page.

This entry was posted in Project, Python, Snippet. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *