Minor improvements to filter_command_handler
[softsnow_xchat2_filter.git] / SoftSnow_filter.pl
blob649143e9f0f8432f8df006fd430612fb1100777a
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
7 my $scriptName = "SoftSnow XChat2 Filter";
8 my $scriptVersion = "2.0.2";
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 my $command_list = 'ON|OFF|STATUS|SERVER|SERVERON|ALL|HELP|DEBUG|PRINT|ALLOW|ADD|DELETE|SAVE|LOAD';
17 my $scriptHelp = <<"EOF";
18 ${B}/FILTER $command_list${B}
19 /FILTER ON|OFF - turns filtering on/off
20 /FILTER HELP - prints this help message
21 /FILTER STATUS - prints if filter is turned on, and with what limits
22 /FILTER DEBUG - shows some info; used in debuggin the filter
23 /FILTER PRINT - prints all the rules
24 /FILTER ALLOW - toggle use of ALLOW rules (before DENY).
25 /FILTER SERVER - limits filtering to current server (host)
26 /FILTER SERVERON - limits to server and turns filter on
27 /FILTER ALL - resumes filtering everywhere i.e. removes limits
28 /FILTER SAVE - saves the rules to the file $filter_file
29 /FILTER LOAD - loads the rules from the file, replacing existing rules
30 /FILTER ADD <rule> - add rule at the end of the DENY rules
31 /FILTER DELETE [<num>] - delete rule number <num>, or last rule
32 /FILTER VERSION - prints the name and version of this script
33 /FILTER without parameter is equivalent to /FILTER STATUS
34 EOF
36 Xchat::register($scriptName, $scriptVersion, $scriptDescr);
38 Xchat::hook_command("FILTER", \&filter_command_handler,
39 { help_text => $scriptHelp });
40 Xchat::hook_server("PRIVMSG", \&privmsg_handler);
42 Xchat::print("Loading ${B}$scriptName $scriptVersion${B}\n".
43 " For help: ${B}/FILTER HELP${B}\n");
45 ### config ###
46 my $filter_file = Xchat::get_info("xchatdir") . "/SoftSnow_filter.conf";
48 my $filter_turned_on = 0; # was default turned ON
49 my $limit_to_server = ''; # don't limit to server (host)
50 my $use_filter_allow = 0; # use overrides
51 ### end config ###
53 # information about (default) options used
54 if ($filter_turned_on) {
55 Xchat::print("Filter turned ${B}ON${B}\n");
56 } else {
57 Xchat::print("Filter turned ${B}OFF${B}\n");
59 if ($limit_to_server) {
60 Xchat::print("Filter limited to server $limit_to_server\n")
62 if ($use_filter_allow) {
63 Xchat::print("Filter uses ALLOW rules\n");
66 # ------------------------------------------------------------
68 my @filter_allow = (
69 q/^\@search\s/,
72 my @filter_deny = (
73 q/\@/,
74 q/^\s*\!/,
75 q/slot\(s\)/,
76 #q/~&~&~/,
78 #xdcc
79 q/^\#\d+/,
81 #fserves
82 q/(?i)fserve.*trigger/,
83 q/(?i)trigger.*\!/,
84 q/(?i)trigger.*\/ctcp/,
85 q/(?i)type\:\s*\!/,
86 q/(?i)file server online/,
88 #ftps
89 q/(?i)ftp.*l\/p/,
91 #CTCPs
92 q/SLOTS/,
93 q/MP3 /,
95 #messages for when a file is received/failed to receive
96 q/(?i)DEFINITELY had the right stuff to get/,
97 q/(?i)has just received/,
98 q/(?i)I have just received/,
100 #mp3 play messages
101 q/is listening to/,
102 q/\]\-MP3INFO\-\[/,
104 #spammy scripts
105 q/\]\-SpR\-\[/,
106 q/We are BORG/,
108 #general messages
109 q/brave soldier in the war/,
112 # return 1 (true) if text given as argument is to be filtered out
113 sub isFiltered {
114 my $text = shift;
115 my $regexp = '';
117 #strip colour, underline, bold codes, etc.
118 $text = Xchat::strip_code($text);
120 if ($use_filter_allow) {
121 foreach $regexp (@filter_allow) {
122 return 0 if ($text =~ /$regexp/);
126 foreach $regexp (@filter_deny) {
127 return 1 if ($text =~ /$regexp/);
130 return 0;
133 #called when someone says something in the channel
134 #1: address of speaker
135 #2: PRIVMSG constant
136 #3: channel
137 #4: text said (prefixed with :)
138 sub privmsg_handler {
139 # $_[0] - array reference containing the IRC message or command
140 # and arguments broken into words
141 # $_[1] - array reference containing the Nth word to the last word
142 #my ($address, $constant, $chan) = @{$_[0]};
143 my $text = $_[1][3]; # Get server message
145 my $server = Xchat::get_info("host");
148 return Xchat::EAT_NONE unless $filter_turned_on;
149 if ($limit_to_server) {
150 return Xchat::EAT_NONE unless $server eq $limit_to_server;
153 $text =~ s/^://;
155 return isFiltered($text) ? Xchat::EAT_ALL : Xchat::EAT_NONE;
158 # ------------------------------------------------------------
160 sub save_filter {
161 open F, ">$filter_file"
162 or do {
163 Xchat::print("${B}FILTER:${B} Couldn't open file to save filter: $!\n");
164 return 1;
167 Xchat::print("${B}FILTER SAVE >$filter_file${B}\n");
168 foreach my $regexp (@filter_deny) {
169 Xchat::print("/".$regexp."/ saved\n");
170 print F $regexp."\n";
172 Xchat::print("${B}FILTER SAVED ----------${B}\n");
173 close F
174 or do {
175 Xchat::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
176 return 1;
178 return 1;
181 sub load_filter {
182 Xchat::print("${B}FILTER:${B} ...loading filter patterns\n");
183 open F, "<$filter_file"
184 or do {
185 Xchat::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
186 return 1;
188 @filter_deny = <F>;
189 map (chomp, @filter_deny);
190 close F;
192 Xchat::print("${B}FILTER DENY ----------${B}\n");
193 for (my $i = 0; $i <= $#filter_deny; $i++) {
194 Xchat::print(" [$i]: /".$filter_deny[$i]."/\n");
196 Xchat::print("${B}FILTER DENY ----------${B}\n");
199 sub add_rule ( $ ) {
200 my $rule = shift;
202 # always ading rules at the end
203 push @filter_deny, $rule;
206 sub delete_rule ( $ ) {
207 my $num = shift || $#filter_deny;
209 splice @filter_deny, $num, 1;
212 # ============================================================
213 # ============================================================
214 # ============================================================
216 sub filter_command_handler {
217 my $arg = $_[1][1]; # 1st word to the last word
218 my $server = Xchat::get_info("host");
221 if (!$arg || $arg =~ /^STATUS\b/i) {
222 if ($filter_turned_on) {
223 Xchat::print("Filter is turned ${B}ON${B}\n");
224 } else {
225 Xchat::print("Filter is turned ${B}OFF${B}\n");
227 if ($limit_to_server) {
228 if ($server eq $limit_to_server) {
229 Xchat::print("Filter is limited to ${B}current${B} ".
230 "server $limit_to_server\n");
231 } else {
232 Xchat::print("Filter is limited to server ".
233 "$limit_to_server != $server\n");
236 if ($use_filter_allow) {
237 Xchat::print("Filter is using ALLOW rules (before DENY)\n");
240 } elsif ($arg =~ /^ON\b/i) {
241 $filter_turned_on = 1;
242 Xchat::print("Filter turned ON\n");
244 } elsif ($arg =~ /^OFF\b/i) {
245 $filter_turned_on = 0;
246 Xchat::print("Filter turned OFF\n");
248 } elsif ($arg =~ /^SERVER\b/i) {
249 if ($limit_to_server) {
250 Xchat::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
251 } else {
252 Xchat::print("${B}FILTER:${B} Limiting filtering to server $server\n");
254 $limit_to_server = $server;
256 } elsif ($arg =~ /^SERVERON\b/i) {
257 if ($limit_to_server) {
258 Xchat::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
259 } else {
260 Xchat::print("${B}FILTER:${B} Limiting filtering to server $server\n");
262 $limit_to_server = $server;
264 $filter_turned_on = 1;
265 Xchat::print("Filter turned ${B}ON${B}\n");
267 } elsif ($arg =~ /^ALL\b/i) {
268 if ($limit_to_server) {
269 Xchat::print("Filter: Removing limit to server $limit_to_server\n");
271 $limit_to_server = 0;
273 } elsif ($arg =~ /^HELP\b/i) {
274 Xchat::print($scriptHelp);
276 } elsif ($arg =~ /^VERSION\b/i) {
277 Xchat::print("${B}$scriptName $scriptVersion${B}\n");
278 Xchat::print(" * URL: http://github.com/jnareb/softsnow-xchat2-filter\n");
279 Xchat::print(" * URL: http://gitorious.org/projects/softsnow-xchat2-filter\n");
280 Xchat::print(" * URL: http://repo.or.cz/w/softsnow_xchat2_filter.git\n");
282 } elsif ($arg =~ /^DEBUG\b/i || $arg =~ /^INFO\b/i) {
283 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
284 Xchat::print("Channel: ".Xchat::get_info("channel")."\n");
285 Xchat::print("Host: ".Xchat::get_info("host")."\n");
286 Xchat::print("Server: ".Xchat::get_info("server")."\n");
287 Xchat::print("Server Id: ".Xchat::get_info("id")."\n");
288 Xchat::print("Network: ".Xchat::get_info("network")."\n");
289 Xchat::print("\n");
290 Xchat::printf("%3u %s rules\n", scalar(@filter_allow), "allow");
291 Xchat::printf("%3u %s rules\n", scalar(@filter_deny), "deny");
292 Xchat::print("${B}FILTER DEBUG ----------${B}\n");
294 } elsif ($arg =~ /^(?:PRINT|LIST)\b/i) {
295 Xchat::print("${B}FILTER PRINT ----------${B}\n");
296 Xchat::print("${B}ALLOW${B}".($use_filter_allow ? ' (on)' : ' (off)')."\n");
297 for (my $i = 0; $i <= $#filter_allow; $i++) {
298 Xchat::print("[$i]: /".$filter_allow[$i]."/\n");
300 Xchat::print("${B}DENY${B}\n");
301 for (my $i = 0; $i <= $#filter_deny; $i++) {
302 Xchat::print("[$i]: /".$filter_deny[$i]."/\n");
304 Xchat::print("${B}FILTER PRINT ----------${B}\n");
306 } elsif ($arg =~ /^ALLOW/i) {
307 $use_filter_allow = !$use_filter_allow;
308 Xchat::print("${B}FILTER:${B} ALLOW rules ".
309 ($use_filter_allow ? "enabled" : "disabled")."\n");
311 } elsif ($arg =~ /^ADD\s+(.*)/i) {
312 my $rule = $1;
313 if ($rule) {
314 add_rule($rule);
315 Xchat::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
316 } else {
317 Xchat::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
320 } elsif ($arg =~ /^DEL(?:ETE)?(.*)/i) {
321 my $num = $1;
322 $num =~ s/^\s*(.*?)\s*$/$1/g;
323 SWITCH: {
324 unless ($num) {
325 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
326 $#filter_deny--;
327 Xchat::print("${B}FILTER:${B} deleted successfully last rule\n");
328 last SWITCH;
330 if ($num !~ /^\d+$/) {
331 Xchat::print("${B}FILTER:${B} $num is not a number\n");
332 last SWITCH;
334 if ($num < 0 || $num > $#filter_deny) {
335 Xchat::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
336 last SWITCH;
338 # default
339 Xchat::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
340 delete_rule($num);
341 Xchat::print("${B}FILTER:${B} deleted successfully rule $num\n");
344 } elsif ($arg =~ /^SAVE\b/i) {
345 save_filter();
346 Xchat::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
348 } elsif ($arg =~ /^(RE)?LOAD\b/i) {
349 load_filter();
350 Xchat::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
352 } else {
353 Xchat::print("Unknown command $ {B}/FILTER $arg${B}\n") if $arg;
355 return 1;