Advertisement
Guest User

Untitled

a guest
May 7th, 2017
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. =begin
  3. ###############################################################################
  4. #
  5. # Copyright (c) 2009 by Dominik Honnef <dominikho@gmx.net>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. ###############################################################################
  22. # History:
  23. # 2009-12-24, Dominik Honnef <dominikho@gmx.net>:
  24. # version 0.0.1
  25. #
  26. ###############################################################################
  27. =end
  28.  
  29. require 'weechat'
  30. include Weechat
  31. include Script::Skeleton
  32. include Weechat::Helper
  33.  
  34. @script = {
  35. :name => "colorize_nicks",
  36. :author => "Dominik Honnef <dominikho@gmx.net>",
  37. :license => "GPL3",
  38. :description => "Colorize nicks as they are being mentioned in messages.",
  39. :version => "0.0.1",
  40. :gem_version => "0.0.3",
  41. }
  42.  
  43. @config = Script::Config.new(
  44. 'whitelist_channels' => [Array, []],
  45. 'blacklist_channels' => [Array, []],
  46. 'blacklist_nicks' => [Array, []],
  47. 'min_nick_length' => [Integer, 1]
  48. )
  49.  
  50. # FIXME do not colorize in URLs
  51. # FIXME do not break existing colors of a message
  52.  
  53. VALID_NICK = /([a-zA-Z0-9\[\]\\`_^\{|\}]+)/
  54.  
  55. def allowed_channel?(channel)
  56. begin
  57. channel_name = channel.name
  58. if @config.whitelist_channels.empty?
  59. return !@config.blacklist_channels.include?(channel_name)
  60. else
  61. return @config.whitelist_channels.include?(channel_name)
  62. end
  63. rescue
  64. # this works around a bug in weechat which populates the channels
  65. # infolist too late
  66. return false
  67. end
  68. end
  69.  
  70. def setup
  71. Modifiers::Print.new do |plugin, buffer, tags, line|
  72. # FIXME this will ignore your own actions (/me) due to a weechat bug
  73. if buffer.channel? && tags.include?("irc_privmsg") && allowed_channel?(buffer.channel)
  74. colors = {}
  75. buffer.channel.nicks.each do |nick|
  76. colors[nick.name] = nick.color
  77. end
  78. reset = Weechat.get_color("reset")
  79.  
  80. line.message.gsub!(VALID_NICK) { |m|
  81. color = nil
  82. if !@config.blacklist_nicks.include?(m) && m.size >= @config.min_nick_length && colors[m]
  83. color = colors[m]
  84. end
  85.  
  86. if color
  87. color + m + reset
  88. else
  89. m
  90. end
  91. }
  92. end
  93.  
  94. line
  95. end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement