From 1ba6fd4ec25afd063c87ec1a4c1c6f9ba2601524 Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 10 Apr 2005 22:05:27 +0000 Subject: [PATCH] Converted Onis::Plugins::Topics --- lib/Onis/Plugins/Topics.pm | 93 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 lib/Onis/Plugins/Topics.pm diff --git a/lib/Onis/Plugins/Topics.pm b/lib/Onis/Plugins/Topics.pm new file mode 100644 index 0000000..4f38fca --- /dev/null +++ b/lib/Onis/Plugins/Topics.pm @@ -0,0 +1,93 @@ +package Onis::Plugins::Topics; + +use strict; +use warnings; + +use Onis::Config (qw(get_config)); +use Onis::Html (qw(html_escape get_filehandle)); +use Onis::Language (qw(translate)); +use Onis::Data::Core (qw(register_plugin)); +use Onis::Data::Persistent (); + +our $TopicCache = Onis::Data::Persistent->new ('TopicCache', 'time', qw(text nick)); +our $TopicData = []; + +register_plugin ('TOPIC', \&add); +register_plugin ('OUTPUT', \&output); + +our $MAX = 10; +if (get_config ('plugin_max')) +{ + my $tmp = get_config ('plugin_max'); + $tmp =~ s/\D//g; + + $MAX = $tmp if ($tmp); +} + +my $VERSION = '$Id$'; +print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG); + +return (1); + +sub add +{ + my $data = shift; + my $text = $data->{'text'}; + my $nick = $data->{'nick'}; + my $time = $data->{'epoch'}; + + $TopicCache->put ($time, $text, $nick); +} + +sub calculate +{ + my $i = 0; + for (sort { $b <=> $a } ($TopicCache->keys ())) + { + my $time = $_; + last if ($i++ >= $MAX); + + my ($text, $nick) = $TopicCache->get ($time); + die unless (defined ($nick)); + + $nick = get_main_nick ($nick); + push (@$TopicData, [$text, $nick, $time]); + } +} + +sub output +{ + calculate (); + + my $fh = get_filehandle (); + + my $topic = translate ('Topic'); + my $setby = translate ('Set by'); + + print $fh < + +   + $topic + $setby + +EOF + + my $i = 0; + for (@$TopicData) + { + $i++; + my ($topic, $nick) = @$_; + my $name = nick_to_name ($nick) || $nick; + + $topic = html_escape ($topic); + + print $fh " \n", + qq# $i\n#, + qq# $topic\n#, + qq# $name\n#, + qq# \n#; + } + + print $fh "\n\n"; +} -- 2.11.0