-
Notifications
You must be signed in to change notification settings - Fork 1
/
nickcolor.pl
505 lines (407 loc) · 13.7 KB
/
nickcolor.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# A hacked version of the standard nickcolor.pl.
#
# The "vanilla" nickcolor would hash each nick to choose a color,
# essentially randomly coloring the names. This version tries to be a
# bit smarter: it uses a few criteria to try to decide on a
# color. When someone first starts talking, it will try to choose a
# color that's not in use by anyone else actively talking; if that
# fails, it will try to collide with a name that's not easily-confused
# with the one to which we're assigning a color. We also allow a name
# to be re-colored, but only if that person hasn't talked for a long
# time.
#
# In addition, you can issue the command
# /recolor <nick>
# to change the color of nick to whatever the script feels is
# best. Use this if a lot of people actively talking have the same
# color. You can also say
# /recolor -all
# to entirely reset the colors.
#
# The script also supports colouring nicks within text. The
# "colorlevel" variable controls this. The settings are:
#
# 0: no colouring.
# 1: just colour names of people talking (the usual behaviour)
# 2: also colour names of people being talked to (nick: text)
# 3: colour all nicknames that occur within all messages.
# (A bug in irssi's built-in highlighter makes this interact poorly with
# hilight -word. To fix this, apply this patch:
# http://www.bswolf.com/irssi/nickcolor-hilight.patch
# to src/fe-common/core/formats.c and recompile irssi.)
#
# The "nickcolor_global" variable enables colouring nicknames the same
# across all channels. It defaults to OFF (meaning the same user may have
# different colours in separate channels).
#
# -mrwright
# TODO:
#
# - Have a table of which colors are most similar to others (with
# separate tables for various kinds of colorblindness), and take
# that into account when assigning colors.
# - Also rank colors by how appealing they are in general, and give
# those priority.
# - Clean up the code.
use strict;
use Irssi 20020101.0250 ();
use vars qw($VERSION %IRSSI);
$VERSION = "1";
%IRSSI = (
authors => "Timo Sirainen, Ian Peters, Matthew Wright, bswolf",
contact => "tss\@iki.fi",
name => "Nick Color",
description => "assign a different color for each nick",
license => "Public Domain",
url => "http://irssi.org/",
changed => "2013-11-27"
);
# hm.. i should make it possible to use the existing one..
Irssi::theme_register([
'pubmsg_hilight', '{pubmsghinick $0 $3 $1}$2'
]);
my %saved_colors;
my %session_colors = {};
my @colors = qw/2 3 4 5 6 7 9 10 11 12 13/;
my %last_talked = {}; # Store the time when each person last talked.
my %recolors = {}; # Nicks whose color we want changed.
my %recent_nicks_chan; # Recent nicks, per-channel.
my $debuglev = 0; # no debugging by default.
# Level 1 = basic messages
# Level 2 = print scoring every time someone talks
# Level 3 = all that and also print time penalty, and last talked list.
my $debug_to_file=1;
sub load_colors {
# Different filename, since we use a different system.
open COLORS, "$ENV{HOME}/.irssi/saved_colors_new";
while (<COLORS>) {
# I don't know why this is necessary only inside of irssi
my @lines = split "\n";
foreach my $line (@lines) {
my($nick, $color) = split ":", $line;
$saved_colors{$nick} = $color;
}
}
close COLORS;
}
sub save_colors {
# Different filename, since we use a different system.
open COLORS, ">$ENV{HOME}/.irssi/saved_colors_new";
foreach my $nick (keys %saved_colors) {
print COLORS "$nick:$saved_colors{$nick}\n";
}
close COLORS;
}
sub debug {
my $msg = shift;
if ($debug_to_file) {
open LOGFILE, ">>$ENV{HOME}/.irssi/nickcolor-new.log";
print LOGFILE $msg . "\n";
close LOGFILE;
} else {
Irssi::print $msg;
}
}
# If someone we've colored (either through the saved colors, or the hash
# function) changes their nick, we'd like to keep the same color associated
# with them (but only in the session_colors, ie a temporary mapping).
sub sig_nick {
my ($server, $newnick, $nick, $address) = @_;
my $color;
$newnick = substr ($newnick, 1) if ($newnick =~ /^:/);
# Update the table of who talked recently.
foreach my $channel (keys %recent_nicks_chan) {
if ($color = $saved_colors{$nick}) {
$session_colors{$channel}{$newnick} = $color;
} elsif ($color = $session_colors{$channel}{$nick}) {
$session_colors{$channel}{$newnick} = $color;
}
foreach my $name (@{$recent_nicks_chan{$channel}}) {
if ($name eq $nick) {
$name=$newnick;
}
}
# Irssi::print("$channel: @{$recent_nicks_chan{$channel}}");
}
}
sub best_color {
my ($server, $msg, $nick, $address, $target) = @_;
my $channel = $target;
my $chanrec = $server->channel_find($target);
return if not $chanrec;
my $nickrec = $chanrec->nick_find($nick);
return if not $nickrec;
my %color_scores;
if (Irssi::settings_get_bool('nickcolor_global') == 1) {
# Yes, it's a horrible hack :)
$channel = 'GLOBAL';
}
if (!$session_colors{$channel}) {
$session_colors{$channel}={};
debug("Created session colors.") if $debuglev > 0;
}
push(@{$recent_nicks_chan{$channel}},$nick);
shift @{$recent_nicks_chan{$channel}}
if (scalar(@{$recent_nicks_chan{$channel}})>=40);
debug("@{$recent_nicks_chan{$channel}}") if $debuglev > 2;
#Irssi::print("@recent_nicks");
####################################################
# Begin assigning scores.
####################################################
# Some of the scoring we do takes a bit longer, and we might
# not want to do it every time. If this flag is unset, it means
# that the extra work is unlikely to be worthwhile, probably because
# some score has been set extremely high.
my $worth_harder_tests=1;
## If the user has set a color specifically, use it.
# Has the user assigned this nick a color?
my $color = $saved_colors{$nick};
# If so, set it to an extremely high priority.
$color_scores{$color} = 1000;
$worth_harder_tests=0 if($color);
my $recolored=0;
# Did they ask that this nick be recolored?
if ($recolors{$channel}{$nick}) {
#Irssi::print("Recoloring $nick");
$color_scores{$session_colors{$channel}{$nick}}=-10000;
delete $recolors{$channel}{$nick};
$recolored=1;
}
## Favour the user's existing color.
my $lasttalked=$last_talked{$channel}{$nick};
my $curtime = time();
$last_talked{$channel}{$nick} = $curtime;
# Hack to avoid div by 0
$curtime = $lasttalked + 1 if($curtime <= $lasttalked);
# A somewhat continuous fallof. This will assign a score of
# 18 for 20 minutes, and 3 for two hours, for example.
# Never let it go below 5.
my $time_penalty=int(3*60*120 / ($curtime - $lasttalked));
debug($nick . ":" . $time_penalty . ":" . ($lasttalked - $curtime)) if $debuglev > 2;
$time_penalty=5 if($time_penalty<5);
# At this point it's already absurdly high; no need to let it go
# higher or do more work later. (This corresponds to ~21 seconds.)
if ($time_penalty>1000) {
$time_penalty=1000;
$worth_harder_tests=0 if $recolored==0;
}
$color_scores{$session_colors{$channel}{$nick}} =
$color_scores{$session_colors{$channel}{$nick}} + $time_penalty;
# Penalize colors already assigned to recently-used nicks of a similar
# length.
#
# Specifically, run through the last several (currently 40;
# TODO: make that an option) nicks, and for each one, calculate a
# penalty for its color based on its similarity to the current name.
# Two things are taken into account so far: the length, and the first
# letter.
# We intentionally include duplicates: if someone's talking a lot, we
# want to try harder to be a different color.
if ($worth_harder_tests) {
# Modest penalty for colours already in use. This will ensure that
# if there are fewer people than colours, we'll never reuse one.
my %used_colors;
foreach my $inick (keys %{$session_colors{$channel}}) {
$used_colors{$session_colors{$channel}{$inick}} = -2;
}
foreach my $color (keys %used_colors) {
$color_scores{$color} = $color_scores{$color} + $used_colors{$color};
}
# Now penalize nicks of people who have spoken recently.
foreach my $inick (@{$recent_nicks_chan{$channel}}) {
if ($inick ne $nick) {
my $penalty = 0;
my $lendiff = abs(length($nick) - length($inick));
if ($lendiff == 0) {
$penalty=-8;
} elsif ($lendiff == 1) {
$penalty=-4;
} else {
$penalty=-1; # Even if lengths are wildly different, we want to
# penalize the color a bit.
}
# Also penalize nicks that start with the same letter.
if (substr($nick,0,1) eq substr($inick,0,1)) {
$penalty = $penalty - 4;
}
$color_scores{$session_colors{$channel}{$inick}} =
$color_scores{$session_colors{$channel}{$inick}} + $penalty;
}
}
}
# For debugging.
my $cstring='';
##### Now choose the best color from the list.
my $best_color=-1;
my $max_val=-100;
for (my $i=0; $i<11; $i++) {
$cstring=$cstring . "$i:$color_scores{$i}; ";
if ($color_scores{$i} > $max_val) {
$max_val=$color_scores{$i};
$best_color=$i;
}
}
$cstring = $cstring . "| BEST: $best_color.";
$color = $best_color;
$session_colors{$channel}{$nick} = $color;
debug($channel . " <$nick> $msg " . $cstring) if $debuglev > 1;
$color = $colors[$color % 11];
$color = "0".$color if ($color < 10);
return $color;
}
sub sig_public {
my ($server, $msg, $nick, $address, $target) = @_;
my $level=Irssi::settings_get_int('colorlevel');
return if($level<1);
my $mynick=$server->{nick};
if($level==2 && $msg=~/^[a-zA-Z0-9_|^\[\]]*[:,] /)
{
$msg=~/^([a-zA-Z0-9_|^\[\]]*)([:,] .*)/;
my $inick=$1;
my $rest=$2;
if($mynick ne $inick)
{
if(grep {$_->{nick} eq $inick} $server->channel_find($target)->nicks())
{
my $color=best_color($server, $msg, $inick, $address, $target);
Irssi::signal_emit('message public',
($server,
"".chr(3) .$color.$inick.chr(15).$rest, $nick, $address, $target));
Irssi::signal_stop();
}
}
}
my $ch="".chr(3)."|".chr(4);
if($level==3 && !($msg=~/^($ch)/))
{
my $chan=$server->channel_find($target);
if(!defined($chan))
{
return;
}
foreach ($chan->nicks())
{
my $inick=$_->{nick};
next if($mynick eq $inick);
if(index($msg,$inick) != -1)
{
my $color=best_color($server, $msg, $inick, $address, $target);
my $tar=chr(3).$color.$inick.chr(15);
$msg=~s/(^|\b|\W)(\Q$inick\E)($|\b|\W)/$1.$tar.$3/ge;
}
}
$msg="".chr(4)."g".$msg;
Irssi::signal_emit('message public',
($server,
$msg, $nick, $address, $target));
Irssi::signal_stop();
}
my $color = best_color($server, $msg, $nick, $address, $target);
$server->command('/^format pubmsg {pubmsgnick $2 {pubnick '.chr(3).$color.'$0}}$1');
}
sub sig_act_public {
my ($server, $msg, $nick, $address, $target) = @_;
my $level=Irssi::settings_get_int('colorlevel');
return if($level<1);
my $mynick=$server->{nick};
my $ch="".chr(3)."|".chr(4);
if($level==3 && !($msg=~/^($ch)/))
{
my $chan=$server->channel_find($target);
if(!defined($chan))
{
return;
}
foreach ($chan->nicks())
{
my $inick=$_->{nick};
next if($mynick eq $inick);
if(index($msg,$inick) != -1)
{
my $color=best_color($server, $msg, $inick, $address, $target);
my $tar=chr(3).$color.$inick.chr(15);
$msg=~s/(^|\b|\W)(\Q$inick\E)($|\b|\W)/$1.$tar.$3/ge;
}
}
$msg="".chr(4)."g".$msg;
Irssi::signal_emit('message irc action',
($server,
$msg, $nick, $address, $target));
Irssi::signal_stop();
return;
}
my $color = best_color($server, $msg, $nick, $address, $target);
$server->command('/^format action_public '.chr(2).'*'.chr(2).' '.chr(3).$color.'$0'.chr(15).' {pubnick $1}');
}
sub cmd_color {
my ($data, $server, $witem) = @_;
my ($op, $nick, $color) = split " ", $data;
$op = lc $op;
if (!$op) {
Irssi::print ("No operation given");
} elsif ($op eq "save") {
save_colors;
} elsif ($op eq "debug") {
$debuglev=$nick;
Irssi::print("Debuglevel set to $debuglev.");
} elsif ($op eq "set") {
if (!$nick) {
Irssi::print ("Nick not given");
} elsif (!$color) {
Irssi::print ("Color not given");
} elsif ($color < 0 || $color > 10) {
Irssi::print ("Color must be between 0 and 10 inclusive");
} else {
$saved_colors{$nick} = $color;
}
} elsif ($op eq "clear") {
if (!$nick) {
Irssi::print ("Nick not given");
} else {
delete ($saved_colors{$nick});
}
} elsif ($op eq "list") {
Irssi::print ("\nSaved Colors:");
foreach my $nick (keys %saved_colors) {
Irssi::print (chr (3) . "$colors[$saved_colors{$nick}]$nick" .
chr (3) . "0 ($saved_colors{$nick})");
}
} elsif ($op eq "preview") {
Irssi::print ("\nAvailable colors:");
foreach my $i (0..10) {
Irssi::print (chr (3) . "$colors[$i]" . "Color #$i");
}
}
}
sub cmd_recolor {
my ($nick, $server, $witem) = @_;
if (!$witem) {
Irssi::print "Not in a channel!";
return;
}
my %chan=%$witem;
my $chan_name=$chan{"name"};
if (!$recolors{$chan_name}) {
$recolors{$chan_name}={};
#Irssi::print("Created recolors.");
}
$nick =~ s/ //g;
#Irssi::print("'$nick' $last_talked{$nick}");
if ($last_talked{$chan_name}{$nick}) {
Irssi::print("Recoloring $nick in $chan_name...");
$recolors{$chan_name}{$nick}=1;
} elsif ($nick eq '-all') {
Irssi::print("Resetting all colors.");
$session_colors{$chan_name}={};
} else {
Irssi::print("$nick isn't assigned any color!");
}
}
load_colors;
Irssi::command_bind('color', 'cmd_color');
Irssi::command_bind('recolor', 'cmd_recolor');
Irssi::signal_add('message public', 'sig_public');
Irssi::signal_add('message irc action', 'sig_act_public');
Irssi::signal_add('event nick', 'sig_nick');
Irssi::settings_add_int('misc','colorlevel',1);
Irssi::settings_add_bool('misc','nickcolor_global',0);