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
|
########################################
# Header ###############################
########################################
use strict;
use Encode();
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = 'r4';
%IRSSI = (
'name' => 'colored_nicks',
'authors' => 'Trilkk',
'contact' => 'trilkk ät iki.fi',
'url' => 'https://github.com/trilkk/irssi-colored-nicks',
'license' => 'BSD',
'description' => 'Exposes colored nickname variables for themes',
);
# List of protocols to act on.
my @action_protos = qw(irc silc xmpp);
# Global expando variable.
my $expando_cnnick = '';
# Global expando variable.
my $expando_cnpadl = '';
# Global expando variable.
my $expando_cnpads = '';
# Global expando variable.
my $expando_cnuser = '';
########################################
# Functions ############################
########################################
# Creates a terminal color command code string.
# Use cubes.pl to check string input.
# Similar function in nickcolor_expando.pl by Nei was used as basis.
# See: https://github.com/irssi/scripts.irssi.org/blob/master/scripts/nickcolor_expando.pl
# \param 0 Irssi color code.
# \return Color command code.
sub create_color_command_code
{
my $input_code = $_[0];
$input_code =~ /%(.*)/;
my $code = $1;
# First, try simple 16-color terminal codes.
my %color_map_16c =
(
'k' => '0',
'b' => '1',
'g' => '2',
'c' => '3',
'r' => '4',
'm' => '5',
'y' => '6',
'w' => '7',
'K' => '8',
'B' => '9',
'G' => ':',
'C' => ';',
'R' => '<',
'M' => '=',
'Y' => '>',
'W' => '?',
);
if(exists $color_map_16c{$code})
{
return "\cD" . $color_map_16c{$code} . '/';
}
# Try 256-color terminal codes.
my @ext_colour_off =
(
'.', '-', ',', '+', "'", '&',
);
if($code =~ /^(x)(?:0([[:xdigit:]])|([1-6])(?:([0-9])|([a-z]))|7([a-x]))$/i)
{
my $bg = $1 eq 'x';
my $col = defined $2 ? hex $2
: defined $6 ? 232 + (ord lc $6) - (ord 'a')
: 16 + 36 * ($3 - 1) + (defined $4 ? $4 : 10 + (ord lc $5) - (ord 'a'));
if ($col < 0x10)
{
my $chr = chr $col + ord '0';
return "\cD" . ($bg ? "/$chr" : "$chr/")
}
else
{
return "\cD" . $ext_colour_off[($col - 0x10) / 0x50 + $bg * 3] . chr (($col - 0x10) % 0x50 - 1 + ord '0')
}
}
# Default fallback is just gray.
return "\cD" . '7/';
}
# Create an irssi nickname string for a nick with maximum amount of characters.
# \param 0 Nick.
# \param 1 Attribution.
# \param 2 Number of characters nick can use at maximum.
# \return Truncated, colored nickname string.
sub create_irssi_nick
{
my $nick = $_[0];
my $attr = $_[1];
my $truncation = $_[2];
my $len = nick_length($nick);
if($attr)
{
$len += nick_length($attr);
}
# Hash the color before modifying nick.
my $color = simple_hash_color($nick);
my $format = create_color_command_code($color);
# Decrease until within parameters.
if($truncation > 0)
{
while($len > $truncation)
{
if(length($attr) > 1)
{
chop($attr);
}
else
{
chop($nick);
}
--$len;
}
}
if($attr)
{
return $format . $nick . create_color_command_code('%K') . $attr;
}
return $format . $nick;
}
# Create a buffer of spaces to pad a nick to n characters.
# \param 0 Nick.
# \param 1 Attribution.
# \param 2 Number of characters nick can use at maximum.
# \return Whitespace padding buffer.
sub create_padding
{
my $nick = $_[0];
my $attr = $_[1];
my $truncation = $_[2];
my $len = nick_length($nick);
if($attr)
{
$len += nick_length($attr);
}
my $ret = '';
if($truncation > 0)
{
while($len < $truncation)
{
++$len;
$ret .= ' ';
}
}
return $ret;
}
# Extracts attribution information from the message.
# \param 0 Nick.
# \return Tuple of (nickname part, attribution part).
sub extract_attribution
{
my $nick = $_[0];
Encode::_utf8_on($nick);
# Split nickname from boundary of allowed IRC nickname characters.
# Non-breakable space and zero-width space are valid characters.
$nick =~ /^([\w\s\|\^_`\-\{\}\[\]\\\x{00A0}\x{200B}\x{202F}]+)(.*)$/;
return ($1, $2);
}
# Calculates djb2 hash over an array.
# \param 0 Input array.
# \return calculated hash.
sub hash_djb2
{
my @array = @_;
my $ret = 5381;
foreach my $cc (@array)
{
my $oo = ord($cc);
if(!is_zero_width($oo))
{
$ret = (($ret * 33) + $oo) & 0xFFFFFFFF;
}
}
return $ret;
}
# Calculates sdbm hash over an array.
# \param 0 Input array.
# \return calculated hash.
sub hash_sdbm
{
my @array = @_;
my $ret = 0;
foreach my $cc (@array)
{
my $oo = ord($cc);
if(!is_zero_width($oo))
{
$ret = (($ret * 65599) + $oo) & 0xFFFFFFFF;
}
}
return $ret;
}
# Gets the color array.
# \return Color array.
sub get_color_array()
{
my $ret = Irssi::settings_get_str('colored_nicks_colors');
$ret =~ s/^\s+//;
$ret =~ s/\s+$//;
return split /\s/, $ret;
}
# Tells if a character has zero width.
# \param 0 Character.
# \return True if character has zero widh, false otherwise.
sub is_zero_width
{
my $cc = $_[0];
my @zero_width_chars =
(
0x200B,
);
foreach my $zz (@zero_width_chars)
{
if(($cc == $zz) || (ord($cc) == $zz))
{
return 1;
}
}
return 0;
}
# Calculate real length of a nickname.
# Non-breakable space is not included.
# \param 0 Nick, must be in unicode.
# \return Nick length in terminal characters.
sub nick_length
{
my $nick = $_[0];
my $ret = 0;
foreach my $cc (split //,$nick)
{
if(!is_zero_width($cc))
{
++$ret;
}
}
return $ret;
};
# Simple hash based on nick.
# \param 0 String to hash.
# \return Hash value.
sub simple_hash
{
# Remove characters that should not be taken into account for.
my $input_string = $_[0];
$input_string =~ s/^[\s_\-^]*//;
$input_string =~ s/[\s_\-^]*$//;
my @array = split(//, $input_string);
# Check for djb2 hash.
my $hash_function = Irssi::settings_get_str('colored_nicks_hash_function');
if($hash_function =~ /^djb2$/i)
{
return hash_djb2(@array);
}
# Fallback to sdbm hash.
return hash_sdbm(@array);
}
# Simple hash, but pick color based on nick.
# \param 0 String to hash.
# \return Color change string.
sub simple_hash_color
{
my $input_string = $_[0];
my @colors = get_color_array();
return $colors[simple_hash($input_string) % @colors];
}
########################################
# Commands #############################
########################################
# Test command to list all colors in use.
sub cmd_cn_list
{
my $window = Irssi::active_win;
my $mode = MSGLEVEL_NEVER | MSGLEVEL_CLIENTCRAP;
my @colors = get_color_array();
for(my $ii = 0; ($ii < @colors); ++$ii)
{
my $color = @colors[$ii];
my $code = create_color_command_code($color);
# Insert non-breakable space as second character so irssi doesn't use the color code.
$color = substr($color, 0, 1) . "\x{200B}" . substr($color, 1);
$window->print($code . 'colored_nicks_' . $ii . '_' . $color, $mode);
}
}
# Test command for given input.
# \param 0 Test nickname.
sub cmd_cn_test
{
my @nicks = split /\s/, $_[0];
my $window = Irssi::active_win;
my $mode = MSGLEVEL_NEVER | MSGLEVEL_CLIENTCRAP;
my $truncation_long = Irssi::settings_get_int('colored_nicks_truncation_long');
# Iterate over the input.
my $ret = '';
foreach my $nick (@nicks)
{
if($ret)
{
$ret .= ' ';
}
$ret .= create_irssi_nick($nick, '', $truncation_long);
}
# Only print if there was legit input.
if($ret)
{
$window->print($ret, $mode);
}
}
########################################
# Signal hooks #########################
########################################
# Signal function for private messages to the user.
# \param 0 Server struct.
# \param 1 ???
# \param 2 Input nickname.
# \param 3 ???
# \param 4 ???
sub signal_cn_private
{
my ($server, $param1, $input_nick, $param3, $param4) = @_;
my ($nick, $attr) = extract_attribution($input_nick);
my $truncation_long = Irssi::settings_get_int('colored_nicks_truncation_long');
$expando_cnnick = create_irssi_nick($nick, $attr, $truncation_long);
$expando_cnpadl = create_padding($nick, $attr, $truncation_long);
$expando_cnpads = '';
$expando_cnuser = '';
}
# Signal function for public messages by others.
# \param 0 Server struct.
# \param 1 ???
# \param 2 Input nickname.
# \param 3 ???
# \param 4 ???
sub signal_cn_public
{
my ($server, $param1, $input_nick, $param3, $param4) = @_;
my ($nick, $attr) = extract_attribution($input_nick);
my $truncation_long = Irssi::settings_get_int('colored_nicks_truncation_long');
my $truncation_short = Irssi::settings_get_int('colored_nicks_truncation_short');
$expando_cnnick = create_irssi_nick($nick, $attr, $truncation_long);
$expando_cnpadl = create_padding($nick, $attr, $truncation_long);
$expando_cnpads = create_padding($nick, $attr, $truncation_short);
$expando_cnuser = '';
}
# Signal function for public messages from the user.
# \param 0 Server struct.
# \param 1 ???
# \param 2 ???
sub signal_cn_own_public
{
my ($server, $param1, $param2) = @_;
my $truncation_long = Irssi::settings_get_int('colored_nicks_truncation_long');
$expando_cnnick = '';
$expando_cnpadl = create_padding($server->{nick}, '', $truncation_long);
$expando_cnpads = '';
$expando_cnuser = create_irssi_nick($server->{nick}, '', $truncation_long);
}
# Signal function for private messages from the user.
# \param 0 Server struct.
# \param 1 ???
# \param 2 Input nick
# \param 3 ???
# \param 4 ???
sub signal_cn_own_private
{
my ($server, $param1, $input_nick, $param3) = @_;
my ($nick, $attr) = extract_attribution($input_nick);
my $truncation_long = Irssi::settings_get_int('colored_nicks_truncation_long');
$expando_cnnick = create_irssi_nick($nick, $attr, $truncation_long);
$expando_cnpadl = create_padding($nick, $attr, $truncation_long);
$expando_cnpads = create_padding($server->{nick}, '', $truncation_long);
$expando_cnuser = create_irssi_nick($server->{nick}, '', $truncation_long);
}
########################################
# Irssi:: ##############################
########################################
Irssi::settings_add_str('misc', 'colored_nicks_colors',
'%c %X3N' . # cyans
' ' .
'%X59 %X4B %X4A %m' . # magentas/purples
' ' .
'%w %X7P %X7Q %X7R' . # whites
' ' .
'%g %X1J %X2I %X2J %X3I' . # greens
' ' .
'%X46 %X4C %X5I' . # browns
' ' .
'%X2N %X2M %X1N %B' . # blues
' ' .
'%X5C %X56 %y' . # oranges/yellows
' ' .
'%X58 %X57' . # pinks/reds
'');
Irssi::settings_add_str('misc', 'colored_nicks_hash_function', 'djb2');
Irssi::settings_add_int('misc', 'colored_nicks_truncation_long', 12);
Irssi::settings_add_int('misc', 'colored_nicks_truncation_short', 11);
Irssi::expando_create('cnnick', sub { $expando_cnnick }, {
'message public' => 'none',
'message own_public' => 'none',
(map { ("message $_ action" => 'none',
"message $_ own_action" => 'none')
} @action_protos),
});
Irssi::expando_create('cnpadl', sub { $expando_cnpadl }, {
'message public' => 'none',
'message own_public' => 'none',
(map { ("message $_ action" => 'none',
"message $_ own_action" => 'none')
} @action_protos),
});
Irssi::expando_create('cnpads', sub { $expando_cnpads }, {
'message public' => 'none',
'message own_public' => 'none',
(map { ("message $_ action" => 'none',
"message $_ own_action" => 'none')
} @action_protos),
});
Irssi::expando_create('cnuser', sub { $expando_cnuser }, {
'message public' => 'none',
'message own_public' => 'none',
(map { ("message $_ action" => 'none',
"message $_ own_action" => 'none')
} @action_protos),
});
Irssi::signal_add({
'message private' => 'signal_cn_private',
'message public' => 'signal_cn_public',
'message own_public' => 'signal_cn_own_public',
'message own_private' => 'signal_cn_own_private',
});
Irssi::command_bind('colored_nicks_list', 'cmd_cn_list');
Irssi::command_bind('colored_nicks_test', 'cmd_cn_test');
|