User:X!/Source

From Wikipedia, the free encyclopedia

The following is automatically generated by X!. It uses ClueBot classes for interacting with the wiki, and also uses some of ClueBot's code for interacting with IRC.

<?PHP

/* 
 * SoxBot Anti Testing Bot
 * Copyright (C) 2009 X! (soxred93 _at_ gmail _dot_ com)
 *
 * This program 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 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
 
	ini_set('memory_limit', '64M');
	define( 'SIGNATURE', '[[User:SoxBot III|SoxBot III]] ([[User talk:SoxBot III|talk]] | [[User:X!|owner]])' );
	define( 'SCORELIMIT', -3 );

	$testlist = array(
		'/\[\[(Link title|File:Example\.jpg|Media:Example\.ogg)\]\]/' => -5,
		'/\'\'\'Bold text\'\'\'/'			                          => -3,
		'/\'\'Italic text\'\'/'			                              => -3,
		'/\'\'\'\'\'Bold text\'\'\'\'\'/'			                  => -6,
		'/\'\'\'\'\'Italic text\'\'\'\'\'/'			                  => -6,
		'/\[http:\/\/www\.example\.com link title\]/'	              => -8,
		'/== Headline text ==/'				                          => -12,
		'/\<math\>Insert formula here\<\/math\>/'			          => -20,
		'/\<nowiki\>Insert non-formatted text here\<\/nowiki\>/'	  => -20,
		'/#REDIRECT \[\[Insert text\]\]/'			                  => -10,
		'/\<s\>Strike-through text\<\/s\>/'			                  => -3,
		'/\<sup\>Superscript text\<\/sup\>/'		                  => -3,
		'/\<sub\>Subscript text\<\/sub\>/'			                  => -3,
		'/\<small\>Small Text\<\/small\>/'				              => -3,
		'/\<!-- Comment --\>/'		                                  => -15,
		'/\<gallery\>
(Image|File):Example.jpg\|Caption1
(Image|File):Example.jpg\|Caption2
\<\/gallery\>/m'													  => -5,
		'/\<blockquote\>
Block quote
\<\/blockquote\>/m'													  => -5,
		'/'.preg_quote('{| class="wikitable" border="1"
|-
! header 1
! header 2
! header 3
|-
| row 1, cell 1
| row 1, cell 2
| row 1, cell 3
|-
| row 2, cell 1
| row 2, cell 2
| row 2, cell 3
|}').'/m'														      => -5,
		'/\<ref\>Insert footnote text here\<\/ref\>/'				  => -5,
		'/(ghjk|asdf|zxcv)/i'			                              => -8,
		'/--\[\[Special:Contributions\/.*\|.*\]\]/'                   => -5
	);
	function score ($list,$data,&$matches = null) {
		$score = 0;
		foreach ($list as $preg => $pts) {
			if ($x = preg_match_all($preg.'S',$data,$m)) {
				$matches[$preg] = $x;
				$score += $pts * $x;
			}
		}
		return $score;
	}
	function getWarningLevel( $user ) {
		global $wpq;
		$warning = 0;
		$talk = $wpq->getpage('User talk:'.$user);
		if (preg_match_all('/<!-- Template:uw-[a-z]*(\d)(im)? -->.*(\d{2}):(\d{2}), (\d+) ([a-zA-Z]+) (\d{4}) \(UTC\)/iU', $talk,$m,PREG_SET_ORDER)) {
			foreach ($m as $r) {
				$month = array('January' => 1, 'February' => 2, 'March' => 3,'April' => 4, 'May' => 5, 'June' => 6, 'July' => 7, 'August' => 8, 'September' => 9, 'October' => 10,'November' => 11, 'December' => 12);
				if ((time() - gmmktime($r[3],$r[4],0,$month[$r[6]],$r[5],$r[7])) <= (2*24*60*60)) {
					if ($r[1] > $warning) { $warning = $r[1]; }
				}
			}
		}
		echo "\n\n\n$warning\n\n\n";
		return $warning;
	}
	
	declare(ticks = 1);
	function sig_handler($signo) {
		switch ($signo) {
			case SIGCHLD:
				while (($x = pcntl_waitpid(0, $status, WNOHANG)) != -1) {
					if ($x == 0) break;
					$status = pcntl_wexitstatus($status);
				}
				break;
		}
	}
	pcntl_signal(SIGCHLD,   "sig_handler");
	
	
	require('./wikibot.classes.php');
	require('./bot.config.php');
	//require('../database.inc');

	$http   = new http;
	$wpapi	= new wikipediaapi;
	$wpq	= new wikipediaquery;
	$wpi	= new wikipediaindex;
	$wpapi->login($user,$pass);
	$whitelist = $wpq->getpage('User:'.$user.'/Whitelist');


	//Parse User:<owner>/Channels.js, to be joined later
	$ircconfig = explode("\n",$wpq->getpage('User:'.$owner.'/Channels.js'));
	$tmp = array();
	foreach($ircconfig as $tmpline) { if (substr($tmpline,0,1) != '#') { $tmpline = explode('=',$tmpline,2); $tmp[trim($tmpline[0])] = trim($tmpline[1]); } }
	$ircchannel = $tmp['ircchannel'];
	$irctechchannel = $tmp['irctechchannel'];
	$ircverbosechannel = $tmp['ircverbosechannel'];
	$ircotherchannels = $tmp['ircotherchannels'];
	$ircvandalismchannel = $tmp['ircvandalismchannel'];
	$ircaivchannel = $tmp['ircaivchannel'];
	$channels = array_filter(array($ircchannel,$irctechchannel,$ircotherchannels,$ircverbosechannel,$ircvandalismchannel,$ircaivchannel));
	unset($tmp,$tmpline);
	
	$stalk = array();
	$edit = array();
	$stalkedit = array();
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalk.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $stalk[$tmp3[0]] = trim($tmp3[1]); } }
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autoedit.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $edit[$tmp3[0]] = trim($tmp3[1]); } }
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalkedit.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,3); $stalkedit[$tmp3[0]][$tmp3[1]] = trim($tmp3[2]); } }
	unset($tmp,$tmp2,$tmp3);
	
	$st2 = array();
	foreach ($stalkedit as $user => $s2) {
		$st2[$user] = $s2;
	}
	print_r($st2);
	
	//print_r($stalk);print_r($edit);

	$channels = array_unique(array_merge($channels,$stalk,$edit,$st2));
	echo implode(', ', $channels);
	unset($st2);
	

	$mysql = mysql_connect( $mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass 
);
	@mysql_select_db( $mysqldb, $mysql ) or die( "MySQL error: " .mysql_error() );
	$irc = fsockopen($ircserver,$ircport,$ircerrno,$ircerrstr,15);
	$ircpid = pcntl_fork();
	if ($ircpid == 0) {
		fwrite( $irc,'PASS '.$ircpass."\n" ); 
		fwrite( $irc,'USER '.$user.' "1" :SoxBot.'."\n" ); 
		fwrite( $irc,'NICK '.$user."\n" ); 
		fwrite( $irc,'IDENTIFY '.$ircpass."\n" ); 
		sleep(5);
		while (!feof($irc)) {
			$data = str_replace(array("\n","\r"),'',fgets($irc,1024));
			echo 'IRC: '.$data."\n";
			$d = explode(' ',$data);
			$m = $d;
			unset($m[0], $m[1], $m[2]);
			$m = substr(implode(' ', $m),1);
			if (strtolower($d[0]) == 'ping') {
				fwrite( $irc,'PONG '.$d[1]."\n" ); 
			} elseif (($d[1] == '376') or ($d[1] == '422') or (preg_match('/ERROR :Closing Link: (.*) \(Excess Flood\)/', $data) ) ) {
				sleep(5);
				foreach ($channels as $chan) {
					fwrite( $irc,'JOIN '.$chan."\n" ); 
					sleep(2);
				}
				//sleep(15);
				foreach (explode(',',$ircchannel) as $y) {
					fwrite( $irc,'PRIVMSG '.$y.' :IRC logging enabled.'."\n" ); 
				}
			} elseif (preg_match('/ERROR :Closing Link: (.*) \(Excess Flood\)/', $data)) {
				sleep(5);
				echo "Trying to rejoin.\n";
				foreach ($channels as $chan) {
					fwrite( $irc,'JOIN '.$chan."\n" ); 
					sleep(2);
				}
				sleep(15);
				foreach (explode(',',$ircchannel) as $y) {
					fwrite( $irc,'PRIVMSG '.$y.' :IRC logging enabled.'."\n" ); 
				}
			} elseif (strtolower($d[1]) == 'privmsg') {
				//echo 'IRC: '.$data."\n";
				if ($m == 'Thanks, SoxBot!') {
					echo "Yep!\n";
					sleep(rand(3,6));
					fwrite( $irc,'PRIVMSG '.$d[2].' :No problem!'."\n" ); 
				}
				if (substr($d[3],0,2) == ':!') {
					if (strtolower($d[2]) == '#wikipedia-en') { $tmp = explode('!',substr($d[0],1)); $cmd = 'NOTICE '.$tmp[0]; }
					elseif (strtolower($d[2]) == strtolower($user)) { $tmp = explode('!',substr($d[0],1)); $cmd = 'NOTICE '.$tmp[0]; }
					else { $cmd = 'PRIVMSG '.$d[2]; }
					switch (substr(strtolower($d[3]),2)) {
						case 'lastedit':
							if (preg_match("/\[\[(.*)\]\]/",$data,$m)) {
								$rv = $wpapi->revisions($m[1],1,'older');
								fwrite($irc,$cmd.' :[['.$m[1].']] http://en.wikipedia.org/w/index.php?title='.urlencode($m[1]).'&diff=prev' .
									'&oldid='.urlencode($rv[0]['revid']).' * '.$rv[0]['user'].' * '.$rv[0]['comment']."\n");
							} else {
								fwrite( $irc,$cmd.' :Couldn\'t find link.'."\n" ); 
							}
							break;
						case 'stalk':
							if (preg_match("/\[\[User:(.*)\]\]/",$data,$m)) {
								$uc = $wpapi->usercontribs($m[1],1);
								fwrite($irc,$cmd.' :[['.$uc[0]['title'].']] http://en.wikipedia.org/w/index.php?title='.urlencode($uc[0]['title']).'&diff=prev' .
									'&oldid='.urlencode($uc[0]['revid']).' * '.$m[1].' * '.$uc[0]['comment']."\n");
							} else {
								fwrite( $irc,$cmd.' :Couldn\'t find link.'."\n" ); 
							}
							break;
						case 'status':
							
							$titles = unserialize(file_get_contents('titles.txt'));
							foreach ($titles as $title => $time) {
								if ((time() - $time) > (24*60*60)) {
									unset($titles[$title]);
								}
							}
							file_put_contents('titles.txt',serialize($titles));
							$count = count($titles);

							
							if (!preg_match('/(yes|enable|true)/i',$wpq->getpage('User:'.$user.'/Run'))) {
								$run = false;
							} else {
								$run = true;
							}
							
							
							$top5beat = array();
							if (!mysql_ping($mysql)) { $mysql = mysql_pconnect($mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass,/* Force reconnect --> */ true); mysql_select_db($mysqldb, $mysql); }
							$q = mysql_query('SELECT `user`,COUNT(`id`) AS `count` FROM `cluebot_enwiki`.`beaten` WHERE `user` != \'\' GROUP BY `user` HAVING `count` > 1 ORDER BY `count` DESC LIMIT 5');
							while ($x = mysql_fetch_assoc($q)) {
								$top5beat[] = $x['user'].' ('.$x['count'].')';
							}
							unset($x,$q);
							$top5beat = implode(' - ',$top5beat);


							fwrite( $irc,$cmd.' :I am '.$user.'.  I am currently '.($run?'enabled':'disabled').'.  I currently have '.$wpq->contribcount($user).' contributions.'."\n" ); 

							fwrite( $irc,$cmd.' :I have attempted to revert '.$count.' unique article/user combinations in the last 24 hours. '."\n" ); 
							fwrite( $irc,$cmd.' :The following users have beat me to the revert the most: '.$top5beat."\n" ); 

							fwrite( $irc,$cmd.' :I log all information to '.$ircchannel.'.  This channel is '.$d[2].'.'."\n" ); 

							unset($count,$run,$time,$top5beat);
							break;
						case 'beaten':
							if (preg_match("/\[\[User:(.*)\]\]/",$data,$m)) {
								$x = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) AS `count` FROM `beaten` WHERE `user` = \''.mysql_real_escape_string($m[1]).'\' GROUP BY `user`'));
								$y = mysql_fetch_assoc(mysql_query('SELECT SQL_CALC_FOUND_ROWS COUNT(`id`) AS `count2` FROM `beaten` GROUP BY `user` HAVING `count2` > \''.mysql_real_escape_string($x['count']).'\' LIMIT 1'));
								$z = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() as `ahead`'));
								fwrite( $irc,$cmd.' :[[User:'.$m[1].']] has beaten me '.(($x['count'] != '')?$x['count']:'0').' times.  There are '.$z['ahead'].' users who have beaten me more times.'."\n" ); 
								unset($x,$y);
							} else {
								fwrite( $irc,$cmd.' :Couldn\'t find link.'."\n" ); 
							}
							break;
						case 'count':
							if (preg_match("/\[\[User:(.*)\]\]/",$data,$n)) {
								fwrite( $irc,$cmd.' :[[User:'.$n[1].']] has '.$wpq->contribcount($n[1])." contributions. For more info, see http://toolserver.org/~soxred93/ec/".$n[1]." \n" ); 
							} else {
								fwrite( $irc,$cmd.' :Couldn\'t find link.'."\n" ); 
							}
							break;
						case 'maxlag':
							$ml = $wpi->getMaxlag();
							print_r($ml);
							fwrite( $irc,$cmd.' :Current maxlag level: '.$ml[2]." seconds lagged. Server: ".$ml[1].". \n" ); 
							break;
						case 'eval':
							$tmp = explode(' ',$data,6);
							$tmp1 = explode('!',substr($d[0],1));
							if ($d[4] == md5($thesecret.'-'.$user)) {
								unset($tmp[0], $tmp[1], $tmp[2], $tmp[3], $tmp[4]);
								$code = implode($tmp, ' ');
								echo "\n\n\n\n\n".$code."\n\n\n\n\n";
								$result = eval($code);
								fwrite( $irc,$cmd.' :Eval result: "'.$result.'"'."\n" ); 
								unset($tmp, $code);
							} else {
								fwrite( $irc,$cmd.' :Code incorrect.'."\n" ); 
							}
							break;
					}
				}
			}
		}
		die();
	}


	$wpi->forcepost('User:'.$user.'/Source',
		'The following is automatically generated by [[User:'.$user.'|'.$user."]]. It uses [[User:ClueBot/Source|ClueBot classes]] for interacting with the wiki, and also uses some of ClueBot's code for interacting with IRC.\n\n".'<pre>'.htmlentities(file_get_contents(__FILE__))."</pre>\n\n\n\n",
		'Automated source upload.');

	
	$stalk = array();
	$edit = array();
	$stalkedit = array();
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalk.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $stalk[$tmp3[0]] = trim($tmp3[1]); } }
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autoedit.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $edit[$tmp3[0]] = trim($tmp3[1]); } }
	$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalkedit.js'));
	foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,3); $stalkedit[$tmp3[0]][$tmp3[1]] = trim($tmp3[2]); } }
	unset($tmp,$tmp2,$tmp3);
	
	print_r($stalk);print_r($edit);print_r($stalkedit);


	while (1) {
		$feed = fsockopen($feedhost,$feedport,$feederrno,$feederrstr,30);

		if (!$feed) {
			sleep(10);
			$feed = fsockopen($feedhost,$feedport,$feederrno,$feederrstr,30);
			if (!$feed) die($feederrstr.' ('.$feederrno.')');
		}

		fwrite( $feed,'USER '.$user.' "1" "1" :SoxBot Wikipedia Bot.'."\n" ); 
		fwrite( $feed,'NICK '.$user."\n" ); 

		while (!feof($feed)) {
			$rawline = fgets($feed,1024);
			$line = str_replace(array("\n","\r","\002"),'',$rawline);
			$line = preg_replace('/\003(\d\d?(,\d\d?)?)?/','',$line);
			
			if (!$line) { fclose($feed); break; }
			$linea= explode(' ',$line,4);

			if (strtolower($linea[0]) == 'ping') {
				fwrite( $feed,'PONG '.$linea[1]."\n" );
			} elseif (($linea[1] == '376') or ($linea[1] == '422')) {
				fwrite( $feed,'JOIN '.$feedchannel."\n" );
			} elseif ((strtolower($linea[1]) == 'privmsg') and (strtolower($linea[2]) == strtolower($feedchannel))) {
				$message = substr($linea[3],1);
				if (preg_match('/^\[\[((Talk|User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal|Special)(( |_)talk)?:)?([^\x5d]*)\]\] (\S*) (http:\/\/en\.wikipedia\.org\/w\/index\.php\?diff=(\d*)&oldid=(\d*)|http:\/\/en\.wikipedia\.org\/wiki\/\S+)? \* ([^*]*) \* (\(([^)]*)\))? (.*)$/S',$message,$m)) {
					$messagereceived = microtime(1);
					$change['namespace'] = $m[1];
					$change['title'] = $m[5];
					$change['flags'] = $m[6];
					$change['url'] = $m[7];
					$change['revid'] = $m[8];
					$change['old_revid'] = $m[9];
					$change['user'] = $m[10];
					$change['length'] = $m[12];
					$change['comment'] = $m[13];
					
					if( $change['namespace'] == '2' ) {
						echo 'FEED: '.$line."\n";
					}


					$stalkchannel = array();
					foreach ($stalk as $key => $value) {
						if (fnmatch(str_replace('_',' ',$key),str_replace('_',' ',$change['user']))) {
							$stalkchannel = array_merge($stalkchannel,explode(',',$value));
						}
					}
					foreach ($stalkedit as $user => $titlechan) {
						//echo $user."\n";
						//print_r($titlechan);
						foreach ($titlechan as $title => $channel) {
							if (fnmatch(str_replace('_',' ',$user),str_replace('_',' ',$change['user']))) {
								if (fnmatch(str_replace('_',' ',$title),str_replace('_',' ',$change['namespace'].$change['title']))) {
									$stalkchannel = array_merge($stalkchannel,explode(',',$channel));
								}
							}
						}
					}
					foreach ($edit as $key => $value) if (fnmatch(str_replace('_',' ',$key),str_replace('_',' ',$change['namespace'].$change['title']))) $stalkchannel = array_merge($stalkchannel,explode(',',$value));
					$stalkchannel = array_unique($stalkchannel);
					print_r($stalkchannel);
					foreach ($stalkchannel as $y) {
						fwrite($irc,'PRIVMSG '.$y.' :'.chr(3).'01'.'New edit: [['.chr(3).chr(3).'07'.$change['namespace'].$change['title'].chr(3).chr(3).'01'.']] '.chr(3).chr(3).'04'.$change['flags'].chr(3).chr(3).'02'.' http://en.wikipedia.org/w/index.php?title=' . urlencode($change['namespace'].$change['title']).'&diff=prev'.'&oldid='.urlencode($change['revid']).chr(3).' '.chr(3).'04'.'* '.chr(3).'03'.$change['user'] .chr(3) . ' '.chr(3).'04'.' * '.chr(3).'('.$change['length'].') '.chr(3).'10'.$change['comment']."\n");
					}
					
					
					if (($change['namespace'] == 'User:')) {
						if (strtolower($change['title']) == strtolower($user.'/Run')) { $run = $wpq->getpage('User:'.$user.'/Run'); }
						if (strtolower($change['title']) == strtolower($user.'/Whitelist')) { $whitelist = $wpq->getpage('User:'.$user.'/Whitelist'); }
						if (strtolower($change['title']) == strtolower($user.'/Autostalk.js')) {
							fwrite( $irc,'PART '.implode(',',$stalk)." Parting due to ".$change['user']." editing User:".$user.'/Autostalk.js'."\n" );
							sleep(2);
							unset($stalk);
							$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalk.js'));
							foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $stalk[$tmp3[0]] = trim($tmp3[1]); } }
							unset($tmp,$tmp2,$tmp3);
							print_r($stalk);
							fwrite( $irc,'JOIN '.implode(',',$stalk)."\n" );
							sleep(2);
						}
						echo $change['title'];
						if (strtolower($change['title']) == strtolower($user.'/Autoedit.js')) {
							fwrite( $irc,'PART '.implode(',',$edit)." Parting due to ".$change['user']." editing User:".$user.'/Autoedit.js'."\n" );
							sleep(2);
							unset($edit);
							$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autoedit.js'));
							foreach ($tmp as $tmp2) { if (substr($tmp2,0,1) != '#') { $tmp3 = explode('|',$tmp2,2); $edit[$tmp3[0]] = trim($tmp3[1]); } }
							unset($tmp,$tmp2,$tmp3);
							print_r($edit);
							fwrite( $irc,'JOIN '.implode(',',$edit)."\n" );
							sleep(2);
						}
						if (strtolower($change['title']) == strtolower($user.'/Autostalkedit.js')) {
							fwrite( $irc,'PART '.implode(',',$stalkedit)." Parting due to ".$change['user']." editing User:".$user.'/Autostalkedit.js'."\n" );
							sleep(2);
							unset($stalkedit);
							$tmp = explode("\n",$wpq->getpage('User:'.$user.'/Autostalkedit.js'));
							foreach ($tmp as $tmp2) { 
								if (substr($tmp2,0,1) != '#') { 
									$tmp3 = explode('|',$tmp2,3);
									$stalkedit[$tmp3[0]][$tmp3[1]] = trim($tmp3[2]); 
								} 
							}
							unset($tmp,$tmp2,$tmp3);
							print_r($stalkedit);
							fwrite( $irc,'JOIN '.implode(',',$stalkedit)."\n" );
							sleep(2);
						}
						if (strtolower($change['title']) == strtolower($owner.'/Channels.js')) {
							$ircconfig = explode("\n",$wpq->getpage('User:'.$owner.'/Channels.js'));
							$tmp = array();
							foreach($ircconfig as $tmpline) { if (substr($tmpline,0,1) != '#') { $tmpline = explode('=',$tmpline,2); $tmp[trim($tmpline[0])] = trim($tmpline[1]); } }
							print_r($tmp);

							$tmpold = array();
							$tmpnew = array();
							
							foreach ($tmp as $tmp2) foreach (explode(',',$tmp2) as $tmp3) $tmpnew[$tmp3] = 1;
							foreach (explode(',',$ircchannel.','.$irctechchannel.','.$ircotherchannels.','.$ircvandalismchannel.','.$ircaivchannel.','.$ircverbosechannel) as $tmp3) $tmpold[$tmp3] = 1;

							foreach ($tmpold as $tmp2 => $tmp3) if (isset($tmpnew[$tmp2])) unset($tmpold[$tmp2],$tmpnew[$tmp2]);
							foreach ($tmpnew as $tmp2 => $tmp3) $tmpnew1[] = $tmp2;
							foreach ($tmpold as $tmp2 => $tmp3) $tmpold1[] = $tmp2;

							$tmpold = $tmpold1; $tmpnew = $tmpnew1; unset($tmpold1,$tmpnew1);

							fwrite( $irc,'JOIN '.implode(',',$tmpnew)."\n" ); 
							fwrite( $irc,'PART '.implode(',',$tmpold)." Parting due to ".$change['user']." editing User:".$owner.'/Channels.js'."\n" ); 

							$ircchannel = $tmp['ircchannel'];
							$irctechchannel = $tmp['irctechchannel'];
							$ircverbosechannel = $tmp['ircverbosechannel'];
							$ircotherchannels = $tmp['ircotherchannels'];
							$ircvandalismchannel = $tmp['ircvandalismchannel'];
							$ircaivchannel = $tmp['ircaivchannel'];

							unset($tmp,$tmpline,$tmpold,$tmpnew,$tmp2,$tmp3);
						}
					}

					if (($change['namespace'] != '') || ($change['flags'] == 'move')) continue;

					$change['justtitle'] = $change['title'];
					$change['title'] = $change['namespace'].$change['title'];
					
					/*if( $status == 'dryrun' ) {
						//print_r($testlist);
						$d = $wpi->diff($change['title'],$change['old_revid'],$change['revid']);
						//print_r($d);
						$s = score($testlist,$d[0],$log);
						$s -= score($testlist,$d[1],$log2);
						echo "\n".$s."\n";
						if( ($s < 0) 
							&& ((($wpq->contribcount($change['user']) < 50) || preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/', $change['user'])))) {
							echo "\n\n\n\n\n\n".$s."\n\n\n\n\n\n";
							$diff = 'http://en.wikipedia.org/w/index.php' .
								'?title='.urlencode($change['title']) .
								'&diff='.urlencode($change['revid']) .
								'&oldid='.urlencode($change['old_revid']);
							$towrite = file_get_contents('trainingdata.txt') . 
								"*Would revert [$diff revision ".$change['revid']."] on page [[".$change['title'] . 
								"]]: Registers a score of $s. \n";
							file_put_contents('trainingdata.txt',$towrite);
							unset($diff, $towrite, $d, $s);
						}
					}
					continue;*/
					 
					$diff = $wpi->diff($change['title'],$change['old_revid'],$change['revid']);
					$score = score($testlist,$diff[0],$log);
					$score -= score($testlist,$diff[1],$log2);
					$url = 'http://en.wikipedia.org/w/index.php?title='.urlencode($change['title']).'&diff='.urlencode($change['revid']).'&oldid='.urlencode($change['old_revid']);
					
					
					$pid = @pcntl_fork();
					if ($pid != 0) continue;
					
					$tmp = unserialize(file_get_contents('titles.txt'));
					
					if(!preg_match('/(yes|enable|true)/i',$wpq->getpage('User:'.$user.'/Run'))) {
						continue;
					}
					
					if (
						($score <= SCORELIMIT)
						&& (!preg_match('/\<code\>/', $wpq->getpage($change['title']))) //Ignore pages with <code>
						&& ((($wpq->contribcount($change['user']) < 50) || preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/', $change['user'])))//Ignore accounts with >50 edits
						&& (!preg_match('/^\* \[\[User:('.preg_quote($change['user'],'/').')|\1\]\] \- .*/',$whitelist)) //Ignore users listed on User:SoxBot III/Whitelist 
						&& ((!isset($tmp[$change['title'].$change['user']])) || ((time() - $tmp[$change['title'].$change['user']]) > (24*60*60)))
						&& ($tmp[$change['title'].$change['user']] = time())
						&& ((file_put_contents('titles.txt',serialize($tmp))) !== false)
						&& (($change['length'] < 800) || ($score <= -35))
						) { 
						$curmaxlag = $wpi->getMaxlag();
						if( $curmaxlag > $maxlag ) {
							foreach(explode(',',$irctechchannel) as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :'.$curmaxlag[1].' is lagged out by '.$curmaxlag[2].' seconds. ('.$curmaxlag[0].')'."\n" ); usleep(500);
							}
							continue;
						}
						unset($curmaxlag);
						
						foreach (explode(',',$ircchannel) as $y) {
							fwrite( $irc,'PRIVMSG '.$y.' :Reverting '.$change['revid'].'.'."\n" ); usleep(500);
						}
						foreach (array() as $y) {
							fwrite( $irc,'PRIVMSG '.$y.' :Reverting revision '.$change['revid'].' by '.$change['user'].'.'."\n" ); usleep(500);
						}
						unset($diff, $revision, $url);
						$currev = $wpapi->revisions($change['title']);
						print_r($currev);
						if (($currev[0]['revid'] != $change['revid']) && ($currev[0]['user'] != $change['user'])) {
							mysql_query('INSERT INTO `beaten` (`id`,`article`,`diff`,`user`) VALUES (NULL,\''.mysql_real_escape_string($change['title']).'\',\''.mysql_real_escape_string($change['url']).'\',\''.mysql_real_escape_string($currev['user']).'\')');
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :Inserting '.$change['revid'].' into `beaten` table.'."\n" ); usleep(500);
							}
						}
						else {
							$query = 'SELECT date,user,article FROM testing WHERE user=\''.
							mysql_real_escape_string($change['user']).
							'\' AND article=\''.
							mysql_real_escape_string($change['title']).
							'\' AND date=\''.
							date( 'Y-m-d' ).
							'\';';
							echo $query;
							if (!mysql_ping($mysql)) { $mysql = 
								mysql_connect($mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass,/* Force reconnect --> */ true);echo "Ping?\n"; 
								mysql_select_db($mysqldb, $mysql); 
							}
							$result = mysql_query($query);
							if( !$result ) { die( "MySQL error: ".mysql_error() ); }
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :Querying if '.$change['user'].' has been reverted recently.'."\n" ); usleep(500);
							}
							echo "Querying if user has been reverted recently.\n";
							if ( mysql_num_rows( $result ) != 0 ) {
								continue;
								foreach (array() as $y) {
									fwrite( $irc,'PRIVMSG '.$y.' :Yes, not reverting.'."\n" ); usleep(500);
								}
							}
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :No, will continue to revert.'."\n" ); usleep(500);
							}
							
							$query = 'INSERT INTO `testing` ' .
								'(`id`,`user`,`article`,`diff`,`old_id`,`new_id`,`reverted`,`date`,`score`) ' .								
								'VALUES ' .
								'(NULL,\''.mysql_real_escape_string($change['user']).'\',' .
								'\''.mysql_real_escape_string($change['title']).'\',' .
								'\''.mysql_real_escape_string($change['url']).'\',' .
								'\''.mysql_real_escape_string($change['old_revid']).'\',' .
								'\''.mysql_real_escape_string($change['revid']).'\',0,' .
								'\''.date( 'Y-m-d' ).'\',' .
								'\''.mysql_real_escape_string($score).'\');';
							echo $query;
							if (!mysql_ping($mysql)) {
								$mysql = mysql_pconnect($mysqlhost.':'.$mysqlport,$mysqluser,$mysqlpass,/* Force reconnect --> */ true);
								if (!$mysql) { die('Could not connect: ' . mysql_error()); }
								if (!mysql_select_db($mysqldb, $mysql)) { die ('Can\'t use database : ' . mysql_error()); }
							}
							mysql_query($query);
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :Inserting '.$change['revid'].' into `testing`.'."\n" ); usleep(500);
							}
							echo "Inserting into `testing`\n";echo $query;
							if (mysql_affected_rows( $mysql ) == 0) {
								echo "Problem?\n";
								foreach (array() as $y) {
									fwrite( $irc,'PRIVMSG '.$y.' :MySQL error? '.mysql_error()."\n" ); usleep(500);
								}
								continue;
							} 
 							$mysqlid = mysql_insert_id();

							
							$token = $http->get('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvtoken=rollback&titles=Main%20Page&format=txtfm');
							$token = unserialize($token);
							$token = $token['query']['pages'][$wpq->getpageid($change['title'])]['revisions'][0]['rollbacktoken'];
							echo "Token: $token\n";
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :Getting rollback token.'."\n" ); usleep(500);
							}
							if( $token == '' ) {
								foreach (array() as $y) {
									fwrite( $irc,'PRIVMSG '.$y.' :Error getting token.'."\n" ); usleep(500);
								}
							}
							$return = $wpapi->rollback(
								$change['title'],
								$change['user'],
								'Reverting possible test edit(s) by [[Special:Contributions/'.$change['user'].'|'.$change['user'].']] ' .
								'to '.(($revid == 0)?'older version':'version by '.$revdata['user']).'. ' .
								'[[User:'.$owner.'/False Positive?|Was this a mistake?]] (BOT EDIT)',
								$token
							);
							foreach (array() as $y) {
								fwrite( $irc,'PRIVMSG '.$y.' :Rolling back '.$change['revid'].'.'."\n" ); usleep(500);
							}
							echo "Reverting.\n";
							
							if (isset($return['rollback'])) {
								foreach (array() as $y) {
									fwrite( $irc,'PRIVMSG '.$y.' :Returned true, attempting to warn.'."\n" ); usleep(500);
								}
								$warning = getWarningLevel( $change['user'] );
								$warning++;

								if ($warning < 5) {
									$append = '';
									if ( $warning == 1 ) {
										$append = ":''If this is a shared [[IP address]], and you didn't make the edit, consider [[Wikipedia:Why create an account?|creating an account]] for yourself so you can avoid further irrelevant notices.''\n";
									}
									elseif ($warning > 2) {
										foreach (explode(',',$ircvandalismchannel) as $y) {
											fwrite( $irc,'PRIVMSG '.$y.' :computer bl add '.$change['user'].' x='.(24*$warning).' r=Test edits to [['.$change['title'].']] (#'.$warning.').'."\n" ); usleep(500);
										}
									}
									else {
									}
									$talk = $wpq->getpage('User talk:'.$change['user']);	
									$wpi->post(
										'User talk:'.$change['user'],$talk."\n\n".'{{subst:User:'.$user.'/warn|1='.$change['title'].'|2='.$warning.'|3='.$mysqlid.'}} '.SIGNATURE.' ~~~~~'."\n".$append,'Warning user of test edits (Warning #'.$warning.')',false,null,false); 
									foreach (array() as $y) {
										fwrite( $irc,'PRIVMSG '.$y.' :Warning '.$change['user'].'.'."\n" ); usleep(500);
									}
								}
								elseif ($warning > 4) {
									$aiv = $wpq->getpage('Wikipedia:Administrator intervention against vandalism/TB2');	
									if ( preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/', $change['user']) ) {
										$template = "IPvandal";
									}
									else {
										$template = "Vandal";
									}

									if (!preg_match('/'.preg_quote($change['user'],'/').'/i',$aiv)) {
										foreach(explode(',',$ircaivchannel) as $y) {
											fwrite( $irc,'PRIVMSG '.$y.' :!admin Reporting [[User:'.$change['user'].']] to [[WP:AIV]]. Contributions: [[Special:Contributions/'.$change['user'].']] Block: [[Special:Blockip/'.$change['user'].']]'."\n" ); usleep(500);
										}
										foreach (explode(',',$ircvandalismchannel) as $y) {
											fwrite( $irc,'PRIVMSG '.$y.' :computer bl add '.$change['user'].' x='.(24*$warning).' r=Vandalism to [['.$change['title'].']] (#'.$warning.").\n" ); usleep(500);
										}
										$aiv = $aiv . "\n" .
										"* {{".$template."|1=".$change['user']."}} ".
										"User made possible test edits, such as [http://en.wikipedia.org/wiki/?diff=".$change['revid']." 1]. ".SIGNATURE." ~~~~~";
										$wpi->post('Wikipedia:Administrator intervention against vandalism/TB2',$aiv,'Reporting [[Special:Contributions/'.$change['user']."|".$change['user']."]] (BOT EDIT)");
										foreach (array() as $y) {
											fwrite( $irc,'PRIVMSG '.$y.' :Warning level is 4, reporting to AIV.'."\n" ); usleep(500);
										} 
									} else {
										foreach (explode(',',$ircreportchannel) as $y) {
											fwrite( $irc,'PRIVMSG '.$y.' :!admin [[User:'.$change['user'].']] has vandalized at least one time while being listed on [[WP:AIV]].  Contributions: [[Special:Contributions/'.$change['user'].']] Block: [[Special:Blockip/'.$change['user'].']]'."\n" ); usleep(500);
										}
									}
								}

								mysql_query('UPDATE `testing` SET `reverted` = 1 WHERE `id` = \''.mysql_real_escape_string($mysqlid).'\'');
							} 
							else {
								foreach (array() as $y) {
									fwrite( $irc,'PRIVMSG '.$y.' :Returned false, figuring out why.'."\n" ); usleep(500);
								}
								$currev = $wpapi->revisions($change['title']);
								if (($currev[0]['revid'] != $change['revid']) && ($currev[0]['user'] != $change['user'])) {
									mysql_query('INSERT INTO `beaten` (`id`,`article`,`diff`,`user`) VALUES (NULL,\''.mysql_real_escape_string($change['title']).'\',\''.mysql_real_escape_string($change['url']).'\',\''.mysql_real_escape_string($rev['user']).'\')');
									foreach (array() as $y) {
										fwrite( $irc,'PRIVMSG '.$y.' :I was beaten by '.$rev['user'].'!'."\n" ); usleep(500);
									}
								}
								else {
									foreach (array() as $y) {
										fwrite( $irc,'PRIVMSG '.$y.' :There was an unknown rollback error.'."\n" ); usleep(500);
									}
									file_put_contents('errors.txt',file_get_contents('errors.txt')."Date: ".date( 'Y-m-d' )."\n".$return."\n----------------------\n") or die('Error');
									foreach (array() as $y) {
										fwrite( $irc,'PRIVMSG '.$y.' :Rollback result has been posted to errors.txt.'."\n" ); usleep(500);
									}
									echo "ERROR\n";
								}
							}//End if ($return !== FALSE)
						}//End if beaten check
						foreach (array() as $y) {
							fwrite( $irc,'PRIVMSG '.$y.' :-- Done --.'."\n" ); usleep(500);
						}
					}//End check for vandalism
					die();
				}//End regex for parsing IRC feed
			}//End checking for privmsg
		}//End while
	}//End while
?>