summaryrefslogtreecommitdiffstats
path: root/scripts/verstats.pl
blob: c40195c23ff6df29bc13f93ad8303fdbd9eea855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use strict;

use vars qw($VERSION %IRSSI);
$VERSION = "20030208";
%IRSSI = (
    authors     => "Stefan 'tommie' Tomanek",
    contact     => "stefan\@pico.ruhr.de",
    name        => "VerStats",
    description => "Draws a diagram of the used clients in a channel",
    license     => "GPLv2",
    url         => "http://scripts.irssi.org",
    changed     => "$VERSION",
    commands	=> "verstats"
);


use Irssi;

use vars qw(%clients $timeout);

sub draw_box ($$$$) {
    my ($title, $text, $footer, $colour) = @_;
    my $box = '';
    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
    foreach (split(/\n/, $text)) {
        $box .= '%R|%n '.$_."\n";
    }
    $box .= '%R`--<%n'.$footer.'%R>->%n';
    $box =~ s/%.//g unless $colour;
    return $box;
}   


sub sig_ctcp_reply_version ($$$$$) {
    my ($server, $args, $nick, $addr, $target) = @_;
    return unless $timeout;
    Irssi::timeout_remove($timeout);
    if ($args =~ /^(.*?)( |\/|$)/) {
	my $client = lc($1);
	$client =~ s/^[^\w]//;
	$client =~ s/%.//g;
	#$clients{$client} = 0 unless defined $clients{$client};
	push @{$clients{$client}}, $nick;
    }
    $timeout = Irssi::timeout_add(5000, \&finished, undef);
}

sub finished {
    my $max=0;
    foreach (keys %clients) {
	$max = @{$clients{$_}} if $max < @{$clients{$_}};
    }
    return if $max == 0;
    my $width = 60;
    my $block = $width/$max;
    my $text;
    foreach (sort {@{$clients{$b}} <=> @{$clients{$a}}} keys %clients) {
	s/%/%%/g;
	$text .= "'".$_."'".': '.@{$clients{$_}}."\n";
	my $bar = '#'x(($block * @{$clients{$_}})-1);
	$text .= $bar.">\n";
	#$text .= $_.' ' foreach (@{$clients{$_}});
	#$text .= "\n";
    }
    %clients = ();
    print CLIENTCRAP draw_box('VerStats', $text, 'stats', 1);
    Irssi::timeout_remove($timeout);
    $timeout = undef;
}

sub cmd_verstats ($$$) {
    my ($args, $server, $witem) = @_;
    return unless ($server && ref $witem && $witem->{type} eq 'CHANNEL');
    $witem->command('ctcp '.$witem->{name}.' version');
    $timeout = Irssi::timeout_add(5000, \&finished, undef)
}

Irssi::signal_add('ctcp reply version' => \&sig_ctcp_reply_version);
Irssi::command_bind('verstats' => \&cmd_verstats);

print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded';