Advertisement
Guest User

colorize_nicks.py

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