-
Notifications
You must be signed in to change notification settings - Fork 235
/
isbanned.pl
671 lines (637 loc) · 14.7 KB
/
isbanned.pl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
use Irssi;
use strict;
use warnings;
use bignum;
use Socket;
use vars qw($VERSION %IRSSI);
%IRSSI =
(
name => "isbanned",
description => "freenode-specific script that checks whether someone is banned on some channel",
commands => "isbanned ismuted islisted isreset",
authors => "mniip",
contact => "mniip \@ freenode",
license => "Public domain",
modified => "2015-09-14"
);
$VERSION = "0.7.0";
# Commands:
# /isbanned <channel> <user>
# Check whether <user> is banned on <channel>
# /ismuted <channel> <user>
# Check whether <user> is muted on <channel>
# /islisted <channel> <mode> <user>
# Check whether <user> is listed in <channel>'s
# <mode> list (can be b, q, e, or I)
# /isreset
# If something screws up, this resets the state to inactive
#
# <user> can either be a nickname, or a hostmask in the form
# nick!ident@host#gecos$account
# where some parts can be omitted. Strictly speaking the form is
# [nick] ['!' ident] ['@' host] ['#' gecos] ['$' account]
# If any part is omitted, it is assumed to be empty string, except for
# account: if the account part is omitted the user is assumed to be
# unidentified (as opposed to identified as empty string)
#
# Supports all kinds of features, misfeatures, and quirks freenode uses.
# Supports $a, $j, $r, $x, and $z extbans, and any edge cases of those.
# Supports CIDR bans, both IPv4 and IPv6, and the misfeature by which
# an invalid IP address is not parsed and zeroes are matched instead.
# Supports the +ikmrS modes. Supports the RFC2812 casemapping in patterns,
# which are, by the way, parsed without backtracking and thus effeciently.
#
# Changelog:
# 0.6.0 (2015.01.12)
# Ported from the hexchat script version 0.6
#
# 0.6.1 (2015.01.13)
# Fixed a few porting issues: the original host is now included
# in the hosts list too. Fixed IPv6 parsing. Fixed $x and $~x.
#
# 0.6.2 (2015.01.13)
# Fixed a few warnings.
#
# 0.6.3 (2015.01.16)
# Improve command argument parsing. Display usage on incorrect use.
# Support multiple modes at once in islisted.
#
# 0.6.4 (2015.03.22)
# Fix translation from python: fix bans containing the letter Z not matching
#
# 0.7.0 (2015.09.14)
# Support trailing characters in CIDR bans.
my $active = 0;
my $user;
my $channel;
my $orig_list;
my $modes;
my @whois;
my $lists_left;
my @bans;
sub parse_ipv6_word
{
my ($w) = @_;
return hex $w if $w =~ /^0*[0-9a-fA-F]{1,4}$/;
die "Invalid IPv6 word";
}
sub parse_ip
{
my ($ip, $strict) = @_;
if($ip =~ /:/)
{
if($ip =~ /::/)
{
my $edge = ($ip =~ /::$/) || ($ip =~ /^::/);
$ip = $ip . "0" if $ip =~ /::$/;
$ip = "0" . $ip if $ip =~ /^::/;
my ($head, $tail) = split /::/, $ip, 2;
my @headwords = split /:/, $head, 8;
my @tailwords = split /:/, $tail, 8;
if(@headwords + @tailwords <= ($edge ? 8 : 7))
{
my $result;
eval
{
@headwords = map { parse_ipv6_word($_) } @headwords;
@tailwords = map { parse_ipv6_word($_) } @tailwords;
my @words = (@headwords, (0) x (8 - @headwords - @tailwords), @tailwords);
$result = 0;
for(my $i = 0; $i < 8; $i++)
{
$result |= $words[$i] << ((7 - $i) * 16);
}
};
return $result if defined $result;
}
}
else
{
my @words = split /:/, $ip, 8;
if(scalar @words == 8)
{
my $result;
eval
{
@words = map { parse_ipv6_word($_) } @words;
$result = 0;
for(my $i = 0; $i < 8; $i++)
{
$result |= $words[$i] << ((7 - $i) * 16);
}
};
return $result if defined $result;
}
}
die "Invalid IPv6" if $strict;
return 0;
}
else
{
if($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
{
my ($o1, $o2, $o3, $o4) = (0 + $1, 0 + $2, 0 + $3, 0 + $4);
return $o1 << 24 | $o2 << 16 | $o3 << 8 | $o4
if $o1 < 256 && $o2 < 256 && $o3 < 256 && $o4 < 256;
}
die "Invalid IPv4" if $strict;
return 0;
}
}
my %char_classes =
(
"[" => "[\[{]",
"{" => "[{\[]",
"|" => "[|\\]",
"\\" => "[\\|]",
"]" => "[\]}]",
"}" => "[}\]]",
"~" => "[~^]",
"?" => "."
);
$char_classes{$_} = $_ foreach split //, '-_`^0123456789';
for(my $c = 0; $c <= 25; $c++)
{
my $lc = chr($c + ord "a");
my $uc = chr($c + ord "A");
$char_classes{$lc} = "[$lc$uc]";
$char_classes{$uc} = "[$uc$lc]";
}
sub match_pattern
{
my ($string, $pattern) = @_;
$string = "" if !defined $string;
$pattern =~ s|[?*]+|"?" x ($& =~ tr/?/?/) . ($& =~ /\*/ ? "*" : "")|ge;
my $last_pos = 0;
my @pieces = split /\*/, $pattern;
push @pieces, "" if $pattern =~ /\*$/;
push @pieces, "" if $pattern eq "*";
for(my $i = 0; $i < scalar @pieces; $i++)
{
my $regex = "";
$regex .= "^" if !$i;
$regex .= defined $char_classes{$_} ? $char_classes{$_} : "\\$_" for split //, $pieces[$i];
$regex .= "\$" if $i == $#pieces;
if((substr $string, $last_pos) =~ qr/$regex/)
{
$last_pos += $+[0];
}
else
{
return 0;
}
}
return 1;
}
my @found_modes;
sub add_ban
{
push @found_modes, "\x0302$_[1] $_[0]\x0F in \x0306$_[2]\x0F set by \x0310$_[3]\x0F on \x0308" . (scalar localtime $_[4]) . "\x0F";
}
sub analyze
{
$active = 0;
my ($nick, $ident, $host, $gecos, $account, $ssl) = @whois;
@found_modes = ();
my @hostile_modes = ();
for my $c(split //, $modes)
{
push @hostile_modes, $c if $orig_list eq "b" && ($c eq "i" || ($c eq "r" && !$account) || ($c eq "S" && !$ssl));
push @hostile_modes, $c if $orig_list eq "q" && ($c eq "m" || ($c eq "r" && !$account));
}
push @found_modes, ("\x0302+" . join("", @hostile_modes) . "\x0F in \x0306$channel\x0F") if(@hostile_modes);
for my $b(@bans)
{
my ($ban) = split /(?<!^)\$/, $b->[0], 2;
if($ban =~ /^\$/)
{
add_ban(@$b) if $ban eq '$a' && $account;
if($ban =~ /^\$a:(.*)$/)
{
add_ban(@$b) if $account && nick_eq($account, $1);
}
add_ban(@$b) if $ban eq '$~a' && !$account;
if($ban =~ /^\$~a:(.*)$/)
{
add_ban(@$b) if !$account || !nick_eq($account, $1);
}
add_ban(@$b) if $ban =~ /^\$~j:/;
if($ban =~ /^\$r:(.*)$/)
{
add_ban(@$b) if match_pattern($gecos, $1);
}
if($ban =~ /^\$~r:(.*)$/)
{
add_ban(@$b) if !match_pattern($gecos, $1);
}
if($ban =~ /^\$x:(.*)$/)
{
for my $h(@$host)
{
if(match_pattern("$nick!$ident\@$h#$gecos", $1))
{
add_ban(@$b);
last;
}
}
}
if($ban =~ /^\$~x:(.*)$/)
{
my $found = 0;
for my $h(@$host)
{
if(match_pattern("$nick!$ident\@$h#$gecos", $1))
{
$found = 1;
last;
}
}
add_ban(@$b) if !$found;
}
add_ban(@$b) if $ban eq '$z' && $ssl;
add_ban(@$b) if $ban eq '$~z' && !$ssl;
}
else
{
my ($v, $bhost) = split /@/, $ban, 2;
my ($bnick, $bident) = split /!/, $v, 2;
if(match_pattern($nick, $bnick) && match_pattern($ident, $bident))
{
my $found = 0;
for my $h(@$host)
{
if(match_pattern($h, $bhost))
{
$found = 1;
add_ban(@$b);
last;
}
}
if(!$found)
{
my ($ip, $width) = split /\//, $bhost, 2;
if(defined $width)
{
$width =~ s/^([0-9]*).*/$1/g;
$width = int("0" . $width);
if($width > 0)
{
my $is_v4 = !($ip =~ /:/);
$width = ($is_v4 ? 32 : 128) - $width;
$width = 0 if $width < 0;
$ip = parse_ip($ip);
for my $h(@$host)
{
if(!($h =~ /:/) == $is_v4)
{
my $last;
eval
{
my $h = parse_ip($h, 1);
if(($ip >> $width) == ($h >> $width))
{
add_ban(@$b);
$last = 1;
}
};
last if $last;
}
}
}
}
}
} # omg so many }
}
}
if(@found_modes)
{
if($orig_list eq "b")
{
Irssi::print("The following are preventing \x0310$user\x0F from joining \x0306$channel\x0F:");
}
elsif($orig_list eq "q")
{
Irssi::print("The following are preventing \x0310$user\x0F from speaking in \x0306$channel\x0F:");
}
else
{
Irssi::print("The following \x0302+$orig_list\x0F modes affect \x0310$user\x0F in \x0306$channel\x0F:");
}
Irssi::print($_) for(@found_modes);
}
else
{
if($orig_list eq "b")
{
Irssi::print("Nothing is preventing \x0310$user\x0F from joining \x0306$channel\x0F");
}
elsif($orig_list eq "q")
{
Irssi::print("Nothing is preventing \x0310$user\x0F from speaking in \x0306$channel\x0F");
}
else
{
Irssi::print("No \x0302+$orig_list\x0F modes affect \x0310$user\x0F in \x0306$channel\x0F");
}
}
}
sub reset
{
$active = 0;
}
sub lookup_host
{
my ($host) = @_;
$host = "" if !defined $host;
Irssi::print("\x0302Resolving <$host>");
my @addresses = gethostbyname($host);
if(@addresses)
{
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
my %seen;
@addresses = grep { !$seen{$_}++ } (@addresses, $host);
Irssi::print("\x0302IPs: <@addresses>");
return @addresses;
}
else
{
Irssi::print("\x0302Found nothing, will use <$host>");
return $host;
}
}
sub query_list
{
my ($server, $channel, $mode) = @_;
$server->command("quote MODE $channel");
for my $m(split //, $mode)
{
$lists_left++;
$server->command("quote MODE $channel $m");
}
}
sub query_whois
{
my ($server, $nick) = @_;
$nick =~ s/\s+//g;
$server->command("quote WHOIS $nick");
}
sub ignored
{
Irssi::signal_stop() if $active;
}
sub nick_eq
{
my ($n1, $n2) = @_;
$n1 = lc $n1;
$n1 =~ tr/[]\\/{}|/;
$n2 = lc $n2;
$n2 =~ tr/[]\\/{}|/;
return $n1 eq $n2;
}
sub modes
{
if($active)
{
my ($server, $data, $nick, $address) = @_;
my @w = split / /, $data;
Irssi::print("\x0302Channel $w[1] is +s, report may be incomplete") if $w[2] =~ /s/;
if(nick_eq($w[1], $channel))
{
$modes = $w[2];
analyze() if !$lists_left && @whois;
}
Irssi::signal_stop();
}
}
sub list_entry_b { list_entry("+b", @_); }
sub list_entry_q { list_entry("+q", @_); }
sub list_entry_I { list_entry("+I", @_); }
sub list_entry_e { list_entry("+e", @_); }
sub list_entry
{
if($active)
{
my ($u, $server, $data, $nick, $address) = @_;
my @w = split / /, $data;
splice @w, 2, 1 if $u eq "+q";
if(nick_eq($w[1], $channel) && $w[2] =~ /^\$j:(.*)$/)
{
query_list($server, $1 =~ s/\$.*$//r, "b")
}
else
{
push @bans, [$w[2], $u, $w[1], $w[3], $w[4]];
}
Irssi::signal_stop();
}
}
sub list_end
{
if($active)
{
$lists_left--;
analyze() if !$lists_left && @whois && $modes;
Irssi::signal_stop();
}
}
sub no_modes
{
if($active)
{
Irssi::print("\x0304Attempted to get modes for a nickname, did you put the arguments in the wrong order?");
if(!$modes)
{
$modes = "+";
}
else
{
$lists_left--;
analyze() if !$lists_left && @whois;
}
Irssi::signal_stop();
}
}
sub mode_error
{
if($active)
{
Irssi::print("\x0304Something went wrong with the modes, report may be incomplete");
if(!$modes)
{
$modes = "+";
analyze() if !$lists_left && @whois;
}
else
{
$lists_left--;
analyze() if !$lists_left && @whois;
}
Irssi::signal_stop();
}
}
sub no_list
{
if($active)
{
my ($server, $data, $nick, $address) = @_;
my @w = split / /, $data;
Irssi::print("\x0304Could not obtain modes for $w[1], report may be incomplete");
if(nick_eq($w[1], $channel) && !$modes)
{
$modes = "+";
analyze() if !$lists_left && @whois;
}
else
{
$lists_left--;
analyze() if !$lists_left && @whois && $modes;
}
Irssi::signal_stop();
}
}
my @wh;
my $ac;
my $ssl;
sub whois_start
{
if($active)
{
my ($server, $data, $nick, $address) = @_;
my @w = split / /, $data;
@wh = ($w[1], $w[2], [lookup_host($w[3])], substr((join " ", @w[5 .. $#w]), 1));
undef $ac;
undef $ssl;
Irssi::signal_stop();
}
}
sub whois_ssl
{
if($active)
{
$ssl = 1;
Irssi::signal_stop();
}
}
sub whois_account
{
if($active)
{
my ($server, $data, $nick, $address) = @_;
my @w = split / /, $data;
$ac = $w[2];
Irssi::signal_stop();
}
}
sub whois_end
{
if($active)
{
my ($server, $data, $nick, $address) = @_;
if(@wh)
{
@whois = (@wh, $ac, $ssl);
@wh = ();
analyze() if !$lists_left && $modes;
}
else
{
Irssi::print("\x0304Whois failed, aborting!");
$active = 0;
}
Irssi::signal_stop();
}
}
sub start_search
{
my ($server, $ch, $u, $mode) = @_;
@whois = ();
$orig_list = $mode;
$channel = $ch;
$user = $u;
if($user =~ /[!@#\$]/)
{
my ($account, $rname, $host, $nick, $ident, $v);
($v, $account) = split /\$/, $user, 2;
($v, $rname) = split /#/, $v, 2;
($v, $host) = split /@/, $v, 2;
($nick, $ident) = split /!/, $v, 2;
@whois = ($nick, $ident, [lookup_host($host)], $rname, $account, 0);
}
undef $modes;
$lists_left = 0;
@bans = ();
$active = 1;
query_list($server, $channel, $mode);
query_whois($server, $user) if !@whois;
}
sub isbanned
{
my ($arg, $server, $witem) = @_;
if($arg =~ /^\s*(\S+)\s+(.*\S)\s*/)
{
my $chan = $1;
my $user = $2;
return start_search($server, $chan, $user, "b") if $chan ne "" && $user ne "";
}
Irssi::print("Usage: /isbanned <channel> <user>");
}
sub ismuted
{
my ($arg, $server, $witem) = @_;
my ($chan, $user) = split / /, $arg, 2;
if($arg =~ /^\s*(\S+)\s+(.*\S)\s*/)
{
my $chan = $1;
my $user = $2;
if($chan ne "" && $user ne "")
{
start_search($server, $chan, $user, "q");
query_list($server, $chan, "b");
return;
}
}
Irssi::print("Usage: /ismuted <channel> <user>");
}
sub islisted
{
my ($arg, $server, $witem) = @_;
my ($chan, $mode, $user) = split / /, $arg, 3;
if($arg =~ /^\s*(\S+)\s+(\S+)\s+(.*\S)\s*/)
{
my $chan = $1;
my $mode = $2;
my $user = $3;
$mode =~ s/\+//g;
return start_search($server, $chan, $user, $mode) if $chan ne "" && $mode ne "" && $user ne "";
}
Irssi::print("Usage: /islisted <channel> <mode> <user>");
}
Irssi::signal_add("event 329", \&ignored);
Irssi::signal_add("event 276", \&ignored);
Irssi::signal_add("event 317", \&ignored);
Irssi::signal_add("event 378", \&ignored);
Irssi::signal_add("event 319", \&ignored);
Irssi::signal_add("event 312", \&ignored);
Irssi::signal_add("event 324", \&modes);
Irssi::signal_add("event 367", \&list_entry_b);
Irssi::signal_add("event 728", \&list_entry_q);
Irssi::signal_add("event 346", \&list_entry_I);
Irssi::signal_add("event 348", \&list_entry_e);
Irssi::signal_add("event 368", \&list_end);
Irssi::signal_add("event 729", \&list_end);
Irssi::signal_add("event 347", \&list_end);
Irssi::signal_add("event 349", \&list_end);
Irssi::signal_add("event 502", \&no_modes);
Irssi::signal_add("event 221", \&no_modes);
Irssi::signal_add("event 472", \&mode_error);
Irssi::signal_add("event 501", \&mode_error);
Irssi::signal_add("event 403", \&no_list);
Irssi::signal_add("event 482", \&no_list);
Irssi::signal_add("event 311", \&whois_start);
Irssi::signal_add("event 671", \&whois_ssl);
Irssi::signal_add("event 330", \&whois_account);
Irssi::signal_add("event 318", \&whois_end);
Irssi::command_bind("isbanned", \&isbanned);
Irssi::command_bind("ismuted", \&ismuted);
Irssi::command_bind("islisted", \&islisted);
Irssi::command_bind("isreset", \&reset);