Bot.php 10 KB

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