Always highlight ON and OFF in messages about (change of) filter status.
[softsnow_xchat2_filter.git] / SoftSnow_filter.pl
blobb3740c3de2f81bf0acb4c60f4aded8e4f3f39687
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
7 my $scriptName = "SoftSnow XChat2 Filter";
8 my $scriptVersion = "2.0.3";
9 my $scriptDescr = "Filter out file server announcements and IRC SPAM";
11 my $B = "\cB"; # bold
12 my $U = "\cU"; # underline
13 my $C = "\cC"; # start of color sequence
15 ### config ###
16 my $filter_file = Xchat::get_info("xchatdir") . "/SoftSnow_filter.conf";
18 my $filter_turned_on = 0; # was default turned ON
19 my $limit_to_server = ''; # don't limit to server (host)
20 my $use_filter_allow = 0; # use overrides
21 ### end config ###
23 my $command_list = 'ON|OFF|STATUS|SERVER|SERVERON|ALL|HELP|DEBUG|PRINT|ALLOW|ADD|DELETE|SAVE|LOAD';
25 my $scriptHelp = <<"EOF";
26 ${B}/FILTER $command_list${B}
27 /FILTER ON|OFF - turns filtering on/off
28 /FILTER HELP - prints this help message
29 /FILTER STATUS - prints if filter is turned on, and with what limits
30 /FILTER DEBUG - shows some info; used in debuggin the filter
31 /FILTER PRINT - prints all the rules
32 /FILTER ALLOW - toggle use of ALLOW rules (before DENY).
33 /FILTER SERVER - limits filtering to current server (host)
34 /FILTER SERVERON - limits to server and turns filter on
35 /FILTER ALL - resumes filtering everywhere i.e. removes limits
36 /FILTER SAVE - saves the rules to the file $filter_file
37 /FILTER LOAD - loads the rules from the file, replacing existing rules
38 /FILTER ADD <rule> - add rule at the end of the DENY rules
39 /FILTER DELETE [<num>] - delete rule number <num>, or last rule
40 /FILTER SHOW [<num>] - show rule number <num>, or last rule
41 /FILTER VERSION - prints the name and version of this script
42 /FILTER without parameter is equivalent to /FILTER STATUS
43 EOF
45 Xchat::register($scriptName, $scriptVersion, $scriptDescr);
47 Xchat::hook_command("FILTER", \&filter_command_handler,
48 { help_text => $scriptHelp });
49 Xchat::hook_server("PRIVMSG", \&privmsg_handler);
51 Xchat::print("Loading ${B}$scriptName $scriptVersion${B}\n".
52 " For help: ${B}/FILTER HELP${B}\n");
55 # information about (default) options used
56 if ($filter_turned_on) {
57 Xchat::print("Filter turned ${B}ON${B}\n");
58 } else {
59 Xchat::print("Filter turned ${B}OFF${B}\n");
61 if ($limit_to_server) {
62 Xchat::print("Filter limited to server $limit_to_server\n")
64 if ($use_filter_allow) {
65 Xchat::print("Filter uses ALLOW rules\n");
68 # ------------------------------------------------------------
70 my @filter_allow = (
71 q/^\@search\s/,
74 my @filter_deny = (
75 q/\@/,
76 q/^\s*\!/,
77 q/slot\(s\)/,
78 #q/~&~&~/,
80 #xdcc
81 q/^\#\d+/,
83 #fserves
84 q/(?i)fserve.*trigger/,
85 q/(?i)trigger.*\!/,
86 q/(?i)trigger.*\/ctcp/,
87 q/(?i)type\:\s*\!/,
88 q/(?i)file server online/,
90 #ftps
91 q/(?i)ftp.*l\/p/,
93 #CTCPs
94 q/SLOTS/,
95 q/MP3 /,
97 #messages for when a file is received/failed to receive
98 q/(?i)DEFINITELY had the right stuff to get/,
99 q/(?i)has just received/,
100 q/(?i)I have just received/,
102 #mp3 play messages
103 q/is listening to/,
104 q/\]\-MP3INFO\-\[/,
106 #spammy scripts
107 q/\]\-SpR\-\[/,
108 q/We are BORG/,
110 #general messages
111 q/brave soldier in the war/,
114 # return 1 (true) if text given as argument is to be filtered out
115 sub isFiltered {
116 my $text = shift;
117 my $regexp = '';
119 #strip colour, underline, bold codes, etc.
120 $text = Xchat::strip_code($text);
122 if ($use_filter_allow) {
123 foreach $regexp (@filter_allow) {
124 return 0 if ($text =~ /$regexp/);
128 foreach $regexp (@filter_deny) {
129 return 1 if ($text =~ /$regexp/);
132 return 0;
135 #called when someone says something in the channel
136 #1: address of speaker
137 #2: PRIVMSG constant
138 #3: channel
139 #4: text said (prefixed with :)
140 sub privmsg_handler {
141 # $_[0] - array reference containing the IRC message or command
142 # and arguments broken into words
143 # $_[1] - array reference containing the Nth word to the last word
144 #my ($address, $constant, $chan) = @{$_[0]};
145 my $text = $_[1][3]; # Get server message
147 my $server = Xchat::get_info("host");
150 return Xchat::EAT_NONE unless $filter_turned_on;
151 if ($limit_to_server) {
152 return Xchat::EAT_NONE unless $server eq $limit_to_server;
155 $text =~ s/^://;
157 return isFiltered($text) ? Xchat::EAT_ALL : Xchat::EAT_NONE;
160 # ------------------------------------------------------------
162 sub save_filter {
163 open F, ">$filter_file"
164 or do {
165 Xchat::print("${B}FILTER:${B} Couldn't open file to save filter: $!\n");
166 return 1;
169 Xchat::print("${B}FILTER SAVE >$filter_file${B}\n");
170 foreach my $regexp (@filter_deny) {
171 Xchat::print("/".$regexp."/ saved\n");
172 print F $regexp."\n";
174 Xchat::print("${B}FILTER SAVED ----------${B}\n");
175 close F
176 or do {
177 Xchat::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
178 return 1;
180 return 1;
183 sub load_filter {
184 Xchat::print("${B}FILTER:${B} ...loading filter patterns\n");
185 open F, "<$filter_file"
186 or do {
187 Xchat::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
188 return 1;
190 @filter_deny = <F>;
191 map (chomp, @filter_deny);
192 close F;
194 Xchat::print("${B}FILTER DENY ----------${B}\n");
195 for (my $i = 0; $i <= $#filter_deny; $i++) {
196 Xchat::print(" [$i]: /".$filter_deny[$i]."/\n");
198 Xchat::print("${B}FILTER DENY ----------${B}\n");
201 sub add_rule ( $ ) {
202 my $rule = shift;
204 # always ading rules at the end
205 push @filter_deny, $rule;
208 sub delete_rule ( $ ) {
209 my $num = shift || $#filter_deny;
211 splice @filter_deny, $num, 1;
214 # ============================================================
215 # ============================================================
216 # ============================================================
218 sub filter_command_handler {
219 my $cmd = $_[0][1]; # 1st parameter (after FILTER)
220 my $arg = $_[1][2]; # 2nd word to the last word
221 my $server = Xchat::get_info("host");
224 if (!$cmd || $cmd =~ /^STATUS$/i) {
225 if ($filter_turned_on) {
226 Xchat::print("Filter is turned ${B}ON${B}\n");
227 } else {
228 Xchat::print("Filter is turned ${B}OFF${B}\n");
230 if ($limit_to_server) {
231 if ($server eq $limit_to_server) {
232 Xchat::print("Filter is limited to ${B}current${B} ".
233 "server $limit_to_server\n");
234 } else {
235 Xchat::print("Filter is limited to server ".
236 "$limit_to_server != $server\n");
239 if ($use_filter_allow) {
240 Xchat::print("Filter is using ALLOW rules (before DENY)\n");
243 } elsif ($cmd =~ /^ON$/i) {
244 $filter_turned_on = 1;
245 Xchat::print("Filter turned ${B}ON${B}\n");
247 } elsif ($cmd =~ /^OFF$/i) {
248 $filter_turned_on = 0;
249 Xchat::print("Filter turned ${B}OFF${B}\n");
251 } elsif ($cmd =~ /^SERVER$/i) {
252 if ($limit_to_server) {
253 Xchat::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
254 } else {
255 Xchat::print("${B}FILTER:${B} Limiting filtering to server $server\n");
257 $limit_to_server = $server;
259 } elsif ($cmd =~ /^SERVERON$/i) {
260 if ($limit_to_server) {
261 Xchat::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
262 } else {
263 Xchat::print("${B}FILTER:${B} Limiting filtering to server $server\n");
265 $limit_to_server = $server;
267 $filter_turned_on = 1;
268 Xchat::print("Filter turned ${B}ON${B}\n");
270 } elsif ($cmd =~ /^ALL$/i) {
271 if ($limit_to_server) {
272 Xchat::print("Filter: Removing limit to server $limit_to_server\n");
274 $limit_to_server = 0;
276 } elsif ($cmd =~ /^HELP$/i) {
277 Xchat::print($scriptHelp);
279 } elsif ($cmd =~ /^VERSION$/i) {
280 Xchat::print("${B}$scriptName $scriptVersion${B}\n");
281 Xchat::print(" * URL: http://github.com/jnareb/softsnow-xchat2-filter\n");
282 Xchat::print(" * URL: http://gitorious.org/projects/softsnow-xchat2-filter\n");
283 Xchat::print(" * URL: http://repo.or.cz/w/softsnow_xchat2_filter.git\n");
285 } elsif ($cmd =~ /^DEBUG$/i || $cmd =~ /^INFO$/i) {
286 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
287 Xchat::print("Channel: ".Xchat::get_info("channel")."\n");
288 Xchat::print("Host: ".Xchat::get_info("host")."\n");
289 Xchat::print("Server: ".Xchat::get_info("server")."\n");
290 Xchat::print("Server Id: ".Xchat::get_info("id")."\n");
291 Xchat::print("Network: ".Xchat::get_info("network")."\n");
292 Xchat::print("\n");
293 Xchat::printf("%3u %s rules\n", scalar(@filter_allow), "allow");
294 Xchat::printf("%3u %s rules\n", scalar(@filter_deny), "deny");
295 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
297 } elsif ($cmd =~ /^(?:PRINT|LIST)$/i) {
298 Xchat::print("${B}FILTER PRINT ----------${B}\n");
299 Xchat::print("${B}ALLOW${B}".($use_filter_allow ? ' (on)' : ' (off)')."\n");
300 for (my $i = 0; $i <= $#filter_allow; $i++) {
301 Xchat::print("[$i]: /".$filter_allow[$i]."/\n");
303 Xchat::print("${B}DENY${B}\n");
304 for (my $i = 0; $i <= $#filter_deny; $i++) {
305 Xchat::print("[$i]: /".$filter_deny[$i]."/\n");
307 Xchat::print("${B}FILTER PRINT ----------${B}\n");
309 } elsif ($cmd =~ /^ALLOW$/i) {
310 $use_filter_allow = !$use_filter_allow;
311 Xchat::print("${B}FILTER:${B} ALLOW rules ".
312 ($use_filter_allow ? "enabled" : "disabled")."\n");
314 } elsif ($cmd =~ /^ADD$/i) {
315 my $rule = $arg;
316 if ($rule) {
317 add_rule($rule);
318 Xchat::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
319 } else {
320 Xchat::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
323 } elsif ($cmd =~ /^DEL(?:ETE)$/i) {
324 my $num = $arg;
325 # strip whitespace
326 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
327 SWITCH: {
328 unless ($num) {
329 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
330 $#filter_deny--;
331 Xchat::print("${B}FILTER:${B} deleted successfully last rule\n");
332 last SWITCH;
334 if ($num !~ /^\d+$/) {
335 Xchat::print("${B}FILTER:${B} $num is not a number\n");
336 last SWITCH;
338 if ($num < 0 || $num > $#filter_deny) {
339 Xchat::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
340 last SWITCH;
342 # default
343 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
344 delete_rule($num);
345 Xchat::print("${B}FILTER:${B} deleted successfully rule $num\n");
348 } elsif ($cmd =~ /^SHOW$/i) {
349 my $num = $arg;
350 # strip whitespace
351 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
353 Xchat::print("${B}FILTER:${B} ".(defined $num ? "[$num]" : "last").
354 " rule /".$filter_deny[defined $num ? $num : -1]."/\n");
356 } elsif ($cmd =~ /^SAVE$/i) {
357 save_filter();
358 Xchat::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
360 } elsif ($cmd =~ /^(RE)?LOAD$/i) {
361 load_filter();
362 Xchat::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
364 } else {
365 Xchat::print("Unknown command ${B}/FILTER $_[1][1]${B}\n") if $cmd;
367 return 1;