summaryrefslogtreecommitdiffstats
path: root/scripts/cgrep.pl
blob: e7afc7cf232ad25dddfd33c238d26e9958261d8c (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
################################################################################
#
# Usage: /cgrep <regexp>
#
# Shows all WHO records matching that regexp in a friendly yet complete format
# Works on the active channel only
#
# This is a bit like c0ffee's ls command, except it matches ALL returned data.
# Since IRSSI doe snot cache realnames properly, this script calls WHO once
# and awaits the results.
#
# Also check out 'joininfo.pl' which shows lots of WHOIS info when a person
# joins the channel.
#
# FORMAT SETTINGS:
#   cgrep_match         Matching record
#   cgrep_line          Start and end line format
#
################################################################################

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
use integer;

$VERSION = "1.0.0";
%IRSSI = (
    authors => "Pieter-Bas IJdens",
    contact => "irssi-scripts\@nospam.mi4.org.uk",
    name    => "cgrep",
    description => "Lists users on the channel matching the specified regexp",
    license => "GPLv2 or later",
    url     => "http://pieter-bas.ijdens.com/irssi/",
    changed => "2005-03-10"
);

################################################################################

my($busy) = 0;
my($regexp) = "";
my($results) = 0;
my($debug) = 0;

################################################################################

sub run_who
{
    my($server, $channel) = @_;

    $server->redirect_event(
        "who",
        1,
        $channel,
        0,
        "redir who_default",
        {
            "event 352" => "redir cgrep_evt_who_result",
            "event 315" => "redir cgrep_evt_who_end",
            "" => "event empty"
        }
    );

    $server->send_raw("WHO $channel");
}

################################################################################

sub event_who_result
{
    my ($server, $data) = @_;

    if ($busy)
    {
        my($start,$realname);
        if ($data =~ /^(.*):([^:]{1,})$/)
        {
            $start = $1;
            $realname = $2;
        }
        else
        {
            Irssi::print("$data can't be parsed");
        }

        # my($start,$realname) = split(":", $data);

        my($me, $channel, $ident, $host, $server, $nick, $mode) =
            split(" ", $start);
        my($hops) = -1;

        if ($realname =~ /^([0-9]{1,} )(.*$)$/i)
        {
            $hops = $1;
            $realname = $2;

            $hops =~ s/[ ]{1,}$//g;
        }

        my($string) = "$nick ($ident\@$host) \"$realname\" $channel " 
                    . "($server, $hops)";

        if ($string =~ /$regexp/i)
        {
            Irssi::printformat(
                MSGLEVEL_CLIENTCRAP,
                'cgrep_match',
                $nick,
                "$ident\@$host",
                "$realname",
                $channel,
                $server,
                $hops
            );

            $results++;
        }
    }
}

################################################################################

sub event_who_end
{
    my ($server, $data) = @_;

    Irssi::printformat(
        MSGLEVEL_CLIENTCRAP,
        'cgrep_line',
        "End of list. Found $results matches."
    );

    $busy = 0;
    $regexp = "";
    $results = 0;
}

################################################################################

sub cmd_cgrep
{
    my ($data, $server, $window) = @_;

    if (!$server)
    {
        Irssi::print("Not connected to a server in this window.");
        return;
    }
    elsif ($window->{type} ne "CHANNEL")
    {
        Irssi::print("Not a channel window.");
        return;
    }
    elsif ($busy)
    {
        Irssi::print("A request seems to be in progress.");
        Irssi::print("Reload script if I'm wrong.");
    }

    $busy = 1;
    $regexp = $data;
    $results = 0;

    Irssi::printformat(
        MSGLEVEL_CLIENTCRAP,
        'cgrep_line',
        "WHO on " . $window->{name} . " filtered on '$regexp'"
    );

    run_who($server, $window->{name});
}

################################################################################

Irssi::theme_register([
    'cgrep_match',
    '%GWHO:%n {channick_hilight $0} [{hilight $1}] is "{hilight $2}"%n on {channel $3} [server: {hilight $4}, hops: {hilight $5}]',
    'cgrep_line',
    '%R------------%n {hilight $0} %R------------%n'
]);

Irssi::signal_add(
    {
    'redir cgrep_evt_who_result'    => \&event_who_result,
    'redir cgrep_evt_who_end'       => \&event_who_end
    }
);

################################################################################

Irssi::command_bind("cgrep", \&cmd_cgrep);

################################################################################