7 my $scriptName = "SoftSnow XChat2 Filter";
8 my $scriptVersion = "2.0.3";
9 my $scriptDescr = "Filter out file server announcements and IRC SPAM";
12 my $U = "\cU"; # underline
13 my $C = "\cC"; # start of color sequence
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
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
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");
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 # ------------------------------------------------------------
84 q
/(?i)fserve.*trigger/,
86 q
/(?i)trigger.*\/ctcp
/,
88 q
/(?i)file server online/,
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/,
111 q
/brave soldier in the war/,
114 # return 1 (true) if text given as argument is to be filtered out
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/);
135 #called when someone says something in the channel
136 #1: address of speaker
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;
157 return isFiltered
($text) ? Xchat
::EAT_ALL
: Xchat
::EAT_NONE
;
160 # ------------------------------------------------------------
163 open F
, ">$filter_file"
165 Xchat
::print("${B}FILTER:${B} Couldn't open file to save filter: $!\n");
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");
177 Xchat
::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
184 Xchat
::print("${B}FILTER:${B} ...loading filter patterns\n");
185 open F
, "<$filter_file"
187 Xchat
::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
191 map (chomp, @filter_deny);
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");
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");
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");
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");
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");
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");
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) {
318 Xchat
::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
320 Xchat
::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
323 } elsif ($cmd =~ /^DEL(?:ETE)$/i) {
326 $num =~ s/^\s*(.*?)\s*$/$1/g if $num;
329 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
331 Xchat
::print("${B}FILTER:${B} deleted successfully last rule\n");
334 if ($num !~ /^\d+$/) {
335 Xchat
::print("${B}FILTER:${B} $num is not a number\n");
338 if ($num < 0 || $num > $#filter_deny) {
339 Xchat
::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
343 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
345 Xchat
::print("${B}FILTER:${B} deleted successfully rule $num\n");
348 } elsif ($cmd =~ /^SHOW$/i) {
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) {
358 Xchat
::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
360 } elsif ($cmd =~ /^(RE)?LOAD$/i) {
362 Xchat
::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
365 Xchat
::print("Unknown command ${B}/FILTER $_[1][1]${B}\n") if $cmd;
370 Xchat
::print("{B}$scriptName $scriptVersion${B} loaded\n");