root/trunk/Phergie/Bot.php @ 71

Revision 71, 3.7 KB (checked in by tobias382, 5 years ago)

* Added support to the Acronym plugin for filtering responses to certain acronyms
* Modified the Acronym plugin to restrict lookups to instances where the acronym takes up the entire post
* Modified the Acronym and Url plugins to add a lookup timeout
* Modified the Nickserv plugin to extend AdminCommand? instead of Command
* Fixed an issue in the tinyUrl method of the base plugin class where lack of URL encoding of the passed URL resulted in truncation of the original URL when computing its TinyURL equivalent
* Modified getIni and setIni methods in and added getPluginIni and setPluginIni to the base plugin case to allow for easier access to both core and plugin-specific settings and modified existing plugins to use the new methods where appropriate

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* Path to the directory containing the Phergie directory
33*
34* @const string
35*/
36define('PHERGIE_DIR', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
37
38/**
39* Add the Phergie directory to the include path
40*/
41set_include_path(
42    get_include_path()
43    . PATH_SEPARATOR .
44    PHERGIE_DIR
45);
46
47/**
48* Check to make sure the CLI SAPI is being used
49*/
50if (php_sapi_name() != 'cli') {
51    trigger_error('Phergie is intended to be run using the CLI SAPI for PHP', E_USER_ERROR);
52}
53
54/**
55* Allow the bot to run indefinitely
56*/
57set_time_limit(0);
58
59/**
60* Determine what configuration file should be used
61*/
62if (!ini_get('register_argc_argv')) {
63    echo 'The register_argc_argv setting in php.ini is disabled, defaulting to ' . PHERGIE_INI . "\n";
64    $ini = PHERGIE_INI;
65} else if ($argc == 1) {
66    echo 'No configuration file specified, defaulting to ' . PHERGIE_INI . "\n";
67    $ini = PHERGIE_INI;
68} else {
69    $ini = $argv[1];
70}
71
72/**
73* Obtain and validate the contents of the configuration file
74*/
75$required = array('server', 'username', 'nick');
76$config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini);
77
78if (count($config) == 0) {
79    echo 'Configuration file inaccessible or empty: ' . $ini . "\n";
80    return;
81}
82
83foreach ($required as $setting) {
84    if (!isset($config[$setting]) || empty($config[$setting])) {
85        echo 'Required configuration setting missing: ' . $setting . "\n";
86        return;
87    }
88}
89
90/**
91* Configure the client
92*/
93if (isset($config['driver'])) {
94    $driver = ucfirst(strtolower($config['driver']));
95} else {
96    $driver = 'Streams';
97}
98require_once 'Phergie/Driver/' . $driver . '.php';
99$class = 'Phergie_Driver_' . $driver;
100$client = new $class();
101
102foreach ($config as $setting => $value) {
103    $client->setIni($setting, $value);
104}
105
106/**
107* Determine which plugins should be loaded
108*/
109$all = true;
110$include = array();
111if (isset($config['plugins'])
112    && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) {
113    $all = $match[1] != 'none';
114    if (isset($match[2])) {
115        $include = array_map('strtolower', preg_split('/[, ]+/', $match[2]));
116    }
117}
118
119/**
120* Remove temporary global configuration variables from memory
121*/
122unset($required, $config, $setting, $driver, $class);
123
124/**
125* Set up plugins
126*/
127$iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin');
128foreach ($iterator as $entry) {
129    if ($iterator->isFile()
130        && substr($entry, -4) == '.php'
131        && ($all xor in_array(strtolower(substr($entry, 0, -4)), $include))) {
132        require_once 'Phergie/Plugin/' . $entry;
133        $class = 'Phergie_Plugin_' . str_replace('.php', '', $entry);
134        $instance = new $class($client);
135        $client->addPlugin($instance);
136        $client->debug('Loaded ' . $instance->getName());
137    }
138}
139
140/**
141* Remove temporary plugin configuration variables from memory
142*/
143unset($iterator, $class, $entry, $all, $exclude, $reflector);
144
145/**
146* Execute the event handling loop for the client
147*/
148$client->run();
Note: See TracBrowser for help on using the browser.