XChat notification and highlighting on phrases instead of single words

I wrote the following xchat plugin after they decided to “fix” the Extra words to highlight feature in XChat 2.8.6

The python plugin can be downloaded here

__module_name__ = 'hilight-phrase'
__module_description__ = 'XChat notification and hilighting on phrases instead of single words'
__module_version__ = '2.0'

import xchat, os, re

CONFFILE = os.environ['HOME'] + '/.xchat2/hilight-phrase.conf'
list=[]

xchat.prnt('%(name)s, version %(version)s' % {'name': __module_name__,  'version': __module_version__})  

def read_list():
    try:
        conf = open(CONFFILE,'r')
    except:
        xchat.prnt(CONFFILE + " currently doesn't exist, creating")
        return None
    lines = conf.readlines()
    for each in lines:
        list.append(re.sub(r'\n','',each))
    conf.close()

def save_list():
    conf = open(CONFFILE,'w')
    for phrase in list:
        conf.write(phrase + '\n')
    conf.close()

def check_msg(word, word_eol, userdata):
    for phrase in list:
        if phrase in word_eol[1].lower():
            xchat.command("gui color 3")
            xchat.emit_print( "Channel Msg Hilight", word[0], word[1] )
            return xchat.EAT_ALL

    return xchat.EAT_NONE

def add_hilight_phrase(word, word_eol, userdata):
    if len(word) == 1:
        return list_hilight_phrase(word, word_eol, userdata)
    phrase = word_eol[1]
    if phrase not in list:
        list.append(phrase)
        xchat.prnt('\x032* "%s" will be hilighted' % phrase)
    else:
        xchat.prnt('\x032* "%s" is already being hilighted' % phrase)
    save_list()
    return xchat.EAT_XCHAT

def list_hilight_phrase(word, word_eol, userdata):
    xchat.prnt('\x032Current hilight-phrase list: %d hilighted.' % len(list))
    for phrase in list:
        xchat.prnt('\x032  -- %s' % phrase)
    xchat.prnt('\x032* End of hilight-phrase list')
    return xchat.EAT_XCHAT

def remove_hilight_phrase(word, word_eol, userdata):
    phrase = word_eol[1]
    if phrase in list:
        list.remove(phrase)
        xchat.prnt('\x032 "%s" has been removed from the hilight list' % phrase)
    else:
        xchat.prnt('\x032 "%s" is not in the hilight list' % phrase)
    save_list()
    return xchat.EAT_XCHAT

read_list()
xchat.hook_command("hilight-add", add_hilight_phrase)
xchat.hook_command("hilight-list", list_hilight_phrase)
xchat.hook_command("hilight-remove", remove_hilight_phrase)
xchat.hook_print("Channel Message", check_msg)

You can use the plugin by either:
1. manually loading the file through the menu (Windows -> Plugins and Scripts) every time you restart xchat
2. copy the hilightphrase.py to your $HOME/.xchat2/ direcory where it will get loaded automatically when xchat is run.

Once loaded, a /hilight-list will list all of the currently active phrases to highlight. A /hilight-add <phrase> where <phrase> is a string will add the specified phrase to the list. As you can probably guess, a /hilight-remove <phrase> where <phrase> is a currently active phrase will remove it from the list.

Feel free to comment and leave suggestions.

Tuesday, May 26th, 2009 Technology

10 Comments to XChat notification and highlighting on phrases instead of single words

  • jt4703 says:

    I installed this and it works for normal conversation if I want to highlight things people would say, but I’d like it to highlight on things that include punctuation like . and []. It doesn’t work for this purpose. Is there a way to modify it so it will?

  • Derrick says:

    I’ve tested with strings with both , and . in them, and they are highlighted correctly. Do you have an example of what doesn’t seem to be working?

  • confluence says:

    This is great! Attempting to hack around the crappy built-in functionality was really frustrating.

    I notice that the nicks are not highlighted — but this doesn’t matter very much, since I still get the alerts.

    How about a version which supports arbitrary regular expressions?

  • hehe says:

    How about making this so that it:
    A) puts the highlighted phrases into a separate tab
    B) orders the “list” by numbers so that you can do /hilight-remove 1 instead of /hilight remove ‘stuff-here’
    C) add a help that tells you the command list
    D) make all the commands under /hilight instead of creating a separate command for each function
    It’s a shame that they “fixed” this :/

  • Derrick says:

    Those are good suggestions.

    a) I’ll look into this. I’m hoping it shouldn’t be too hard
    b) just changed it to print the index, and now I need to update the remove_hilight_phrase method to remove that list item
    c) done
    d) that will require a little more work, but I’ve been thinking about doing it for long time

    I’ll update with the new code once I’ve got it all worked out.

  • Derrick says:

    Done! I’ve implemented all of the changes.

    http://wiki.control-d.com/index.php?title=Hilightphrase

    I’ve been working on packaging it into a rpm and deb to make distributing it easier.

  • Rob Freeman says:

    Are you making this script available under the GPL or another open-source / creative commons license? If so, would you mind adding it as a header?

    Thanks for the great script! :)

  • Tfphyfyw says:

    Very nice.

    By the way, Rob Freeman, licenses have nothing to do with whether or not the program is open-source. One can just publish the source code, and it will be open-source regardless of license. Rather, the license has to do with the permissions or freedom you grant others, which is why the license can make it free software. The GPL is a free license.

    As a person with “Free” in their last name, you should know that.

  • Leave a Reply