Fixes this bug:
[onis.git] / lib / Onis / Plugins / Urls.pm
1 package Onis::Plugins::Urls;
2
3 use strict;
4 use warnings;
5
6 use Onis::Config (qw(get_config));
7 use Onis::Html (qw(html_escape get_filehandle));
8 use Onis::Language (qw(translate));
9 use Onis::Data::Core (qw(register_plugin get_main_nick nick_to_name));
10 use Onis::Data::Persistent ();
11
12 register_plugin ('TEXT', \&add);
13 register_plugin ('ACTION', \&add);
14 register_plugin ('TOPIC', \&add);
15 register_plugin ('OUTPUT', \&output);
16
17 our $URLCache = Onis::Data::Persistent->new ('URLCache', 'url', qw(counter lastusedtime lastusedby));
18 our $URLData = [];
19
20 my $VERSION = '$Id$';
21 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
22
23 return (1);
24
25 sub add
26 {
27         my $data = shift;
28         my $text = $data->{'text'};
29         my $nick = $data->{'nick'};
30         my $time = $data->{'epoch'};
31         
32         while ($text =~ m#(?:(?:ftp|https?)://|www\.)[\w\.-]+\.[A-Za-z]{2,4}(?::\d+)?(?:/[\w\d\.\%\/\-\~]*(?:\?[\+\w\&\%\=]+)?)?(?=\W|$)#ig)
33         {
34                 my $match = $&;
35
36                 if ($match =~ m/^www/) { $match = 'http://' . $match; }
37                 if ($match !~ m#://[^/]+/#) { $match .= '/'; }
38                 
39                 my ($counter) = $URLCache->get ($match);
40                 $counter ||= 0;
41                 $counter++;
42                 $URLCache->put ($match, $counter, $time, $nick);
43         }
44 }
45
46 sub calculate
47 {
48         my $max = 10;
49         my @data = ();
50         if (get_config ('plugin_max'))
51         {
52                 my $tmp = get_config ('plugin_max');
53                 $tmp =~ s/\D//g;
54
55                 $max = $tmp if ($tmp);
56         }
57
58         for ($URLCache->keys ())
59         {
60                 my $url = $_;
61                 my ($counter, $lastusedtime, $lastusedby) = $URLCache->get ($url);
62                 die unless (defined ($lastusedby));
63
64                 $lastusedby = get_main_nick ($lastusedby);
65                 push (@data, [$url, $counter, $lastusedby, $lastusedtime]);
66         }
67
68         @$URLData = sort { $b->[1] <=> $a->[1] } (@data);
69         splice (@$URLData, $max) if (scalar (@$URLData) > $max);
70 }
71
72 sub output
73 {
74         calculate ();
75
76         my $fh = get_filehandle ();
77
78         my $url = translate ('URL');
79         my $times = translate ('Times used');
80         my $last = translate ('Last used by');
81         
82         print $fh <<EOF;
83 <table class="plugin urls">
84   <tr>
85     <td class="invis">&nbsp;</td>
86     <th>$url</th>
87     <th>$times</th>
88     <th>$last</th>
89   </tr>
90 EOF
91         my $i = 0;
92         foreach (@$URLData)
93         {
94                 $i++;
95                 my ($url, $count, $usedby) = @$_;
96                 my $name = nick_to_name ($usedby) || $usedby;
97
98                 $url = html_escape ($url);
99                 
100                 print $fh "  <tr>\n",
101                 qq#    <td class="numeration">$i</td>\n#,
102                 qq#    <td>$url</td>\n#,
103                 qq#    <td>$count</td>\n#,
104                 qq#    <td>$usedby</td>\n#,
105                 qq#  </tr>\n#;
106         }
107
108         print $fh "</table>\n\n";
109 }