archive- Random tools & helpful resources for IRC |
git clone git://git.acid.vegas/archive.git |
Log | Files | Refs | Archive |
nickcolor.pl (3753B)
1 use strict; 2 use Irssi 20020101.0250 (); 3 use vars qw($VERSION %IRSSI); 4 $VERSION = "2"; 5 %IRSSI = ( 6 authors => "Timo Sirainen, Ian Peters, David Leadbeater", 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 => "Sun 15 Jun 19:10:44 BST 2014", 13 ); 14 15 # Settings: 16 # nickcolor_colors: List of color codes to use. 17 # e.g. /set nickcolor_colors 2 3 4 5 6 7 9 10 11 12 13 18 # (avoid 8, as used for hilights in the default theme). 19 20 my %saved_colors; 21 my %session_colors = {}; 22 23 sub load_colors { 24 open my $color_fh, "<", "$ENV{HOME}/.irssi/saved_colors"; 25 while (<$color_fh>) { 26 chomp; 27 my($nick, $color) = split ":"; 28 $saved_colors{$nick} = $color; 29 } 30 } 31 32 sub save_colors { 33 open COLORS, ">", "$ENV{HOME}/.irssi/saved_colors"; 34 35 foreach my $nick (keys %saved_colors) { 36 print COLORS "$nick:$saved_colors{$nick}\n"; 37 } 38 39 close COLORS; 40 } 41 42 # If someone we've colored (either through the saved colors, or the hash 43 # function) changes their nick, we'd like to keep the same color associated 44 # with them (but only in the session_colors, ie a temporary mapping). 45 46 sub sig_nick { 47 my ($server, $newnick, $nick, $address) = @_; 48 my $color; 49 50 $newnick = substr ($newnick, 1) if ($newnick =~ /^:/); 51 52 if ($color = $saved_colors{$nick}) { 53 $session_colors{$newnick} = $color; 54 } elsif ($color = $session_colors{$nick}) { 55 $session_colors{$newnick} = $color; 56 } 57 } 58 59 # This gave reasonable distribution values when run across 60 # /usr/share/dict/words 61 62 sub simple_hash { 63 my ($string) = @_; 64 chomp $string; 65 my @chars = split //, $string; 66 my $counter; 67 68 foreach my $char (@chars) { 69 $counter += ord $char; 70 } 71 72 my @colors = split / /, Irssi::settings_get_str('nickcolor_colors'); 73 $counter = $colors[$counter % @colors]; 74 75 return $counter; 76 } 77 78 sub sig_public { 79 my ($server, $msg, $nick, $address, $target) = @_; 80 81 # Has the user assigned this nick a color? 82 my $color = $saved_colors{$nick}; 83 84 # Have -we- already assigned this nick a color? 85 if (!$color) { 86 $color = $session_colors{$nick}; 87 } 88 89 # Let's assign this nick a color 90 if (!$color) { 91 $color = simple_hash $nick; 92 $session_colors{$nick} = $color; 93 } 94 95 $color = sprintf "\003%02d", $color; 96 $server->command('/^format pubmsg {pubmsgnick $2 {pubnick ' . $color . '$0}}$1'); 97 } 98 99 sub cmd_color { 100 my ($data, $server, $witem) = @_; 101 my ($op, $nick, $color) = split " ", $data; 102 103 $op = lc $op; 104 105 if (!$op) { 106 Irssi::print ("No operation given (save/set/clear/list/preview)"); 107 } elsif ($op eq "save") { 108 save_colors; 109 } elsif ($op eq "set") { 110 if (!$nick) { 111 Irssi::print ("Nick not given"); 112 } elsif (!$color) { 113 Irssi::print ("Color not given"); 114 } elsif ($color < 2 || $color > 14) { 115 Irssi::print ("Color must be between 2 and 14 inclusive"); 116 } else { 117 $saved_colors{$nick} = $color; 118 } 119 } elsif ($op eq "clear") { 120 if (!$nick) { 121 Irssi::print ("Nick not given"); 122 } else { 123 delete ($saved_colors{$nick}); 124 } 125 } elsif ($op eq "list") { 126 Irssi::print ("\nSaved Colors:"); 127 foreach my $nick (keys %saved_colors) { 128 Irssi::print (chr (3) . "$saved_colors{$nick}$nick" . 129 chr (3) . "1 ($saved_colors{$nick})"); 130 } 131 } elsif ($op eq "preview") { 132 Irssi::print ("\nAvailable colors:"); 133 foreach my $i (2..14) { 134 Irssi::print (chr (3) . "$i" . "Color #$i"); 135 } 136 } 137 } 138 139 load_colors; 140 141 Irssi::settings_add_str('misc', 'nickcolor_colors', '2 3 4 5 6 7 9 10 11 12 13'); 142 Irssi::command_bind('color', 'cmd_color'); 143 144 Irssi::signal_add('message public', 'sig_public'); 145 Irssi::signal_add('event nick', 'sig_nick');