logo

dotfiles

My dotfiles, one branch per machine, rebased on base git clone https://hacktivis.me/git/dotfiles.git

colorize_nicks.py (16123B)


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