7 my $scriptName = "SoftSnow XChat2 Filter";
8 my $scriptVersion = "2.0.2";
9 my $scriptDescr = "Filter out file server announcements and IRC SPAM";
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
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");
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
53 # information about (default) options used
54 if ($filter_turned_on) {
55 Xchat
::print("Filter turned ${B}ON${B}\n");
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 # ------------------------------------------------------------
82 q
/(?i)fserve.*trigger/,
84 q
/(?i)trigger.*\/ctcp
/,
86 q
/(?i)file server online/,
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/,
109 q
/brave soldier in the war/,
112 # return 1 (true) if text given as argument is to be filtered out
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/);
133 #called when someone says something in the channel
134 #1: address of speaker
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;
155 return isFiltered
($text) ? Xchat
::EAT_ALL
: Xchat
::EAT_NONE
;
158 # ------------------------------------------------------------
161 open F
, ">$filter_file"
163 Xchat
::print("${B}FILTER:${B} Couldn't open file to save filter: $!\n");
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");
175 Xchat
::print("${B}FILTER:${B} Couldn't close file to save filter: $!\n");
182 Xchat
::print("${B}FILTER:${B} ...loading filter patterns\n");
183 open F
, "<$filter_file"
185 Xchat
::print("${B}FILTER:${B} Couldn't open file to load filter: $!\n");
189 map (chomp, @filter_deny);
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");
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");
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");
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");
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");
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");
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) {
315 Xchat
::print("${B}FILTER RULE [$#filter_deny]:${B} /$rule/\n");
317 Xchat
::print("Syntax: ${B}/FILTER ADD ${U}rule${U}${B} to add\n")
320 } elsif ($arg =~ /^DEL(?:ETE)?(.*)/i) {
322 $num =~ s/^\s*(.*?)\s*$/$1/g;
325 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[-1]."/\n");
327 Xchat
::print("${B}FILTER:${B} deleted successfully last rule\n");
330 if ($num !~ /^\d+$/) {
331 Xchat
::print("${B}FILTER:${B} $num is not a number\n");
334 if ($num < 0 || $num > $#filter_deny) {
335 Xchat
::print("${B}FILTER:${B} $num outside range [0,$#filter_deny]\n");
339 Xchat
::print("${B}FILTER:${B} deleting /".$filter_deny[$num]."/\n");
341 Xchat
::print("${B}FILTER:${B} deleted successfully rule $num\n");
344 } elsif ($arg =~ /^SAVE\b/i) {
346 Xchat
::print("${B}FILTER:${B} saved DENY rules to $filter_file\n");
348 } elsif ($arg =~ /^(RE)?LOAD\b/i) {
350 Xchat
::print("${B}FILTER:${B} loaded DENY rules from $filter_file\n");
353 Xchat
::print("Unknown command $ {B}/FILTER $arg${B}\n") if $arg;