_file = $file; } /** * Executes the bootstrap process. */ public function run() { // Obtain and validate the contents of the configuration file $required = array('server', 'username', 'nick'); $global = $required + array('realname', 'password', 'nick'); $configuration = @parse_ini_file($this->_file); if (count($configuration) == 0) { throw new Phergie_Exception('Configuration file inaccessible or empty: ' . $this->_file); } foreach ($required as $setting) { if (!isset($configuration[$setting])) { throw new Phergie_Exception('Required configuration setting missing: ' . $setting); } } // Set up the client connection $connection = new Phergie_Connection(); $connection->setServer($configuration['server']); $connection->setUsername($configuration['username']); $connection->setNick($configuration['nick']); if (isset($configuration['realname'])) { $connection->setRealName($configuration['realname']); } if (isset($configuration['password'])) { $connection->setPassword($configuration['password']); } if (isset($configuration['port'])) { $connection->setPort($configuration['port']); } // Configure the client if (isset($configuration['driver'])) { require_once str_replace('_', '/', $configuration['driver']) . '.php'; $client = new Phergie_Client($connection, new $configuration['driver']()); } else { $client = new Phergie_Client($connection); } foreach ($configuration as $setting => $value) { if (!in_array($setting, $global)) { $client->setIni($setting, $value); } } unset($required, $global, $configuration, $setting); // Set up event handlers $iterator = new DirectoryIterator('Event/Handler'); foreach ($iterator as $entry) { if ($iterator->isFile() && substr($entry, -4) == '.php') { require_once 'Phergie/Event/Handler/' . $entry; $class = 'Phergie_Event_Handler_' . str_replace('.php', '', $entry); // Instantiate each handler if they are not abstract classes $reflector = new ReflectionClass($class); if ($reflector->isInstantiable()) { $client->addEventHandler(new $class()); } } } unset($iterator, $class, $entry); // Execute the client $client->run(); } } /** * Instantiate and run the bot using the first CLI parameter as the path to * the config file to use if specified */ if (php_sapi_name() != 'cli') { trigger_error('Phergie is intended to be run with PHP in CLI mode', E_USER_ERROR); } else if (!ini_get('register_argc_argv')) { trigger_error('The register_argc_argv setting in php.ini must be enabled to run Phergie', E_USER_ERROR); } else if ($argc > 1) { $config = $argv[1]; } else { $config = 'phergie.ini'; } $bot = new Phergie_Bot($config); $bot->run();