Ejemplo n.º 1
0
 public function pluginList($data)
 {
     //Computing server list for each plugin
     $servers = array();
     foreach (Leelabot::$instance->plugins->getInfoFromFiles() as $plugin) {
         $servers[$plugin['name']] = array();
     }
     foreach (ServerList::getList() as $servername) {
         $server = ServerList::getServer($servername);
         $list = $server->getPlugins();
         foreach ($list as $p) {
             if (!isset($servers[$p])) {
                 $servers[$p] = array();
             }
             $servers[$p][] = $servername;
         }
     }
     foreach ($servers as &$s) {
         if (empty($s)) {
             $s = 'Not used';
         } else {
             $s = join(', ', $s);
         }
     }
     $this->_dispatcher->parser->assign('plugins', Leelabot::$instance->plugins->getInfoFromFiles());
     $this->_dispatcher->parser->assign('loaded', Leelabot::$instance->plugins->getLoadedPlugins());
     $this->_dispatcher->parser->assign('servers', $servers);
     return $this->_dispatcher->parser->draw('plugins');
 }
Ejemplo n.º 2
0
 public function onReadyToReceive()
 {
     print "Something received from UDP\n";
     $datalen = @socket_recvfrom($this->_socket, $data, 1500, 0, $fromip, $fromport);
     while ($datalen > 0) {
         $srvdata = ServerList::getServer($fromip, $fromport);
         if ($srvdata === NULL) {
             print "Received UDP packet from unknown server: {$fromip}:{$fromport}\n";
         } else {
             print "Received UDP packet from {$fromip}:{$fromport}\n";
             $srvdata->touch();
         }
         $datalen = @socket_recvfrom($udpsock, $data, 1500, 0, $fromip, $fromport);
     }
 }
Ejemplo n.º 3
0
 /** Returns the instance of the class.
  *This function returns the auto-reference to the singleton instance of the class. It should not be called by other classes.
  * 
  * \return The auto-reference to the singleton.
  */
 public static function getInstance()
 {
     if (!self::$_instance instanceof self) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Ejemplo n.º 4
0
 /** Initializes the bot.
  * Initializes the bot, by reading arguments, parsing configs sections, initializes server instances, and many other things.
  * 
  * \param $CLIArguments list of arguments provided to the launcher, or generated ones (for further integration into other systems).
  * \return TRUE in case of success, FALSE otherwise.
  */
 public function init($CLIArguments)
 {
     //Start Cpu Monitoring
     $this->cpuRequestStart();
     //Setting default values for class attributes
     Leelabot::$instance =& $this;
     $this->_configDirectory = 'conf';
     Leelabot::$verbose = FALSE;
     $this->servers = array();
     $this->system = php_uname('a');
     $this->_showIPS = FALSE;
     $this->_iterations = 0;
     $this->_IPSHistory = array_fill(0, 10, 0);
     //Parsing CLI arguments
     $logContent = NULL;
     $CLIArguments = Leelabot::parseArgs($CLIArguments);
     //Checking CLI argument for root modification, and modification in case
     if ($rootParam = array_intersect(array('r', 'root'), array_keys($CLIArguments))) {
         chdir($CLIArguments[$rootParam[0]]);
     }
     //Setting root
     $this->root = getcwd();
     //Opening default log file (can be modified after, if requested)
     Leelabot::$_logFile = fopen('leelabot.log', 'a+');
     fseek(Leelabot::$_logFile, 0, SEEK_END);
     $initPos = ftell(Leelabot::$_logFile);
     //Loading Intl class (if it is not loadable, load a dummy substitute)
     $this->intl = new Intl(Leelabot::DEFAULT_LOCALE);
     if (!$this->intl->init) {
         $this->intl = new Intl();
         //Load class without a locale defined
         Leelabot::message('Can\'t load Intl class with default locale ($0).', array(Leelabot::DEFAULT_LOCALE), E_ERROR);
         exit;
     }
     Leelabot::message('Leelabot version $0 starting...', array(Leelabot::VERSION), E_NOTICE, TRUE);
     //Loading plugin manager class
     $this->plugins = new PluginManager($this);
     Plugins::setPluginManager($this->plugins);
     //Pre-parsing CLI arguments (these arguments are relative to config loading and files location)
     $this->processCLIPreparsingArguments($CLIArguments);
     //Loading config
     if (!$this->loadConfig()) {
         Leelabot::message("Startup aborted : Can't parse startup config.", array(), E_ERROR);
         exit;
     }
     //Checking if the number of servers defined in the config is not greater than the limit defined in the CLI
     if ($this->maxServers > 0 && count($this->config['Server']) > $this->maxServers) {
         Leelabot::message("Number of set servers in the config is greater than the limit (\$0).", array($this->maxServers), E_ERROR);
         exit;
     }
     //Processing loaded config (for main parameters)
     if (isset($this->config['Main'])) {
         $logContent = '';
         //Setting the locale in accordance with the configuration (if set)
         foreach ($this->config['Main'] as $name => $value) {
             switch ($name) {
                 case 'Locale':
                     Leelabot::message('Changed locale by configuration : $0', array($this->config['Main']['Locale']));
                     if (!$this->intl->setLocale($this->config['Main']['Locale'])) {
                         Leelabot::message('Cannot change locale to "$0"', array($this->config['Main']['Locale']), E_WARNING);
                     } else {
                         Leelabot::message('Locale successfully changed to "$0".', array($this->config['Main']['Locale']));
                     }
                     break;
                 case 'UseLog':
                     if (Leelabot::parseBool($value) == FALSE) {
                         Leelabot::message('Disabling log (by Config).');
                         //Save log content for later parameters (like when using --nolog -log file.log)
                         if (Leelabot::$_logFile) {
                             $logContent = '';
                             fseek(Leelabot::$_logFile, $initPos);
                             while (!feof(Leelabot::$_logFile)) {
                                 $logContent .= fgets(Leelabot::$_logFile);
                             }
                             //If the file was empty before logging into it, delete it
                             if ($initPos == 0) {
                                 $logFileInfo = stream_get_meta_data(Leelabot::$_logFile);
                                 fclose(Leelabot::$_logFile);
                                 unlink($logFileInfo['uri']);
                             } else {
                                 fclose(Leelabot::$_logFile);
                             }
                             Leelabot::$_logFile = FALSE;
                         }
                     }
                     break;
                 case 'BotName':
                     $this->botName = $value;
                     break;
                 case 'ShowIPS':
                     $this->_showIPS = Leelabot::parseBool($value);
                     break;
                 case 'LogFile':
                     Leelabot::message('Changing log file to $0 (by Config)', array($value));
                     //Save log content for later parameters (like when using --nolog -log file.log)
                     if (Leelabot::$_logFile) {
                         $logContent = '';
                         fseek(Leelabot::$_logFile, $initPos);
                         while (!feof(Leelabot::$_logFile)) {
                             $logContent .= fgets(Leelabot::$_logFile);
                         }
                         //If the file was empty before logging into it, delete it
                         if ($initPos == 0) {
                             $logFileInfo = stream_get_meta_data(Leelabot::$_logFile);
                             fclose(Leelabot::$_logFile);
                             unlink($logFileInfo['uri']);
                         } else {
                             fclose(Leelabot::$_logFile);
                         }
                         Leelabot::$_logFile = FALSE;
                     }
                     //Load new file, and put the old log content into it (if opening has not failed, else we re-open the old log file)
                     if (!(Leelabot::$_logFile = fopen($value, 'a+'))) {
                         Leelabot::$_logFile = fopen($logFileInfo['uri'], 'a+');
                         Leelabot::message('Cannot open new log file ($0), reverting to old.', array($value), E_WARNING);
                     } else {
                         fseek(Leelabot::$_logFile, 0, SEEK_END);
                         $initPos = ftell(Leelabot::$_logFile);
                         fputs(Leelabot::$_logFile, $logContent);
                     }
                     break;
             }
         }
         unset($logContent);
     }
     //Post-parsing CLI arguments (after loading the config because they override file configuration)
     $this->processCLIPostparsingArguments($CLIArguments, $initPos);
     //Loading ServerList InnerAPI class
     ServerList::setLeelabotClass($this);
     //Loading Locale InnerAPI class
     Locales::init($this->intl);
     //Loading the OuterAPI (if required, i.e. There is an API section in config and if there's an Enable parameter, it is active)
     if (isset($this->config['API']) && (!isset($this->config['API']['Enable']) || Leelabot::parseBool($this->config['API']['Enable']) == TRUE)) {
         $this->outerAPI = new OuterAPI();
         $this->outerAPI->load($this->config['API']);
     } else {
         $this->outerAPI = NULL;
     }
     //Loading Update class if needed.
     if (isset($this->config['Update']) && isset($this->config['Update']['Enabled']) && Leelabot::parseBool($this->config['Update']['Enabled'])) {
         include 'core/update.class.php';
         $this->update = new Updater($this->config['Update'], $this->plugins);
     }
     //Loading plugins (throws a warning if there is no plugin general config, because using leelabot without plugins is as useful as eating corn flakes hoping to fly)
     if (isset($this->config['Plugins']) && isset($this->config['Plugins']['AutoLoad']) && $this->config['Plugins']['AutoLoad']) {
         //Getting automatically loaded plugins
         $this->config['Plugins']['AutoLoad'] = explode(',', $this->config['Plugins']['AutoLoad']);
         //Setting priority order.
         $order = array();
         $unordered = array();
         foreach ($this->config['Plugins']['AutoLoad'] as $plugin) {
             $plugin = trim($plugin);
             if (strpos($plugin, '/')) {
                 $priority = explode('/', $plugin);
                 $i = 0;
                 for (; isset($order[$priority[1] . '.' . $i]); $i++) {
                 }
                 $order[$priority[0] . '.' . $i] = $priority[0];
             } else {
                 $unordered[] = $plugin;
             }
         }
         ksort($order);
         $this->config['Plugins']['AutoLoad'] = array_merge(array_values($order), $unordered);
         //Setting default right level for commands
         if (isset($this->config['Commands']['DefaultLevel'])) {
             $this->plugins->setDefaultRightLevel($this->config['Commands']['DefaultLevel']);
         } else {
             $this->plugins->setDefaultRightLevel(0);
         }
         //We load plugins
         $this->plugins->loadPlugins($this->config['Plugins']['AutoLoad'], TRUE);
         //Setting user-defined levels for commands
         if (isset($this->config['Commands']['Levels'])) {
             foreach ($this->config['Commands']['Levels'] as $key => $value) {
                 if ($key[0] = '!') {
                     //We check if the param name is a command
                     $this->plugins->setCommandLevel($key, $value, TRUE);
                 } elseif (intval($key) == $key) {
                     $value = explode(',', $value);
                     foreach ($value as $command) {
                         $this->plugins->setCommandLevel($command, $key, TRUE);
                     }
                 }
             }
         }
         //Setting the verbosity for the command replies
         if (isset($this->config['Commands']['QuietReplies'])) {
             $this->plugins->setQuietReply($this->config['Commands']['QuietReplies']);
         }
     } else {
         Leelabot::message('There is no plugin configuration', array(), E_WARNING);
     }
     //Loading server instances
     $this->loadServerInstances();
     //Notice that we have loaded successfully if more than one server loaded
     if (count($this->servers)) {
         Leelabot::message('Leelabot loaded successfully for $0 server$1', array(count($this->servers), count($this->servers) > 1 ? 's' : ''));
     } else {
         Leelabot::message('Can\'t load Leelabot for any configured server.', array(), E_ERROR);
         exit;
     }
 }
Ejemplo n.º 5
0
 public function CommandBroadcast($id, $command)
 {
     $servers = ServerList::getList();
     $message = join(" ", $command);
     foreach ($servers as $server) {
         ServerList::getServerRCon($server)->say($message, array(), FALSE);
     }
 }
# 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
require_once "config.inc.php";
require_once "ClientSock.php";
require_once "ServerInfo.php";
require_once "ServerList.php";
require_once "MasterList.php";
require_once "MasterInfo.php";
require_once "UDPListener.php";
require_once "TCPListener.php";
set_time_limit(0);
$udpListener = new UDPListener($listenip, $listenport);
$tcpListener = new TCPListener($listenip, $listenport);
ServerList::Initialize();
MasterList::Initialize();
SocketManager::Initialize();
$testms = new MasterInfo("83.142.228.166", 28900, false);
MasterList::addServer($testms);
SocketManager::addSocket($udpListener);
SocketManager::addSocket($tcpListener);
$isRunning = true;
while ($isRunning) {
    ServerList::checkTimeouts();
    MasterList::checkTimeouts();
    MasterList::dumpServers();
    ServerList::dumpServers();
    SocketManager::handleEvents();
}
Ejemplo n.º 7
0
 private function _removeMessage($id, $serv = NULL)
 {
     if ($serv === NULL) {
         $server = Server::getInstance();
     } else {
         $server = ServerList::getServer($serv);
     }
     $_messages = $server->get('msgs');
     unset($_messages[$id]);
     $server->set('msgs', $_messages);
 }
Ejemplo n.º 8
0
 public function IrcPlayers($pseudo, $channel, $cmd, $message)
 {
     $serverlist = ServerList::getList();
     $actual = Server::getName();
     if (isset($cmd[1]) && in_array($cmd[1], $serverlist)) {
         Server::setServer($this->_main->servers[$cmd[1]]);
         $this->_printPlayers($cmd[1]);
     } else {
         foreach ($serverlist as $server) {
             Server::setServer($this->_main->servers[$server]);
             $this->_printPlayers();
         }
     }
     Server::setServer($this->_main->servers[$actual]);
 }
Ejemplo n.º 9
0
 private function handleQuitCommand($attributes)
 {
     if (!isset($attributes['port']) || $attributes['port'] < 1 || $attributes['port'] > 65535) {
         $this->sendError("Wrong port for quit");
     } else {
         $srvinfo = ServerList::getServer($this->ip, $attributes['port']);
         if (isset($srvinfo)) {
             socket_write($this->_socket, "\\final\\");
             ServerList::removeServer($this->ip, $attributes['port']);
         }
     }
     $this->removeme = true;
 }
Ejemplo n.º 10
0
 public function IrcServerList($pseudo, $channel, $cmd, $message)
 {
     $serverlist = ServerList::getList();
     LeelaBotIrc::sendMessage("Servers : " . join(', ', $serverlist));
 }
Ejemplo n.º 11
0
 public function IrcReload($pseudo, $channel, $cmd, $message)
 {
     $server = LeelaBotIrc::nameOfServer($cmd[1], FALSE);
     if ($server !== false) {
         $rcon = ServerList::getServerRCon($server);
         $rcon->reload();
     }
 }
Ejemplo n.º 12
0
 public function RoutineWarns()
 {
     // We browse all servers
     $servers = ServerList::getList();
     foreach ($servers as $serv) {
         $server = ServerList::getServer($serv);
         $rcon = ServerList::getServerRCon($serv);
         $_warns = $server->get('warns');
         if (count($_warns)) {
             foreach ($_warns as $player => $warns) {
                 if ($warns['num'] >= $this->config['WarnsKick'] && time() >= $warns['last'] + $this->config['SecondsBeforeKick']) {
                     $rcon->tell($player, 'You have $warns warnings. You will be kicked', array('warns' => $warns['num']));
                     $this->_clearWarn($player, $server);
                     $rcon->kick($player);
                 }
             }
         }
     }
 }