/** * This method adds the !getname-command internally since the code is too long. */ private function addGetNameCommand() { Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->registerCommand(new Command('getname', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (strtolower($sChannel) == strtolower('#lvp.echo') || strtolower($sChannel) == strtolower('#lvp')) { $oLastSeenPerson = new Nuwani\Model('lvp_person_last_seen', 'iId', $aParams[0]); if ($aParams[0] == '') { echo '10* Usage: !getname [playerId]'; } else { if (!is_null($oLastSeenPerson->lvp_person_last_seen_id) && $oLastSeenPerson->sReason == 'online') { echo '6*** The player with ID ' . $oLastSeenPerson->iId . ' is called: ' . $oLastSeenPerson->lvp_person_last_seen_id . '.'; } else { echo '04* Error: No player with ID ' . $aParams[0] . ' is connected.'; } } } })); }
require 'config_test.php'; if ($_SERVER['argc'] >= 1 && (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'restart')) { /** Make sure the still running bot has time to disconnect. * */ sleep(1); } Nuwani\Configuration::getInstance()->register($aConfiguration); Nuwani\NetworkManager::getInstance()->Initialize($aConfiguration['Networks']); Nuwani\ModuleManager::getInstance()->Initialize(); Nuwani\BotManager::getInstance()->Initialize($aConfiguration['Bots']); Nuwani\Database::getInstance(); Nuwani\Memory::Initialize(); Nuwani\ErrorExceptionHandler::getInstance()->Initialize($aConfiguration['ErrorHandling']); $g_bRun = true; while ($g_bRun) { try { Nuwani\BotManager::getInstance()->process(); Nuwani\ModuleManager::getInstance()->onTick(); Nuwani\Timer::process(); Nuwani\Memory::process(); if (count(Nuwani\BotManager::getInstance()) == 0) { $g_bRun = false; } usleep($aConfiguration['SleepTimer']); } catch (Exception $pException) { Nuwani\ErrorExceptionHandler::getInstance()->processException($pException); if (ob_get_level() >= 1) { ob_end_flush(); } } } /** Destructors will be called by PHP automatically. **/
/** * This method registers a couple of commands which can be used by the * bot owner to control one another from IRC, like adding, removing and * renaming, changing properties of commands, (un)loading modules, etc. * Just have a skim through this method and you'll figure it out. */ private function registerInternalCommands() { $this->registerCommand(new Command('cmdadd', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) < 2) { echo '!cmdadd CommandName Code'; return Command::OUTPUT_USAGE; } else { Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->addCommand(array_shift($aParams), implode(' ', $aParams)); echo 'The command has been added.'; return Command::OUTPUT_SUCCESS; } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdremove', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!cmdremove CommandName'; return Command::OUTPUT_USAGE; } else { if (Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->deleteCommand($aParams[0])) { echo 'The command has been deleted successfully.'; return Command::OUTPUT_SUCCESS; } else { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdrename', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 2) { echo '!cmdrename OldName NewName'; return Command::OUTPUT_USAGE; } else { if (Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->renameCommand($aParams[0], $aParams[1])) { echo 'The command has been renamed successfully.'; return Command::OUTPUT_SUCCESS; } else { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdchannel', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) < 2) { echo '!cmdchannel CommandName [- / Channel1 14ChannelN]'; return Command::OUTPUT_USAGE; } else { $c = Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->offsetGet($aParams[0]); if ($c) { $c->setChannels(array_slice($aParams, 1)); echo 'The channels have been updated successfully.'; return Command::OUTPUT_SUCCESS; } else { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdnetwork', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) < 2) { echo '!cmdnetwork CommandName [- / Network1 14NetworkN]'; return Command::OUTPUT_USAGE; } else { $c = Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->offsetGet($aParams[0]); if ($c) { $c->setNetworks(array_slice($aParams, 1)); echo 'The networks have been updated successfully.'; return Command::OUTPUT_SUCCESS; } else { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdcode', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!cmdcode CommandName'; return Command::OUTPUT_USAGE; } else { $c = Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->offsetGet($aParams[0]); if ($c) { if (is_string($c->getCode())) { echo $c->getCode(); return Command::OUTPUT_NORMAL; } else { echo 'The command is a function call, no code can be displayed.'; return Command::OUTPUT_ERROR; } } else { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdlist', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { $aCmd = array(); $iAmountOfCommands = 0; foreach (Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->getIterator() as $sName => $pCommand) { if ($sName[0] == '.') { continue; } $aCmd[] = $sName; $iAmountOfCommands++; } echo wordwrap('10* Commands (' . $iAmountOfCommands . '): !' . implode(', !', $aCmd), 400); }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('moduleload', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!moduleload ModuleName'; return Command::OUTPUT_USAGE; } else { if (Nuwani\ModuleManager::getInstance()->loadModule($aParams[0])) { echo 'The module has been loaded.'; return Command::OUTPUT_SUCCESS; } else { echo 'The module could not be loaded.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('moduleunload', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!moduleunload ModuleName'; return Command::OUTPUT_USAGE; } else { if (Nuwani\ModuleManager::getInstance()->unloadModule($aParams[0])) { echo 'The module has been unloaded.'; return Command::OUTPUT_SUCCESS; } else { echo 'The module could not be unloaded.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('modulereload', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!modulereload ModuleName'; return Command::OUTPUT_USAGE; } else { if (Nuwani\ModuleManager::getInstance()->reloadModule($aParams[0])) { echo 'The module has been reloaded.'; return Command::OUTPUT_SUCCESS; } else { echo 'The module could not be reloaded.'; return Command::OUTPUT_ERROR; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('modulelist', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { $c = Nuwani\ModuleManager::getInstance(); $aModules = array(); foreach ($c as $sName => $pModule) { $aModules[] = $sName; } echo '10* Modules (' . count($c) . '): ' . implode(', ', $aModules); }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('botlist', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { $c = Nuwani\BotManager::getInstance()->getBotList(); $aBots = array(); foreach ($c as $sName => $Bot) { $aBots[] = $sName . ' (' . $Bot['Network'] . ')'; } echo '10* Bots (' . count($c) . '): ' . implode(', ', $aBots); }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('meminfo', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { $aGarbage = Nuwani\Memory::getStatistics(); echo '10* Current usage: ', sprintf('%.2f MB', memory_get_usage() / 1024 / 1024), ' | 10Top usage: ', sprintf('%.2f MB', memory_get_peak_usage() / 1024 / 1024), ' | 10Garbage: ', sprintf('%.2f kB', $aGarbage['Memory']); }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('restart', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { Nuwani\BotManager::getInstance()->getBotList()->send('QUIT :Restart requested by ' . $sNickname); usleep(150000); die(exec('php ' . $_SERVER['argv'][0] . ' restart')); }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('cmdpermission', function ($pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 2) { echo '!cmdpermission command level'; return Command::OUTPUT_USAGE; } else { if (!Nuwani\ModuleManager::getInstance()->offsetGet('Commands')->setCommandPermission($aParams[0], $aParams[1])) { echo 'The command has not been found.'; return Command::OUTPUT_ERROR; } else { echo 'The required permission has been updated successfully.'; return Command::OUTPUT_SUCCESS; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('useradd', function (Bot $pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!useradd usermask'; return Command::OUTPUT_USAGE; } else { if ($pBot->getSecurityManager()->isUserKnown($aParams[0])) { echo 'Given user mask is already known.'; return Command::OUTPUT_ERROR; } $pBot->getSecurityManager()->addUser($aParams[0]); echo 'User ' . $aParams[0] . ' has been added.'; return Command::OUTPUT_SUCCESS; } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('permissiongrant', function (Bot $pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 2) { echo '!permissiongrant usermask permission'; return Command::OUTPUT_USAGE; } else { if (!$pBot->getSecurityManager()->isUserKnown($aParams[0])) { echo 'Given user mask does not match any known users.'; return Command::OUTPUT_ERROR; } $user = new Nuwani\User($pBot['Network'], $aParams[0]); if ($pBot->getSecurityManager()->grantPermission($user, $aParams[1])) { echo 'User ' . (string) $user . ' has been granted the "' . $aParams[1] . '" permission.'; return Command::OUTPUT_SUCCESS; } else { echo 'User ' . (string) $user . ' already has the "' . $aParams[1] . '" permission.'; return Command::OUTPUT_NOTICE; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('permissionrevoke', function (Bot $pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 2) { echo '!permissionrevoke usermask permission'; return Command::OUTPUT_USAGE; } else { if (!$pBot->getSecurityManager()->isUserKnown($aParams[0])) { echo 'Given user mask does not match any known users.'; return Command::OUTPUT_ERROR; } $user = new Nuwani\User($pBot['Network'], $aParams[0]); if ($pBot->getSecurityManager()->revokePermission($user, $aParams[1])) { echo 'The "' . $aParams[1] . '" permission has been revoked from user ' . (string) $user . '.'; return Command::OUTPUT_SUCCESS; } else { echo 'User ' . (string) $user . ' doesn\'t have the "' . $aParams[1] . '" permission.'; return Command::OUTPUT_NOTICE; } } }, SecurityManager::PERMISSION_BOT_OWNER))->registerCommand(new Command('permissionshow', function (Bot $pBot, $sDestination, $sChannel, $sNickname, $aParams, $sMessage) { if (count($aParams) != 1) { echo '!permissionshow usermask'; return Command::OUTPUT_USAGE; } else { $user = null; if ($aParams[0] == 'me' || $aParams[0] == 'self') { $user = $pBot->In->User; } else { $user = new Nuwani\User($pBot['Network'], $aParams[0]); } $permissions = $pBot->getSecurityManager()->getPermissionList($user); if (empty($permissions)) { echo 'User ' . (string) $user . ' doesn\'t have any permissions.'; return Command::OUTPUT_INFO; } else { echo 'User ' . (string) $user . ' has the following permissions: ' . implode(', ', $permissions); return Command::OUTPUT_INFO; } } }, null))->registerCommand(new Command('runningtimers', function ($pBot, $sDestination, $sChannel, $sNickname, $sParams, $sMessage) { echo '10* Running timers: '; $message = ''; foreach (Nuwani\Timer::getActiveTimers() as $timerInfo) { $message .= $timerInfo['name']; $message .= ' 14(ID: ' . $timerInfo['id']; $message .= ', in ' . Util::formatTime($timerInfo['next_run'] - microtime(true), true); $message .= $timerInfo['type'] == Nuwani\Timer::TIMEOUT ? ', once' : ''; $message .= '), '; } echo wordwrap(substr($message, 0, -2), 400); }, SecurityManager::PERMISSION_BOT_OWNER)); }