]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Bot.php
Merge branch 'master' into social-master
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Bot.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie
20  */
21
22 /**
23  * Composite class for other components to represent the bot.
24  *
25  * @category Phergie
26  * @package  Phergie
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie
30  */
31 class Phergie_Bot
32 {
33     /**
34      * Current version of Phergie
35      */
36     const VERSION = '2.0.1';
37
38     /**
39      * Current driver instance
40      *
41      * @var Phergie_Driver_Abstract
42      */
43     protected $driver;
44
45     /**
46      * Current configuration instance
47      *
48      * @var Phergie_Config
49      */
50     protected $config;
51
52     /**
53      * Current connection handler instance
54      *
55      * @var Phergie_Connection_Handler
56      */
57     protected $connections;
58
59     /**
60      * Current plugin handler instance
61      *
62      * @var Phergie_Plugin_Handler
63      */
64     protected $plugins;
65
66     /**
67      * Current event handler instance
68      *
69      * @var Phergie_Event_Handler
70      */
71     protected $events;
72
73     /**
74      * Current end-user interface instance
75      *
76      * @var Phergie_Ui_Abstract
77      */
78     protected $ui;
79
80     /**
81      * Current processor instance
82      *
83      * @var Phergie_Process_Abstract
84      */
85     protected $processor;
86
87     /**
88      * Returns a driver instance, creating one of the default class if
89      * none has been set.
90      *
91      * @return Phergie_Driver_Abstract
92      */
93     public function getDriver()
94     {
95         if (empty($this->driver)) {
96             // Check if a driver has been defined in the configuration to use
97             // as the default
98             $config = $this->getConfig();
99             if (isset($config['driver'])) {
100                 $class = 'Phergie_Driver_' . ucfirst($config['driver']);
101             } else {
102                 // Otherwise default to the Streams driver.
103                 $class = 'Phergie_Driver_Streams';
104             }
105
106             $this->driver = new $class;
107         }
108         return $this->driver;
109     }
110
111     /**
112      * Sets the driver instance to use.
113      *
114      * @param Phergie_Driver_Abstract $driver Driver instance
115      *
116      * @return Phergie_Bot Provides a fluent interface
117      */
118     public function setDriver(Phergie_Driver_Abstract $driver)
119     {
120         $this->driver = $driver;
121         return $this;
122     }
123
124     /**
125      * Sets the configuration to use.
126      *
127      * @param Phergie_Config $config Configuration instance
128      *
129      * @return Phergie_Runner_Abstract Provides a fluent interface
130      */
131     public function setConfig(Phergie_Config $config)
132     {
133         $this->config = $config;
134         return $this;
135     }
136
137     /**
138      * Returns the entire configuration in use or the value of a specific
139      * configuration setting.
140      *
141      * @param string $index   Optional index of a specific configuration
142      *        setting for which the corresponding value should be returned
143      * @param mixed  $default Value to return if no match is found for $index
144      *
145      * @return mixed Value corresponding to $index or the entire
146      *         configuration if $index is not specified
147      */
148     public function getConfig($index = null, $default = null)
149     {
150         if (empty($this->config)) {
151             $this->config = new Phergie_Config;
152             $this->config->read('Settings.php');
153         }
154         if ($index !== null) {
155             if (isset($this->config[$index])) {
156                 return $this->config[$index];
157             } else {
158                 return $default;
159             }
160         }
161         return $this->config;
162     }
163
164     /**
165      * Returns a plugin handler instance, creating it if it does not already
166      * exist and using a default class if none has been set.
167      *
168      * @return Phergie_Plugin_Handler
169      */
170     public function getPluginHandler()
171     {
172         if (empty($this->plugins)) {
173             $this->plugins = new Phergie_Plugin_Handler(
174                 $this->getConfig(),
175                 $this->getEventHandler()
176             );
177         }
178         return $this->plugins;
179     }
180
181     /**
182      * Sets the plugin handler instance to use.
183      *
184      * @param Phergie_Plugin_Handler $handler Plugin handler instance
185      *
186      * @return Phergie_Bot Provides a fluent interface
187      */
188     public function setPluginHandler(Phergie_Plugin_Handler $handler)
189     {
190         $this->plugins = $handler;
191         return $this;
192     }
193
194     /**
195      * Returns an event handler instance, creating it if it does not already
196      * exist and using a default class if none has been set.
197      *
198      * @return Phergie_Event_Handler
199      */
200     public function getEventHandler()
201     {
202         if (empty($this->events)) {
203             $this->events = new Phergie_Event_Handler;
204         }
205         return $this->events;
206     }
207
208     /**
209      * Sets the event handler instance to use.
210      *
211      * @param Phergie_Event_Handler $handler Event handler instance
212      *
213      * @return Phergie_Bot Provides a fluent interface
214      */
215     public function setEventHandler(Phergie_Event_Handler $handler)
216     {
217         $this->events = $handler;
218         return $this;
219     }
220
221     /**
222      * Returns a connection handler instance, creating it if it does not
223      * already exist and using a default class if none has been set.
224      *
225      * @return Phergie_Connection_Handler
226      */
227     public function getConnectionHandler()
228     {
229         if (empty($this->connections)) {
230             $this->connections = new Phergie_Connection_Handler;
231         }
232         return $this->connections;
233     }
234
235     /**
236      * Sets the connection handler instance to use.
237      *
238      * @param Phergie_Connection_Handler $handler Connection handler instance
239      *
240      * @return Phergie_Bot Provides a fluent interface
241      */
242     public function setConnectionHandler(Phergie_Connection_Handler $handler)
243     {
244         $this->connections = $handler;
245         return $this;
246     }
247
248     /**
249      * Returns an end-user interface instance, creating it if it does not
250      * already exist and using a default class if none has been set.
251      *
252      * @return Phergie_Ui_Abstract
253      */
254     public function getUi()
255     {
256         if (empty($this->ui)) {
257             $this->ui = new Phergie_Ui_Console;
258         }
259         return $this->ui;
260     }
261
262     /**
263      * Sets the end-user interface instance to use.
264      *
265      * @param Phergie_Ui_Abstract $ui End-user interface instance
266      *
267      * @return Phergie_Bot Provides a fluent interface
268      */
269     public function setUi(Phergie_Ui_Abstract $ui)
270     {
271         $this->ui = $ui;
272         return $this;
273     }
274
275     /**
276      * Returns a processer instance, creating one if none exists.
277      *
278      * @return Phergie_Process_Abstract
279      */
280     public function getProcessor()
281     {
282         if (empty($this->processor)) {
283             $class = 'Phergie_Process_Standard';
284
285             $type = $this->getConfig('processor');
286             if (!empty($type)) {
287                 $class = 'Phergie_Process_' . ucfirst($type);
288             }
289
290             $this->processor = new $class(
291                 $this,
292                 $this->getConfig('processor.options', array())
293             );
294         }
295         return $this->processor;
296     }
297
298     /**
299      * Sets the processer instance to use.
300      *
301      * @param Phergie_Process_Abstract $processor Processer instance
302      *
303      * @return Phergie_Bot Provides a fluent interface
304      */
305     public function setProcessor(Phergie_Process_Abstract $processor)
306     {
307         $this->processor = $processor;
308         return $this;
309     }
310
311     /**
312      * Loads plugins into the plugin handler.
313      *
314      * @return void
315      */
316     protected function loadPlugins()
317     {
318         $config = $this->getConfig();
319         $plugins = $this->getPluginHandler();
320         $ui = $this->getUi();
321
322         $plugins->setAutoload($config['plugins.autoload']);
323         foreach ($config['plugins'] as $name) {
324             try {
325                 $plugin = $plugins->addPlugin($name);
326                 $ui->onPluginLoad($name);
327             } catch (Phergie_Plugin_Exception $e) {
328                 $ui->onPluginFailure($name, $e->getMessage());
329                 if (!empty($plugin)) {
330                     $plugins->removePlugin($plugin);
331                 }
332             }
333         }
334     }
335
336     /**
337      * Configures and establishes connections to IRC servers.
338      *
339      * @return void
340      */
341     protected function loadConnections()
342     {
343         $config = $this->getConfig();
344         $driver = $this->getDriver();
345         $connections = $this->getConnectionHandler();
346         $plugins = $this->getPluginHandler();
347         $ui = $this->getUi();
348
349         foreach ($config['connections'] as $data) {
350             $connection = new Phergie_Connection($data);
351             $connections->addConnection($connection);
352
353             $ui->onConnect($data['host']);
354             $driver->setConnection($connection)->doConnect();
355             $plugins->setConnection($connection);
356             $plugins->onConnect();
357         }
358     }
359
360     /**
361      * Establishes server connections and initiates an execution loop to
362      * continuously receive and process events.
363      *
364      * @return Phergie_Bot Provides a fluent interface
365      */
366     public function run()
367     {
368         set_time_limit(0);
369
370         $timezone = $this->getConfig('timezone', 'UTC');
371         date_default_timezone_set($timezone);
372
373         $ui = $this->getUi();
374         $ui->setEnabled($this->getConfig('ui.enabled'));
375
376         $this->loadPlugins();
377         $this->loadConnections();
378
379         $processor = $this->getProcessor();
380
381         $connections = $this->getConnectionHandler();
382         while (count($connections)) {
383             $processor->handleEvents();
384         }
385
386         $ui->onShutdown();
387
388         return $this;
389     }
390 }