Onis::Plugins::{Userdetails,Interestingnumbers} have been looked at for a start....
[onis.git] / lib / Onis / Plugins / Interestingnumbers.pm
1 package Onis::Plugins::Interestingnumbers;
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(nick_to_ident register_plugin));
10
11 our $SOLILOQUIES = {};
12
13 register_plugin ('ACTION', \&add_action);
14 register_plugin ('JOIN', \&add_join);
15 register_plugin ('KICK', \&add_kick);
16 register_plugin ('MODE', \&add_mode);
17 register_plugin ('TEXT', \&add_text);
18 register_plugin ('OUTPUT', \&output);
19
20 our $InterestingNumbersCache = ('InterestingNumbersCache', 'nick', qw(actions joins kicks_given kicks_received modes soliloquies));
21
22 our $SOLILOQUIES_COUNT = 5;
23 if (get_config ('soliloquies_count'))
24 {
25         my $tmp = get_config ('soliloquies_count');
26         $tmp =~ s/\D//g;
27
28         $SOLILOQUIES_COUNT = $tmp if ($tmp);
29 }
30                 
31 my $VERSION = '$Id$';
32 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
33
34 return (1);
35
36 sub add_action
37 {
38         my $data = shift;
39         my $nick = $data->{'nick'};
40
41         my $ident = $data->{'ident'};
42         
43         $DATA->{'byident'}{$ident}{'actions'}++;
44         
45         return (1);
46 }
47
48 sub add_join
49 {
50         my $data = shift;
51
52         my $ident = $data->{'ident'};
53         
54         $DATA->{'byident'}{$ident}{'joins'}++;
55         
56         return (1);
57 }
58
59 sub add_kick
60 {
61         my $data = shift;
62
63         my $ident_give = $data->{'ident'};
64         my $ident_rcvt = nick_to_ident ($data->{'nick_received'});
65
66         $DATA->{'byident'}{$ident_give}{'kick_given'}++;
67
68         if ($ident_rcvt)
69         {
70                 $DATA->{'byident'}{$ident_rcvt}{'kick_received'}++;
71         }
72         
73         return (1);
74 }
75
76 sub add_mode
77 {
78         my $data = shift;
79
80         my $ident = $data->{'ident'};
81         my $text = $data->{'mode'};
82         
83         my ($mode) = split (m/\s+/, $text);
84         my $modifier = '';
85
86         for (split (m//, $mode))
87         {
88                 my $tmp = $_;
89                 if (($tmp eq '-') or ($tmp eq '+'))
90                 {
91                         $modifier = $tmp;
92                         next;
93                 }
94                 elsif (!$modifier)
95                 {
96                         next;
97                 }
98
99                 if ($tmp eq 'o')
100                 {
101                         if ($modifier eq '-')
102                         {
103                                 $DATA->{'byident'}{$ident}{'op_taken'}++;
104                         }
105                         else # ($modifier eq '+')
106                         {
107                                 $DATA->{'byident'}{$ident}{'op_given'}++;
108                         }
109                 }
110         }
111
112         return (1);
113 }
114
115 sub add_text
116 {
117         my $data = shift;
118
119         my $ident = $data->{'ident'};
120
121         if (!defined ($SOLILOQUIES->{'ident'}))
122         {
123                 $SOLILOQUIES->{'ident'} = $ident;
124                 $SOLILOQUIES->{'count'} = 1;
125         }
126         else
127         {
128                 if ($SOLILOQUIES->{'ident'} eq $ident)
129                 {
130                         my $count = ++$SOLILOQUIES->{'count'};
131                         if ($count == $SOLILOQUIES_COUNT)
132                         {
133                                 $DATA->{'byident'}{$ident}{'soliloquies'}++;
134                         }
135                 }
136                 else
137                 {
138                         $SOLILOQUIES->{'ident'} = $ident;
139                         $SOLILOQUIES->{'count'} = 1;
140                 }
141         }
142
143         return (1);
144 }
145
146 sub output
147 {
148         my $first;
149         my $second;
150
151         my $fh = get_filehandle ();
152
153         my $trans = translate ('Interesting Numbers');
154         
155         print $fh <<EOF;
156 <table class="plugin interestingnumbers">
157   <tr>
158     <th>$trans</th>
159   </tr>
160 EOF
161         ($first, $second) = sort_by_field ('kick_received');
162         if ($first)
163         {
164                 my $num = $DATA->{'byname'}{$first}{'kick_received'};
165                 $trans = translate ('kick_received0: %s %u');
166
167                 print $fh "  <tr>\n    <td>";
168                 printf $fh ($trans, $first, $num);
169                 
170                 if ($second)
171                 {
172                         $num = $DATA->{'byname'}{$second}{'kick_received'};
173                         $trans = translate ('kick_received1: %s %u');
174
175                         print $fh "<br />\n",
176                         qq#      <span class="small">#;
177                         printf $fh ($trans, $second, $num);
178                         print $fh '</span>';
179                 }
180                 
181                 print $fh "</td>\n  </tr>\n";
182         }
183
184         ($first, $second) = sort_by_field ('kick_given');
185         if ($first)
186         {
187                 my $num = $DATA->{'byname'}{$first}{'kick_given'};
188                 $trans = translate ('kick_given0: %s %u');
189
190                 print $fh "  <tr>\n    <td>";
191                 printf $fh ($trans, $first, $num);
192
193                 if ($second)
194                 {
195                         $num = $DATA->{'byname'}{$second}{'kick_given'};
196                         $trans = translate ('kick_given1: %s %u');
197
198                         print $fh "<br />\n",
199                         qq#      <span class="small">#;
200                         printf $fh ($trans, $second, $num);
201                         print $fh '</span>';
202                 }
203
204                 print $fh "</td>\n  </tr>\n";
205         }
206
207         ($first, $second) = sort_by_field ('op_given');
208         if ($first)
209         {
210                 my $num = $DATA->{'byname'}{$first}{'op_given'};
211                 $trans = translate ('op_given0: %s %u');
212
213                 print $fh "  <tr>\n    <td>";
214                 printf $fh ($trans, $first, $num);
215                 
216                 if ($second)
217                 {
218                         $num = $DATA->{'byname'}{$second}{'op_given'};
219                         $trans = translate ('op_given1: %s %u');
220
221                         print $fh "<br />\n",
222                         qq#      <span class="small">#;
223                         printf $fh ($trans, $second, $num);
224                         print $fh '</span>';
225                 }
226                 
227                 print $fh "</td>\n  </tr>\n";
228         }
229
230         ($first, $second) = sort_by_field ('op_taken');
231         if ($first)
232         {
233                 my $num = $DATA->{'byname'}{$first}{'op_taken'};
234                 $trans = translate ('op_taken0: %s %u');
235
236                 print $fh "  <tr>\n    <td>";
237                 printf $fh ($trans, $first, $num);
238                 
239                 if ($second)
240                 {
241                         $num = $DATA->{'byname'}{$second}{'op_taken'};
242                         $trans = translate ('op_taken1: %s %u');
243
244                         print $fh "<br />\n",
245                         qq#      <span class="small">#;
246                         printf $fh ($trans, $second, $num);
247                         print $fh '</span>';
248                 }
249                 
250                 print $fh "</td>\n  </tr>\n";
251         }
252
253         ($first, $second) = sort_by_field ('actions');
254         if ($first)
255         {
256                 my $num = $DATA->{'byname'}{$first}{'actions'};
257                 $trans = translate ('action0: %s %u');
258
259                 print $fh "  <tr>\n    <td>";
260                 printf $fh ($trans, $first, $num);
261                 
262                 if ($second)
263                 {
264                         $num = $DATA->{'byname'}{$second}{'actions'};
265                         $trans = translate ('action1: %s %u');
266
267                         print $fh "<br />\n",
268                         qq#      <span class="small">#;
269                         printf $fh ($trans, $second, $num);
270                         print $fh '</span>';
271                 }
272
273                 print $fh "</td>\n  </tr>\n";
274         }
275         
276         ($first, $second) = sort_by_field ('soliloquies');
277         if ($first)
278         {
279                 my $num = $DATA->{'byname'}{$first}{'soliloquies'};
280                 $trans = translate ('soliloquies0: %s %u');
281
282                 print $fh "  <tr>\n    <td>";
283                 printf $fh ($trans, $first, $num);
284                 
285                 if ($second)
286                 {
287                         $num = $DATA->{'byname'}{$second}{'soliloquies'};
288                         $trans = translate ('soliloquies1: %s %u');
289
290                         print $fh "<br />\n",
291                         qq#      <span class="small">#;
292                         printf $fh ($trans, $second, $num);
293                         print $fh '</span>';
294                 }
295
296                 print $fh "</td>\n  </tr>\n";
297         }
298         
299         ($first, $second) = sort_by_field ('joins');
300         if ($first)
301         {
302                 my $num = $DATA->{'byname'}{$first}{'joins'};
303                 $trans = translate ('joins0: %s %u');
304
305                 print $fh "  <tr>\n    <td>";
306                 printf $fh ($trans, $first, $num);
307                 
308                 if ($second)
309                 {
310                         $num = $DATA->{'byname'}{$second}{'joins'};
311                         $trans = translate ('joins1: %s %u');
312
313                         print $fh "<br />\n",
314                         qq#      <span class="small">#;
315                         printf $fh ($trans, $second, $num);
316                         print $fh '</span>';
317                 }
318
319                 print $fh "</td>\n  </tr>\n";
320         }
321
322         print $fh "</table>\n\n";
323 }
324
325 sub sort_by_field
326 {
327         my $field = shift;
328         
329         my @retval = sort
330         {
331                 $DATA->{'byname'}{$b}{$field}
332                 <=>
333                 $DATA->{'byname'}{$a}{$field}
334         } grep
335         {
336                 defined ($DATA->{'byname'}{$_}{$field})
337                         and defined ($DATA->{'byname'}{$_}{'lines'})
338                         and ($DATA->{'byname'}{$_}{'lines'} >= 100)
339         } (keys (%{$DATA->{'byname'}}));
340
341         while (scalar (@retval) < 2)
342         {
343                 push (@retval, '');
344         }
345         
346         return (@retval);
347 }