StatusnetBot.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Extends the bot class (Phergie_Bot) to allow connection and access to
  19. * sockets and to allow StatusNet to 'drive' the bot
  20. *
  21. * @category Phergie
  22. * @package Phergie_StatusnetBot
  23. * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
  24. * @copyright 2010 StatusNet, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  26. * @link http://status.net/
  27. */
  28. class Phergie_StatusnetBot extends Phergie_Bot {
  29. /**
  30. * Set up bot and connect to servers
  31. *
  32. * @return void
  33. */
  34. public function connect() {
  35. $ui = $this->getUi();
  36. $ui->setEnabled($this->getConfig('ui.enabled'));
  37. $this->loadPlugins();
  38. $this->loadConnections();
  39. }
  40. /**
  41. * Transmit raw command to server using driver
  42. *
  43. * Handles construction of command strings and their transmission to the
  44. * server.
  45. *
  46. * @param string $command Command to send
  47. * @param string|array $args Optional string or array of sequential
  48. * arguments
  49. *
  50. * @return string Command string that was sent
  51. * @throws Phergie_Driver_Exception
  52. */
  53. public function send($command, $args = '') {
  54. return $this->getDriver()->send($command, $args);
  55. }
  56. /**
  57. * Handle incoming data on the socket using the handleEvents
  58. * method of the Processor
  59. *
  60. * @return void
  61. */
  62. public function handleEvents() {
  63. $this->getProcessor()->handleEvents();
  64. }
  65. /**
  66. * Close the current connection and reconnect to the server
  67. *
  68. * @return void
  69. */
  70. public function reconnect() {
  71. $driver = $this->getDriver();
  72. $sockets = $driver->getSockets();
  73. // Close any existing connections
  74. try {
  75. $driver->forceQuit();
  76. } catch (Phergie_Driver_Exception $e){}
  77. try {
  78. $driver->doConnect();
  79. } catch (Phergie_Driver_Exception $e){
  80. $driver->forceQuit();
  81. throw $e;
  82. }
  83. }
  84. /**
  85. * Get the sockets used by the bot
  86. *
  87. * @return array Array of socket resources
  88. */
  89. public function getSockets() {
  90. return $this->getDriver()->getSockets();
  91. }
  92. }