#!/usr/bin/perl

# sqrmimport.pl (C)1998 by Miles Lott <milos@insync.net>
# Parse Squirm /usr/local/squirm/etc/squirm.in file, convert to regex patterns
# and create /usr/local/squirm/etc/squirm.patterns file.
# This assumes all sites get redirected to the same page, as this
# was written primarily for site blocking.
# As this is one of my first perl scripts, it probably looks pretty messy
# to the trained eye.  But it works fine for me.

# Display usage information if no command-line arguments are given
if ( $ARGV[0] ) { 
	goto MAIN;
}
else {
	print "\nSqrmimport (C)1998 Miles Lott <milos\@insync.net>\n\nUsage:\n\t$0 rawsitefile > ~/squirmetcdir/squirm.patterns\n\nWhere rawsitefile contains a list of websites to be blocked\n\n"; 
	exit
}

# Main works
MAIN:

# Define the sole redirect site
$redir = "http:/your.redirect.site";

# Open main input file - usually /usr/local/squirm/etc/squirm.in
open ( FILE, "@ARGV[0]") || die "\nCouldn't open input file \"$ARGV[0]\"\nType $0 alone for usage info\n\n";

# Print self-promotional and referential info
$date = localtime;
print"\n\# Squirm site blocking regex patterns\n";
print"\# Generated $date by $0\n\# (C)1998 by Miles Lott <milos\@insync.net>\n\#\n";
print"\# Squirm redirector (C)1998 Chris Foote <chris\@senet.com.au>\n\#\n";
print"\# for use with Squid Proxy Caching software <http:\/\/squid.nlanr.net>\n\#\n";
print"\# The ordering of lines is this file is critical\n";
print"\# Please see http://www.senet.com.au/squirm/ for examples\n\#\n";
print"\# Lines have the form:\n";
print"\#       regex|regexi pattern replacement [[^]accelerator_string[$]]\n";
print"\# or\n\#       abort filename_extension\n\#\n";

# Now scan the input file and rewrite the regex patterns
while (<FILE>) {
	#@field = $_;

	print "regexi ^http://";
	#
	if ( /www.(.*?)\.(.*?)$/ ) { print ".*\\.$1\\.$2"; goto ENDIT }
	if ( /www\.(.*?)\.(.*?)\.(.*?)$/ ) { print ".*\.$2\.$3\.$4"; goto ENDIT }
	if ( /www\.(.*?)\.(.*?)\.(.*?)\.(.*?)$/ ) { print ".*\.$2\.$3\.$4\.$5"; goto ENDIT }
	if ( /(.*?)\.(.*?)\.(.*?)\.(.*?)\.(.*?)$/ ) { print ".*\\.$1\\.$2\\.$3\\.$4\\.$5"; goto ENDIT }
	if ( /(.*?)\.(.*?)\.(.*?)\.(.*?)$/ ) { print ".*\\.$1\\.$2\\.$3\\.$4"; goto ENDIT }
	if ( /(.*?)\.(.*?)\.(.*?)$/ ) { print ".*\\.$1\\.$2\\.$3"; goto ENDIT }
	if ( /(.*?)\.(.*?)$/ ) { print "$1\\.$2"; goto ENDIT }
	ENDIT:
	print "/* $redir";
	print"\n";
}
close FILE;

# These are optional (I think) and configurable.
print "abort *.gif\n";
print "abort *.jpg\n";
print "abort *.htm\n";
print "abort *.html\n"
