nickcolor.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. use strict;
  2. use Irssi 20020101.0250 ();
  3. use vars qw($VERSION %IRSSI);
  4. $VERSION = "1";
  5. %IRSSI = (
  6. authors => "Timo Sirainen, Ian Peters",
  7. contact => "tss\@iki.fi",
  8. name => "Nick Color",
  9. description => "assign a different color for each nick",
  10. license => "Public Domain",
  11. url => "http://irssi.org/",
  12. changed => "2002-03-04T22:47+0100"
  13. );
  14. # hm.. i should make it possible to use the existing one..
  15. Irssi::theme_register([
  16. 'pubmsg_hilight', '{pubmsghinick $0 $3 $1}$2'
  17. ]);
  18. my %saved_colors;
  19. my %session_colors = {};
  20. my @colors = qw/2 3 4 5 6 7 9 10 11 12 13/;
  21. sub load_colors {
  22. open COLORS, "$ENV{HOME}/.irssi/saved_colors";
  23. while (<COLORS>) {
  24. # I don't know why this is necessary only inside of irssi
  25. my @lines = split "\n";
  26. foreach my $line (@lines) {
  27. my($nick, $color) = split ":", $line;
  28. $saved_colors{$nick} = $color;
  29. }
  30. }
  31. close COLORS;
  32. }
  33. sub save_colors {
  34. open COLORS, ">$ENV{HOME}/.irssi/saved_colors";
  35. foreach my $nick (keys %saved_colors) {
  36. print COLORS "$nick:$saved_colors{$nick}\n";
  37. }
  38. close COLORS;
  39. }
  40. # If someone we've colored (either through the saved colors, or the hash
  41. # function) changes their nick, we'd like to keep the same color associated
  42. # with them (but only in the session_colors, ie a temporary mapping).
  43. sub sig_nick {
  44. my ($server, $newnick, $nick, $address) = @_;
  45. my $color;
  46. $newnick = substr ($newnick, 1) if ($newnick =~ /^:/);
  47. if ($color = $saved_colors{$nick}) {
  48. $session_colors{$newnick} = $color;
  49. } elsif ($color = $session_colors{$nick}) {
  50. $session_colors{$newnick} = $color;
  51. }
  52. }
  53. # This gave reasonable distribution values when run across
  54. # /usr/share/dict/words
  55. sub simple_hash {
  56. my ($string) = @_;
  57. chomp $string;
  58. my @chars = split //, $string;
  59. my $counter;
  60. foreach my $char (@chars) {
  61. $counter += ord $char;
  62. }
  63. $counter = $colors[$counter % 11];
  64. return $counter;
  65. }
  66. # FIXME: breaks /HILIGHT etc.
  67. sub sig_public {
  68. my ($server, $msg, $nick, $address, $target) = @_;
  69. my $chanrec = $server->channel_find($target);
  70. return if not $chanrec;
  71. my $nickrec = $chanrec->nick_find($nick);
  72. return if not $nickrec;
  73. my $nickmode = $nickrec->{op} ? "@" : $nickrec->{voice} ? "+" : "";
  74. # Has the user assigned this nick a color?
  75. my $color = $saved_colors{$nick};
  76. # Have -we- already assigned this nick a color?
  77. if (!$color) {
  78. $color = $session_colors{$nick};
  79. }
  80. # Let's assign this nick a color
  81. if (!$color) {
  82. $color = simple_hash $nick;
  83. $session_colors{$nick} = $color;
  84. }
  85. $color = "0".$color if ($color < 10);
  86. $server->command('/^format pubmsg {pubmsgnick $2 {pubnick '.chr(3).$color.'$0}}$1');
  87. }
  88. sub cmd_color {
  89. my ($data, $server, $witem) = @_;
  90. my ($op, $nick, $color) = split " ", $data;
  91. $op = lc $op;
  92. if (!$op) {
  93. Irssi::print ("No operation given");
  94. } elsif ($op eq "save") {
  95. save_colors;
  96. } elsif ($op eq "set") {
  97. if (!$nick) {
  98. Irssi::print ("Nick not given");
  99. } elsif (!$color) {
  100. Irssi::print ("Color not given");
  101. } elsif ($color < 2 || $color > 14) {
  102. Irssi::print ("Color must be between 2 and 14 inclusive");
  103. } else {
  104. $saved_colors{$nick} = $color;
  105. }
  106. } elsif ($op eq "clear") {
  107. if (!$nick) {
  108. Irssi::print ("Nick not given");
  109. } else {
  110. delete ($saved_colors{$nick});
  111. }
  112. } elsif ($op eq "list") {
  113. Irssi::print ("\nSaved Colors:");
  114. foreach my $nick (keys %saved_colors) {
  115. Irssi::print (chr (3) . "$saved_colors{$nick}$nick" .
  116. chr (3) . "1 ($saved_colors{$nick})");
  117. }
  118. } elsif ($op eq "preview") {
  119. Irssi::print ("\nAvailable colors:");
  120. foreach my $i (2..14) {
  121. Irssi::print (chr (3) . "$i" . "Color #$i");
  122. }
  123. }
  124. }
  125. load_colors;
  126. Irssi::command_bind('color', 'cmd_color');
  127. Irssi::signal_add('message public', 'sig_public');
  128. Irssi::signal_add('event nick', 'sig_nick');