1################################################################################
2#
3# Usage: /cgrep <regexp>
4#
5# Shows all WHO records matching that regexp in a friendly yet complete format
6# Works on the active channel only
7#
8# This is a bit like c0ffee's ls command, except it matches ALL returned data.
9# Since IRSSI doe snot cache realnames properly, this script calls WHO once
10# and awaits the results.
11#
12# Also check out 'joininfo.pl' which shows lots of WHOIS info when a person
13# joins the channel.
14#
15# FORMAT SETTINGS:
16#   cgrep_match         Matching record
17#   cgrep_line          Start and end line format
18#
19################################################################################
20
21use strict;
22use Irssi;
23use vars qw($VERSION %IRSSI);
24use integer;
25
26$VERSION = "1.0.0";
27%IRSSI = (
28    authors => "Pieter-Bas IJdens",
29    contact => "irssi-scripts\@nospam.mi4.org.uk",
30    name    => "cgrep",
31    description => "Lists users on the channel matching the specified regexp",
32    license => "GPLv2 or later",
33    url     => "http://pieter-bas.ijdens.com/irssi/",
34    changed => "2005-03-10"
35);
36
37################################################################################
38
39my($busy) = 0;
40my($regexp) = "";
41my($results) = 0;
42my($debug) = 0;
43
44################################################################################
45
46sub run_who
47{
48    my($server, $channel) = @_;
49
50    $server->redirect_event(
51        "who",
52        1,
53        $channel,
54        0,
55        "redir who_default",
56        {
57            "event 352" => "redir cgrep_evt_who_result",
58            "event 315" => "redir cgrep_evt_who_end",
59            "" => "event empty"
60        }
61    );
62
63    $server->send_raw("WHO $channel");
64}
65
66################################################################################
67
68sub event_who_result
69{
70    my ($server, $data) = @_;
71
72    if ($busy)
73    {
74        my($start,$realname);
75        if ($data =~ /^(.*):([^:]{1,})$/)
76        {
77            $start = $1;
78            $realname = $2;
79        }
80        else
81        {
82            Irssi::print("$data can't be parsed");
83        }
84
85        # my($start,$realname) = split(":", $data);
86
87        my($me, $channel, $ident, $host, $server, $nick, $mode) =
88            split(" ", $start);
89        my($hops) = -1;
90
91        if ($realname =~ /^([0-9]{1,} )(.*$)$/i)
92        {
93            $hops = $1;
94            $realname = $2;
95
96            $hops =~ s/[ ]{1,}$//g;
97        }
98
99        my($string) = "$nick ($ident\@$host) \"$realname\" $channel "
100                    . "($server, $hops)";
101
102        if ($string =~ /$regexp/i)
103        {
104            Irssi::printformat(
105                MSGLEVEL_CLIENTCRAP,
106                'cgrep_match',
107                $nick,
108                "$ident\@$host",
109                "$realname",
110                $channel,
111                $server,
112                $hops
113            );
114
115            $results++;
116        }
117    }
118}
119
120################################################################################
121
122sub event_who_end
123{
124    my ($server, $data) = @_;
125
126    Irssi::printformat(
127        MSGLEVEL_CLIENTCRAP,
128        'cgrep_line',
129        "End of list. Found $results matches."
130    );
131
132    $busy = 0;
133    $regexp = "";
134    $results = 0;
135}
136
137################################################################################
138
139sub cmd_cgrep
140{
141    my ($data, $server, $window) = @_;
142
143    if (!$server)
144    {
145        Irssi::print("Not connected to a server in this window.");
146        return;
147    }
148    elsif ($window->{type} ne "CHANNEL")
149    {
150        Irssi::print("Not a channel window.");
151        return;
152    }
153    elsif ($busy)
154    {
155        Irssi::print("A request seems to be in progress.");
156        Irssi::print("Reload script if I'm wrong.");
157    }
158
159    $busy = 1;
160    $regexp = $data;
161    $results = 0;
162
163    Irssi::printformat(
164        MSGLEVEL_CLIENTCRAP,
165        'cgrep_line',
166        "WHO on " . $window->{name} . " filtered on '$regexp'"
167    );
168
169    run_who($server, $window->{name});
170}
171
172################################################################################
173
174Irssi::theme_register([
175    'cgrep_match',
176    '%GWHO:%n {channick_hilight $0} [{hilight $1}] is "{hilight $2}"%n on {channel $3} [server: {hilight $4}, hops: {hilight $5}]',
177    'cgrep_line',
178    '%R------------%n {hilight $0} %R------------%n'
179]);
180
181Irssi::signal_add(
182    {
183    'redir cgrep_evt_who_result'    => \&event_who_result,
184    'redir cgrep_evt_who_end'       => \&event_who_end
185    }
186);
187
188################################################################################
189
190Irssi::command_bind("cgrep", \&cmd_cgrep);
191
192################################################################################
193