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";
15 my $U = chr 31; # underline
16 my $C = chr 3; # start of color sequence
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)";
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
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
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");
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");
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 # ------------------------------------------------------------
106 q
/(?i)fserve.*trigger/,
108 q
/(?i)trigger.*\/ctcp
/,
110 q
/(?i)file server online/,
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/,
133 q
/brave soldier in the war/,
136 # return 1 (true) if text given as argument is to be filtered out
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/);
157 #called when someone says something in the channel
158 #1: address of speaker
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;
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 # ------------------------------------------------------------
199 my ($fh, $tmpfile) = tempfile
($filter_file.'.XXXXXX', UNLINK
=>1);
202 Xchat
::print("${B}FILTER:${B} ".
203 "Couldn't open temporary file $tmpfile to save filter: $!\n");
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";
214 Xchat
::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
217 #move($tmpfile, $filter_file);
218 rename($tmpfile, $filter_file);
219 Xchat
::print("${B}FILTER SAVED ----------${B}\n");
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");
233 @filter_deny = <$fh>;
234 map (chomp, @filter_deny);
237 Xchat
::print("${B}FILTER:${B} Couldn't close file to load filter: $!\n");
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");
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 # ............................................................
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");
275 if ($filter_turned_on) {
276 Xchat
::print("Filter is turned ${B}ON${B}\n");
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");
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");
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");
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
{
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");
315 Xchat
::print("${B}FILTER:${B} Limiting filtering to server $server\n");
317 $limit_to_server = $server;
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");
348 Xchat
::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
350 Xchat
::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
354 sub cmd_delete_rule
{
358 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
361 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
363 Xchat
::print("${B}FILTER:${B} deleted successfully last rule\n");
366 if ($num !~ /^\d+$/) {
367 Xchat
::print("${B}FILTER:${B} $num is not a number\n");
370 if ($num < 0 || $num > $#filter_deny) {
371 Xchat
::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
376 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
378 Xchat
::print("${B}FILTER:${B} deleted successfully rule $num\n");
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");
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) {
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) {
438 } elsif ($cmd =~ /^DEBUG$/i || $cmd =~ /^INFO$/i) {
441 } elsif ($cmd =~ /^(?:PRINT|LIST)$/i) {
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) {
452 } elsif ($cmd =~ /^DEL(?:ETE)$/i) {
453 cmd_delete_rule
($arg);
455 } elsif ($cmd =~ /^SHOW$/i) {
458 } elsif ($cmd =~ /^SAVE$/i) {
460 Xchat
::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
462 } elsif ($cmd =~ /^(RE)?LOAD$/i) {
464 Xchat
::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
467 Xchat
::print("Unknown command ${B}/FILTER $_[1][1]${B}\n") if $cmd;
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");
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'};
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",
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",
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);
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");
536 # ======================================================================
537 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
538 # ----------------------------------------------------------------------
540 Xchat
::print("${B}$scriptName $scriptVersion${B} loaded\n");