1use strict;
2use vars qw($VERSION %IRSSI);
3
4# This script assumes all windows have the same width, which will
5# practically always be true.
6
7use Irssi qw(active_win command_bind);
8$VERSION = '1.20';
9%IRSSI = (
10    authors	=> 'Juerd',
11    contact	=> 'juerd@juerd.nl',
12    name	=> 'Script Information',
13    description	=> 'Access script information',
14    license	=> 'Public Domain',
15    url		=> 'http://juerd.nl/irssi/',
16    changed	=> 'Tue Mar 19 11:00 CET 2002',
17);
18
19sub iprint {
20    Irssi::print(join('', @_), MSGLEVEL_CRAP);
21}
22
23command_bind 'script info' => sub {
24    my ($data, $server) = @_;
25    if ($data !~ /\S/) {
26        iprint 'Usage: /script info <scriptname>';
27        return;
28    }
29
30    no strict 'refs';
31    iprint "\c_== Script info for $data ==";
32
33    if (not exists $Irssi::Script::{ "${data}::" }) {
34        iprint 'Script is not loaded.';
35        return;
36    }
37
38    my %info = %{ "Irssi::Script::${data}::IRSSI" };
39    $info{version} = ${ "Irssi::Script::${data}::VERSION" };
40
41    if (join('', values %info) eq '') {
42	iprint 'Script has no $VERSION and no %IRSSI. ',
43	       'Please ask the author to read ',
44	       'http://juerd.nl/irssi/proposal.txt';
45
46	return;
47    }
48    my $max = 0;
49    length > $max and $max = length for keys %info;
50    my $width = active_win->{width} - 14 - $max;
51    s/([^\n]{$width})/$1\n/g      for values %info;
52    s/(?<=\n)/' ' x ($max + 2)/eg for values %info;
53    for (qw/name version description authors contact/) {
54        if (exists $info{$_}) {
55    	    iprint"\cC5$_\cC", ' ' x (2 + $max - length $_), $info{$_};
56    	    delete $info{$_};
57        }
58    }
59    for (sort keys %info) {
60        iprint "\cC5$_\cC", ' ' x (2 + $max - length $_), $info{$_};
61    }
62};
63
64command_bind 'script sv' => sub {
65    my ($data, $server) = @_;
66    if ($data !~ /\S/) {
67	iprint 'Usage: /script sv <scriptname>';
68        return;
69    }
70
71    no strict 'refs';
72    if (not exists $Irssi::Script::{ "${data}::" }) {
73        iprint 'Module is not loaded.';
74        return;
75    }
76
77    my $name    = ${ "Irssi::Script::${data}::IRSSI" }{name};
78    my $url     = ${ "Irssi::Script::${data}::IRSSI" }{url};
79    my $version = ${ "Irssi::Script::${data}::VERSION" };
80
81    my $text = "$name $version";
82    $text .= " - $url" if $url;
83
84    if ($text !~ /\S/) {
85        iprint 'Script has no information.';
86        return;
87    }
88
89    active_win->command("say $text");
90};
91
92command_bind 'script versions' => sub {
93    # Actually, upgrading them would be quite easy :)
94    # Update: Actually, it's possible now! use scriptadmin.pl :)
95    my ($data, $server) = @_;
96
97    no strict 'refs';
98    my @modules;
99    for (sort grep s/::$//, keys %Irssi::Script::) {
100        my $name    = ${ "Irssi::Script::${_}::IRSSI" }{name};
101	my $version = ${ "Irssi::Script::${_}::VERSION" };
102	push @modules, [$_, $name, $version] if $name && $version;
103    }
104    my @max;
105    for (@modules) {
106	my $i = -1;;
107	length > $max[++$i] and $max[$i] = length for @$_;
108    }
109    my $i;
110    my $text = join "\n", map {
111        $i = 0 ||
112        join ' ', map {
113	    $_ . ' ' x ($max[$i++] - length)
114	} @$_
115    } @modules;
116    iprint $text;
117};
118
119