1use Irssi 20020300;
2use strict;
3
4use vars qw($VERSION %IRSSI);
5$VERSION = "1.1";
6%IRSSI = (
7        authors         => "Maciek \'fahren\' Freudenheim",
8        contact         => "fahren\@bochnia.pl",
9        name            => "Topic Lock",
10        description     => "/TLOCK [-d] [channel] [topic] - locks current or specified topic on [channel]",
11        license         => "GNU GPLv2 or later",
12        changed         => "Fri Mar 15 15:09:42 CET 2002"
13);
14
15my %tlock = ();
16
17sub cmd_tlock {
18	my ($args, $server, $win) = @_;
19
20	my $tag = $server->{tag};
21	my $delete = ($args =~ s/^-d\s//)? 1 : 0;
22	my ($chan) = $args =~ /^([^\s]+)/;
23
24	unless ($chan) {
25		my $i = 0;
26		for my $ch (keys %{$tlock{$tag}}) {
27			if ($tlock{$tag}{$ch}) {
28				$i = 1;
29				Irssi::print("Lock on $tag%W/%n$ch%W:%n $tlock{$tag}{$ch}");
30			}
31		}
32		Irssi::print("%R>>%n You dont have any active topic locks at this moment.") unless $i;
33		return;
34	}
35
36	$chan = lc($chan);
37	if ($delete) {
38		Irssi::print("%W>>%n topic lock on $chan removed") if $tlock{$tag}{$chan};
39		undef $tlock{$tag}{$chan};
40		return;
41	}
42
43	my $channel = $server->channel_find($chan);
44	unless ($channel && $channel->{chanop}) {
45		Irssi::print("%R>>%n You are not channel operator/not on channel on $chan.");
46		return;
47	}
48
49	$args =~ s/^$chan\s?//;
50	my $topic = ($args)? $args : $channel->{topic};
51
52	if ($tlock{$tag}{$chan}) {
53		Irssi::print("Changed tlock on $chan to%W:%n $topic");
54	} else {
55		Irssi::print("Set tlock on $chan to%W:%n $topic");
56	}
57
58	$server->send_raw("TOPIC $chan :$topic") if $channel->{topic} ne $topic;
59
60	$tlock{$tag}{$chan} = $topic;
61
62}
63
64sub sub_tlock {
65	# "event "<cmd>, SERVER_REC, char *args, char *sender_nick, char *sender_address
66	my ($server, $args, $nick, $uh) = @_;
67
68	return if $server->{nick} eq $nick;
69	my ($chan, $topic) = split(/ :/, $args);
70	return unless $server->channel_find($chan)->{chanop};
71	$chan = lc($chan);
72	my $tag = $server->{tag};
73
74	if ($tlock{$tag}{$chan} && $topic ne $tlock{$tag}{$chan}) {
75		Irssi::print("%W>>%n tlock: changing topic back on $chan");
76		$server->send_raw("TOPIC $chan :$tlock{$tag}{$chan}");
77	}
78}
79
80Irssi::signal_add('event topic', 'sub_tlock');
81Irssi::command_bind('tlock', 'cmd_tlock');
82