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