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;
15 @Onis::Plugins::Conversations::EXPORT_OK = (qw(get_conversations));
16 @Onis::Plugins::Conversations::ISA = ('Exporter');
18 our $ConversationCache = Onis::Data::Persistent->new ('ConversationCache', 'partners', qw(time0 time1 time2 time3));
19 our $ConversationData = {};
21 our @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
24 if (get_config ('horizontal_images'))
26 my @tmp = get_config ('horizontal_images');
29 if (scalar (@tmp) != 4)
31 print STDERR $/, __FILE__, ": The number of horizontal images is not four. The output might look weird.", $/;
34 for ($i = 0; $i < 4; $i++)
36 next unless (defined ($tmp[$i]));
37 $H_IMAGES[$i] = $tmp[$i];
40 if (get_config ('bar_width'))
42 my $tmp = get_config ('bar_width');
44 $BAR_WIDTH = 2 * $tmp if ($tmp >= 10);
47 register_plugin ('TEXT', \&add);
48 register_plugin ('OUTPUT', \&output);
50 my $VERSION = '$Id: Conversations.pm,v 1.7 2004/09/15 19:42:04 octo Exp $';
51 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
58 my $text = $data->{'text'};
59 my $nick = $data->{'nick'};
60 my $ident = $data->{'ident'};
62 my $time = int ($data->{'hour'} / 6);
64 # <taken from lib/Onis/Plugins/Nicks.pm>
65 my @potential_nicks = split (/[^\w\`\~\^\-\|\[\]]+/, $text);
68 for (@potential_nicks)
71 my $other_ident = nick_to_ident ($other_nick);
75 $talk_to = $other_nick;
83 my $key = "$nick:$talk_to";
84 my @data = $ConversationCache->get ($key);
85 @data = (0, 0, 0, 0) unless (@data);
87 my $chars = length ($text);
89 $data[$time] += $chars;
91 $ConversationCache->put ($key, @data);
97 for ($ConversationCache->keys ())
100 my ($nick_from, $nick_to) = split (m/:/, $key);
101 my @data = $ConversationCache->get ($key);
103 $nick_from = get_main_nick ($nick_from);
104 $nick_to = get_main_nick ($nick_to);
106 next if (!$nick_from or !$nick_to);
107 next if ($nick_from eq $nick_to);
109 if (!defined ($ConversationData->{$nick_from}{$nick_to}))
111 $ConversationData->{$nick_from}{$nick_to} =
116 $nick_from => [0, 0, 0, 0],
117 $nick_to => [0, 0, 0, 0]
120 $ConversationData->{$nick_to}{$nick_from} = $ConversationData->{$nick_from}{$nick_to};
123 for (my $i = 0; $i < 4; $i++)
125 $ConversationData->{$nick_from}{$nick_to}{'nicks'}{$nick_from}[$i] += $data[$i];
126 $ConversationData->{$nick_from}{$nick_to}{'total'} += $data[$i];
136 for (keys %$ConversationData)
140 for (keys %{$ConversationData->{$nick0}})
143 next unless ($nick0 lt $nick1);
145 push (@data, [$ConversationData->{$nick0}{$nick1}{'total'}, $nick0, $nick1]);
149 @data = sort { $b->[0] <=> $a->[0] } (@data);
150 splice (@data, $num) if (scalar (@data) > $num);
159 my $fh = get_filehandle ();
160 my $title = translate ('Conversation partners');
165 my @img = get_config ('horizontal_images');
168 my @data = get_top (10);
169 return (undef) unless (@data);
175 my $rec = $ConversationData->{$nick0}{$nick1};
180 for (my $i = 0; $i < 4; $i++)
182 $sum0 += $rec->{'nicks'}{$nick0}[$i];
183 $sum1 += $rec->{'nicks'}{$nick1}[$i];
186 $max_num = $sum0 if ($max_num < $sum0);
187 $max_num = $sum1 if ($max_num < $sum1);
190 $factor = $BAR_WIDTH / $max_num;
193 <table class="plugin conversations">
195 <th colspan="2">$title</th>
202 my $name0 = nick_to_name ($nick0) || $nick0;
203 my $name1 = nick_to_name ($nick1) || $nick1;
204 my $rec = $ConversationData->{$nick0}{$nick1};
208 <td class="nick left">$name0</td>
209 <td class="nick right">$name1</td>
214 print $fh ' <td class="bar left">';
218 my $w = int (0.5 + ($rec->{'nicks'}{$nick0}[$_] * $factor));
224 if ($_ == 3) { $c = qq# class="first"#; }
225 elsif ($_ == 0) { $c = qq# class="last"#; }
227 print $fh qq#<img src="$i" style="width: $w;"$c alt="" />#;
230 print $fh qq#</td>\n <td class="bar right">#;
235 my $w = int (0.5 + ($rec->{'nicks'}{$nick1}[$_] * $factor));
241 if ($_ == 0) { $c = qq# class="first"#; }
242 elsif ($_ == 3) { $c = qq# class="last"#; }
244 print $fh qq#<img src="$i" style="width: $w;"$c alt=""/>#;
246 print $fh "</td>\n </tr>\n";
249 print $fh "</table>\n\n";
252 sub get_conversations
256 if (!defined ($ConversationData->{$nick}))
262 return ($ConversationData->{$nick});