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::Users (qw(ident_to_name));
13 use Onis::Data::Persistent;
17 Onis::Plugins::Conversations - Who talks with who
21 This plugins tries to recignise conversations and counts the amount that people
26 @Onis::Plugins::Conversations::EXPORT_OK = (qw(get_conversations));
27 @Onis::Plugins::Conversations::ISA = ('Exporter');
29 our $ConversationCache = Onis::Data::Persistent->new ('ConversationCache', 'partners', qw(time0 time1 time2 time3));
30 our $ConversationData = {};
32 our @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
35 if (get_config ('horizontal_images'))
37 my @tmp = get_config ('horizontal_images');
40 if (scalar (@tmp) != 4)
42 print STDERR $/, __FILE__, ": The number of horizontal images is not four. The output might look weird.", $/;
45 for ($i = 0; $i < 4; $i++)
47 next unless (defined ($tmp[$i]));
48 $H_IMAGES[$i] = $tmp[$i];
51 if (get_config ('bar_width'))
53 my $tmp = get_config ('bar_width');
55 $BAR_WIDTH = 2 * $tmp if ($tmp >= 10);
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');
176 my @img = get_config ('horizontal_images');
179 my @data = get_top (10);
180 return (undef) unless (@data);
186 my $rec = $ConversationData->{$nick0}{$nick1};
191 for (my $i = 0; $i < 4; $i++)
193 $sum0 += $rec->{'nicks'}{$nick0}[$i];
194 $sum1 += $rec->{'nicks'}{$nick1}[$i];
197 $max_num = $sum0 if ($max_num < $sum0);
198 $max_num = $sum1 if ($max_num < $sum1);
201 $factor = $BAR_WIDTH / $max_num;
204 <table class="plugin conversations">
206 <th colspan="2">$title</th>
213 my $name0 = nick_to_name ($nick0) || $nick0;
214 my $name1 = nick_to_name ($nick1) || $nick1;
215 my $rec = $ConversationData->{$nick0}{$nick1};
219 <td class="nick left">$name0</td>
220 <td class="nick right">$name1</td>
225 print $fh ' <td class="bar left">';
229 my $w = int (0.5 + ($rec->{'nicks'}{$nick0}[$_] * $factor));
235 if ($_ == 3) { $c = qq# class="first"#; }
236 elsif ($_ == 0) { $c = qq# class="last"#; }
238 print $fh qq#<img src="$i" style="width: $w;"$c alt="" />#;
241 print $fh qq#</td>\n <td class="bar right">#;
246 my $w = int (0.5 + ($rec->{'nicks'}{$nick1}[$_] * $factor));
252 if ($_ == 0) { $c = qq# class="first"#; }
253 elsif ($_ == 3) { $c = qq# class="last"#; }
255 print $fh qq#<img src="$i" style="width: $w;"$c alt=""/>#;
257 print $fh "</td>\n </tr>\n";
260 print $fh "</table>\n\n";
263 =head1 EXPORTED FUNCTIONS
267 =item B<get_conversations> (I<$nick>)
269 Returns a hashref to the conversations this nick was involved with. The layout
270 is the following. I<$other> is the nick of the person I<$nick> chattet with.
271 The arrays hold the number of characters written by the nick used as the key.
272 The first field contains the characters for the hours 0-5, the second field
273 holds 6-11 and so on.
281 $nick => [0, 0, 0, 0],
282 $other => [0, 0, 0, 0]
290 sub get_conversations
294 if (!defined ($ConversationData->{$nick}))
300 return ($ConversationData->{$nick});
308 Florian octo Forster, E<lt>octo at verplant.orgE<gt>