1## Usage: /CWHO [-a | -l | -o | -v ] [ mask ]
2
3##  ver 1.1
4# - added sorting
5# - few fixes
6
7use Irssi 20020300;
8use strict;
9
10use vars qw($VERSION %IRSSI);
11$VERSION = "1.1";
12%IRSSI = (
13        authors         => "Maciek \'fahren\' Freudenheim",
14        contact         => "fahren\@bochnia.pl",
15        name            => "Cached WHO",
16        description     => "Usage: /CWHO [-a | -l | -o | -v ] [ mask ]",
17        license         => "GNU GPLv2 or later",
18        changed         => "Mon May  6 14:02:25 CEST 2002"
19);
20
21Irssi::theme_register([
22	'cwho_line', '%K[%W$[!-3]0%K][%C$1%B$[9]2%K][%B$[-10]3%P@%B$[34]4%K]%n'
23]);
24
25sub sort_mode {
26	if ($a->[0] eq $b->[0]) {
27		return 0;
28	} elsif ($a->[0] eq "@") {
29		return -1;
30	} elsif ($b->[0] eq "@") {
31		return 1;
32	} elsif ($a->[0] eq "v") {
33		return -1;
34	} elsif ($b->[0] eq "v") {
35		return 1
36	};
37}
38
39Irssi::command_bind 'cwho' => sub {
40	my ($pars, $server, $winit) = @_;
41	$pars =~ s/^\s+//;
42	my @data = split(/ +/, $pars);
43	my ($cmode, $cmask, $i) = ('.', "*!*@*", 0);
44
45	unless ($winit && $winit->{type} eq "CHANNEL") {
46	    Irssi::print("You don't have active channel in that window");
47	    return;
48	}
49
50	my $channel = $winit->{name};
51
52	while ($_ = shift(@data)) {
53		/^-a$/ and $cmode = '.', next;
54		/^-l$/ and $cmode = 'X', next;
55		/^-o$/ and $cmode = '@', next;
56		/^-v$/ and $cmode = 'v', next;
57		/[!@.]+/ and $cmask = $_, next;
58	}
59
60	my @sorted = ();
61	for my $hash ($winit->nicks()) {
62		my $mode = $hash->{op}? "@" : $hash->{voice}? "v" : " ";
63
64		if ($cmode eq "X") {
65			next if $mode ne " ";
66		} elsif ($mode !~ /$cmode/) {next}
67
68		next unless $server->mask_match_address($cmask, $hash->{nick}, $hash->{host});
69
70		my ($user, $host) = split(/@/, $hash->{host});
71		push @sorted, [ $mode, $hash->{nick}, $user, $host ];
72	}
73
74	@sorted = sort { sort_mode || lc $a->[1] cmp lc $b->[1] } @sorted;
75
76	$server->printformat($channel, MSGLEVEL_CLIENTCRAP, 'cwho_line', ++$i, @$_) for (@sorted);
77
78	Irssi::print("No matches for \'$cmask\'.") unless $i;
79}
80