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 @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
34 if (get_config ('horizontal_images'))
36 my @tmp = get_config ('horizontal_images');
39 if (scalar (@tmp) != 4)
41 print STDERR $/, __FILE__, ": The number of horizontal images is not four. The output might look weird.", $/;
44 for ($i = 0; $i < 4; $i++)
46 next unless (defined ($tmp[$i]));
47 $H_IMAGES[$i] = $tmp[$i];
50 if (get_config ('bar_width'))
52 my $tmp = get_config ('bar_width');
54 $BAR_WIDTH = 2 * $tmp if ($tmp >= 10);
57 register_plugin ('TEXT', \&add);
58 register_plugin ('OUTPUT', \&output);
60 my $VERSION = '$Id: Conversations.pm,v 1.7 2004/09/15 19:42:04 octo Exp $';
61 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
68 my $text = $data->{'text'};
69 my $nick = $data->{'nick'};
70 my $ident = $data->{'ident'};
72 my $time = int ($data->{'hour'} / 6);
74 # <taken from lib/Onis/Plugins/Nicks.pm>
75 my @potential_nicks = split (/[^\w\`\~\^\-\|\[\]]+/, $text);
78 for (@potential_nicks)
81 my $other_ident = nick_to_ident ($other_nick);
85 $talk_to = $other_nick;
93 my $key = "$nick:$talk_to";
94 my @data = $ConversationCache->get ($key);
95 @data = (0, 0, 0, 0) unless (@data);
97 my $chars = length ($text);
99 $data[$time] += $chars;
101 $ConversationCache->put ($key, @data);
107 for ($ConversationCache->keys ())
110 my ($nick_from, $nick_to) = split (m/:/, $key);
111 my @data = $ConversationCache->get ($key);
113 $nick_from = get_main_nick ($nick_from);
114 $nick_to = get_main_nick ($nick_to);
116 next if (!$nick_from or !$nick_to);
117 next if ($nick_from eq $nick_to);
119 if (!defined ($ConversationData->{$nick_from}{$nick_to}))
121 $ConversationData->{$nick_from}{$nick_to} =
126 $nick_from => [0, 0, 0, 0],
127 $nick_to => [0, 0, 0, 0]
130 $ConversationData->{$nick_to}{$nick_from} = $ConversationData->{$nick_from}{$nick_to};
133 for (my $i = 0; $i < 4; $i++)
135 $ConversationData->{$nick_from}{$nick_to}{'nicks'}{$nick_from}[$i] += $data[$i];
136 $ConversationData->{$nick_from}{$nick_to}{'total'} += $data[$i];
146 for (keys %$ConversationData)
150 for (keys %{$ConversationData->{$nick0}})
153 next unless ($nick0 lt $nick1);
155 push (@data, [$ConversationData->{$nick0}{$nick1}{'total'}, $nick0, $nick1]);
159 @data = sort { $b->[0] <=> $a->[0] } (@data);
160 splice (@data, $num) if (scalar (@data) > $num);
169 my $fh = get_filehandle ();
170 my $title = translate ('Conversation partners');
175 my @img = get_config ('horizontal_images');
178 my @data = get_top (10);
179 return (undef) unless (@data);
185 my $rec = $ConversationData->{$nick0}{$nick1};
190 for (my $i = 0; $i < 4; $i++)
192 $sum0 += $rec->{'nicks'}{$nick0}[$i];
193 $sum1 += $rec->{'nicks'}{$nick1}[$i];
196 $max_num = $sum0 if ($max_num < $sum0);
197 $max_num = $sum1 if ($max_num < $sum1);
200 $factor = $BAR_WIDTH / $max_num;
203 <table class="plugin conversations">
205 <th colspan="2">$title</th>
212 my $name0 = nick_to_name ($nick0) || $nick0;
213 my $name1 = nick_to_name ($nick1) || $nick1;
214 my $rec = $ConversationData->{$nick0}{$nick1};
218 <td class="nick left">$name0</td>
219 <td class="nick right">$name1</td>
224 print $fh ' <td class="bar left">';
228 my $w = int (0.5 + ($rec->{'nicks'}{$nick0}[$_] * $factor));
234 if ($_ == 3) { $c = qq# class="first"#; }
235 elsif ($_ == 0) { $c = qq# class="last"#; }
237 print $fh qq#<img src="$i" style="width: $w;"$c alt="" />#;
240 print $fh qq#</td>\n <td class="bar right">#;
245 my $w = int (0.5 + ($rec->{'nicks'}{$nick1}[$_] * $factor));
251 if ($_ == 0) { $c = qq# class="first"#; }
252 elsif ($_ == 3) { $c = qq# class="last"#; }
254 print $fh qq#<img src="$i" style="width: $w;"$c alt=""/>#;
256 print $fh "</td>\n </tr>\n";
259 print $fh "</table>\n\n";
262 =head1 EXPORTED FUNCTIONS
266 =item B<get_conversations> (I<$nick>)
268 Returns a hashref to the conversations this nick was involved with. The layout
269 is the following. I<$other> is the nick of the person I<$nick> chattet with.
270 The arrays hold the number of characters written by the nick used as the key.
271 The first field contains the characters for the hours 0-5, the second field
272 holds 6-11 and so on.
280 $nick => [0, 0, 0, 0],
281 $other => [0, 0, 0, 0]
289 sub get_conversations
293 if (!defined ($ConversationData->{$nick}))
299 return ($ConversationData->{$nick});
307 Florian octo Forster, E<lt>octo at verplant.orgE<gt>