html/relm.pl
1 ## Usage: /RELM [-l || index] [target]
2 ## to list last 15 messages:
3 ## /RELM -l
4 ## to redirect msg #4, 7, 8, 9, 10, 13 to current channel/query:
5 ## /RELM 4,7-10,13
6 ## to redirect last message to current channel/query:
7 ## /RELM
8
9 use Irssi;
10
11 use vars qw($VERSION %IRSSI);
12 $VERSION = "1.0";
13 %IRSSI = (
14 authors => "Maciek \'fahren\' Freudenheim",
15 contact => "fahren\@bochnia.pl",
16 name => "REdirect Last Message",
17 description => "Keeps last 15 messages in cache",
18 license => "GNU GPLv2 or later",
19 changed => "Fri Mar 15 15:09:42 CET 2002"
20 );
21
22 sub cmd_relm {
23 my ($args, $server, $winit) = @_;
24 my $ircnet = lc($server->{tag});
25 my ($which, $where) = split(/ +/, $args, 2);
26
27 $where = $which unless $which =~ /[0-9]/;
28
29 $which = scalar(@{$relm{lc($ircnet)}}) unless ($which);
30
31 unless (@relm{$ircnet}) {
32 Irssi::print("%R>>%n Nothing in relm buffer on $ircnet.", MSGLEVEL_CRAP);
33 return;
34 }
35
36 if ($where eq "-l") {
37 my $numspace;
38 Irssi::print(">> ---- Context ------------------------", MSGLEVEL_CRAP);
39 for (my $i = 0; $i < scalar(@{$relm{$ircnet}}); $i++) {
40 $numspace = sprintf("%.2d", $i+1);
41 Irssi::print("[%W$numspace%n] $relm{$ircnet}[$i]", MSGLEVEL_CRAP);
42 }
43 return;
44 }
45
46 unless ($where) {
47 unless ($winit && ($winit->{type} eq "CHANNEL" || $winit->{type} eq "QUERY")) {
48 Irssi::print("%R>>%n You have to join channel first", MSGLEVEL_CRAP);
49 return;
50 }
51 $where = $winit->{name};
52 }
53
54 $which =~ s/,/ /g;
55 my @nums;
56 for my $num (split(/ /, $which)) {
57 if ($num =~ /-/) {
58 my ($start, $end) = $num =~ /([0-9]+)-([0-9]*)/;
59 for (;$start <= $end; $start++) {
60 push(@nums, $start - 1);
61 }
62 } else {
63 push(@nums, $num - 1);
64 }
65 }
66
67 for my $num (@nums) {
68 unless ($relm{$ircnet}[$num]) {
69 Irssi::print("%R>>%n No such message in relm buffer /" . ($num + 1). "/", MSGLEVEL_CRAP);
70 } else {
71 Irssi::active_server()->command("msg $where $relm{$ircnet}[$num]");
72 }
73 }
74 }
75
76 sub event_privmsg {
77 my ($server, $data, $nick, $address) = @_;
78 my ($target, $text) = split(/ :/, $data, 2);
79 my $ircnet = lc($server->{tag});
80
81 return if ($server->{nick} ne $target);
82 my $relm = "\00312[ \00310$nick!$address \00312]\003 $text";
83 shift(@{$relm{$ircnet}}) if scalar(@{$relm{$ircnet}}) > 14;
84 push(@{$relm{$ircnet}}, $relm);
85 }
86
87 Irssi::command_bind("relm", "cmd_relm");
88 Irssi::signal_add("event privmsg", "event_privmsg");