#!/usr/bin/perl
use MediaWiki::Bot;
our $VERSION = '1.0.1';

=head1 COPYING

Copyright (C) 2006, 2007 by Dan Collins <DCOLLINS@cpan.org>

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut

$editor=new MediaWiki::Bot('Bot/WP/EN/ST47/CSDWarnBot', "&assert=bot", "ST47");
open(FH, './pass'); 
$password=<FH>;
chomp($password);
close FH;
$editor->login('CSDWarnBot', $password);

#First, we need a list of pages
@pages=$editor->get_pages_in_category('Category:Candidates for speedy deletion');

#Ok. Here's how it works.
#First, figure out the deletion reason
#Use an assoc.array to figure out what to do with it
#Warn the user
%templates=("db-web" => "User:CSDWarnBot/web-warn",
"db-empty" => "User:CSDWarnBot/empty-warn",
"db-nocontext" => "User:CSDWarnBot/empty-warn",
"db-foreign" => "User:CSDWarnBot/foreign-warn",
"db-notenglish" => "User:CSDWarnBot/foreign-warn",
"db-nocontent" => "User:CSDWarnBot/empty-warn",
"db-blankcsd" => "User:CSDWarnBot/blank-warn",
"db-transwiki" => "User:CSDWarnBot/transwiki-warn",
"db-attack" => "User:CSDWarnBot/attack-warn",
"db-band" => "User:CSDWarnBot/nn-warn",
"db-club" => "User:CSDWarnBot/nn-warn",
"db-bio" => "User:CSDWarnBot/nn-warn",
"db-inc" => "User:CSDWarnBot/nn-warn",
"db-shoutout" => "User:CSDWarnBot/nn-warn",
"db-catempty" => "User:CSDWarnBot/catempty-warn",
"db-c2" => "User:CSDWarnBot/c2-warn",
"db-catfd" => "User:CSDWarnBot/catfd-warn",
"db-pagemove" => "User:CSDWarnBot/pagemove-warn",
"db-nonsense" => "User:CSDWarnBot/nonsense-warn",
"db-spam" => "User:CSDWarnBot/spam-warn",
"db-copyvio" => "User:CSDWarnBot/copyvio-warn",
"db-g12" => "User:CSDWarnBot/copyvio-warn",
"db-test" => "User:CSDWarnBot/test-warn",
"db-vandalism" => "User:CSDWarnBot/vandalism-warn",
"db-repost" => "User:CSDWarnBot/repost-warn",
"db-disambig" => "User:CSDWarnBot/disambig-warn",
"db-redundantimage" => "User:CSDWarnBot/badname-warn",
"db-noimage" => "User:CSDWarnBot/noimage-warn",
"db-noncom" => "User:CSDWarnBot/noncom-warn",
"db-ccnoncom" => "User:CSDWarnBot/noncom-warn",
"db-unksource" => "User:CSDWarnBot/unksource-warn",
"db-norat" => "User:CSDWarnBot/norat-warn",
"db-badfairuse" => "User:CSDWarnBot/badfairuse-warn",
"duplicate" => "User:CSDWarnBot/badname-warn",
"db-emptyportal" => "User:CSDWarnBot/emptyportal-warn",
"db-redirnone" => "User:CSDWarnBot/redirnone-warn",
"db-redirtypo" => "User:CSDWarnBot/redirtypo-warn",
"csd-c3" => "User:CSDWarnBot/catfd-warn",
"cc-by-nc-sa" => "User:CSDWarnBot/noncom-warn",
"cc-nd-nc" => "User:CSDWarnBot/noncom-warn",
"cc-nc" => "User:CSDWarnBot/noncom-warn",
"cc-by-nc-2.0" => "User:CSDWarnBot/noncom-warn",
"cc-by-nc-sa-2.0" => "User:CSDWarnBot/noncom-warn",
"cc-by-nd-nc-2.0" => "User:CSDWarnBot/noncom-warn",
"cc-by-2.0-nc-nd" => "User:CSDWarnBot/noncom-warn",
"cc-by-nc-nd-2.0" => "User:CSDWarnBot/noncom-warn",
"db-contact" => "User:CSDWarnBot/empty-warn",
"db-i2" => "User:CSDWarnBot/noimage-warn",
"db-i1" => "User:CSDWarnBot/badname-warn",
"communityUseOnly" => "User:CSDWarnBot/noncom-warn",
"cc-by-nc-nd-2.5-it" => "User:CSDWarnBot/noncom-warn",
"db-disparage" => "User:CSDWarnBot/disparage-warn",
"db-u3" => "User:CSDWarnBot/u3-warn");

open(FH, '.CWB');
foreach $record (<FH>) {
	chomp($record);
	$warned{$record}++; #Pages we've already seen.
}
close FH;

MAIN: foreach (@pages) {
	$warntemplate="";
	$page=$_;
	print "-"x80;
	print "$page\n";
	$pagetext=$editor->get_text($page);
	$pagetext='' if $pagetext==2;
	#Now, let's iterate through the templates until we find one we can use
	TT: while ($pagetext=~/\{\{(.+?)[\|\}]/g) {
		$template=$1;
#		print "$template\n";
		foreach (keys %templates) { #This loop could be done with a single $templates{lc($template)}
			$testing=$_;
#			print ":$testing\n";
			if (lc($template) eq lc($testing)) {
				$warntemplate=$templates{$testing};
				print "$template\n";
				print "$warntemplate\n";
				last TT;
			}
		}
	}
	if ($warntemplate) {
		@hist=$editor->get_users($page, 50);
		unless (@hist) {next MAIN}
		if (scalar(@hist) >= 48) {next MAIN}
		$user1=shift(@hist);
		$user=pop(@hist);
		$user2=shift(@hist);
		print "$user, $user1\n";
		if (($user eq $user1) or (not $user1)) {print "No other contributors\n"; next MAIN;}
		if ($user=~/ecretlondon/) {next MAIN;} #Optout.
		$ut="User talk:$user";
		$utt=$editor->get_text($ut);
		$utt='' if $utt==2;
		next MAIN if $utt==1;
		($pagenons=$page)=~s/.+:(.+)/\1/; #There was a time I knew what this did. Not anymore. I believe the assignment returns the left side, if that helps.
		if ($utt=~/$page/i) {
			print "Warned\n";
			next;
		}
		if ($utt=~/\Q$page\E/i) { #Why can't we use one single regex?
			print "Warned\n";
			next;
		}
		if ($utt=~/:$pagenons/i) {
			print "Warned\n";
			next;
		}
		if ($utt=~/\Q:$pagenons\E/i) {
			print "Warned\n";
			next;
		}
		if ($utt=~/User:CSDWarnBot/s) {
			print "Not spamming\n";
			next;
		}
		if ($utt=~/\{nobots/i) { #I can't be bothered to implement the full exclusion standard. If you're using nobots and you want these messages, fuck you.
			print "Bot excluded from page\n";
			next;
		}
		if ($warned{lc($pagenons)}) {
			print "Flood control\n";
			next;
		}
		$warned{lc($pagenons)}++;
		$utt .= "\n\n{{subst:$warntemplate|$page}} ~~~~";
		$editor->edit($ut, $utt, "Regarding [[$page]]");
		print "Sent!\n";
	}
}

open(FH, ">.CWB"); #This text file is probably fucking huge.
foreach (keys %warned) { 
	print FH "$_\n";
}
close FH;

=head1 NAME

CSDWarnBot, a Wikipedia bot for advising users of pages which are tagged 
for speedy deletion.

=pod SCRIPT CATEGORIES

Web

=cut
