1 package Onis::Plugins::Conversations;
8 use Onis::Config qw(get_config);
9 use Onis::Html qw(get_filehandle);
10 use Onis::Language qw(translate);
11 use Onis::Data::Core qw(register_plugin get_main_nick nick_to_ident nick_to_name);
12 use Onis::Data::Persistent ();
16 Onis::Plugins::Conversations - Who talks with who
20 This plugins tries to recignise conversations and counts the amount that people
25 @Onis::Plugins::Conversations::EXPORT_OK = (qw(get_conversations));
26 @Onis::Plugins::Conversations::ISA = ('Exporter');
28 our $ConversationCache = Onis::Data::Persistent->new ('ConversationCache', 'partners', qw(time0 time1 time2 time3));
29 our $ConversationData = {};
31 our @HorizontalImages = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
33 if (get_config ('horizontal_images'))
35 my @tmp = get_config ('horizontal_images');
38 if (scalar (@tmp) != 4)
40 print STDERR $/, __FILE__, ": The number of horizontal images is not four. The output might look weird.", $/;
43 for ($i = 0; $i < 4; $i++)
45 next unless (defined ($tmp[$i]));
46 $HorizontalImages[$i] = $tmp[$i];
50 our $NumConversations = 10;
51 if (get_config ('conversations_number'))
53 my $tmp = get_config ('conversations_number');
55 $NumConversations = $tmp if ($tmp);
58 register_plugin ('TEXT', \&add);
59 register_plugin ('OUTPUT', \&output);
61 my $VERSION = '$Id: Conversations.pm,v 1.7 2004/09/15 19:42:04 octo Exp $';
62 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
69 my $text = $data->{'text'};
70 my $nick = $data->{'nick'};
71 my $ident = $data->{'ident'};
73 my $time = int ($data->{'hour'} / 6);
75 # <taken from lib/Onis/Plugins/Nicks.pm>
76 my @potential_nicks = split (/[^\w\`\~\^\-\|\[\]]+/, $text);
79 for (@potential_nicks)
82 my $other_ident = nick_to_ident ($other_nick);
86 $talk_to = $other_nick;
94 my $key = "$nick:$talk_to";
95 my @data = $ConversationCache->get ($key);
96 @data = (0, 0, 0, 0) unless (@data);
98 my $chars = length ($text);
100 $data[$time] += $chars;
102 $ConversationCache->put ($key, @data);
108 for ($ConversationCache->keys ())
111 my ($nick_from, $nick_to) = split (m/:/, $key);
112 my @data = $ConversationCache->get ($key);
114 $nick_from = get_main_nick ($nick_from);
115 $nick_to = get_main_nick ($nick_to);
117 next if (!$nick_from or !$nick_to);
118 next if ($nick_from eq $nick_to);
120 if (!defined ($ConversationData->{$nick_from}{$nick_to}))
122 $ConversationData->{$nick_from}{$nick_to} =
127 $nick_from => [0, 0, 0, 0],
128 $nick_to => [0, 0, 0, 0]
131 $ConversationData->{$nick_to}{$nick_from} = $ConversationData->{$nick_from}{$nick_to};
134 for (my $i = 0; $i < 4; $i++)
136 $ConversationData->{$nick_from}{$nick_to}{'nicks'}{$nick_from}[$i] += $data[$i];
137 $ConversationData->{$nick_from}{$nick_to}{'total'} += $data[$i];
147 for (keys %$ConversationData)
151 for (keys %{$ConversationData->{$nick0}})
154 next unless ($nick0 lt $nick1);
156 push (@data, [$ConversationData->{$nick0}{$nick1}{'total'}, $nick0, $nick1]);
160 @data = sort { $b->[0] <=> $a->[0] } (@data);
161 splice (@data, $num) if (scalar (@data) > $num);
170 my $fh = get_filehandle ();
171 my $title = translate ('Conversation partners');
175 my @data = get_top ($NumConversations);
176 return (undef) unless (@data);
182 my $rec = $ConversationData->{$nick0}{$nick1};
187 for (my $i = 0; $i < 4; $i++)
189 $sum0 += $rec->{'nicks'}{$nick0}[$i];
190 $sum1 += $rec->{'nicks'}{$nick1}[$i];
193 $max_num = $sum0 if ($max_num < $sum0);
194 $max_num = $sum1 if ($max_num < $sum1);
198 <table class="plugin conversations">
200 <th colspan="2">$title</th>
207 my $name0 = nick_to_name ($nick0) || $nick0;
208 my $name1 = nick_to_name ($nick1) || $nick1;
209 my $rec = $ConversationData->{$nick0}{$nick1};
213 <td class="nick left">$name0</td>
214 <td class="nick right">$name1</td>
219 print $fh ' <td class="bar horizontal left">';
220 for (my $i = 3; $i >= 0; $i--)
222 my $width = sprintf ("%.2f", 95 * $rec->{'nicks'}{$nick0}[$i] / $max_num);
223 my $image = $HorizontalImages[$i];
226 if ($i == 3) { $class = qq# class="first"#; }
227 elsif ($i == 0) { $class = qq# class="last"#; }
229 print $fh qq#<img src="$image" style="width: $width%;"$class alt="" />#;
231 print $fh qq#</td>\n <td class="bar horizontal right">#;
232 for (my $i = 0; $i < 4; $i++)
234 my $width = sprintf ("%.2f", 95 * $rec->{'nicks'}{$nick1}[$i] / $max_num);
235 my $image = $HorizontalImages[$i];
238 if ($i == 0) { $class = qq# class="first"#; }
239 elsif ($i == 3) { $class = qq# class="last"#; }
241 print $fh qq#<img src="$image" style="width: $width%;"$class alt="" />#;
243 print $fh "</td>\n </tr>\n";
246 print $fh "</table>\n\n";
249 =head1 EXPORTED FUNCTIONS
253 =item B<get_conversations> (I<$nick>)
255 Returns a hashref to the conversations this nick was involved with. The layout
256 is the following. I<$other> is the nick of the person I<$nick> chattet with.
257 The arrays hold the number of characters written by the nick used as the key.
258 The first field contains the characters for the hours 0-5, the second field
259 holds 6-11 and so on.
267 $nick => [0, 0, 0, 0],
268 $other => [0, 0, 0, 0]
276 sub get_conversations
280 if (!defined ($ConversationData->{$nick}))
286 return ($ConversationData->{$nick});
294 Florian octo Forster, E<lt>octo at verplant.orgE<gt>