Add '/FILTERWINDOW DEBUG' command
[softsnow_xchat2_filter.git] / SoftSnow_filter.pl
blob8e71cd4a5920d08093b712d44e7a5b06c13bd7c9
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use File::Temp qw(tempfile);
7 use File::Copy qw(move);
10 my $scriptName = "SoftSnow XChat2 Filter";
11 my $scriptVersion = "2.1.2";
12 my $scriptDescr = "Filter out file server announcements and IRC SPAM";
14 my $B = chr 2; # bold
15 my $U = chr 31; # underline
16 my $C = chr 3; # start of color sequence
18 ### config ###
19 my $filter_file = Xchat::get_info("xchatdir") . "/SoftSnow_filter.conf";
21 my $filter_turned_on = 0; # is filter is turned on
22 my $limit_to_server = ''; # if true limit to given server (host)
23 my $use_filter_allow = 0; # use overrides (ALLOW before DENY)
25 my $filtered_to_window = 0;
26 my $filter_window = "(filtered)";
27 ### end config ###
29 my $filter_commands = 'ON|OFF|STATUS|SERVER|SERVERON|ALL|HELP|DEBUG|PRINT|ALLOW|ADD|DELETE|SAVE|LOAD';
31 my $filter_help = <<"EOF";
32 ${B}/FILTER $filter_commands${B}
33 /FILTER ON|OFF - turns filtering on/off
34 /FILTER HELP - prints this help message
35 /FILTER STATUS - prints if filter is turned on, and with what limits
36 /FILTER DEBUG - shows some info; used in debuggin the filter
37 /FILTER PRINT - prints all the rules
38 /FILTER ALLOW - toggle use of ALLOW rules (before DENY).
39 /FILTER SERVER - limits filtering to current server (host)
40 /FILTER SERVERON - limits to server and turns filter on
41 /FILTER ALL - resumes filtering everywhere i.e. removes limits
42 /FILTER SAVE - saves the rules to the file $filter_file
43 /FILTER LOAD - loads the rules from the file, replacing existing rules
44 /FILTER ADD <rule> - add rule at the end of the DENY rules
45 /FILTER DELETE [<num>] - delete rule number <num>, or last rule
46 /FILTER SHOW [<num>] - show rule number <num>, or last rule
47 /FILTER VERSION - prints the name and version of this script
48 /FILTER without parameter is equivalent to /FILTER STATUS
49 EOF
51 my $filterwindow_commands = 'ON|OFF|HELP|STATUS|DEBUG';
53 my $filterwindow_help = <<"EOF";
54 ${B}/FILTERWINDOW $filterwindow_commands${B}
55 /FILTERWINDOW ON|OFF - turns saving filtered content to ${U}$filter_window${U}
56 /FILTERWINDOW STATUS - prints if saving to ${U}$filter_window${U} is turned on
57 /FILTERWINDOW HELP - prints this help message
58 /FILTERWINDOW DEBUG - shows some info; used in debugging this part of filter
59 EOF
61 Xchat::register($scriptName, $scriptVersion, $scriptDescr);
63 Xchat::hook_command("FILTER", \&filter_command_handler,
64 { help_text => $filter_help });
65 Xchat::hook_command("FILTERWINDOW", \&filterwindow_command_handler,
66 { help_text => $filterwindow_help });
67 Xchat::hook_server("PRIVMSG", \&privmsg_handler);
69 Xchat::print("Loading ${B}$scriptName $scriptVersion${B}\n".
70 " For help: ${B}/FILTER HELP${B}\n");
72 # GUI, windows, etc.
73 if ($filtered_to_window) {
74 Xchat::command("QUERY $filter_window");
77 # information about (default) options used
78 if ($filter_turned_on) {
79 Xchat::print("Filter turned ${B}ON${B}\n");
80 } else {
81 Xchat::print("Filter turned ${B}OFF${B}\n");
83 if ($limit_to_server) {
84 Xchat::print("Filter limited to server $limit_to_server\n")
86 if ($use_filter_allow) {
87 Xchat::print("Filter uses ALLOW rules\n");
90 # ------------------------------------------------------------
92 my @filter_allow = (
93 q/^\@search\s/,
96 my @filter_deny = (
97 q/\@/,
98 q/^\s*\!/,
99 q/slot\(s\)/,
100 #q/~&~&~/,
102 #xdcc
103 q/^\#\d+/,
105 #fserves
106 q/(?i)fserve.*trigger/,
107 q/(?i)trigger.*\!/,
108 q/(?i)trigger.*\/ctcp/,
109 q/(?i)type\:\s*\!/,
110 q/(?i)file server online/,
112 #ftps
113 q/(?i)ftp.*l\/p/,
115 #CTCPs
116 q/SLOTS/,
117 q/MP3 /,
119 #messages for when a file is received/failed to receive
120 q/(?i)DEFINITELY had the right stuff to get/,
121 q/(?i)has just received/,
122 q/(?i)I have just received/,
124 #mp3 play messages
125 q/is listening to/,
126 q/\]\-MP3INFO\-\[/,
128 #spammy scripts
129 q/\]\-SpR\-\[/,
130 q/We are BORG/,
132 #general messages
133 q/brave soldier in the war/,
136 # return 1 (true) if text given as argument is to be filtered out
137 sub isFiltered {
138 my $text = shift;
139 my $regexp = '';
141 #strip colour, underline, bold codes, etc.
142 $text = Xchat::strip_code($text);
144 if ($use_filter_allow) {
145 foreach $regexp (@filter_allow) {
146 return 0 if ($text =~ /$regexp/);
150 foreach $regexp (@filter_deny) {
151 return 1 if ($text =~ /$regexp/);
154 return 0;
157 #called when someone says something in the channel
158 #1: address of speaker
159 #2: PRIVMSG constant
160 #3: channel
161 #4: text said (prefixed with :)
162 sub privmsg_handler {
163 # $_[0] - array reference containing the IRC message or command
164 # and arguments broken into words
165 # $_[1] - array reference containing the Nth word to the last word
166 my ($address, $msgtype, $channel) = @{$_[0]};
167 my ($nick, $user, $host) = ($address =~ /^:(.*?)!(.*?)@(.*)$/);
169 my $text = $_[1][3]; # Get server message
171 my $server = Xchat::get_info("host");
173 return Xchat::EAT_NONE unless $filter_turned_on;
174 if ($limit_to_server) {
175 return Xchat::EAT_NONE unless $server eq $limit_to_server;
178 $text =~ s/^://;
180 if (isFiltered($text)) {
181 if (defined $nick && $filtered_to_window) {
182 #Xchat::print($text, $filter_window)
184 my $ctx = Xchat::get_context();
185 Xchat::set_context($filter_window);
186 Xchat::emit_print('Channel Message', $nick, $text);
187 Xchat::set_context($ctx);
189 #return Xchat::EAT_XCHAT;
190 return Xchat::EAT_ALL;
192 return Xchat::EAT_NONE;
196 # ------------------------------------------------------------
198 sub save_filter {
199 my ($fh, $tmpfile) = tempfile($filter_file.'.XXXXXX', UNLINK=>1);
201 unless ($fh) {
202 Xchat::print("${B}FILTER:${B} ".
203 "Couldn't open temporary file $tmpfile to save filter: $!\n");
204 return;
207 Xchat::print("${B}FILTER SAVE >$filter_file${B}\n");
208 foreach my $regexp (@filter_deny) {
209 Xchat::print("/".$regexp."/ saved\n");
210 print $fh $regexp."\n";
213 unless (close $fh) {
214 Xchat::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
215 return;
217 #move($tmpfile, $filter_file);
218 rename($tmpfile, $filter_file);
219 Xchat::print("${B}FILTER SAVED ----------${B}\n");
221 return 1;
224 sub load_filter {
225 my $fh;
227 Xchat::print("${B}FILTER:${B} ...loading filter patterns\n");
228 unless (open $fh, '<', $filter_file) {
229 Xchat::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
230 return;
233 @filter_deny = <$fh>;
234 map (chomp, @filter_deny);
236 unless (close $fh) {
237 Xchat::print("${B}FILTER:${B} Couldn't close file to load filter: $!\n");
238 return;
241 Xchat::print("${B}FILTER DENY ----------${B}\n");
242 for (my $i = 0; $i <= $#filter_deny; $i++) {
243 Xchat::print(" [$i]: /".$filter_deny[$i]."/\n");
245 Xchat::print("${B}FILTER DENY ----------${B}\n");
248 sub add_rule ( $ ) {
249 my $rule = shift;
251 # always ading rules at the end
252 push @filter_deny, $rule;
255 sub delete_rule ( $ ) {
256 my $num = shift || $#filter_deny;
258 splice @filter_deny, $num, 1;
261 # ============================================================
262 # ------------------------------------------------------------
263 # ............................................................
265 sub cmd_version {
266 Xchat::print("${B}$scriptName $scriptVersion${B}\n");
267 Xchat::print(" * URL: http://github.com/jnareb/softsnow-xchat2-filter\n");
268 Xchat::print(" * URL: http://gitorious.org/projects/softsnow-xchat2-filter\n");
269 Xchat::print(" * URL: http://repo.or.cz/w/softsnow_xchat2_filter.git\n");
272 sub cmd_status {
273 my $server = shift;
275 if ($filter_turned_on) {
276 Xchat::print("Filter is turned ${B}ON${B}\n");
277 } else {
278 Xchat::print("Filter is turned ${B}OFF${B}\n");
280 if ($limit_to_server) {
281 if ($server eq $limit_to_server) {
282 Xchat::print("Filter is limited to ${B}current${B} ".
283 "server $limit_to_server\n");
284 } else {
285 Xchat::print("Filter is limited to server ".
286 "$limit_to_server != $server\n");
289 if ($use_filter_allow) {
290 Xchat::print("Filter is using ALLOW rules (before DENY)\n");
294 sub cmd_debug {
295 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
296 Xchat::print("Channel: ".Xchat::get_info("channel")."\n");
297 Xchat::print("Host: ".Xchat::get_info("host")."\n");
298 Xchat::print("Server: ".Xchat::get_info("server")."\n");
299 Xchat::print("Server Id: ".Xchat::get_info("id")."\n");
300 Xchat::print("Network: ".Xchat::get_info("network")."\n");
301 Xchat::print("\n");
302 Xchat::printf("%3u %s rules\n", scalar(@filter_allow), "allow");
303 Xchat::printf("%3u %s rules\n", scalar(@filter_deny), "deny");
304 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
307 sub cmd_server_limit {
308 my $server = shift;
310 if ($server) {
311 # adding limiting to given (single) server
312 if ($limit_to_server) {
313 Xchat::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
314 } else {
315 Xchat::print("${B}FILTER:${B} Limiting filtering to server $server\n");
317 $limit_to_server = $server;
319 } else {
320 # removing limiting to server
321 if ($limit_to_server) {
322 Xchat::print("Filter: Removing limit to server $limit_to_server\n");
324 $limit_to_server = '';
329 sub cmd_print_rules {
330 Xchat::print("${B}FILTER PRINT ----------${B}\n");
331 Xchat::print("${B}ALLOW${B}".($use_filter_allow ? ' (on)' : ' (off)')."\n");
333 for (my $i = 0; $i <= $#filter_allow; $i++) {
334 Xchat::print("[$i]: /".$filter_allow[$i]."/\n");
336 Xchat::print("${B}DENY${B}\n");
337 for (my $i = 0; $i <= $#filter_deny; $i++) {
338 Xchat::print("[$i]: /".$filter_deny[$i]."/\n");
340 Xchat::print("${B}FILTER PRINT ----------${B}\n");
343 sub cmd_add_rule {
344 my $rule = shift;
346 if ($rule) {
347 add_rule($rule);
348 Xchat::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
349 } else {
350 Xchat::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
354 sub cmd_delete_rule {
355 my $num = shift;
357 # strip whitespace
358 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
359 SWITCH: {
360 unless ($num) {
361 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
362 $#filter_deny--;
363 Xchat::print("${B}FILTER:${B} deleted successfully last rule\n");
364 last SWITCH;
366 if ($num !~ /^\d+$/) {
367 Xchat::print("${B}FILTER:${B} $num is not a number\n");
368 last SWITCH;
370 if ($num < 0 || $num > $#filter_deny) {
371 Xchat::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
372 last SWITCH;
374 # default
376 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
377 delete_rule($num);
378 Xchat::print("${B}FILTER:${B} deleted successfully rule $num\n");
383 sub cmd_show_rule {
384 my $num = shift;
386 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
388 if (defined $num && $num !~ /^\d+$/) {
389 Xchat::print("${B}FILTER:${B} $num is not a number\n");
390 } elsif (defined $num && !defined $filter_deny[$num]) {
391 Xchat::print("${B}FILTER:${B} rule $num does not exist\n");
392 } else {
393 Xchat::print("${B}FILTER:${B} ".(defined $num ? "[$num]" : "last").
394 " rule /".$filter_deny[defined $num ? $num : -1]."/\n");
398 # ============================================================
399 # ============================================================
400 # ============================================================
402 sub filter_command_handler {
403 my $cmd = $_[0][1]; # 1st parameter (after FILTER)
404 my $arg = $_[1][2]; # 2nd word to the last word
405 my $server = Xchat::get_info("host");
408 if (!$cmd || $cmd =~ /^STATUS$/i) {
409 cmd_status($server);
411 } elsif ($cmd =~ /^ON$/i) {
412 $filter_turned_on = 1;
413 Xchat::print("Filter turned ${B}ON${B}\n");
415 } elsif ($cmd =~ /^OFF$/i) {
416 $filter_turned_on = 0;
417 Xchat::print("Filter turned ${B}OFF${B}\n");
419 } elsif ($cmd =~ /^SERVER$/i) {
420 cmd_server_limit($server);
422 } elsif ($cmd =~ /^SERVERON$/i) {
423 cmd_server_limit($server);
425 $filter_turned_on = 1;
426 Xchat::print("Filter turned ${B}ON${B}\n");
428 } elsif ($cmd =~ /^ALL$/i) {
429 cmd_server_limit(undef);
431 } elsif ($cmd =~ /^HELP$/i) {
432 Xchat::print($filter_help);
433 Xchat::print($filterwindow_help);
435 } elsif ($cmd =~ /^VERSION$/i) {
436 cmd_version();
438 } elsif ($cmd =~ /^DEBUG$/i || $cmd =~ /^INFO$/i) {
439 cmd_debug();
441 } elsif ($cmd =~ /^(?:PRINT|LIST)$/i) {
442 cmd_print_rules();
444 } elsif ($cmd =~ /^ALLOW$/i) {
445 $use_filter_allow = !$use_filter_allow;
446 Xchat::print("${B}FILTER:${B} ALLOW rules ".
447 ($use_filter_allow ? "enabled" : "disabled")."\n");
449 } elsif ($cmd =~ /^ADD$/i) {
450 cmd_add_rule($arg);
452 } elsif ($cmd =~ /^DEL(?:ETE)$/i) {
453 cmd_delete_rule($arg);
455 } elsif ($cmd =~ /^SHOW$/i) {
456 cmd_show_rule($arg);
458 } elsif ($cmd =~ /^SAVE$/i) {
459 save_filter();
460 Xchat::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
462 } elsif ($cmd =~ /^(RE)?LOAD$/i) {
463 load_filter();
464 Xchat::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
466 } else {
467 Xchat::print("Unknown command ${B}/FILTER $_[1][1]${B}\n") if $cmd;
469 return 1;
472 sub filterwindow_command_handler {
473 my $cmd = $_[0][1]; # 1st parameter (after FILTER)
474 #my $arg = $_[1][2]; # 2nd word to the last word
475 my $ctx = Xchat::find_context($filter_window);
477 if (!$cmd || $cmd =~ /^STATUS$/i) {
478 Xchat::print(($filtered_to_window ? "Show" : "Don't show").
479 " filtered content in ".
480 (defined $ctx ? "open" : "closed").
481 " window ${B}$filter_window${B}\n");
483 } elsif ($cmd =~ /^DEBUG$/i) {
484 my $ctx_info = Xchat::context_info($ctx);
485 Xchat::print("${B}FILTERWINDOW DEBUG ----------${B}\n");
486 Xchat::print("filtered_to_window = $filtered_to_window\n");
487 Xchat::print("filter_window = $filter_window\n");
488 if (defined $ctx) {
489 Xchat::print("$filter_window is ${B}open${B}\n");
490 Xchat::print("$filter_window: network => $ctx_info->{network}\n")
491 if defined $ctx_info->{'network'};
492 Xchat::print("$filter_window: host => $ctx_info->{host}\n")
493 if defined $ctx_info->{'host'};
494 Xchat::print("$filter_window: channel => $ctx_info->{channel}\n");
495 Xchat::print("$filter_window: server_id => $ctx_info->{id}\n");
496 if defined $ctx_info->{'id'};
497 } else {
498 Xchat::print("$filter_window is ${B}closed${B}\n");
500 # requires XChat >= 2.8.2
501 #Xchat::print("'Channel Message' format: ".
502 # Xchat::get_info("event_text Channel Message")."\n");
503 #Xchat::print("'Channel Msg Hilight' format: ".
504 # Xchat::get_info("event_text Channel Msg Hilight")."\n");
505 Xchat::print("${B}FILTERWINDOW DEBUG ----------${B}\n");
507 } elsif ($cmd =~ /^ON$/i) {
508 Xchat::command("QUERY $filter_window");
509 Xchat::print("${B}----- START LOGGING FILTERED CONTENTS -----${B}\n",
510 $filter_window)
511 if !$filtered_to_window;
513 $filtered_to_window = 1;
514 Xchat::print("Show filtered content in ${B}$filter_window${B}\n");
516 } elsif ($cmd =~ /^OFF$/i) {
517 Xchat::print("${B}----- STOP LOGGING FILTERED CONTENTS -----${B}\n",
518 $filter_window)
519 if $filtered_to_window;
520 #Xchat::command("CLOSE", $FilterWindow);
522 $filtered_to_window = 0;
523 Xchat::print("Don't show filtered content in ${B}$filter_window${B}\n");
525 } elsif ($cmd =~ /^HELP$/i) {
526 Xchat::print($filterwindow_help);
528 } else {
529 Xchat::print("Unknown command ${B}/FILTERWINDOW $_[1][1]${B}\n") if $cmd;
530 Xchat::print("${B}${U}USAGE:${U} /FILTERWINDOW ON|OFF${B}\n");
533 return 1;
536 # ======================================================================
537 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
538 # ----------------------------------------------------------------------
540 Xchat::print("${B}$scriptName $scriptVersion${B} loaded\n");