]> git.rmz.io Git - dotfiles.git/blob - weechat/python/colorize_nicks.py
lazyvim: absorb treesitter plugins
[dotfiles.git] / weechat / python / colorize_nicks.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2010 by xt <xt@bash.no>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # This script colors nicks in IRC channels in the actual message
20 # not just in the prefix section.
21 #
22 #
23 # History:
24 # 2023-10-30: Sébastien Helleu <flashcode@flashtux.org>
25 # version 32: revert to info "nick_color" with WeeChat >= 4.1.1
26 # 2023-10-16: Sébastien Helleu <flashcode@flashtux.org>
27 # version 31: use info "irc_nick_color" on IRC buffers with WeeChat >= 4.1.0
28 # 2022-11-07: mva
29 # version 30: add ":" and "," to VALID_NICK regexp,
30 # to don't reset colorization in input_line
31 # 2022-07-11: ncfavier
32 # version 29: check nick for exclusion *after* stripping
33 # decrease minimum min_nick_length to 1
34 # 2020-11-29: jess
35 # version 28: fix ignore_tags having been broken by weechat 2.9 changes
36 # 2020-05-09: Sébastien Helleu <flashcode@flashtux.org>
37 # version 27: add compatibility with new weechat_print modifier data
38 # (WeeChat >= 2.9)
39 # 2018-04-06: Joey Pabalinas <joeypabalinas@gmail.com>
40 # version 26: fix freezes with too many nicks in one line
41 # 2018-03-18: nils_2
42 # version 25: fix unable to run function colorize_config_reload_cb()
43 # 2017-06-20: lbeziaud <louis.beziaud@ens-rennes.fr>
44 # version 24: colorize utf8 nicks
45 # 2017-03-01, arza <arza@arza.us>
46 # version 23: don't colorize nicklist group names
47 # 2016-05-01, Simmo Saan <simmo.saan@gmail.com>
48 # version 22: invalidate cached colors on hash algorithm change
49 # 2015-07-28, xt
50 # version 21: fix problems with nicks with commas in them
51 # 2015-04-19, xt
52 # version 20: fix ignore of nicks in URLs
53 # 2015-04-18, xt
54 # version 19: new option ignore nicks in URLs
55 # 2015-03-03, xt
56 # version 18: iterate buffers looking for nicklists instead of servers
57 # 2015-02-23, holomorph
58 # version 17: fix coloring in non-channel buffers (#58)
59 # 2014-09-17, holomorph
60 # version 16: use weechat config facilities
61 # clean unused, minor linting, some simplification
62 # 2014-05-05, holomorph
63 # version 15: fix python2-specific re.search check
64 # 2013-01-29, nils_2
65 # version 14: make script compatible with Python 3.x
66 # 2012-10-19, ldvx
67 # version 13: Iterate over every word to prevent incorrect colorization of
68 # nicks. Added option greedy_matching.
69 # 2012-04-28, ldvx
70 # version 12: added ignore_tags to avoid colorizing nicks if tags are present
71 # 2012-01-14, nesthib
72 # version 11: input_text_display hook and modifier to colorize nicks in input bar
73 # 2010-12-22, xt
74 # version 10: hook config option for updating blacklist
75 # 2010-12-20, xt
76 # version 0.9: hook new config option for weechat 0.3.4
77 # 2010-11-01, nils_2
78 # version 0.8: hook_modifier() added to communicate with rainbow_text
79 # 2010-10-01, xt
80 # version 0.7: changes to support non-irc-plugins
81 # 2010-07-29, xt
82 # version 0.6: compile regexp as per patch from Chris quigybo@hotmail.com
83 # 2010-07-19, xt
84 # version 0.5: fix bug with incorrect coloring of own nick
85 # 2010-06-02, xt
86 # version 0.4: update to reflect API changes
87 # 2010-03-26, xt
88 # version 0.3: fix error with exception
89 # 2010-03-24, xt
90 # version 0.2: use ignore_channels when populating to increase performance.
91 # 2010-02-03, xt
92 # version 0.1: initial (based on ruby script by dominikh)
93 #
94 # Known issues: nicks will not get colorized if they begin with a character
95 # such as ~ (which some irc networks do happen to accept)
96
97 import weechat
98 import re
99 w = weechat
100
101 SCRIPT_NAME = "colorize_nicks"
102 SCRIPT_AUTHOR = "xt <xt@bash.no>"
103 SCRIPT_VERSION = "32"
104 SCRIPT_LICENSE = "GPL"
105 SCRIPT_DESC = "Use the weechat nick colors in the chat area"
106
107 # Based on the recommendations in RFC 7613. A valid nick is composed
108 # of anything but " ,*?.!@".
109 VALID_NICK = r'([@~&!%+-])?([^\s,\*?\.!@:,]+)'
110 valid_nick_re = re.compile(VALID_NICK)
111 ignore_channels = []
112 ignore_nicks = []
113
114 # Dict with every nick on every channel with its color as lookup value
115 colored_nicks = {}
116
117 CONFIG_FILE_NAME = "colorize_nicks"
118
119 # config file and options
120 colorize_config_file = ""
121 colorize_config_option = {}
122
123 def colorize_config_init():
124 '''
125 Initialization of configuration file.
126 Sections: look.
127 '''
128 global colorize_config_file, colorize_config_option
129 colorize_config_file = weechat.config_new(CONFIG_FILE_NAME,
130 "", "")
131 if colorize_config_file == "":
132 return
133
134 # section "look"
135 section_look = weechat.config_new_section(
136 colorize_config_file, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
137 if section_look == "":
138 weechat.config_free(colorize_config_file)
139 return
140 colorize_config_option["blacklist_channels"] = weechat.config_new_option(
141 colorize_config_file, section_look, "blacklist_channels",
142 "string", "Comma separated list of channels", "", 0, 0,
143 "", "", 0, "", "", "", "", "", "")
144 colorize_config_option["blacklist_nicks"] = weechat.config_new_option(
145 colorize_config_file, section_look, "blacklist_nicks",
146 "string", "Comma separated list of nicks", "", 0, 0,
147 "so,root", "so,root", 0, "", "", "", "", "", "")
148 colorize_config_option["min_nick_length"] = weechat.config_new_option(
149 colorize_config_file, section_look, "min_nick_length",
150 "integer", "Minimum length nick to colorize", "",
151 1, 20, "2", "2", 0, "", "", "", "", "", "")
152 colorize_config_option["colorize_input"] = weechat.config_new_option(
153 colorize_config_file, section_look, "colorize_input",
154 "boolean", "Whether to colorize input", "", 0,
155 0, "off", "off", 0, "", "", "", "", "", "")
156 colorize_config_option["ignore_tags"] = weechat.config_new_option(
157 colorize_config_file, section_look, "ignore_tags",
158 "string", "Comma separated list of tags to ignore; i.e. irc_join,irc_part,irc_quit", "", 0, 0,
159 "", "", 0, "", "", "", "", "", "")
160 colorize_config_option["greedy_matching"] = weechat.config_new_option(
161 colorize_config_file, section_look, "greedy_matching",
162 "boolean", "If off, then use lazy matching instead", "", 0,
163 0, "on", "on", 0, "", "", "", "", "", "")
164 colorize_config_option["match_limit"] = weechat.config_new_option(
165 colorize_config_file, section_look, "match_limit",
166 "integer", "Fall back to lazy matching if greedy matches exceeds this number", "",
167 20, 1000, "", "", 0, "", "", "", "", "", "")
168 colorize_config_option["ignore_nicks_in_urls"] = weechat.config_new_option(
169 colorize_config_file, section_look, "ignore_nicks_in_urls",
170 "boolean", "If on, don't colorize nicks inside URLs", "", 0,
171 0, "off", "off", 0, "", "", "", "", "", "")
172
173 def colorize_config_read():
174 ''' Read configuration file. '''
175 global colorize_config_file
176 return weechat.config_read(colorize_config_file)
177
178 def colorize_nick_color(buffer, nick, my_nick):
179 ''' Retrieve nick color from weechat. '''
180 if nick == my_nick:
181 return w.color(w.config_string(w.config_get('weechat.color.chat_nick_self')))
182 else:
183 version = int(w.info_get('version_number', '') or 0)
184 if w.buffer_get_string(buffer, 'plugin') == 'irc' and version == 0x4010000:
185 server = w.buffer_get_string(buffer, 'localvar_server')
186 return w.info_get('irc_nick_color', '%s,%s' % (server, nick))
187 return w.info_get('nick_color', nick)
188
189 def colorize_cb(data, modifier, modifier_data, line):
190 ''' Callback that does the colorizing, and returns new line if changed '''
191
192 global ignore_nicks, ignore_channels, colored_nicks
193
194 if modifier_data.startswith('0x'):
195 # WeeChat >= 2.9
196 buffer, tags = modifier_data.split(';', 1)
197 else:
198 # WeeChat <= 2.8
199 plugin, buffer_name, tags = modifier_data.split(';', 2)
200 buffer = w.buffer_search(plugin, buffer_name)
201
202 channel = w.buffer_get_string(buffer, 'localvar_channel')
203 tags = tags.split(',')
204
205 # Check if buffer has colorized nicks
206 if buffer not in colored_nicks:
207 return line
208
209 if channel and channel in ignore_channels:
210 return line
211
212 min_length = w.config_integer(colorize_config_option['min_nick_length'])
213 reset = w.color('reset')
214
215 # Don't colorize if the ignored tag is present in message
216 tag_ignores = w.config_string(colorize_config_option['ignore_tags']).split(',')
217 for tag in tags:
218 if tag in tag_ignores:
219 return line
220
221 for words in valid_nick_re.findall(line):
222 nick = words[1]
223
224 # If the matched word is not a known nick, we try to match the
225 # word without its first or last character (if not a letter).
226 # This is necessary as "foo:" is a valid nick, which could be
227 # adressed as "foo::".
228 if nick not in colored_nicks[buffer]:
229 if not nick[-1].isalpha() and not nick[0].isalpha():
230 if nick[1:-1] in colored_nicks[buffer]:
231 nick = nick[1:-1]
232 elif not nick[0].isalpha():
233 if nick[1:] in colored_nicks[buffer]:
234 nick = nick[1:]
235 elif not nick[-1].isalpha():
236 if nick[:-1] in colored_nicks[buffer]:
237 nick = nick[:-1]
238
239 # Check that nick is not ignored and longer than minimum length
240 if len(nick) < min_length or nick in ignore_nicks:
241 continue
242
243 # Check that nick is in the dictionary colored_nicks
244 if nick in colored_nicks[buffer]:
245 nick_color = colored_nicks[buffer][nick]
246
247 try:
248 # Let's use greedy matching. Will check against every word in a line.
249 if w.config_boolean(colorize_config_option['greedy_matching']):
250 cnt = 0
251 limit = w.config_integer(colorize_config_option['match_limit'])
252
253 for word in line.split():
254 cnt += 1
255 assert cnt < limit
256 # if cnt > limit:
257 # raise RuntimeError('Exceeded colorize_nicks.look.match_limit.');
258
259 if w.config_boolean(colorize_config_option['ignore_nicks_in_urls']) and \
260 word.startswith(('http://', 'https://')):
261 continue
262
263 if nick in word:
264 # Is there a nick that contains nick and has a greater lenght?
265 # If so let's save that nick into var biggest_nick
266 biggest_nick = ""
267 for i in colored_nicks[buffer]:
268 cnt += 1
269 assert cnt < limit
270
271 if nick in i and nick != i and len(i) > len(nick):
272 if i in word:
273 # If a nick with greater len is found, and that word
274 # also happens to be in word, then let's save this nick
275 biggest_nick = i
276 # If there's a nick with greater len, then let's skip this
277 # As we will have the chance to colorize when biggest_nick
278 # iterates being nick.
279 if len(biggest_nick) > 0 and biggest_nick in word:
280 pass
281 elif len(word) < len(biggest_nick) or len(biggest_nick) == 0:
282 new_word = word.replace(nick, '%s%s%s' % (nick_color, nick, reset))
283 line = line.replace(word, new_word)
284
285 # Switch to lazy matching
286 else:
287 raise AssertionError
288
289 except AssertionError:
290 # Let's use lazy matching for nick
291 nick_color = colored_nicks[buffer][nick]
292 # The two .? are in case somebody writes "nick:", "nick,", etc
293 # to address somebody
294 regex = r"(\A|\s).?(%s).?(\Z|\s)" % re.escape(nick)
295 match = re.search(regex, line)
296 if match is not None:
297 new_line = line[:match.start(2)] + nick_color+nick+reset + line[match.end(2):]
298 line = new_line
299
300 return line
301
302 def colorize_input_cb(data, modifier, modifier_data, line):
303 ''' Callback that does the colorizing in input '''
304
305 global ignore_nicks, ignore_channels, colored_nicks
306
307 min_length = w.config_integer(colorize_config_option['min_nick_length'])
308
309 if not w.config_boolean(colorize_config_option['colorize_input']):
310 return line
311
312 buffer = w.current_buffer()
313 # Check if buffer has colorized nicks
314 if buffer not in colored_nicks:
315 return line
316
317 channel = w.buffer_get_string(buffer, 'name')
318 if channel and channel in ignore_channels:
319 return line
320
321 reset = w.color('reset')
322
323 for words in valid_nick_re.findall(line):
324 nick = words[1]
325 # Check that nick is not ignored and longer than minimum length
326 if len(nick) < min_length or nick in ignore_nicks:
327 continue
328 if nick in colored_nicks[buffer]:
329 nick_color = colored_nicks[buffer][nick]
330 line = line.replace(nick, '%s%s%s' % (nick_color, nick, reset))
331
332 return line
333
334 def populate_nicks(*args):
335 ''' Fills entire dict with all nicks weechat can see and what color it has
336 assigned to it. '''
337 global colored_nicks
338
339 colored_nicks = {}
340
341 buffers = w.infolist_get('buffer', '', '')
342 while w.infolist_next(buffers):
343 buffer_ptr = w.infolist_pointer(buffers, 'pointer')
344 my_nick = w.buffer_get_string(buffer_ptr, 'localvar_nick')
345 nicklist = w.infolist_get('nicklist', buffer_ptr, '')
346 while w.infolist_next(nicklist):
347 if buffer_ptr not in colored_nicks:
348 colored_nicks[buffer_ptr] = {}
349
350 if w.infolist_string(nicklist, 'type') != 'nick':
351 continue
352
353 nick = w.infolist_string(nicklist, 'name')
354 nick_color = colorize_nick_color(buffer_ptr, nick, my_nick)
355
356 colored_nicks[buffer_ptr][nick] = nick_color
357
358 w.infolist_free(nicklist)
359
360 w.infolist_free(buffers)
361
362 return w.WEECHAT_RC_OK
363
364 def add_nick(data, signal, type_data):
365 ''' Add nick to dict of colored nicks '''
366 global colored_nicks
367
368 # Nicks can have , in them in some protocols
369 splitted = type_data.split(',')
370 pointer = splitted[0]
371 nick = ",".join(splitted[1:])
372 if pointer not in colored_nicks:
373 colored_nicks[pointer] = {}
374
375 my_nick = w.buffer_get_string(pointer, 'localvar_nick')
376 nick_color = colorize_nick_color(pointer, nick, my_nick)
377
378 colored_nicks[pointer][nick] = nick_color
379
380 return w.WEECHAT_RC_OK
381
382 def remove_nick(data, signal, type_data):
383 ''' Remove nick from dict with colored nicks '''
384 global colored_nicks
385
386 # Nicks can have , in them in some protocols
387 splitted = type_data.split(',')
388 pointer = splitted[0]
389 nick = ",".join(splitted[1:])
390
391 if pointer in colored_nicks and nick in colored_nicks[pointer]:
392 del colored_nicks[pointer][nick]
393
394 return w.WEECHAT_RC_OK
395
396 def update_blacklist(*args):
397 ''' Set the blacklist for channels and nicks. '''
398 global ignore_channels, ignore_nicks
399 ignore_channels = w.config_string(colorize_config_option['blacklist_channels']).split(',')
400 ignore_nicks = w.config_string(colorize_config_option['blacklist_nicks']).split(',')
401 return w.WEECHAT_RC_OK
402
403 if __name__ == "__main__":
404 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
405 SCRIPT_DESC, "", ""):
406 colorize_config_init()
407 colorize_config_read()
408
409 # Run once to get data ready
410 update_blacklist()
411 populate_nicks()
412
413 w.hook_signal('nicklist_nick_added', 'add_nick', '')
414 w.hook_signal('nicklist_nick_removed', 'remove_nick', '')
415 w.hook_modifier('weechat_print', 'colorize_cb', '')
416 # Hook config for changing colors
417 w.hook_config('weechat.color.chat_nick_colors', 'populate_nicks', '')
418 w.hook_config('weechat.look.nick_color_hash', 'populate_nicks', '')
419 # Hook for working togheter with other scripts (like colorize_lines)
420 w.hook_modifier('colorize_nicks', 'colorize_cb', '')
421 # Hook for modifying input
422 w.hook_modifier('250|input_text_display', 'colorize_input_cb', '')
423 # Hook for updating blacklist (this could be improved to use fnmatch)
424 weechat.hook_config('%s.look.blacklist*' % SCRIPT_NAME, 'update_blacklist', '')