コード例 #1
0
ファイル: leelabot.class.php プロジェクト: Geolim4/Leelabot
 /** 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;
     }
 }