root/trunk/Phergie/Bot.php @ 59

Revision 59, 3.3 KB (checked in by tobias382, 5 years ago)

Unstable commit - driver seems to work, most plugins still need testing
* Modified the streams driver to correctly parse the first argument for

PRIVMSG events into an array

* Added a new debug flag setting to the bundled configuration file
* Moved the debug method from the streams driver to the base driver and

modified it to use the new debug flag setting

* Renamed CtcpAction? methods to Action in order to make logic for

construction and parsing of related requests more compatible with the
same for other commands

* Moved Event_Handler::getEventSource to Event_Request::getSource
* Added Event_Handler::tinyUrl convenience method for converting URLs to

their TinyURL equivalents

* Improved docblock coverage in various classes
* Changed all plugin calls to getConnection()->getNick() to getIni('nick')
* Fixed a memory leak in the Command plugin class
* Added alternate nick support to the Nickserv plugin
* Too many more to name!

Line 
1<?php
2
3/**
4* This file functions as a daemon process used to bootstrap and execute the
5* client by loading its configuration file, instantiating it and event
6* handlers for it, configuring it, and executing its event handling loop.
7*/
8
9/**
10* If this file is being included rather than executed, just terminate
11*/
12if ($_SERVER['SCRIPT_NAME'] == __FILE__) {
13    return;
14}
15
16/**
17* Code base version
18*
19* @const string
20*/
21define('PHERGIE_VERSION', '1.0.2');
22
23/**
24* Path to the configuration file used by default when one is not specified or
25* register_argc_argv is disabled in php.ini
26*
27* @const string
28*/
29define('PHERGIE_INI', 'phergie.ini');
30
31/**
32* Add the Phergie directory to the include path
33*/
34set_include_path(
35    get_include_path()
36    . PATH_SEPARATOR .
37    dirname(dirname(__FILE__))
38);
39
40/**
41* Check to make sure the CLI SAPI is being used
42*/
43if (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* Determine what configuration file should be used
49*/
50if (!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* Obtain and validate the contents of the configuration file
62*/
63$required = array('server', 'username', 'nick');
64$config = @parse_ini_file($ini);
65
66if (count($config) == 0) {
67    echo 'Configuration file inaccessible or empty: ' . $ini . "\n";
68    return;
69}
70
71foreach ($required as $setting) {
72    if (!isset($config[$setting])) {
73        echo 'Required configuration setting missing: ' . $setting . "\n";
74        return;
75    }
76}
77
78/**
79* Configure the client
80*/
81if (isset($config['driver'])) {
82    $driver = ucfirst(strtolower($config['driver']));
83} else {
84    $driver = 'Streams';
85}
86require_once 'Phergie/Driver/' . $driver . '.php';
87$class = 'Phergie_Driver_' . $driver;
88$client = new $class();
89
90foreach ($config as $setting => $value) {
91    $client->setIni($setting, $value);
92}
93
94/**
95* Determine which plugins should be loaded
96*/
97$all = true;
98$include = array();
99if (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* Remove temporary global configuration variables from memory
109*/
110unset($required, $config, $setting, $driver, $class);
111
112/**
113* Set up event handlers
114*/
115$iterator = new DirectoryIterator('Plugin');
116foreach ($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* Remove temporary event handler configuration variables from memory
128*/
129unset($iterator, $class, $entry, $all, $exclude, $reflector);
130
131/**
132* Execute the event handling loop for the client
133*/
134$client->run();
Note: See TracBrowser for help on using the browser.