# Title:	MT-Blacklist
# Summary:	A plugin for preventing comment and Trackback spam 
# Author:	Jay Allen (http://www.jayallen.org/)
# Version:	1.6.5
# Date:		Aug 4, 2004
#
# Information about this plugin can be found at
# http://www.jayallen.org/projects/mt-blacklist
#
# Until I get some sleep and can think about licenses:
# Copyright 2003 Jay Allen
# This code cannot be redistributed without
# permission from the author. 

package plugins::Blacklist;
use strict;
use MT::Template::Context;
use vars qw($VERSION $config
			$mt_comments_post_handler 
			$mt_ping_post_handler 
			$mt_app_cms_prerun_handler
);
#DEBUG only
#use Data::Dumper;	

$VERSION = '1.6.5.01';


my($MT_DIR);
BEGIN {
  if ($0 =~ m!(.*[/\\])!) {
    $MT_DIR = $1;
  } else {
    $MT_DIR = './';
  }
  unshift @INC, $MT_DIR . 'lib';
  unshift @INC, $MT_DIR . 'extlib';
}

require jayallen::Blacklist;
$config = jayallen::Blacklist::_getConfig();

# The Master Switch
return 1 unless $config && $config->{blacklistActive};


########################################
# Overriding of MT Comment posting 
# and Trackback pinging methods
# (i.e. not via Plugin API)
########################################
#
# Redefinition of MT::App::Comments::post and
# MT::App:Trackback::ping is done in order to catch
# spam before it is posted.  
#
# Originally, my own methods were just a blacklist test.
# If the submission passed the test, then we fell through
# to the original default MT method.  This is the preferred
# way to go.
#
# However, because we want to include a link in the comment/
# trackback notification email, and that code is not 
# abstracted out of the above methods, we must override the 
# WHOLE method and insert our code where it is appropriate.
# This may cause the plugin to be incompatible with the MT
# install if the plugin version is incompatible with the MT
# version.  That said, no harm will come in trying.  If it
# doesn't work, uninstall the plugin and all is normal.
{
	# Trap ugly redefinition warnings
	local $SIG{__WARN__} = sub {  }; 

	# Replace MT::App::Comments::post with our 
	# own subroutine commentPost() but save
	# the code in $mt_comments_post_handler.
	#
	# Right now, because of the damn embedded
	# email, we aren't doing anything with
	# the original method.  I am thinking
	# that it would be good to use it if
	# the user has an incompatible version
	# in which case all they lose is the
	# de-spam link in the email.
    $mt_comments_post_handler = \&MT::App::Comments::post;
    *MT::App::Comments::post = \&commentPost; 

	# Here we use our own pre_run routine for 
	# MT::App::Trackback::pre_run in order to 
	# redefine the MT::App::Trackback::ping
	# method.  I tried to do it similarly to
	# above but never could get it to work for
	# some reason so we hijaack an unused (in
	# MT::App::Trackback, at least) pre_run
	# method...  
    *MT::App::Trackback::pre_run = \&mtAppTrackbackPreRunHdlr; 


}  


sub commentPost {
	my $app = shift;
	my %override = map { $_, 1 } @{$config->{overrideCommentPosting}};

    my $entry_id = $app->{query}->param('entry_id');
    require MT::Entry;
    my $entry = MT::Entry->load($entry_id)
        or return $app->error("No entry_id");

	#warn ("Testing for comment post overriding");
	if ($override{$entry->blog_id}) {
		#warn ("We are now overriding ping post");
        require jayallen::MTBlPost;
        &jayallen::MTBlPost::comment_post_hdlr ($app,$mt_comments_post_handler);

    } else {
	    &$mt_comments_post_handler($app);
	}
}

sub mtAppTrackbackPreRunHdlr {
 	my $app = shift;
	my %override = map { $_, 1 } @{$config->{overridePingPosting}};

	#print STDERR "In ".__PACKAGE__." at ".__LINE__."\n";
	#warn "In ".__PACKAGE__." at ".__LINE__."\n";

    my $entry_id = $app->{query}->param('entry_id');
    require MT::Entry;
    my $entry = MT::Entry->load($entry_id)
        or return $app->error("No entry_id");

	#warn ("Testing for ping post overriding");
	if ($override{$entry->blog_id} && $app->{vtbl}->{'ping'}) {
		#warn ("We are now overriding ping post");
        undef $app->{vtbl}->{'ping'};
        require jayallen::MTBlPing;
        $app->add_methods(ping => \&jayallen::MTBlPing::ping_post_hdlr);

    } 
    
	1;
}

1;
