]> git.sthu.org Git - shutils.git/blob - dotfiles/irssi/scripts/xchatnickcolor.pl
vim: add color scheme
[shutils.git] / dotfiles / irssi / scripts / xchatnickcolor.pl
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
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 14 15/;
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 %b<%w$2'.chr(3).$color.'$[-11]0%b> %K|%n $1');
109 # $server->command('/^format action_public {pubaction '.chr(3).$color.'$0}$1');
110 }
111
112 sub cmd_color {
113 my ($data, $server, $witem) = @_;
114 my ($op, $nick, $color) = split " ", $data;
115
116 $op = lc $op;
117
118 if (!$op) {
119 Irssi::print ("No operation given");
120 } elsif ($op eq "save") {
121 save_colors;
122 } elsif ($op eq "set") {
123 if (!$nick) {
124 Irssi::print ("Nick not given");
125 } elsif (!$color) {
126 Irssi::print ("Color not given");
127 } elsif ($color < 2 || $color > 14) {
128 Irssi::print ("Color must be between 2 and 14 inclusive");
129 } else {
130 $saved_colors{$nick} = $color;
131 }
132 } elsif ($op eq "clear") {
133 if (!$nick) {
134 Irssi::print ("Nick not given");
135 } else {
136 delete ($saved_colors{$nick});
137 }
138 } elsif ($op eq "list") {
139 Irssi::print ("\nSaved Colors:");
140 foreach my $nick (keys %saved_colors) {
141 Irssi::print (chr (3) . "$saved_colors{$nick}$nick" .
142 chr (3) . "1 ($saved_colors{$nick})");
143 }
144 } elsif ($op eq "preview") {
145 Irssi::print ("\nAvailable colors:");
146 foreach my $i (2..14) {
147 Irssi::print (chr (3) . "$i" . "Color #$i");
148 }
149 }
150 }
151
152 load_colors;
153
154 Irssi::command_bind('color', 'cmd_color');
155
156 Irssi::signal_add('message public', 'sig_public');
157 Irssi::signal_add('event nick', 'sig_nick');