#!/usr/bin/perl



use File::Find;

@DIRLIST = (".");  # The directories to treat.

# This function encodes one 'mailto:' link
sub EncodeMailTo {
   local($Mail,$tag)=@_;
   my($name,$domain)=split("@",$Mail);
   local($ind);
   $ind++;
   $code="<SCRIPT LANGUAGE=\"javascript\">"."\n"
   ."<!-- "."\n"
   ."var Domain = \"$domain\";"."\n"
   ."var MailLink${ind} = \"mail\" + \"to:\" + \"$name\&\#64;\" + Domain;"."\n"
   ."var MailAnchor${ind} = \"$name\&\#64;\" + Domain;"."\n"
   ."document.write(\"<a href=\\\"\" + MailLink${ind} + \"\\\">\");"."\n"
   ."document.write(MailAnchor${ind} + \"</a>\");"."\n"
   ."// -->"."\n"
   ."</SCRIPT>"."\n";
   return $code;
}

# Make an html page resistant to spam.
sub ResistSPAM {
   local($fvalue)=@_;

   $fvalue =~ s/<a\s+href\s*=\s*\"mailto:([^"]+)"\s*>(.*?)<\/a>/EncodeMailTo($1,$2)/eig;

   return $fvalue;
}

sub process_file {
   my $filename;
   my $sep;
   my $content;
   my $newcontent;
   $filename = $File::Find::name;
   if ($filename =~ /\.(htm|html|asp)$/) {
     # This is an html file
     $sep = $/;
     $/ = undef;
     open(FILE,"+< $filename");		 		 
     $content = <FILE>;
     $newcontent = ResistSPAM($content);
     if($content ne $newcontent) {
       print "$filename changed";
     }
     seek FILE,0,SEEK_SET;
     print FILE $newcontent;  # Newcontent is bigger
     close FILE;
     $/ = $sep;
   }
}

find(\&process_file, @DIRLIST);