Skip to content

Instantly share code, notes, and snippets.

@MikeRixWolfe
Last active November 6, 2015 14:44
Show Gist options
  • Save MikeRixWolfe/97fc5ef9a70019b5e9b8 to your computer and use it in GitHub Desktop.
Save MikeRixWolfe/97fc5ef9a70019b5e9b8 to your computer and use it in GitHub Desktop.
rainbow.pl v1.9
#!/usr/bin/perl -w
# USAGE:
#
# /RSAY <text>
# - same as /say, but outputs a coloured text
#
# /RME <text>
# - same as /me, but outputs a coloured text
#
# /RTOPIC <text>
# - same as /topic, but outputs a coloured text :)
#
# /RKICK <nick> [reason]
# - kicks nick from the current channel with coloured reason
#
# /RKILL <nick> [reason]
# - Kills the specified nickname. Requires /oper
#
# /RKNOCKOUT [time] <nicks> [reason]
# - knockouts nicks from the current channel with coloured reason for specified time
# Originally written by Jakub Jankowski <[email protected]>
# Maintained and expanded by Alex Shafer and Mike Wolfe
# for Irssi 0.7.98.4 and newer
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = "1.9";
%IRSSI = (
authors => 'Alex Shafer, Mike Wolfe, Jakub Jankowski',
contact => 'N/A',
name => 'rainbow',
description => 'Prints colored text. Rather simple than sophisticated.',
license => 'GNU GPLv3 or later',
url => 'N/A',
);
use Irssi;
use Irssi::Irc;
use Encode;
# colors list
# 0 == white
# 2 == blue
# 3 == green
# 4 == light red
# 5 == red
# 6 == magenta
# 7 == yellow
# 8 == light yellow
# 9 == light green
# 10 == cyan
# 11 == light cyan
# 12 == light blue
# 13 == light magenta
# 14 == grey
my @colors = ('5','4','7','8','3','9','10','11','2','12','6','13');
# str make_colors($string)
# returns random-coloured string
sub make_colors {
my ($string) = @_;
Encode::_utf8_on($string);
my $newstr = "";
my $color = int(rand(scalar(@colors)));
for (my $c = 0; $c < length($string); $c++) {
my $char = substr($string, $c, 1);
if ($char eq ' ') {
$newstr .= $char;
next;
}
$color = $color < int(scalar(@colors))-1 ? int($color+1) : 0;
$newstr .= "\003";
$newstr .= sprintf("%02d", $colors[$color]);
$newstr .= $char;
}
return $newstr;
}
# void rsay($text, $server, $destination)
# handles /rsay
sub rsay {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
return unless $dest;
if ($dest->{type} eq "CHANNEL" || $dest->{type} eq "QUERY") {
$dest->command("/msg " . $dest->{name} . " " . make_colors($text));
}
}
sub rkill {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
#return unless $dest;
my ($nick, $reason) = split(/ +/, $text, 2);
return unless $nick;
$dest->command("/kill $nick ".make_colors($reason));
}
# void rme($text, $server, $destination)
# handles /rme
sub rme {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
if ($dest && ($dest->{type} eq "CHANNEL" || $dest->{type} eq "QUERY")) {
$dest->command("/me " . make_colors($text));
}
}
# void rtopic($text, $server, $destination)
# handles /rtopic
sub rtopic {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
if ($dest && $dest->{type} eq "CHANNEL") {
$dest->command("/topic " . make_colors($text));
}
}
# void rkick($text, $server, $destination)
# handles /rkick
sub rkick {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
if ($dest && $dest->{type} eq "CHANNEL") {
my ($nick, $reason) = split(/ +/, $text, 2);
return unless $nick;
$reason = "Irssi power!" if ($reason =~ /^[\ ]*$/);
$dest->command("/kick " . $nick . " " . make_colors($reason));
}
}
sub rknockout {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
if ($dest && $dest->{type} eq "CHANNEL") {
my ($time, $nick, $reason) = split(/ +/, $text, 3);
($time, $nick, $reason) = (300, $time, $nick . " " . $reason) if ($time !~ m/^\d+$/);
return unless $nick;
$reason = "See you in " . $time . " seconds!" if ($reason =~ /^[\ ]*$/);
$dest->command("/knockout " . $time . " " . $nick . " " . make_colors($reason));
}
}
sub rnotice {
my ($text, $server, $dest) = @_;
if (!$server || !$server->{connected}) {
Irssi::print("Not connected to server");
return;
}
if ($dest && $dest->{type} eq "CHANNEL") {
my ($nick, $reason) = split(/ +/, $text, 2);
return unless $nick;
$reason = "RoYaL RaInBoW!" if ($reason =~ /^[\ ]*$/);
$dest->command("/notice " . $nick . " " . make_colors($reason));
}
}
Irssi::command_bind("rsay", "rsay");
Irssi::command_bind("rtopic", "rtopic");
Irssi::command_bind("rme", "rme");
Irssi::command_bind("rkick", "rkick");
Irssi::command_bind("rkill", "rkill");
Irssi::command_bind("rknockout", "rknockout");
Irssi::command_bind("rnotice", "rnotice");
# changes:
#
# 2002.01.25: Initial release (v1.0)
# 2002.01.26: /rtopic added (v1.1)
# 2002.01.29: /rsay works with dcc chats now (v1.2)
# 2002.02.02: make_colors() doesn't assign any color to spaces (v1.3)
# 2002.02.23: /rkick added
# 2014.01.12: /rknockout added
# 2015.03.02: unicode, more colors, remove double comma thing (v1.6)
# 2015.02.24: in order colors (v1.7)
# 2015.04.01: Added /rkill
# 2015.06.30: Added /rnotice (v1.9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment