html/verstats.pl


   1 use strict;
   2 
   3 use vars qw($VERSION %IRSSI);
   4 $VERSION = "20030208";
   5 %IRSSI = (
   6     authors     => "Stefan 'tommie' Tomanek",
   7     contact     => "stefan\@pico.ruhr.de",
   8     name        => "VerStats",
   9     description => "Draws a diagram of the used clients in a channel",
  10     license     => "GPLv2",
  11     url         => "http://scripts.irssi.org",
  12     changed     => "$VERSION",
  13     commands	=> "verstats"
  14 );
  15 
  16 
  17 use Irssi;
  18 
  19 use vars qw(%clients $timeout);
  20 
  21 sub draw_box ($$$$) {
  22     my ($title, $text, $footer, $colour) = @_;
  23     my $box = '';
  24     $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
  25     foreach (split(/\n/, $text)) {
  26         $box .= '%R|%n '.$_."\n";
  27     }
  28     $box .= '%R`--<%n'.$footer.'%R>->%n';
  29     $box =~ s/%.//g unless $colour;
  30     return $box;
  31 }   
  32 
  33 
  34 sub sig_ctcp_reply_version ($$$$$) {
  35     my ($server, $args, $nick, $addr, $target) = @_;
  36     return unless $timeout;
  37     Irssi::timeout_remove($timeout);
  38     if ($args =~ /^(.*?)( |\/|$)/) {
  39 	my $client = lc($1);
  40 	$client =~ s/^[^\w]//;
  41 	$client =~ s/%.//g;
  42 	#$clients{$client} = 0 unless defined $clients{$client};
  43 	push @{$clients{$client}}, $nick;
  44     }
  45     $timeout = Irssi::timeout_add(5000, \&finished, undef);
  46 }
  47 
  48 sub finished {
  49     my $max=0;
  50     foreach (keys %clients) {
  51 	$max = @{$clients{$_}} if $max < @{$clients{$_}};
  52     }
  53     return if $max == 0;
  54     my $width = 60;
  55     my $block = $width/$max;
  56     my $text;
  57     foreach (sort {@{$clients{$b}} <=> @{$clients{$a}}} keys %clients) {
  58 	s/%/%%/g;
  59 	$text .= "'".$_."'".': '.@{$clients{$_}}."\n";
  60 	my $bar = '#'x(($block * @{$clients{$_}})-1);
  61 	$text .= $bar.">\n";
  62 	#$text .= $_.' ' foreach (@{$clients{$_}});
  63 	#$text .= "\n";
  64     }
  65     %clients = ();
  66     print CLIENTCRAP draw_box('VerStats', $text, 'stats', 1);
  67     Irssi::timeout_remove($timeout);
  68     $timeout = undef;
  69 }
  70 
  71 sub cmd_verstats ($$$) {
  72     my ($args, $server, $witem) = @_;
  73     return unless ($server && ref $witem && $witem->{type} eq 'CHANNEL');
  74     $witem->command('ctcp '.$witem->{name}.' version');
  75     $timeout = Irssi::timeout_add(5000, \&finished, undef)
  76 }
  77 
  78 Irssi::signal_add('ctcp reply version' => \&sig_ctcp_reply_version);
  79 Irssi::command_bind('verstats' => \&cmd_verstats);
  80 
  81 print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded';