#!/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

# Written by Jakub Jankowski <shasta@atn.pl>
# for Irssi 0.7.98.4 and newer

use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "1.4";
%IRSSI = (
    authors     => 'Jakub Jankowski',
    contact     => 'shasta@atn.pl',
    name        => 'rainbow',
    description => 'Prints colored text. Rather simple than sophisticated.',
    license     => 'GNU GPLv2 or later',
    url         => 'http://irssi.atn.pl/',
);

use Irssi;
use Irssi::Irc;

# colors list
#  0 == white
#  4 == light red
#  8 == yellow
#  9 == light green
# 11 == light cyan
# 12 == light blue
# 13 == light magenta
# my @colors = ('0', '4', '8', '9', '11', '12', '13');
# my @colors = ('2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13');
my @colors = ('4', '8', '9', '11', '12', '13');

# str make_colors($string)
# returns random-coloured string
sub make_colors_rand {
	my ($string) = @_;
	my $newstr = "";
	my $last = 255;
	my $color = 0;

	for (my $c = 0; $c < length($string); $c++) {
		my $char = substr($string, $c, 1);
		if ($char eq ' ') {
			$newstr .= $char;
			next;
		}
		while (($color = int(rand(scalar(@colors)))) == $last) {};
		$last = $color;
		$newstr .= "\003";
		$newstr .= sprintf("%02d", $colors[$color]);
		$newstr .= (($char eq ",") ? ",," : $char);
	}

	return $newstr;
}

# str make_colors($string)
# returns random-coloured string
my $last_color = 0;
sub make_colors {
	my ($string) = @_;
	my $newstr = "";
	my $last = $last_color;
	my $color = 0;
	my $colorrep = 2;

	for (my $c = 0; $c < length($string); $c++) {
		my $char = substr($string, $c, 1);
		#if ($char eq ' ') {
		#	$newstr .= $char;
		#	next;
		#}
		
		$last++;
		$color =  ($last / $colorrep)  % scalar(@colors);
		$newstr .= "\003";
		$newstr .= sprintf("%02d", $colors[$color]);
		$newstr .= (($char eq ",") ? "\," : $char);
	}

	$last_color += 2;#rotate  $last;

	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));
	}
}

# 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));
	}
}

Irssi::command_bind("rsay", "rsay");
Irssi::command_bind("rtopic", "rtopic");
Irssi::command_bind("rme", "rme");
Irssi::command_bind("rkick", "rkick");

# changes:
#
# 25.01.2002: Initial release (v1.0)
# 26.01.2002: /rtopic added (v1.1)
# 29.01.2002: /rsay works with dcc chats now (v1.2)
# 02.02.2002: make_colors() doesn't assign any color to spaces (v1.3)
# 23.02.2002: /rkick added
