1#!/usr/local/bin/perl -w
2
3# USAGE:
4#
5# /RSAY <text>
6#  - same as /say, but outputs a coloured text
7#
8# /RME <text>
9#  - same as /me, but outputs a coloured text
10#
11# /RTOPIC <text>
12#  - same as /topic, but outputs a coloured text :)
13#
14# /RKICK <nick> [reason]
15#  - kicks nick from the current channel with coloured reason
16#
17# /RKNOCKOUT [time] <nicks> [reason]
18#  - knockouts nicks from the current channel with coloured reason for time
19
20# Written by Jakub Jankowski <shasta@atn.pl>
21# for Irssi 0.7.98.4 and newer
22
23use strict;
24use vars qw($VERSION %IRSSI);
25
26$VERSION = "1.6";
27%IRSSI = (
28    authors     => 'Jakub Jankowski',
29    contact     => 'shasta@atn.pl',
30    name        => 'rainbow',
31    description => 'Prints colored text. Rather simple than sophisticated.',
32    license     => 'GNU GPLv2 or later',
33    url         => 'http://irssi.atn.pl/',
34);
35
36use Irssi;
37use Irssi::Irc;
38use Encode;
39
40# colors list
41#  0 == white
42#  4 == light red
43#  8 == yellow
44#  9 == light green
45# 11 == light cyan
46# 12 == light blue
47# 13 == light magenta
48my @colors = ('0', '4', '8', '9', '11', '12', '13');
49
50# str make_colors($string)
51# returns random-coloured string
52sub make_colors {
53	my ($string) = @_;
54	Encode::_utf8_on($string);
55	my $newstr = "";
56	my $last = 255;
57	my $color = 0;
58
59	for (my $c = 0; $c < length($string); $c++) {
60		my $char = substr($string, $c, 1);
61		if ($char eq ' ') {
62			$newstr .= $char;
63			next;
64		}
65		while (($color = int(rand(scalar(@colors)))) == $last) {};
66		$last = $color;
67		$newstr .= "\003";
68		$newstr .= sprintf("%02d", $colors[$color]);
69		$newstr .= $char;
70	}
71
72	return $newstr;
73}
74
75# void rsay($text, $server, $destination)
76# handles /rsay
77sub rsay {
78	my ($text, $server, $dest) = @_;
79
80	if (!$server || !$server->{connected}) {
81		Irssi::print("Not connected to server");
82		return;
83	}
84
85	return unless $dest;
86
87	if ($dest->{type} eq "CHANNEL" || $dest->{type} eq "QUERY") {
88		$dest->command("/msg " . $dest->{name} . " " . make_colors($text));
89	}
90}
91
92# void rme($text, $server, $destination)
93# handles /rme
94sub rme {
95	my ($text, $server, $dest) = @_;
96
97	if (!$server || !$server->{connected}) {
98		Irssi::print("Not connected to server");
99		return;
100	}
101
102	if ($dest && ($dest->{type} eq "CHANNEL" || $dest->{type} eq "QUERY")) {
103		$dest->command("/me " . make_colors($text));
104	}
105}
106
107# void rtopic($text, $server, $destination)
108# handles /rtopic
109sub rtopic {
110	my ($text, $server, $dest) = @_;
111
112	if (!$server || !$server->{connected}) {
113		Irssi::print("Not connected to server");
114		return;
115	}
116
117	if ($dest && $dest->{type} eq "CHANNEL") {
118		$dest->command("/topic " . make_colors($text));
119	}
120}
121
122# void rkick($text, $server, $destination)
123# handles /rkick
124sub rkick {
125	my ($text, $server, $dest) = @_;
126
127	if (!$server || !$server->{connected}) {
128		Irssi::print("Not connected to server");
129		return;
130	}
131
132	if ($dest && $dest->{type} eq "CHANNEL") {
133		my ($nick, $reason) = split(/ +/, $text, 2);
134		return unless $nick;
135		$reason = "Irssi power!" if ($reason =~ /^[\ ]*$/);
136		$dest->command("/kick " . $nick . " " . make_colors($reason));
137	}
138}
139
140# void rknockout($text, $server, $destination)
141# handles /rknockout
142sub rknockout {
143    my ($text, $server, $dest) = @_;
144
145    if (!$server || !$server->{connected}) {
146        Irssi::print("Not connected to server");
147        return;
148    }
149
150    if ($dest && $dest->{type} eq "CHANNEL") {
151        my ($time, $nick, $reason) = split(/ +/, $text, 3);
152        ($time, $nick, $reason) = (300, $time, $nick . " " . $reason) if ($time !~ m/^\d+$/);
153        return unless $nick;
154        $reason = "See you in " . $time . " seconds!" if ($reason =~ /^[\ ]*$/);
155        $dest->command("/knockout " . $time . " " . $nick . " " . make_colors($reason));
156    }
157}
158
159Irssi::command_bind("rsay", "rsay");
160Irssi::command_bind("rtopic", "rtopic");
161Irssi::command_bind("rme", "rme");
162Irssi::command_bind("rkick", "rkick");
163Irssi::command_bind("rknockout", "rknockout");
164
165# changes:
166#
167# 25.01.2002: Initial release (v1.0)
168# 26.01.2002: /rtopic added (v1.1)
169# 29.01.2002: /rsay works with dcc chats now (v1.2)
170# 02.02.2002: make_colors() doesn't assign any color to spaces (v1.3)
171# 23.02.2002: /rkick added
172# 26.11.2014: utf-8 support
173# 01.12.2014: /rknockout added (v1.6)
174