User:TKN Bot/source

From Encyclopedia Dramatica
Jump to navigation Jump to search
<?php
/*
Copyright (c) 2012, [email protected]
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
$wiki = ;
$wiki_token = ;
$errors = ;

// Settings

$irc_net  = 'irc.wtfux.org';
$RC_feed  = '#ed-logging';
$irc_join = '#wiki';
$irc_dbg  = '#wiki';
$irc_nick = 'TKN_Bot';
$irc_alt  = 'NKT_Bot';
$irc_user = 'USER autobot 0 0 :TKN\'s Bot | problems? /ms send TKN <problem>';
$irc_001  = "PRIVMSG NickServ :id REMOVED\r\n
JOIN $RC_feed,$irc_join,$irc_dbg\r\n";

$wiki_api = 'http://encyclopediadramatica.es/api.php';
$run_page = 'http://encyclopediadramatica.es/User:TKN_Bot/run?action=raw';
$wiki_user = 'TKN Bot';
$wiki_pass = 'REMOVED';
if (file_get_contents($run_page) != "true")
{
	die('DO NOT RUN SET!!!!');
}

$irc_socket = fsockopen("ssl://" . $irc_net,6697,$e1,$e2);
// Reconnect every 30 minutes
$wiki_reconnect = strtotime("30 minutes");
if ($irc_socket)
{
	fwrite($irc_socket,"NICK $irc_nick\r\n");
	fwrite($irc_socket,$irc_user . "\r\n");
	while (!feof($irc_socket))
	{
		if (time() >= $wiki_reconnect)
		{
			wiki_connect();
			$wiki_reconnect = strtotime("30 minutes");
		}
		$data = fgets($irc_socket);
		if (!isset($irc_connected) && preg_match("/:(.+?){1} 001 (.+?)/i",$data))
		{
			$irc_connected = true;
			fwrite($irc_socket,$irc_001);
			wiki_connect();
		}
		// Color codes are evil
		$data = preg_replace("/(\x03([0-9]){1,2},([0-9]){1,2}|\x03([0-9]){1,2}|\x02|\x16|\x18|\x03)/","",$data);
		if (!isset($irc_connected) && preg_match("/:(.+?){1} 433/",$data))
		{
			if (!isset($used_alt))
			{
				$used_alt = true;
				fwrite($irc_socket,"NICK $irc_alt\r\n"); 
			}
			else { die("No nick avail"); }
		}
		if (preg_match("@(\[WikiBot\]\!zalgo\@encyclopediadramatica\.es|NKT\!TKN\@ed\.sysop) PRIVMSG ($RC_feed|$irc_dbg) \:\[\[Special\:Log/newusers\]\]@i",$data))
		{
			preg_match("@\[\[Special\:Log/newusers\]\] create  \* (.+?) \*  A challenger appears!@i",$data,$matches);
			welcome($matches[1]);
		}
		if (preg_match("/^PING (.+?)/",$data,$matches))
		{
			fwrite($irc_socket,"PONG " . $matches[1]);
		}
		echo $data;
	}
}
function wiki_connect() {
	global $wiki_api,$wiki_user,$wiki_pass,$wiki,$irc_socket,$irc_dbg,$wiki_token;
	$temp = tempnam(sys_get_temp_dir(), 'php');
	$wiki = curl_init($wiki_api);
	curl_setopt($wiki,CURLOPT_COOKIEJAR,$temp);
	curl_setopt($wiki,CURLOPT_COOKIEFILE,$temp);
	curl_setopt($wiki,CURLOPT_CUSTOMREQUEST,"POST");
	curl_setopt($wiki,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($wiki,CURLOPT_URL,$wiki_api . "?format=php&action=login&lgname=$wiki_user&lgpass=needTheToken");
	$data = curl_exec($wiki);
	$data = unserialize($data);
	if (!isset($data['error']))
	{
		$token = $data['login']['token'];
		curl_setopt($wiki,CURLOPT_URL,$wiki_api . "?format=php&action=login&lgtoken=$token&lgname=$wiki_user&lgpassword=$wiki_pass");
		$data = curl_exec($wiki);
		$data = unserialize($data);
		$lgstatus = $data['login']['result'];
		// Get an edit token
		curl_setopt($wiki,CURLOPT_URL,$wiki_api . "?format=php&action=query&prop=info&titles=Main_Page&intoken=edit");
		$data = curl_exec($wiki);
		$data = unserialize($data);
		$pageid = array_keys($data['query']['pages']);
		$pageid = $pageid[0];
		$wiki_token = urlencode($data['query']['pages'][$pageid]['edittoken']);
	}
	if ($lgstatus != "Success")
	{
		fwrite($irc_socket,"QUIT :See debug data\r\n");
		die;
	}
}


function welcome($user)
{
	echo "Attempting to welcome $user";
	global $irc_dbg,$irc_socket,$wiki,$wiki_token,$run_page,$wiki_api;
	//fwrite($irc_socket,"PRIVMSG $irc_dbg :Welcoming $user\r\n");
	// Check emergency shutdown before doing anything!
	if (file_get_contents($run_page) != "true" || $wiki_token == )
	{
		//fwrite($irc_socket,"PRIVMSG $irc_dbg :Disregard that, I suck cocks\r\n");
	}
	else
	{
		curl_setopt($wiki,CURLOPT_URL,$wiki_api . "?format=php&action=edit&title=User_talk:$user&createonly&appendtext=Welcome, TKN Bot

This is serious. READ IT.


Before making your first article look over Encyclopedia Dramatica:How 2 EDit for Dummies. If you don't, it is unlikely that your article will survive for more than sixty seconds and you risk being banned by Special:ListAdmins. We realize a lot of you are busy or may be new to ED, so here's just the most important info: :*Read Encyclopedia Dramatica:How 2 EDit for Dummies. Seriously. The information contained within is invaluable. Further tutorials of use can be found here. :*To answer a message, reply on the user's talk page (Tab at the top that says 'discussion'). :*Sign your comments by using four tildes (like this ~~~~) :*These are the sysops. If you have a problem, talk to them. They're here to help; we promise. :*The TJC is the community portal. It's linked to from every page; just look at the top left of the page. Check it daily. :*Use Special:Upload to upload pictures. Be sure to give them meaningful names. Also, do not upload any CP or Bestiality pr0n or you will be b&. :*Feeling social? Enjoy forums? Join here. :*See ED:HELP and ED:POLICY for everything else.
~~~~&token=$wiki_token"); $data = curl_exec($wiki); var_dump($data); } }