Expression Based XChat Commands via the Python Plugin Interface

XChat does not support implicit or regular expression based hooks or something similar. You can only register a callback for a /SOMETHING command. But there are ways.

Basically what you have to do is to register your callback to the “” command and make sure that it handles it’s own dispatching. Here a small example that converts [[Foo]] to http://en.wikipedia.org/wiki/Foo:

import re
from urllib import quote

link_re = re.compile(r'\[\[([^\]]+)\]\]')
link_base = 'http://en.wikipedia.org/wiki/'
listening = [True]

def expand_wikipedia_link(word, word_eol, userdata):
    if listening and word_eol:
        del listening[:]
        try:
            has_match = []
            def handle_match(m):
                has_match[:] = [True]
                return link_base + quote(m.group(1).replace(' ', '_')
            text = link_re.sub(handle_match, word_eol[0])
            if has_match:
                xchat.command('say ' + text)
                return xchat.EAT_ALL
        finally:
            listening[:] = [True]

xchat.hook_command('', expand_wikipedia_link)

Yes. This code is ugly and it requires a lot of closure hacking but at least it shows that it’s possible.

Leave a Reply

cogitations driven by wordpress