xmppmanager.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) {
  20. exit(1);
  21. }
  22. use XMPPHP\Log;
  23. /**
  24. * XMPP background connection manager for XMPP-using queue handlers,
  25. * allowing them to send outgoing messages on the right connection.
  26. *
  27. * Input is handled during socket select loop, keepalive pings during idle.
  28. * Any incoming messages will be handled.
  29. *
  30. * In a multi-site queuedaemon.php run, one connection will be instantiated
  31. * for each site being handled by the current process that has XMPP enabled.
  32. */
  33. class XmppManager extends ImManager
  34. {
  35. const PING_INTERVAL = 120;
  36. public $conn = null;
  37. protected $lastping = null;
  38. protected $pingid = null;
  39. /**
  40. * Initialize connection to server.
  41. * @param $master
  42. * @return boolean true on success
  43. */
  44. public function start($master)
  45. {
  46. if (parent::start($master)) {
  47. $this->connect();
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. function connect()
  54. {
  55. if (!$this->conn || $this->conn->isDisconnected()) {
  56. $resource = 'queue' . posix_getpid();
  57. $this->conn = new SharingXMPP($this->plugin->host ?
  58. $this->plugin->host :
  59. $this->plugin->server,
  60. $this->plugin->port,
  61. $this->plugin->user,
  62. $this->plugin->password,
  63. $this->plugin->resource,
  64. $this->plugin->server,
  65. $this->plugin->debug ?
  66. true : false,
  67. $this->plugin->debug ?
  68. Log::LEVEL_VERBOSE : null
  69. );
  70. if (!$this->conn) {
  71. return false;
  72. }
  73. $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
  74. $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
  75. $this->conn->setReconnectTimeout(600);
  76. $this->conn->autoSubscribe();
  77. $this->conn->useEncryption($this->plugin->encryption);
  78. $this->conn->connect(true);
  79. $this->conn->processUntil('session_start');
  80. // TRANS: Presence announcement for XMPP.
  81. $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
  82. }
  83. return $this->conn;
  84. }
  85. /**
  86. * sends a presence stanza on the XMPP network
  87. *
  88. * @param string $status current status, free-form string
  89. * @param string $show structured status value
  90. * @param string $to recipient of presence, null for general
  91. * @param string $type type of status message, related to $show
  92. * @param int $priority priority of the presence
  93. *
  94. * @return boolean success value
  95. */
  96. function send_presence($status, $show = 'available', $to = null,
  97. $type = 'available', $priority = null)
  98. {
  99. $this->connect();
  100. if (!$this->conn || $this->conn->isDisconnected()) {
  101. return false;
  102. }
  103. $this->conn->presence($status, $show, $to, $type, $priority);
  104. return true;
  105. }
  106. function send_raw_message($data)
  107. {
  108. $this->connect();
  109. if (!$this->conn || $this->conn->isDisconnected()) {
  110. return false;
  111. }
  112. $this->conn->send($data);
  113. return true;
  114. }
  115. /**
  116. * Message pump is triggered on socket input, so we only need an idle()
  117. * call often enough to trigger our outgoing pings.
  118. */
  119. function timeout()
  120. {
  121. return self::PING_INTERVAL;
  122. }
  123. /**
  124. * Process XMPP events that have come in over the wire.
  125. * @fixme may kill process on XMPP error
  126. * @param resource $socket
  127. */
  128. public function handleInput($socket)
  129. {
  130. // Process the queue for as long as needed
  131. common_log(LOG_DEBUG, "Servicing the XMPP queue.");
  132. $this->stats('xmpp_process');
  133. $this->conn->processTime(0);
  134. }
  135. /**
  136. * Lists the IM connection socket to allow i/o master to wake
  137. * when input comes in here as well as from the queue source.
  138. *
  139. * @return array of resources
  140. */
  141. public function getSockets()
  142. {
  143. $this->connect();
  144. if ($this->conn) {
  145. return array($this->conn->getSocket());
  146. } else {
  147. return array();
  148. }
  149. }
  150. /**
  151. * Idle processing for io manager's execution loop.
  152. * Send keepalive pings to server.
  153. *
  154. * Side effect: kills process on exception from XMPP library.
  155. *
  156. * @param int $timeout
  157. * @todo FIXME: non-dying error handling
  158. */
  159. public function idle($timeout = 0)
  160. {
  161. $now = time();
  162. if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
  163. $this->send_ping();
  164. }
  165. }
  166. function send_ping()
  167. {
  168. $this->connect();
  169. if (!$this->conn || $this->conn->isDisconnected()) {
  170. return false;
  171. }
  172. $now = time();
  173. if (!isset($this->pingid)) {
  174. $this->pingid = 0;
  175. } else {
  176. $this->pingid++;
  177. }
  178. common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
  179. $this->conn->send("<iq from='{$this->plugin->daemonScreenname()}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
  180. $this->lastping = $now;
  181. return true;
  182. }
  183. function handle_xmpp_message(&$pl)
  184. {
  185. $this->plugin->enqueueIncomingRaw($pl);
  186. return true;
  187. }
  188. /**
  189. * Callback for Jabber reconnect event
  190. * @param $pl
  191. */
  192. function handle_xmpp_reconnect(&$pl)
  193. {
  194. common_log(LOG_NOTICE, 'XMPP reconnected');
  195. $this->conn->processUntil('session_start');
  196. // TRANS: Message for XMPP reconnect.
  197. $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
  198. }
  199. /**
  200. * sends a "special" presence stanza on the XMPP network
  201. *
  202. * @param string $type Type of presence
  203. * @param string $to JID to send presence to
  204. * @param string $show show value for presence
  205. * @param string $status status value for presence
  206. *
  207. * @return boolean success flag
  208. *
  209. * @see send_presence()
  210. */
  211. function special_presence($type, $to = null, $show = null, $status = null)
  212. {
  213. // @todo FIXME: why use this instead of send_presence()?
  214. $this->connect();
  215. if (!$this->conn || $this->conn->isDisconnected()) {
  216. return false;
  217. }
  218. $to = htmlspecialchars($to);
  219. $status = htmlspecialchars($status);
  220. $out = "<presence";
  221. if ($to) {
  222. $out .= " to='$to'";
  223. }
  224. if ($type) {
  225. $out .= " type='$type'";
  226. }
  227. if ($show == 'available' and !$status) {
  228. $out .= "/>";
  229. } else {
  230. $out .= ">";
  231. if ($show && ($show != 'available')) {
  232. $out .= "<show>$show</show>";
  233. }
  234. if ($status) {
  235. $out .= "<status>$status</status>";
  236. }
  237. $out .= "</presence>";
  238. }
  239. $this->conn->send($out);
  240. return true;
  241. }
  242. }