summaryrefslogtreecommitdiffstats
path: root/scripts/topics.pl
blob: 61a672ba10137fef3353c052a75038cb7473b420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# by Stefan 'tommie' Tomanek
use strict;

use vars qw($VERSION %IRSSI);
$VERSION = '2003020801';
%IRSSI = (
    authors     => 'Stefan \'tommie\' Tomanek',
    contact     => 'stefan@pico.ruhr.de',
    name        => 'topics',
    description => 'records a topic history and locks the channel topic',
    license     => 'GPLv2',
    url         => 'http://irssi.org/scripts/',
    changed     => $VERSION,
    commands     => 'topics'
);

use Irssi 20020324;
use vars qw(%topics);

sub show_help() {
    my $help = "$IRSSI{name} $VERSION
/topics
    List all topics that have been set in the current channel
/topics <num>
    Restore topic <num>
/topics lock
    Lock the current topic
/topics unlock
    Unlock the channeltopic
";
    my $text='';
    foreach (split(/\n/, $help)) {
        $_ =~ s/^\/(.*)$/%9\/$1%9/;
        $text .= $_."\n";
    }
    print CLIENTCRAP &draw_box("Topics", $text, "topics help", 1);
}


sub draw_box ($$$$) {
    my ($title, $text, $footer, $colour) = @_;
    my $box = '';
    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
    foreach (split(/\n/, $text)) {
        $box .= '%R|%n '.$_."\n";
    }
    $box .= '%R`--<%n'.$footer.'%R>->%n';
    $box =~ s/%.//g unless $colour;
    return $box;
}

sub sig_channel_topic_changed ($) {
    my ($channel) = @_;
    my $ircnet = $channel->{server}->{tag};
    my $name = $channel->{name};
    my $data = {'topic'      => $channel->{topic}, 
                'topic_by'   => $channel->{topic_by},
		'topic_time' => $channel->{topic_time}
    };
    push @{$topics{$ircnet}{$name}{list}}, $data;
    if ($topics{$ircnet}{$name}{lock}) {
	my $topic = $topics{$ircnet}{$name}{lock}{topic};
	return if ($topic eq $channel->{topic});
	$channel->print("%B>>%n Restoring locked topic...", MSGLEVEL_CLIENTCRAP);
	$channel->command("TOPIC -- ".$topic);
    }
}

sub cmd_topics ($$$) {
    my ($args, $server, $witem) = @_;
    my @args = split / /, $args;
    if ($args[0] =~ /^\d+$/) {
	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
	my $ircnet = $server->{tag};
	my $name = $witem->{name};
	if (defined $topics{$ircnet}{$name}{list}->[$args]) {
	    $witem->print("%B>>%n Restoring Topic ".$args, MSGLEVEL_CLIENTCRAP);
	    my $topic = $topics{$ircnet}{$name}{list}->[$args]->{topic};
	    $witem->command("TOPIC -- ".$topic);
	}
    } elsif ($args[0] eq 'lock') {
	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
	my $ircnet = $server->{tag};
	my $name = $witem->{name};
	my $data = {'topic'      => $witem->{topic},
		    'topic_by'   => $witem->{topic_by},
		    'topic_time' => $witem->{topic_time}
	};
	$topics{$ircnet}{$name}{lock} = $data;
	$witem->print("%B>>%n %ro-m%n Topic locked", MSGLEVEL_CLIENTCRAP);
    } elsif ($args[0] eq 'unlock') {
	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
	my $ircnet = $server->{tag};
	my $name = $witem->{name};
	delete $topics{$ircnet}{$name}{lock};
	$witem->print("%B>>%n %g�-m%n Topic unlocked", MSGLEVEL_CLIENTCRAP);
    } elsif ($args[0] eq 'help') {
	show_help();
    } else {
        return unless (ref $witem && $witem->{type} eq 'CHANNEL');
        my $ircnet = $server->{tag};
        my $name = $witem->{name};
	my $i = 0;
	my $text;
	foreach (@{$topics{$ircnet}{$name}{list}}) {
	    $text .= "%r[".$i."]%n ".$_->{topic_time}." (by ".$_->{topic_by}.")\n";
	    my $topic = $_->{topic};
	    $topic =~ s/%/%%/g;
	    $text .= '     "'.$topic.'"'."\n";
	    $i++;
	}
	$witem->print($_, MSGLEVEL_CLIENTCRAP) foreach (split(/\n/, draw_box('Topics', $text, $name, 1)));
    }
}

Irssi::signal_add('channel topic changed', \&sig_channel_topic_changed);
sig_channel_topic_changed($_) foreach (Irssi::channels());

Irssi::command_bind('topics', \&cmd_topics);
foreach my $cmd ('lock', 'unlock', 'help') {
    Irssi::command_bind('topics '.$cmd => sub {
			cmd_topics("$cmd ".$_[0], $_[1], $_[2]); });
}

print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded: /topics help for help';