1package Onis::Plugins::Words; 2 3use strict; 4use warnings; 5 6use Onis::Config (qw(get_config)); 7use Onis::Html (qw(get_filehandle)); 8use Onis::Language (qw(translate)); 9use Onis::Data::Core (qw(register_plugin get_main_nick nick_to_ident nick_to_name)); 10use Onis::Data::Persistent (); 11 12register_plugin ('TEXT', \&add); 13register_plugin ('ACTION', \&add); 14register_plugin ('OUTPUT', \&output); 15 16our $WordCache = Onis::Data::Persistent->new ('WordCache', 'word', qw(counter lastusedtime lastusedby)); 17our $WordData = []; 18 19our $MIN_LENGTH = 5; 20 21if (get_config ('ignore_words')) 22{ 23 my $tmp = get_config ('ignore_words'); 24 $tmp =~ s/\D//g; 25 26 $MIN_LENGTH = $tmp if ($tmp); 27} 28 29my $VERSION = '$Id$'; 30print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG); 31 32return (1); 33 34sub add 35{ 36 my $data = shift; 37 my $text = $data->{'text'}; 38 my $nick = $data->{'nick'}; 39 my $words = $data->{'words'}; 40 my $time = $data->{'epoch'}; 41 42 for (@$words) 43 { 44 my $word = lc ($_); 45 46 next if (length ($word) < $MIN_LENGTH); 47 48 my ($counter) = $WordCache->get ($word); 49 $counter ||= 0; 50 $counter++; 51 $WordCache->put ($word, $counter, $time, $nick); 52 } 53} 54 55sub calculate 56{ 57 my $max = 10; 58 my @data = (); 59 if (get_config ('plugin_max')) 60 { 61 my $tmp = get_config ('plugin_max'); 62 $tmp =~ s/\D//g; 63 64 $max = $tmp if ($tmp); 65 } 66 67 for ($WordCache->keys ()) 68 { 69 my $word = $_; 70 my $ident = nick_to_ident ($word); 71 72 if ($ident) 73 { 74 $WordCache->del ($word); 75 next; 76 } 77 78 my ($counter, $lastusedtime, $lastusedby) = $WordCache->get ($word); 79 die unless (defined ($lastusedby)); 80 81 my $nick = get_main_nick ($lastusedby); 82 push (@data, [$word, $counter, $nick, $lastusedtime]); 83 } 84 85 @$WordData = sort { $b->[1] <=> $a->[1] } (@data); 86 splice (@$WordData, $max) if (scalar (@$WordData) > $max); 87} 88 89sub output 90{ 91 calculate (); 92 return (undef) unless (@$WordData); 93 94 my $fh = get_filehandle (); 95 96 my $word = translate ('Word'); 97 my $times = translate ('Times used'); 98 my $last = translate ('Last used by'); 99 100 print $fh <<EOF; 101<table class="plugin"> 102 <tr> 103 <td class="invis"> </td> 104 <th>$word</th> 105 <th>$times</th> 106 <th>$last</th> 107 </tr> 108EOF 109 110 my $i = 0; 111 for (@$WordData) 112 { 113 $i++; 114 115 my ($word, $count, $nick) = @$_; 116 my $name = nick_to_name ($nick) || $nick; 117 118 print $fh " <tr>\n", 119 qq# <td class="numeration">$i</td>\n#, 120 qq# <td>$word</td>\n#, 121 qq# <td>$count</td>\n#, 122 qq# <td class="nick">$name</td>\n#, 123 qq# </tr>\n#; 124 } 125 print $fh "</table>\n\n"; 126 127 return (1); 128} 129