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