source: npl/overig/irssi/hostcolor.pl

Last change on this file was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 5.6 KB
Line 
1use strict;
2use Irssi 20020101.0250 ();
3use vars qw($VERSION %IRSSI);
4$VERSION = "1";
5%IRSSI = (
6    authors     => "Timo Sirainen, Ian Peters (Modfied by Edwin Eefting aka Psy)",
7    contact     => "tss\@iki.fi (psy\@datux.nl)",
8    name        => "HostColor (based on nickcolor.pl)",
9    description => "assign a different color for each hostname",
10    license     => "Public Domain",
11    url         => "http://www.datux.nl/",
12    changed     => "2003-30-07T3:47+0100"
13);
14
15# 30-jul 2003, changed by psy@datux.nl (#leip @ EFNET)
16# -colors are now assigned on hostmask. (no need for nick tracking anymore!)
17#  So people will always have their own color, even if they join with a different nick.
18# -session colors are saved as well. (automaticly)
19# -much more colors, also using background color. (see table below)
20#
21# This should be a default feature of irssi. ;)
22# Just try it, colored nicks is the natural way for our brain to
23# follow multiple conversations at once. (especially when your
24# drunk or stoned!)
25############## #leip @ EFNET the stonerz chan
26
27
28# hm.. i should make it possible to use the existing one..
29Irssi::theme_register([
30  'pubmsg_hilight', '{pubmsghinick $0 $3 $1}$2'
31]);
32
33my %saved_colors;
34my %session_colors ;
35
36#list of cool colors that will be automatcily assigned
37my @colors = qw/18 19 20 21 22 23 24 25 26 27 28 29 30 31
38                32 33 35 36 37 39 40 41 42 43 44 45 47
39                48 49 50 53 56 57 59 61 62 63
40                80 81 82 84 88 89 90 91 93 95
41                96 97 98 100 104 105 106 107 109 111
42                112 113 114 117 120 121 123 125 127
43                160 161 162 165 166 167 168 169 171 172 174
44                240 241 242 243 245 246 247 248 250 251 252 254/;
45
46sub load_colors {
47  open COLORS, "$ENV{HOME}/.irssi/saved_colors";
48  while (<COLORS>) {
49    chomp;
50    my $line =$_;
51    my($nick, $color) = split ":", $line;
52    $saved_colors{$nick} = $color;
53  }
54  close COLORS;
55
56}
57
58sub save_colors {
59  open COLORS, ">$ENV{HOME}/.irssi/saved_colors";
60  foreach my $nick (keys %saved_colors) {
61    print COLORS "$nick:$saved_colors{$nick}\n";
62  }
63  close COLORS;
64
65}
66
67
68# This gave reasonable distribution values when run across
69# /usr/share/dict/words
70
71sub simple_hash {
72  my ($string) = @_;
73  chomp $string;
74  my @chars = split //, $string;
75  my $counter;
76
77  foreach my $char (@chars) {
78    $counter += ord $char;
79  }
80
81 # $counter = $colors[$counter % 11];
82 $counter =$counter % scalar(@colors);
83 $counter = $colors[$counter];
84 return $counter;
85}
86
87sub colorcode
88{
89    my $colornr=shift;
90    return chr(3).sprintf("%02d,%02d",$colornr%16, $colornr/16);
91
92}
93
94# FIXME: breaks /HILIGHT etc.
95sub sig_public {
96  my ($server, $msg, $nick, $address, $target) = @_;
97  my $chanrec = $server->channel_find($target);
98  return if not $chanrec;
99  my $nickrec = $chanrec->nick_find($nick);
100  return if not $nickrec;
101  my $nickmode = $nickrec->{op} ? "@" : $nickrec->{voice} ? "+" : "";
102   
103  # Has the user assigned this nick a color?
104  my $color = $saved_colors{$address};
105
106  # Have -we- already assigned this nick a color?
107  if (!$color) {
108    $color = $session_colors{$address};
109  }
110
111  # Let's assign this nick a color
112  if (!$color) {
113    $color = simple_hash $address;
114    $session_colors{$address} = $color;
115    #save automaticly
116    save_colors;
117  }
118
119  $server->command('/^format pubmsg {pubmsgnick $2 {pubnick '.colorcode($color).'$0}}$1');
120}
121
122# FIXME: breaks /HILIGHT etc.
123sub sig_publicaction {
124  my ($server, $msg, $nick, $address, $target) = @_;
125  my $chanrec = $server->channel_find($target);
126  return if not $chanrec;
127  my $nickrec = $chanrec->nick_find($nick);
128  return if not $nickrec;
129  my $nickmode = $nickrec->{op} ? "@" : $nickrec->{voice} ? "+" : "";
130   
131  # Has the user assigned this nick a color?
132  my $color = $saved_colors{$address};
133
134  # Have -we- already assigned this nick a color?
135  if (!$color) {
136    $color = $session_colors{$address};
137  }
138
139  # Let's assign this nick a color
140  if (!$color) {
141    $color = simple_hash $address;
142    $session_colors{$address} = $color;
143  }
144
145  $server->command('/^format pubmsg {pubmsgnick $2 {pubnick '.colorcode($color).'$0}}$1');
146}
147
148
149
150sub cmd_color {
151  my ($data, $server, $witem) = @_;
152  my ($op, $nick, $color) = split " ", $data;
153
154  $op = lc $op;
155
156  if (!$op) {
157    Irssi::print ("No operation given");
158  } elsif ($op eq "save") {
159    save_colors;
160  } elsif ($op eq "set") {
161    if (!$nick) {
162      Irssi::print ("Host not given");
163    } elsif (!$color) {
164      Irssi::print ("Color not given");
165    } elsif ($color < 1 || $color > 256) {
166      Irssi::print ("Color must be between 1 and 256 inclusive");
167    } else {
168      $saved_colors{$nick} = $color;
169      save_colors; #i hate manual saving :D
170    }
171  } elsif ($op eq "clear") {
172    if (!$nick) {
173      Irssi::print ("Host not given");
174    } else {
175      delete ($saved_colors{$nick});
176    }
177  } elsif ($op eq "list") {
178    Irssi::print ("\nSaved Colors:");
179    foreach my $nick (keys %saved_colors) {
180      Irssi::print (colorcode($saved_colors{$nick})."$nick" .
181                    chr (3) . "0,1 ($saved_colors{$nick})");
182    }
183    Irssi::print ("\nAutomaticly assigned colors:");
184    foreach my $nick (keys %session_colors) {
185      Irssi::print (colorcode($session_colors{$nick})."$nick" .
186                    chr (3) . "0,1 ($session_colors{$nick})");
187    }
188  } elsif ($op eq "preview") {
189    Irssi::print ("\nAvailable colors:");
190    foreach my $i (0..(16*16)) {
191      Irssi::print (colorcode($i). "Color #$i");
192    }
193  }
194}
195
196Irssi::print(chr(3)."03Chill mode activated - nicks are now colored. Psy #datux \@ FREENODE \\|/");
197
198load_colors;
199
200Irssi::command_bind('color', 'cmd_color');
201
202Irssi::signal_add('message public', 'sig_public');
203#Irssi::signal_add('ctcp action', 'sig_publicaction');
204#Irssi::signal_add('event nick', 'sig_nick');
Note: See TracBrowser for help on using the repository browser.