From 572ca233bb6a1ad08df5f42175595129bdd5c04f Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 10 Apr 2005 17:29:38 +0000 Subject: [PATCH] Converted Onis::Plugins::Urls. Fixed minor error in Nicks/Words plugin.. --- lib/Onis/Plugins/Nicks.pm | 4 +- lib/Onis/Plugins/Urls.pm | 110 ++++++++++++++++++++++++++++++++++++++++++++++ lib/Onis/Plugins/Words.pm | 5 +-- 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 lib/Onis/Plugins/Urls.pm diff --git a/lib/Onis/Plugins/Nicks.pm b/lib/Onis/Plugins/Nicks.pm index 00d27da..a90e3a9 100644 --- a/lib/Onis/Plugins/Nicks.pm +++ b/lib/Onis/Plugins/Nicks.pm @@ -16,7 +16,7 @@ register_plugin ('OUTPUT', \&output); our $MentionedNicksCache = Onis::Data::Persistent->new ('MentionedNicksCache', 'nick', qw(counter lastusedtime lastusedby)); our $MentionedNicksData = []; -my $VERSION = '$Id: Nicks.pm,v 1.7 2004/10/31 15:01:12 octo Exp $'; +my $VERSION = '$Id$'; print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG); return (1); @@ -74,7 +74,7 @@ sub calculate push (@data, [$nick, $counter, $lastusedby, $lastusedtime]); } - @$MentionedNicksData = sort { $b->[0] <=> $a->[0] } (@data); + @$MentionedNicksData = sort { $b->[1] <=> $a->[1] } (@data); splice (@$MentionedNicksData, $max); } diff --git a/lib/Onis/Plugins/Urls.pm b/lib/Onis/Plugins/Urls.pm new file mode 100644 index 0000000..01ed084 --- /dev/null +++ b/lib/Onis/Plugins/Urls.pm @@ -0,0 +1,110 @@ +package Onis::Plugins::Urls; + +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 get_main_nick)); +use Onis::Data::Persistent (); +use Onis::Users (qw(nick_to_name)); + +register_plugin ('TEXT', \&add); +register_plugin ('ACTION', \&add); +register_plugin ('TOPIC', \&add); +register_plugin ('OUTPUT', \&output); + +our $URLCache = Onis::Data::Persistent->new ('URLCache', 'url', qw(counter lastusedtime lastusedby)); +our $URLData = []; + +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'}; + + while ($text =~ m#(?:(?:ftp|https?)://|www\.)[\w\.-]+\.[A-Za-z]{2,4}(?::\d+)?(?:/[\w\d\.\%\/\-\~]*(?:\?[\+\w\&\%\=]+)?)?(?=\W|$)#ig) + { + my $match = $&; + + if ($match =~ m/^www/) { $match = 'http://' . $match; } + if ($match !~ m#://[^/]+/#) { $match .= '/'; } + + my ($counter) = $URLCache->get ($match); + $counter ||= 0; + $counter++; + $URLCache->put ($match, $counter, $time, $nick); + } +} + +sub calculate +{ + my $max = 10; + my @data = (); + if (get_config ('plugin_max')) + { + my $tmp = get_config ('plugin_max'); + $tmp =~ s/\D//g; + + $max = $tmp if ($tmp); + } + + for ($URLCache->keys ()) + { + my $url = $_; + my ($counter, $lastusedtime, $lastusedby) = $URLCache->get ($url); + die unless (defined ($lastusedby)); + + $lastusedby = get_main_nick ($lastusedby); + push (@data, [$url, $counter, $lastusedby, $lastusedtime]); + } + + @$URLData = sort { $b->[1] <=> $a->[1] } (@data); + splice (@$URLData, $max); +} + +sub output +{ + calculate (); + + my $fh = get_filehandle (); + + my $url = translate ('URL'); + my $times = translate ('Times used'); + my $last = translate ('Last used by'); + + print $fh < + +   + $url + $times + $last + +EOF + my $i = 0; + foreach (@$URLData) + { + $i++; + my ($url, $count, $usedby) = @$_; + my $name = nick_to_name ($usedby) || $usedby; + + $url = html_escape ($url); + + print $fh " \n", + qq# $i\n#, + qq# $url\n#, + qq# $count\n#, + qq# $usedby\n#, + qq# \n#; + } + + print $fh "\n\n"; +} diff --git a/lib/Onis/Plugins/Words.pm b/lib/Onis/Plugins/Words.pm index 7171b75..65c1f7b 100644 --- a/lib/Onis/Plugins/Words.pm +++ b/lib/Onis/Plugins/Words.pm @@ -6,7 +6,7 @@ use warnings; use Onis::Config (qw(get_config)); use Onis::Html (qw(get_filehandle)); use Onis::Language (qw(translate)); -use Onis::Data::Core (qw(register_plugin)); +use Onis::Data::Core (qw(register_plugin get_main_nick)); use Onis::Data::Persistent (); use Onis::Users (qw(nick_to_name)); @@ -49,7 +49,6 @@ sub add my ($counter) = $WordCache->get ($word); $counter ||= 0; $counter++; - $WordCache->put ($word, $counter, $time, $nick); } } @@ -84,7 +83,7 @@ sub calculate push (@data, [$word, $counter, $nick, $lastusedtime]); } - @$WordData = sort { $b->[0] <=> $a->[0] } (@data); + @$WordData = sort { $b->[1] <=> $a->[1] } (@data); splice (@$WordData, $max); } -- 2.11.0