7 my $filter_file = Xchat
::get_info
("xchatdir") . "/SoftSnow_filter.conf";
10 my $scriptName = "SoftSnow XChat2 Filter";
11 my $scriptVersion = "2.0.1";
12 my $scriptDescr = "Filter out file server announcements and IRC SPAM";
15 my $U = "\cU"; # underline
16 my $C = "\cC"; # start of color sequence
18 my $command_list = 'ON|OFF|STATUS|SERVER|SERVERON|ALL|HELP|DEBUG|PRINT|ALLOW|ADD|DELETE|SAVE|LOAD';
20 my $scriptHelp = <<"EOF";
21 ${B}/FILTER $command_list${B}
22 /FILTER ON|OFF - turns filtering on/off
23 /FILTER HELP - prints this help message
24 /FILTER STATUS - prints if filter is turned on, and with what limits
25 /FILTER DEBUG - shows some info; used in debuggin the filter
26 /FILTER PRINT - prints all the rules
27 /FILTER ALLOW - toggle use of ALLOW rules (before DENY).
28 /FILTER SERVER - limits filtering to current server (host)
29 /FILTER SERVERON - limits to server and turns filter on
30 /FILTER ALL - resumes filtering everywhere i.e. removes limits
31 /FILTER SAVE - saves the rules to the file $filter_file
32 /FILTER LOAD - loads the rules from the file, replacing existing rules
33 /FILTER ADD <rule> - add rule at the end of the DENY rules
34 /FILTER DELETE [<num>] - delete rule number <num>, or last rule
35 /FILTER VERSION - prints the name and version of this script
36 /FILTER without parameter is equivalent to /FILTER STATUS
39 Xchat
::register
($scriptName, $scriptVersion, $scriptDescr);
41 Xchat
::hook_command
("FILTER", \
&filter_command_handler
,
42 { help_text
=> $scriptHelp });
43 Xchat
::hook_server
("PRIVMSG", \
&privmsg_handler
);
45 Xchat
::print("Loading ${B}$scriptName $scriptVersion${B}\n".
46 " For help: ${B}/FILTER HELP${B}\n");
48 my $filter_turned_on = 0; # was default turned ON
49 my $limit_to_server = 0; # don't limit to server (host)
50 my $use_filter_allow = 0; # use overrides
52 # information about (default) options used
53 if ($filter_turned_on) {
54 Xchat
::print("Filter turned ${B}ON${B}\n");
56 Xchat
::print("Filter turned ${B}OFF${B}\n");
58 if ($limit_to_server) {
59 Xchat
::print("Filter limited to server $limit_to_server\n")
61 if ($use_filter_allow) {
62 Xchat
::print("Filter uses ALLOW rules\n");
65 # ------------------------------------------------------------
81 q
/(?i)fserve.*trigger/,
83 q
/(?i)trigger.*\/ctcp
/,
85 q
/(?i)file server online/,
94 #messages for when a file is received/failed to receive
95 q
/(?i)DEFINITELY had the right stuff to get/,
96 q
/(?i)has just received/,
97 q
/(?i)I have just received/,
108 q
/brave soldier in the war/,
111 # return 1 (true) if text given as argument is to be filtered out
116 #strip colour, underline, bold codes, etc.
117 $text = Xchat
::strip_code
($text);
119 if ($use_filter_allow) {
120 foreach $regexp (@filter_allow) {
121 return 0 if ($text =~ /$regexp/);
125 foreach $regexp (@filter_deny) {
126 return 1 if ($text =~ /$regexp/);
132 #called when someone says something in the channel
133 #1: address of speaker
136 #4: text said (prefixed with :)
137 sub privmsg_handler
{
138 # $_[0] - array reference containing the IRC message or command
139 # and arguments broken into words
140 # $_[1] - array reference containing the Nth word to the last word
141 #my ($address, $constant, $chan) = @{$_[0]};
142 my $text = $_[1][3]; # Get server message
144 my $server = Xchat
::get_info
("host");
147 return Xchat
::EAT_NONE
unless $filter_turned_on;
148 if ($limit_to_server) {
149 return Xchat
::EAT_NONE
unless $server eq $limit_to_server;
154 return isFiltered
($text) ? Xchat
::EAT_ALL
: Xchat
::EAT_NONE
;
157 # ------------------------------------------------------------
160 open F
, ">$filter_file"
162 Xchat
::print("${B}FILTER:${B} Couldn't open file to save filter: $!\n");
166 Xchat
::print("${B}FILTER SAVE >$filter_file${B}\n");
167 foreach my $regexp (@filter_deny) {
168 Xchat
::print("/".$regexp."/ saved\n");
169 print F
$regexp."\n";
171 Xchat
::print("${B}FILTER SAVED ----------${B}\n");
174 Xchat
::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
181 Xchat
::print("${B}FILTER:${B} ...loading filter patterns\n");
182 open F
, "<$filter_file"
184 Xchat
::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
188 map (chomp, @filter_deny);
191 Xchat
::print("${B}FILTER DENY ----------${B}\n");
192 for (my $i = 0; $i <= $#filter_deny; $i++) {
193 Xchat
::print(" [$i]: /".$filter_deny[$i]."/\n");
195 Xchat
::print("${B}FILTER DENY ----------${B}\n");
201 # always ading rules at the end
202 push @filter_deny, $rule;
205 sub delete_rule
( $ ) {
208 # we have checked that $num is inside the boundaries
209 @filter_deny = (@filter_deny[0..$num-1],@filter_deny[$num+1..$#filter_deny]);
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");
220 #Xchat::print("/filter arg: |$arg|\n");
221 if (!$arg || $arg =~ /^STATUS\b/i) {
222 if ($filter_turned_on) {
223 Xchat
::print("Filter is turned ${B}ON${B}\n");
225 Xchat
::print("Filter is turned ${B}OFF${B}\n");
227 if ($limit_to_server) {
228 Xchat
::print("Filter is limited to ".
229 ($server eq $limit_to_server ?
"${B}current${B} " : "" ).
230 "server $limit_to_server");
232 if ($use_filter_allow) {
233 Xchat
::print("Filter is using ALLOW rules (before DENY)\n");
236 } elsif ($arg =~ /^ON\b/i) {
237 $filter_turned_on = 1;
238 Xchat
::print("Filter turned ON\n");
240 } elsif ($arg =~ /^OFF\b/i) {
241 $filter_turned_on = 0;
242 Xchat
::print("Filter turned OFF\n");
244 } elsif ($arg =~ /^SERVER\b/i) {
245 if ($limit_to_server) {
246 Xchat
::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
248 Xchat
::print("${B}FILTER:${B} Limiting filtering to server $server\n");
250 $limit_to_server = $server;
252 } elsif ($arg =~ /^SERVERON\b/i) {
253 if ($limit_to_server) {
254 Xchat
::print("${B}FILTER:${B} Changing server from $limit_to_server to $server\n");
256 Xchat
::print("${B}FILTER:${B} Limiting filtering to server $server\n");
258 $limit_to_server = $server;
260 $filter_turned_on = 1;
261 Xchat
::print("Filter turned ${B}ON${B}\n");
262 } elsif ($arg =~ /^ALL\b/i) {
263 if ($limit_to_server) {
264 Xchat
::print("Filter: Removing limit to server $limit_to_server\n");
266 $limit_to_server = 0;
268 } elsif ($arg =~ /^HELP\b/i) {
269 Xchat
::print($scriptHelp);
271 } elsif ($arg =~ /^VERSION\b/i) {
272 Xchat
::print("${B}$scriptName $scriptVersion${B}\n");
274 } elsif ($arg =~ /^DEBUG\b/i || $arg =~ /^INFO\b/i) {
275 Xchat
::print("${B}FILTER DEBUG ----------${B}\n");
276 Xchat
::print("Channel: ".Xchat
::get_info
("channel")."\n");
277 Xchat
::print("Server: ".Xchat
::get_info
("server")."\n");
278 Xchat
::print("Network: ".Xchat
::get_info
("network")."\n");
279 Xchat
::print("Host: ".Xchat
::get_info
("host")."\n");
280 Xchat
::print("${B}FILTER DEBUG ----------${B}\n");
282 } elsif ($arg =~ /^PRINT/i) {
283 Xchat
::print("${B}FILTER PRINT ----------${B}\n");
284 Xchat
::print("${B}ALLOW${B}".($use_filter_allow ?
' (on)' : ' (off)')."\n");
285 for (my $i = 0; $i <= $#filter_allow; $i++) {
286 Xchat
::print("[$i]: /".$filter_allow[$i]."/\n");
288 Xchat
::print("${B}DENY${B}\n");
289 for (my $i = 0; $i <= $#filter_deny; $i++) {
290 Xchat
::print("[$i]: /".$filter_deny[$i]."/\n");
292 Xchat
::print("${B}FILTER PRINT ----------${B}\n");
294 } elsif ($arg =~ /^ALLOW/i) {
295 $use_filter_allow = !$use_filter_allow;
296 Xchat
::print("${B}FILTER:${B} ALLOW rules ".
297 ($use_filter_allow ?
"enabled" : "disabled")."\n");
299 } elsif ($arg =~ /^ADD\s+(.*)/i) {
302 Xchat
::print("${B}FILTER RULE [$#filter_deny]:${B} /$1/\n");
304 Xchat
::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
307 } elsif ($arg =~ /^DELETE(.*)/i) {
309 $num =~ s/^\s*(.*?)\s*$/$1/g;
312 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
314 Xchat
::print("${B}FILTER:${B} deleted successfully last rule\n");
317 if ($num !~ /^\d+$/) {
318 Xchat
::print("${B}FILTER:${B} $num is not a number\n");
321 if ($num < 0 || $num > $#filter_deny) {
322 Xchat
::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
325 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
327 Xchat
::print("${B}FILTER:${B} deleted successfully rule $num\n");
330 } elsif ($arg =~ /^SAVE/i) {
332 Xchat
::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
334 } elsif ($arg =~ /^(RE)?LOAD/i) {
336 Xchat
::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
339 Xchat
::print("/filter arg: |$arg|\n") if $arg;