From 24b87b572d856967e42a9824718f85b514218a57 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Tue, 20 Jan 2015 00:28:29 +0100 Subject: weechat: colorize_nicks.py: 15 → 16 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weechat/python/colorize_nicks.py | 226 ++++++++++++++++++++------------------- 1 file changed, 118 insertions(+), 108 deletions(-) (limited to 'weechat/python') diff --git a/weechat/python/colorize_nicks.py b/weechat/python/colorize_nicks.py index 3f8f2c7..413e450 100644 --- a/weechat/python/colorize_nicks.py +++ b/weechat/python/colorize_nicks.py @@ -21,6 +21,9 @@ # # # History: +# 2014-09-17, holomorph +# version 16: use weechat config facilities +# clean unused, minor linting, some simplification # 2014-05-05, holomorph # version 15: fix python2-specific re.search check # 2013-01-29, nils_2 @@ -62,71 +65,107 @@ w = weechat SCRIPT_NAME = "colorize_nicks" SCRIPT_AUTHOR = "xt " -SCRIPT_VERSION = "15" +SCRIPT_VERSION = "16" SCRIPT_LICENSE = "GPL" SCRIPT_DESC = "Use the weechat nick colors in the chat area" -settings = { - "blacklist_channels" : '', # comma separated list of channels (use short_name) - "blacklist_nicks" : 'so,root', # comma separated list of nicks - "min_nick_length" : '2', # length - "colorize_input" : 'off', # boolean - "ignore_tags" : '', # comma separated list of tags to ignore. I.e. irc_join,irc_part,irc_quit - "greedy_matching" : 'on', # if off, then let's use lazy matching instead -} - - VALID_NICK = r'([@~&!%+])?([-a-zA-Z0-9\[\]\\`_^\{|\}]+)' valid_nick_re = re.compile(VALID_NICK) -PREFIX_COLORS = { - '@' : 'nicklist_prefix1', - '~' : 'nicklist_prefix1', - '&' : 'nicklist_prefix1', - '!' : 'nicklist_prefix1', - '%' : 'nicklist_prefix2', - '+' : 'nicklist_prefix3', -} ignore_channels = [] ignore_nicks = [] # Dict with every nick on every channel with its color as lookup value colored_nicks = {} +CONFIG_FILE_NAME = "colorize_nicks" + +# config file and options +colorize_config_file = "" +colorize_config_option = {} + +def colorize_config_init(): + ''' + Initialization of configuration file. + Sections: look. + ''' + global colorize_config_file, colorize_config_option + colorize_config_file = weechat.config_new(CONFIG_FILE_NAME, + "colorize_config_reload_cb", "") + if colorize_config_file == "": + return + + # section "look" + section_look = weechat.config_new_section( + colorize_config_file, "look", 0, 0, "", "", "", "", "", "", "", "", "", "") + if section_look == "": + weechat.config_free(colorize_config_file) + return + colorize_config_option["blacklist_channels"] = weechat.config_new_option( + colorize_config_file, section_look, "blacklist_channels", + "string", "Comma separated list of channels", "", 0, 0, + "", "", 0, "", "", "", "", "", "") + colorize_config_option["blacklist_nicks"] = weechat.config_new_option( + colorize_config_file, section_look, "blacklist_nicks", + "string", "Comma separated list of nicks", "", 0, 0, + "so,root", "so,root", 0, "", "", "", "", "", "") + colorize_config_option["min_nick_length"] = weechat.config_new_option( + colorize_config_file, section_look, "min_nick_length", + "integer", "Minimum length nick to colorize", "", + 2, 20, "", "", 0, "", "", "", "", "", "") + colorize_config_option["colorize_input"] = weechat.config_new_option( + colorize_config_file, section_look, "colorize_input", + "boolean", "Whether to colorize input", "", 0, + 0, "off", "off", 0, "", "", "", "", "", "") + colorize_config_option["ignore_tags"] = weechat.config_new_option( + colorize_config_file, section_look, "ignore_tags", + "string", "Comma separated list of tags to ignore; i.e. irc_join,irc_part,irc_quit", "", 0, 0, + "", "", 0, "", "", "", "", "", "") + colorize_config_option["greedy_matching"] = weechat.config_new_option( + colorize_config_file, section_look, "greedy_matching", + "boolean", "If off, then use lazy matching instead", "", 0, + 0, "on", "on", 0, "", "", "", "", "", "") + +def colorize_config_read(): + ''' Read configuration file. ''' + global colorize_config_file + return weechat.config_read(colorize_config_file) + +def colorize_nick_color(nick, my_nick): + ''' Retrieve nick color from weechat. ''' + if nick == my_nick: + return w.color(w.config_string(w.config_get('weechat.color.chat_nick_self'))) + else: + return w.info_get('irc_nick_color', nick) + def colorize_cb(data, modifier, modifier_data, line): ''' Callback that does the colorizing, and returns new line if changed ''' global ignore_nicks, ignore_channels, colored_nicks full_name = modifier_data.split(';')[1] - server = full_name.split('.')[0] channel = '.'.join(full_name.split('.')[1:]) buffer = w.buffer_search('', full_name) # Check if buffer has colorized nicks - if not buffer in colored_nicks: + if buffer not in colored_nicks: return line if channel in ignore_channels: return line - try: - min_length = int(w.config_get_plugin('min_nick_length')) - except ValueError: - w.prnt('', '%sError with option min_nick_length, should be a integer' % weechat.prefix('error')) - w.config_set_plugin('min_nick_length', settings['min_nick_length']) - + min_length = w.config_integer(colorize_config_option['min_nick_length']) reset = w.color('reset') # Don't colorize if the ignored tag is present in message tags_line = modifier_data.rsplit(';') if len(tags_line) >= 3: tags_line = tags_line[2].split(',') - for i in w.config_get_plugin('ignore_tags').split(','): + for i in w.config_string(colorize_config_option['ignore_tags']).split(','): if i in tags_line: return line for words in valid_nick_re.findall(line): - prefix, nick = words[0], words[1] + nick = words[1] # Check that nick is not ignored and longer than minimum length if len(nick) < min_length or nick in ignore_nicks: continue @@ -135,37 +174,36 @@ def colorize_cb(data, modifier, modifier_data, line): nick_color = colored_nicks[buffer][nick] # Let's use greedy matching. Will check against every word in a line. - if w.config_get_plugin('greedy_matching') == "on": + if w.config_boolean(colorize_config_option['greedy_matching']): for word in line.split(): - if nick in word: - # Is there a nick that contains nick and has a greater lenght? - # If so let's save that nick into var biggest_nick - biggest_nick = "" - for i in colored_nicks[buffer]: - if nick in i and nick != i and len(i) > len(nick): - if i in word: - # If a nick with greater len is found, and that word - # also happens to be in word, then let's save this nick - biggest_nick = i - # If there's a nick with greater len, then let's skip this - # As we will have the chance to colorize when biggest_nick - # iterates being nick. - if len(biggest_nick) > 0 and biggest_nick in word: - pass - elif len(word) < len(biggest_nick) or len(biggest_nick) == 0: - new_word = word.replace(nick, '%s%s%s' %(nick_color, nick, reset)) - line = line.replace(word, new_word) + if nick in word: + # Is there a nick that contains nick and has a greater lenght? + # If so let's save that nick into var biggest_nick + biggest_nick = "" + for i in colored_nicks[buffer]: + if nick in i and nick != i and len(i) > len(nick): + if i in word: + # If a nick with greater len is found, and that word + # also happens to be in word, then let's save this nick + biggest_nick = i + # If there's a nick with greater len, then let's skip this + # As we will have the chance to colorize when biggest_nick + # iterates being nick. + if len(biggest_nick) > 0 and biggest_nick in word: + pass + elif len(word) < len(biggest_nick) or len(biggest_nick) == 0: + new_word = word.replace(nick, '%s%s%s' % (nick_color, nick, reset)) + line = line.replace(word, new_word) # Let's use lazy matching for nick - elif w.config_get_plugin('greedy_matching') == "off": - if nick in colored_nicks[buffer]: - nick_color = colored_nicks[buffer][nick] - # The two .? are in case somebody writes "nick:", "nick,", etc - # to address somebody - regex = r"(\A|\s).?(%s).?(\Z|\s)" % re.escape(nick) - match = re.search(regex, line) - if match is not None: - new_line = line[:match.start(2)] + nick_color+nick+reset + line[match.end(2):] - line = new_line + else: + nick_color = colored_nicks[buffer][nick] + # The two .? are in case somebody writes "nick:", "nick,", etc + # to address somebody + regex = r"(\A|\s).?(%s).?(\Z|\s)" % re.escape(nick) + match = re.search(regex, line) + if match is not None: + new_line = line[:match.start(2)] + nick_color+nick+reset + line[match.end(2):] + line = new_line return line def colorize_input_cb(data, modifier, modifier_data, line): @@ -173,40 +211,30 @@ def colorize_input_cb(data, modifier, modifier_data, line): global ignore_nicks, ignore_channels, colored_nicks - try: - min_length = int(w.config_get_plugin('min_nick_length')) - except ValueError: - w.prnt('', '%sError with option min_nick_length, should be a integer' % weechat.prefix('error')) - w.config_set_plugin('min_nick_length', settings['min_nick_length']) + min_length = w.config_integer(colorize_config_option['min_nick_length']) - if w.config_get_plugin('colorize_input') == 'on': - pass - elif w.config_get_plugin('colorize_input') == 'off': - return line - else: - w.prnt('', '%sError with option colorize_input, should be on or off' % weechat.prefix('error')) - w.config_set_plugin('colorize_input', settings['colorize_input']) + if not w.config_boolean(colorize_config_option['colorize_input']): return line buffer = w.current_buffer() # Check if buffer has colorized nicks - if not buffer in colored_nicks: + if buffer not in colored_nicks: return line - channel = w.buffer_get_string(buffer,'name') + channel = w.buffer_get_string(buffer, 'name') if channel in ignore_channels: return line reset = w.color('reset') for words in valid_nick_re.findall(line): - prefix, nick = words[0], words[1] + nick = words[1] # Check that nick is not ignored and longer than minimum length if len(nick) < min_length or nick in ignore_nicks: continue if nick in colored_nicks[buffer]: nick_color = colored_nicks[buffer][nick] - line = line.replace(nick, '%s%s%s' %(nick_color, nick, reset)) + line = line.replace(nick, '%s%s%s' % (nick_color, nick, reset)) return line @@ -226,19 +254,13 @@ def populate_nicks(*args): while w.infolist_next(channels): pointer = w.infolist_pointer(channels, 'buffer') nicklist = w.infolist_get('nicklist', pointer, '') - channelname = w.infolist_string(channels, 'name') - if not pointer in colored_nicks: + if pointer not in colored_nicks: colored_nicks[pointer] = {} while w.infolist_next(nicklist): nick = w.infolist_string(nicklist, 'name') - if nick == my_nick: - nick_color = w.color(\ - w.config_string(\ - w.config_get('weechat.color.chat_nick_self'))) - else: - nick_color = w.info_get('irc_nick_color', nick) + nick_color = colorize_nick_color(nick, my_nick) colored_nicks[pointer][nick] = nick_color @@ -255,18 +277,11 @@ def add_nick(data, signal, type_data): global colored_nicks pointer, nick = type_data.split(',') - if not pointer in colored_nicks: + if pointer not in colored_nicks: colored_nicks[pointer] = {} - servername = w.buffer_get_string(pointer, 'localvar_server') my_nick = w.buffer_get_string(pointer, 'localvar_nick') - - if nick == my_nick: - nick_color = w.color(\ - w.config_string(\ - w.config_get('weechat.color.chat_nick_self'))) - else: - nick_color = w.info_get('irc_nick_color', nick) + nick_color = colorize_nick_color(nick, my_nick) colored_nicks[pointer][nick] = nick_color @@ -284,27 +299,22 @@ def remove_nick(data, signal, type_data): return w.WEECHAT_RC_OK def update_blacklist(*args): + ''' Set the blacklist for channels and nicks. ''' global ignore_channels, ignore_nicks - if w.config_get_plugin('blacklist_channels'): - ignore_channels = w.config_get_plugin('blacklist_channels').split(',') - ignore_nicks = w.config_get_plugin('blacklist_nicks').split(',') + ignore_channels = w.config_string(colorize_config_option['blacklist_channels']).split(',') + ignore_nicks = w.config_string(colorize_config_option['blacklist_nicks']).split(',') return w.WEECHAT_RC_OK if __name__ == "__main__": if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, - SCRIPT_DESC, "", ""): - # Set default settings -# for option, default_value in settings.iteritems(): - for option, default_value in list(settings.items()): - if not w.config_is_set_plugin(option): - w.config_set_plugin(option, default_value) - - for key, value in list(PREFIX_COLORS.items()): -# for key, value in PREFIX_COLORS.iteritems(): - PREFIX_COLORS[key] = w.color(w.config_string(w.config_get('weechat.look.%s'%value))) - - update_blacklist() # Set blacklist - populate_nicks() # Run it once to get data ready + SCRIPT_DESC, "", ""): + colorize_config_init() + colorize_config_read() + + # Run once to get data ready + update_blacklist() + populate_nicks() + w.hook_signal('nicklist_nick_added', 'add_nick', '') w.hook_signal('nicklist_nick_removed', 'remove_nick', '') w.hook_modifier('weechat_print', 'colorize_cb', '') @@ -315,4 +325,4 @@ if __name__ == "__main__": # Hook for modifying input w.hook_modifier('250|input_text_display', 'colorize_input_cb', '') # Hook for updating blacklist (this could be improved to use fnmatch) - weechat.hook_config('plugins.var.python.%s.blacklist*' %SCRIPT_NAME, 'update_blacklist', '') + weechat.hook_config('%s.look.blacklist*' % SCRIPT_NAME, 'update_blacklist', '') -- cgit v1.2.3-70-g09d2