#!/usr/bin/perl -w
# VIM: set ts=4, set sw=4
# 
# Adapted from a script or two online. This is:
# BLOCKER
# An xchat script to avoid /msgs from off-channel.
# Alpha version, May 2002
# Em Tonkin (etonkin@free.fr)

package IRC::XChat::HilightExec;

#my $filename=`echo \$USER`;
my $filename=$ENV{'HOME'};
$filename="$filename/IRCunblockedlist";
my $script_name    = "hilight-exec.pl";
my $script_version = '0.1.0';

# config... just the command line ...
my $command = '/path/to/prog -with -some -args';
# end config 
$command .= ' &';
IRC::register($script_name,$script_version, "", "");
IRC::print("\cC3\cB'Blocker'\cO - a spamfilter by \cB\cC7emling\cO the lesser\n");
IRC::print("\cB\cC20Type /blocker for online help!\cO\n");


my $pkg = __PACKAGE__;

# Need to load this from file :-)
my @unblockedlist;
my $status=1;
# Read unblockedlist
if(-e $filename ){
    

open FILEHANDLE, $filename or die ("Couldn't read unblockedlist  !\n") ;
while(<FILEHANDLE>){
	chomp;
	$temp=$_;
	push(@unblockedlist,$temp);
	
}
close FILEHANDLE;
$temp=join(',',@unblockedlist);
IRC::print("\cB\cC11Currently unblocked:\cO $temp \n");

}
IRC::add_message_handler("PRIVMSG", "${pkg}::msg_privmsg");
IRC::add_command_handler("UNBLOCK", "${pkg}::sub_unblock");
IRC::add_command_handler("BLOCK", "${pkg}::sub_block");
IRC::add_command_handler("SAVEBLOCK", "${pkg}::sub_saveblock");
IRC::add_command_handler("VIEWBLOCK", "${pkg}::sub_viewblock");
IRC::add_command_handler("BLOCKER", "${pkg}::sub_blocker");

sub sub_blocker {
	my $command=shift;
	if($command eq "on") {
	IRC::print("Turning blocker \cB\cC4on\cO\n");

	$status=1;
	} elsif ($command eq "off") {
		$status=0;
		IRC::print("Turning blocker \cB\cC3off\cO\n");
	} elsif($command eq "all"){
		IRC::print("Turning off \cB\cC4ALL messages\cO\n");
		$status=2;
	} else {
		IRC::print("\cB\cC7Blocker\cO usage instructions:\n");
		IRC::print("\cC3/blocker on\cO turns it on, \cC3/blocker off\cO turns it off\n");
		IRC::print("\cC3/blocker all\cO blocks ALL messages (undo with /blocker off or on)\n");
		IRC::print("\cC3/unblock somebody\cO unblocks that person, \cC3/block somebody\cO blocks them\n");
		IRC::print("\cC3/saveblock\cO saves the current blocker settings\n");
		IRC::print("\cC3/viewblock\cO displays them!   -- that's it!\n");
	}
 	return 1;
}

sub sub_viewblock {
	IRC::print("These nicks are currently unblocked from private msg:",join(',',@unblockedlist),"\n");
return 1;
}

sub sub_block { 
   my $nick_to_block=shift;
   push(@temp,@unblockedlist);
   @unblockedlist=grep { $nick_to_block ne $_ } @unblockedlist;
   $tmp=join(',',@unblockedlist);
   IRC::print("Still unblocked: $tmp\n");
  return 1;
}

sub sub_saveblock {
	open(FILEHANDLE,">$filename") or die("Couldn't create file $filename\n");
	foreach my $foo ( @unblockedlist ) {
		print FILEHANDLE "$foo\n";
	}
	close FILEHANDLE;
	IRC::print("Unblock list has been saved!\n");
	return 1;
}

sub sub_unblock {
	my $nick_to_unblock=shift;
	$nick_to_unblock=lc($nick_to_unblock);
        if (grep(/$nick_to_unblock/,@unblockedlist)) {
		IRC::print("Nick $nick_to_unblock already unblocked! \n");
	} else {
		IRC::print("Am unblocking $nick_to_unblock \n");
		if (-e $filename) {
		open (FILEHANDLE, ">>$filename") or die("Couldn't write to file $filename\n");
		print FILEHANDLE "$nick_to_unblock\n";
		close FILEHANDLE;		
	} else {
                open (FILEHANDLE, ">$filename") or die("Couldn't create file $filename\n");
                print FILEHANDLE "$nick_to_unblock\n";
                close FILEHANDLE;

	}
	}
        push(@unblockedlist,$nick_to_unblock);
        return 1;
}

sub msg_privmsg {
    # The private message handler. Does lots of pretty things
    my $line = shift;
    my @namesonline;
    my $returnvalue=1;
    reset(@namesonline);    
   if($status==1) {
   # Get channels that you are on...
   # should alter this later so it checks one server at a time :-/
   my @servers=IRC::server_list();    
   my $foobar=IRC::server_list();
   foreach my $server (@servers) {
       @channels=get_chans($server);
      foreach my $chan_total(@channels) {
          # See who's on those channels
   	  # This doesn't work - it's all a little too complex for that

          # push (@namesonline,IRC::user_list($chan_total,$server));

	  # We get the output of irc::userlist
	  @test=IRC::user_list($chan_total,$server);
         for($temp=0;$temp<$#test; $temp=$temp+5) {
		#IRC::print("Test : $test[$temp]\n"); 
		push(@namesonline,$test[$temp]);
			       
         } 
	  #IRC::print("Test: ",join(' ',@test),"\n");
      }
   }

    push(@namesonline,@unblockedlist);
    $temp=join(',',@namesonline);
    IRC::print("Total valid nicks: $temp\n");
	

    $line =~ /:.*?\s*([^!]*)!([^@]*@[^ ]*) +(.*?)\s*:(.*)/;
    my ($nick,$host,$chan,$text) = ($1,$2,$3,$4);


    foreach my $possiblenick (@namesonline) {
	if(lc($possiblenick) eq lc($nick)){
		$returnvalue=0;
	}
    }
    if($returnvalue==1){
    	IRC::print("There was a message for you from $nick, which was blocked \n");
    	IRC::print(" $nick, $host $chan $text \n");
    	IRC::print("To receive further messages from them, type /unblock $nick \n");
    }
   } elsif($status==2) {
	$returnvalue=1;
	}else {
	$returnvalue=0;
   }
    return $returnvalue;
}

sub get_chans {
    my $server= shift;
#    IRC::print("Server is $server");
    # Returns the names of the channels you're currently on...
    my @channel_listing=IRC::channel_list();
    my $tempint=1;
    my @channels;
    reset(@channels);

    # There are $#array entries in an array...
    for($temp=0;$temp<$#channel_listing; $temp=$temp+3) {
	if(@channel_listing[$temp+1]=~/$server/i){

        	#IRC::print("Channel name on $server ",@channel_listing[$temp]);
                push(@channels,@channel_listing[$temp]);
	}

        # IRC::print("Servername  ",@channel_listing[$temp+1]);
        # nick YOU have on that channel
        # IRC::print(@channel_listing[$temp+2]);
    }
    #foreach my $value (@channel_listing) {
	#if($tempint==1) {
	#	push(@channels,$value);		   
	#} elsif ($tempint==2){
	#	IRC::print(" server:  $value");
	#}

     #   if ($tempint%3==0) {
      #          $tempint=1;
       # } else {
       #         $tempint++;
       # }
   #}
    my $temp=join(' ',@channels);
#    IRC::print("You are on channels: $temp \n");
	
	return @channels;
}

1;
#end
