#!/usr/bin/perl -w

# Michael Lake, mikel AT speleonics.com.au
# Version: 2002-05-01 (master copy is in /home/mikel/cgi-bin on Alpha !)
# This script is normally run from a cgi-bin directory by a php script.

# TODO: 
# * Add option like "spamob -f animals" to select words only from 
#   the animals list. Or "spamob -o animals" for only animals and
#   "spamob -n animals" for no animals.

use strict;

my $wordFileDir= "/home/mikel/.spamob"; # where all the word files are kept
my @word_files;		# holds a list of all the word files in the directory
my $word_filename;	# the filename of the randomly selected word file
my @words;			# holds a list of all the words read from a word file
my $input;			# stores the string from stdin
my $phrase;			# the phrase which introduces the email
my $word;			# the word that is inserted into the email	

my $tmp;
my $keyword;		# not really used but keep as we might use it in a later version.


srand;	# This initialises the random number pool for later use.
$input = <STDIN>;


# Read in the word files that are found in the Spam Obfuscator directory
# (the grep returns all files except those that match . and ..)
opendir(DIR,"$wordFileDir") || 
	die "Can't find the Spam Obfuscator directory where the word files should be.\n$!"; 
@word_files = grep !/^\.\.?$/, readdir DIR;
closedir(DIR);


# Pick a random file from the word_files directory and open it.
$word_filename = $word_files[rand(@word_files)];
open(FILE,"$wordFileDir/$word_filename") || die "Can't find file: $!"; 

while (<FILE>) {
	if (/^#/) {
		# This is a comment line but may contain a keyword so
		# do a case insensitive search for a "keyword =" string.
		# Then throw away the keyword :-)
		if (/phrase\s*=/i) {
			($keyword, $phrase) = split(/=/);
			$phrase =~ s/^\s*//;	# Remove any whitespace at the start.
			chomp($phrase);
		}
		else {
			# Found just a plain old comment; ditch it.
		}
	}
	else {
		# Assume anything else is a word or just blank lines. 
		s/\s*//;# Get rid of any white space on the line. 
				# This ensures we don't get null words and removes any 
				# whitespace from between words in case the user has written 
				# more than one word. That way we always end up with a technically
				# valid email address. 
		if ($_) {
			push(@words, $_); # add the word to our array...
		}
	}
}

close(FILE);

# Define a phrase to use just in case there was none set in the word file.
if ( !defined $phrase) {
	$tmp = $word_filename;	# Use the word file name as a default.
	$tmp =~ s/_/ /; 
	$phrase = "Remove the $tmp for my email.   ";
}


# Pick a random word from our array.
$word = $words[rand(@words)];
chomp($word);


if (!defined ($input) ) {
	$input  = "%s" . "<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>";
	$input .= "<TT>Mike.Lake@%s.speleonics.com.au</TT>";
}

$tmp = sprintf ("$input", $phrase, $word);

print $tmp;
__END__

# The code below is an example for the case where this script was a 
# CGI script which outputs the HTML page.
# In that case comment out the print $tmp and the __END__ block above.

# Note that there MUST be a blank line after the MIME type header.
print <<End_of_text;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<BODY>
<H1>Mikes Test Web Page</H1>

This is mikes spam obfuscator.<P> 
$tmp
<P>

</BODY>
</HTML>
End_of_text


