| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | if ($_SERVER['SCRIPT_NAME'] == __FILE__) { |
|---|
| 13 | return; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | define('PHERGIE_VERSION', '1.0.2'); |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | define('PHERGIE_INI', 'phergie.ini'); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | set_include_path( |
|---|
| 35 | get_include_path() |
|---|
| 36 | . PATH_SEPARATOR . |
|---|
| 37 | dirname(dirname(__FILE__)) |
|---|
| 38 | ); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if (php_sapi_name() != 'cli') { |
|---|
| 44 | trigger_error('Phergie is intended to be run using the CLI SAPI for PHP', E_USER_ERROR); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | if (!ini_get('register_argc_argv')) { |
|---|
| 51 | echo 'The register_argc_argv setting in php.ini is disabled, defaulting to ' . PHERGIE_INI . "\n"; |
|---|
| 52 | $ini = PHERGIE_INI; |
|---|
| 53 | } else if ($argc == 1) { |
|---|
| 54 | echo 'No configuration file specified, defaulting to ' . PHERGIE_INI . "\n"; |
|---|
| 55 | $ini = PHERGIE_INI; |
|---|
| 56 | } else { |
|---|
| 57 | $ini = $argv[1]; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | $required = array('server', 'username', 'nick'); |
|---|
| 64 | $config = @parse_ini_file($ini); |
|---|
| 65 | |
|---|
| 66 | if (count($config) == 0) { |
|---|
| 67 | echo 'Configuration file inaccessible or empty: ' . $ini . "\n"; |
|---|
| 68 | return; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | foreach ($required as $setting) { |
|---|
| 72 | if (!isset($config[$setting])) { |
|---|
| 73 | echo 'Required configuration setting missing: ' . $setting . "\n"; |
|---|
| 74 | return; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | if (isset($config['driver'])) { |
|---|
| 82 | $driver = ucfirst(strtolower($config['driver'])); |
|---|
| 83 | } else { |
|---|
| 84 | $driver = 'Streams'; |
|---|
| 85 | } |
|---|
| 86 | require_once 'Phergie/Driver/' . $driver . '.php'; |
|---|
| 87 | $class = 'Phergie_Driver_' . $driver; |
|---|
| 88 | $client = new $class(); |
|---|
| 89 | |
|---|
| 90 | foreach ($config as $setting => $value) { |
|---|
| 91 | $client->setIni($setting, $value); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | $all = true; |
|---|
| 98 | $include = array(); |
|---|
| 99 | if (isset($config['plugins']) |
|---|
| 100 | && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { |
|---|
| 101 | $all = $match[1] != 'none'; |
|---|
| 102 | if (isset($match[2])) { |
|---|
| 103 | $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | unset($required, $config, $setting, $driver, $class); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | $iterator = new DirectoryIterator('Plugin'); |
|---|
| 116 | foreach ($iterator as $entry) { |
|---|
| 117 | if ($iterator->isFile() |
|---|
| 118 | && substr($entry, -4) == '.php' |
|---|
| 119 | && ($all xor in_array(strtolower(substr($entry, 0, -4)), $include))) { |
|---|
| 120 | require_once 'Phergie/Plugin/' . $entry; |
|---|
| 121 | $class = 'Phergie_Plugin_' . str_replace('.php', '', $entry); |
|---|
| 122 | $client->addEventHandler(new $class($client)); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | unset($iterator, $class, $entry, $all, $exclude, $reflector); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | $client->run(); |
|---|