html/topics.pl


   1 
   2 # by Stefan 'tommie' Tomanek
   3 use strict;
   4 
   5 use vars qw($VERSION %IRSSI);
   6 $VERSION = '2003020801';
   7 %IRSSI = (
   8     authors     => 'Stefan \'tommie\' Tomanek',
   9     contact     => 'stefan@pico.ruhr.de',
  10     name        => 'topics',
  11     description => 'records a topic history and locks the channel topic',
  12     license     => 'GPLv2',
  13     url         => 'http://irssi.org/scripts/',
  14     changed     => $VERSION,
  15     commands     => 'topics'
  16 );
  17 
  18 use Irssi 20020324;
  19 use vars qw(%topics);
  20 
  21 sub show_help() {
  22     my $help = "$IRSSI{name} $VERSION
  23 /topics
  24         List all topics that have been set in the current channel
  25 /topics <num>
  26         Restore topic <num>
  27 /topics lock
  28         Lock the current topic
  29 /topics unlock
  30         Unlock the channeltopic
  31 ";
  32     my $text='';
  33     foreach (split(/\n/, $help)) {
  34         $_ =~ s/^\/(.*)$/%9\/$1%9/;
  35         $text .= $_."\n";
  36     }
  37     print CLIENTCRAP &draw_box("Topics", $text, "topics help", 1);
  38 }
  39 
  40 
  41 sub draw_box ($$$$) {
  42     my ($title, $text, $footer, $colour) = @_;
  43     my $box = '';
  44     $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
  45     foreach (split(/\n/, $text)) {
  46         $box .= '%R|%n '.$_."\n";
  47     }
  48     $box .= '%R`--<%n'.$footer.'%R>->%n';
  49     $box =~ s/%.//g unless $colour;
  50     return $box;
  51 }
  52 
  53 sub sig_channel_topic_changed ($) {
  54     my ($channel) = @_;
  55     my $ircnet = $channel->{server}->{tag};
  56     my $name = $channel->{name};
  57     my $data = {'topic'      => $channel->{topic}, 
  58                 'topic_by'   => $channel->{topic_by},
  59 		'topic_time' => $channel->{topic_time}
  60     };
  61     push @{$topics{$ircnet}{$name}{list}}, $data;
  62     if ($topics{$ircnet}{$name}{lock}) {
  63 	my $topic = $topics{$ircnet}{$name}{lock}{topic};
  64 	return if ($topic eq $channel->{topic});
  65 	$channel->print("%B>>%n Restoring locked topic...", MSGLEVEL_CLIENTCRAP);
  66 	$channel->command("TOPIC -- ".$topic);
  67     }
  68 }
  69 
  70 sub cmd_topics ($$$) {
  71     my ($args, $server, $witem) = @_;
  72     my @args = split / /, $args;
  73     if ($args[0] =~ /^\d+$/) {
  74 	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
  75 	my $ircnet = $server->{tag};
  76 	my $name = $witem->{name};
  77 	if (defined $topics{$ircnet}{$name}{list}->[$args]) {
  78 	    $witem->print("%B>>%n Restoring Topic ".$args, MSGLEVEL_CLIENTCRAP);
  79 	    my $topic = $topics{$ircnet}{$name}{list}->[$args]->{topic};
  80 	    $witem->command("TOPIC -- ".$topic);
  81 	}
  82     } elsif ($args[0] eq 'lock') {
  83 	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
  84 	my $ircnet = $server->{tag};
  85 	my $name = $witem->{name};
  86 	my $data = {'topic'      => $witem->{topic},
  87 		    'topic_by'   => $witem->{topic_by},
  88 		    'topic_time' => $witem->{topic_time}
  89 	};
  90 	$topics{$ircnet}{$name}{lock} = $data;
  91 	$witem->print("%B>>%n %ro-m%n Topic locked", MSGLEVEL_CLIENTCRAP);
  92     } elsif ($args[0] eq 'unlock') {
  93 	return unless (ref $witem && $witem->{type} eq 'CHANNEL');
  94 	my $ircnet = $server->{tag};
  95 	my $name = $witem->{name};
  96 	delete $topics{$ircnet}{$name}{lock};
  97 	$witem->print("%B>>%n %gø-m%n Topic unlocked", MSGLEVEL_CLIENTCRAP);
  98     } elsif ($args[0] eq 'help') {
  99 	show_help();
 100     } else {
 101         return unless (ref $witem && $witem->{type} eq 'CHANNEL');
 102         my $ircnet = $server->{tag};
 103         my $name = $witem->{name};
 104 	my $i = 0;
 105 	my $text;
 106 	foreach (@{$topics{$ircnet}{$name}{list}}) {
 107 	    $text .= "%r[".$i."]%n ".$_->{topic_time}." (by ".$_->{topic_by}.")\n";
 108 	    my $topic = $_->{topic};
 109 	    $topic =~ s/%/%%/g;
 110 	    $text .= '     "'.$topic.'"'."\n";
 111 	    $i++;
 112 	}
 113 	$witem->print($_, MSGLEVEL_CLIENTCRAP) foreach (split(/\n/, draw_box('Topics', $text, $name, 1)));
 114     }
 115 }
 116 
 117 Irssi::signal_add('channel topic changed', \&sig_channel_topic_changed);
 118 sig_channel_topic_changed($_) foreach (Irssi::channels());
 119 
 120 Irssi::command_bind('topics', \&cmd_topics);
 121 foreach my $cmd ('lock', 'unlock', 'help') {
 122     Irssi::command_bind('topics '.$cmd => sub {
 123 			cmd_topics("$cmd ".$_[0], $_[1], $_[2]); });
 124 }
 125 
 126 print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded: /topics help for help';