PERL   10

nickcolor pl

Guest on 25th January 2023 01:48:52 AM


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

Raw Paste


Login or Register to edit or fork this paste. It's free.