ImPlugin.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Superclass for plugins that do instant messaging
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://status.net/
  27. */
  28. if (!defined('STATUSNET') && !defined('LACONICA')) {
  29. exit(1);
  30. }
  31. /**
  32. * Superclass for plugins that do IM transport
  33. *
  34. * Implementations will likely want to override onStartIoManagerClasses() so that their
  35. * IO manager is used
  36. *
  37. * @category Module
  38. * @package GNUsocial
  39. * @author Craig Andrews <candrews@integralblue.com>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. abstract class ImPlugin extends Plugin
  44. {
  45. public $widgetOpts;
  46. public $scoped;
  47. //name of this IM transport
  48. public $transport = null;
  49. //list of screennames that should get all public notices
  50. public $public = array();
  51. protected $requires_cli = true;
  52. /**
  53. * normalize a screenname for comparison
  54. *
  55. * @param string $screenname screenname to normalize
  56. *
  57. * @return string an equivalent screenname in normalized form
  58. */
  59. abstract function normalize($screenname);
  60. /**
  61. * validate (ensure the validity of) a screenname
  62. *
  63. * @param string $screenname screenname to validate
  64. *
  65. * @return boolean
  66. */
  67. abstract function validate($screenname);
  68. /**
  69. * get the internationalized/translated display name of this IM service
  70. *
  71. * @return string
  72. */
  73. abstract function getDisplayName();
  74. /**
  75. * send a single notice to a given screenname
  76. * The implementation should put raw data, ready to send, into the outgoing
  77. * queue using enqueueOutgoingRaw()
  78. *
  79. * @param string $screenname screenname to send to
  80. * @param Notice $notice notice to send
  81. *
  82. * @return boolean success value
  83. */
  84. function sendNotice($screenname, Notice $notice)
  85. {
  86. return $this->sendMessage($screenname, $this->formatNotice($notice));
  87. }
  88. /**
  89. * send a message (text) to a given screenname
  90. * The implementation should put raw data, ready to send, into the outgoing
  91. * queue using enqueueOutgoingRaw()
  92. *
  93. * @param string $screenname screenname to send to
  94. * @param Notice $body text to send
  95. *
  96. * @return boolean success value
  97. */
  98. abstract function sendMessage($screenname, $body);
  99. /**
  100. * receive a raw message
  101. * Raw IM data is taken from the incoming queue, and passed to this function.
  102. * It should parse the raw message and call handleIncoming()
  103. *
  104. * Returning false may CAUSE REPROCESSING OF THE QUEUE ITEM, and should
  105. * be used for temporary failures only. For permanent failures such as
  106. * unrecognized addresses, return true to indicate your processing has
  107. * completed.
  108. *
  109. * @param object $data raw IM data
  110. *
  111. * @return boolean true if processing completed, false for temporary failures
  112. */
  113. abstract function receiveRawMessage($data);
  114. /**
  115. * get the screenname of the daemon that sends and receives message for this service
  116. *
  117. * @return string screenname of this plugin
  118. */
  119. abstract function daemonScreenname();
  120. //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - MISC ========================\
  121. /**
  122. * Put raw message data (ready to send) into the outgoing queue
  123. *
  124. * @param object $data
  125. */
  126. function enqueueOutgoingRaw($data)
  127. {
  128. $qm = QueueManager::get();
  129. $qm->enqueue($data, $this->transport . '-out');
  130. }
  131. /**
  132. * Put raw message data (received, ready to be processed) into the incoming queue
  133. *
  134. * @param object $data
  135. */
  136. function enqueueIncomingRaw($data)
  137. {
  138. $qm = QueueManager::get();
  139. $qm->enqueue($data, $this->transport . '-in');
  140. }
  141. /**
  142. * given a screenname, get the corresponding user
  143. *
  144. * @param string $screenname
  145. *
  146. * @return User user
  147. */
  148. function getUser($screenname)
  149. {
  150. $user_im_prefs = $this->getUserImPrefsFromScreenname($screenname);
  151. if($user_im_prefs){
  152. $user = User::getKV('id', $user_im_prefs->user_id);
  153. $user_im_prefs->free();
  154. return $user;
  155. }else{
  156. return false;
  157. }
  158. }
  159. /**
  160. * given a screenname, get the User_im_prefs object for this transport
  161. *
  162. * @param string $screenname
  163. *
  164. * @return User_im_prefs user_im_prefs
  165. */
  166. function getUserImPrefsFromScreenname($screenname)
  167. {
  168. $user_im_prefs = User_im_prefs::pkeyGet(
  169. array('transport' => $this->transport,
  170. 'screenname' => $this->normalize($screenname)));
  171. if ($user_im_prefs) {
  172. return $user_im_prefs;
  173. } else {
  174. return false;
  175. }
  176. }
  177. /**
  178. * given a User, get their screenname
  179. *
  180. * @param User $user
  181. *
  182. * @return string screenname of that user
  183. */
  184. function getScreenname($user)
  185. {
  186. $user_im_prefs = $this->getUserImPrefsFromUser($user);
  187. if ($user_im_prefs) {
  188. return $user_im_prefs->screenname;
  189. } else {
  190. return false;
  191. }
  192. }
  193. /**
  194. * given a User, get their User_im_prefs
  195. *
  196. * @param User $user
  197. *
  198. * @return User_im_prefs user_im_prefs of that user
  199. */
  200. function getUserImPrefsFromUser($user)
  201. {
  202. $user_im_prefs = User_im_prefs::pkeyGet(
  203. array('transport' => $this->transport,
  204. 'user_id' => $user->id));
  205. if ($user_im_prefs){
  206. return $user_im_prefs;
  207. } else {
  208. return false;
  209. }
  210. }
  211. //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - SENDING ========================\
  212. /**
  213. * Send a message to a given screenname from the site
  214. *
  215. * @param string $screenname screenname to send the message to
  216. * @param string $msg message contents to send
  217. *
  218. * @param boolean success
  219. */
  220. protected function sendFromSite($screenname, $msg)
  221. {
  222. $text = '['.common_config('site', 'name') . '] ' . $msg;
  223. $this->sendMessage($screenname, $text);
  224. }
  225. /**
  226. * Send a confirmation code to a user
  227. *
  228. * @param string $screenname screenname sending to
  229. * @param string $code the confirmation code
  230. * @param Profile $target For whom the code is valid for
  231. *
  232. * @return boolean success value
  233. */
  234. function sendConfirmationCode($screenname, $code, Profile $target)
  235. {
  236. // TRANS: Body text for confirmation code e-mail.
  237. // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename,
  238. // TRANS: %3$s is the display name of an IM plugin.
  239. $body = sprintf(_('User "%1$s" on %2$s has said that your %3$s screenname belongs to them. ' .
  240. 'If that is true, you can confirm by clicking on this URL: ' .
  241. '%4$s' .
  242. ' . (If you cannot click it, copy-and-paste it into the ' .
  243. 'address bar of your browser). If that user is not you, ' .
  244. 'or if you did not request this confirmation, just ignore this message.'),
  245. $target->getNickname(), common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', null, array('code' => $code)));
  246. return $this->sendMessage($screenname, $body);
  247. }
  248. /**
  249. * send a notice to all public listeners
  250. *
  251. * For notices that are generated on the local system (by users), we can optionally
  252. * forward them to remote listeners by XMPP.
  253. *
  254. * @param Notice $notice notice to broadcast
  255. *
  256. * @return boolean success flag
  257. */
  258. function publicNotice($notice)
  259. {
  260. // Now, users who want everything
  261. // FIXME PRIV don't send out private messages here
  262. // XXX: should we send out non-local messages if public,localonly
  263. // = false? I think not
  264. foreach ($this->public as $screenname) {
  265. common_log(LOG_INFO,
  266. 'Sending notice ' . $notice->id .
  267. ' to public listener ' . $screenname,
  268. __FILE__);
  269. $this->sendNotice($screenname, $notice);
  270. }
  271. return true;
  272. }
  273. /**
  274. * broadcast a notice to all subscribers and reply recipients
  275. *
  276. * This function will send a notice to all subscribers on the local server
  277. * who have IM addresses, and have IM notification enabled, and
  278. * have this subscription enabled for IM. It also sends the notice to
  279. * all recipients of @-replies who have IM addresses and IM notification
  280. * enabled. This is really the heart of IM distribution in StatusNet.
  281. *
  282. * @param Notice $notice The notice to broadcast
  283. *
  284. * @return boolean success flag
  285. */
  286. function broadcastNotice($notice)
  287. {
  288. $ni = $notice->whoGets();
  289. foreach ($ni as $user_id => $reason) {
  290. $user = User::getKV($user_id);
  291. if (empty($user)) {
  292. // either not a local user, or just not found
  293. continue;
  294. }
  295. $user_im_prefs = $this->getUserImPrefsFromUser($user);
  296. if(!$user_im_prefs || !$user_im_prefs->notify){
  297. continue;
  298. }
  299. switch ($reason) {
  300. case NOTICE_INBOX_SOURCE_REPLY:
  301. if (!$user_im_prefs->replies) {
  302. continue 2;
  303. }
  304. break;
  305. case NOTICE_INBOX_SOURCE_SUB:
  306. $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
  307. 'subscribed' => $notice->profile_id));
  308. if (empty($sub) || !$sub->jabber) {
  309. continue 2;
  310. }
  311. break;
  312. case NOTICE_INBOX_SOURCE_GROUP:
  313. break;
  314. default:
  315. // TRANS: Exception thrown when trying to deliver a notice to an unknown inbox.
  316. // TRANS: %d is the unknown inbox ID (number).
  317. throw new Exception(sprintf(_('Unknown inbox source %d.'), $reason));
  318. }
  319. common_log(LOG_INFO,
  320. 'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname,
  321. __FILE__);
  322. $this->sendNotice($user_im_prefs->screenname, $notice);
  323. $user_im_prefs->free();
  324. }
  325. return true;
  326. }
  327. /**
  328. * makes a plain-text formatted version of a notice, suitable for IM distribution
  329. *
  330. * @param Notice $notice notice being sent
  331. *
  332. * @return string plain-text version of the notice, with user nickname prefixed
  333. */
  334. protected function formatNotice(Notice $notice)
  335. {
  336. $profile = $notice->getProfile();
  337. $nicknames = $profile->getNickname();
  338. try {
  339. $parent = $notice->getParent();
  340. $orig_profile = $parent->getProfile();
  341. $nicknames = sprintf('%1$s => %2$s', $profile->getNickname(), $orig_profile->getNickname());
  342. } catch (NoParentNoticeException $e) {
  343. // Not a reply, no parent notice stored
  344. } catch (NoResultException $e) {
  345. // Parent notice was probably deleted
  346. }
  347. return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id);
  348. }
  349. //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - RECEIVING ========================\
  350. /**
  351. * Attempt to handle a message as a command
  352. * @param User $user user the message is from
  353. * @param string $body message text
  354. * @return boolean true if the message was a command and was executed, false if it was not a command
  355. */
  356. protected function handleCommand($user, $body)
  357. {
  358. $inter = new CommandInterpreter();
  359. $cmd = $inter->handle_command($user, $body);
  360. if ($cmd) {
  361. $chan = new IMChannel($this);
  362. $cmd->execute($chan);
  363. return true;
  364. }
  365. return false;
  366. }
  367. /**
  368. * Is some text an autoreply message?
  369. * @param string $txt message text
  370. * @return boolean true if autoreply
  371. */
  372. protected function isAutoreply($txt)
  373. {
  374. if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
  375. return true;
  376. } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
  377. return true;
  378. } else {
  379. return false;
  380. }
  381. }
  382. /**
  383. * Is some text an OTR message?
  384. * @param string $txt message text
  385. * @return boolean true if OTR
  386. */
  387. protected function isOtr($txt)
  388. {
  389. if (preg_match('/^\?OTR/', $txt)) {
  390. return true;
  391. } else {
  392. return false;
  393. }
  394. }
  395. /**
  396. * Helper for handling incoming messages
  397. * Your incoming message handler will probably want to call this function
  398. *
  399. * @param string $from screenname the message was sent from
  400. * @param string $message message contents
  401. *
  402. * @param boolean success
  403. */
  404. protected function handleIncoming($from, $notice_text)
  405. {
  406. $user = $this->getUser($from);
  407. // For common_current_user to work
  408. global $_cur;
  409. $_cur = $user;
  410. if (!$user) {
  411. $this->sendFromSite($from, 'Unknown user; go to ' .
  412. common_local_url('imsettings') .
  413. ' to add your address to your account');
  414. common_log(LOG_WARNING, 'Message from unknown user ' . $from);
  415. return;
  416. }
  417. if ($this->handleCommand($user, $notice_text)) {
  418. common_log(LOG_INFO, "Command message by $from handled.");
  419. return;
  420. } else if ($this->isAutoreply($notice_text)) {
  421. common_log(LOG_INFO, 'Ignoring auto reply from ' . $from);
  422. return;
  423. } else if ($this->isOtr($notice_text)) {
  424. common_log(LOG_INFO, 'Ignoring OTR from ' . $from);
  425. return;
  426. } else {
  427. common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
  428. $this->addNotice($from, $user, $notice_text);
  429. }
  430. $user->free();
  431. unset($user);
  432. unset($_cur);
  433. unset($message);
  434. }
  435. /**
  436. * Helper for handling incoming messages
  437. * Your incoming message handler will probably want to call this function
  438. *
  439. * @param string $from screenname the message was sent from
  440. * @param string $message message contents
  441. *
  442. * @param boolean success
  443. */
  444. protected function addNotice($screenname, $user, $body)
  445. {
  446. $body = trim(strip_tags($body));
  447. $content_shortened = common_shorten_links($body);
  448. if (Notice::contentTooLong($content_shortened)) {
  449. $this->sendFromSite($screenname,
  450. // TRANS: Message given when a status is too long. %1$s is the maximum number of characters,
  451. // TRANS: %2$s is the number of characters sent (used for plural).
  452. sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
  453. 'Message too long - maximum is %1$d characters, you sent %2$d.',
  454. Notice::maxContent()),
  455. Notice::maxContent(),
  456. mb_strlen($content_shortened)));
  457. return;
  458. }
  459. try {
  460. $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
  461. } catch (Exception $e) {
  462. common_log(LOG_ERR, $e->getMessage());
  463. $this->sendFromSite($from, $e->getMessage());
  464. return;
  465. }
  466. common_log(LOG_INFO,
  467. 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
  468. $notice->free();
  469. unset($notice);
  470. }
  471. //========================EVENT HANDLERS========================\
  472. /**
  473. * Register notice queue handler
  474. *
  475. * @param QueueManager $manager
  476. *
  477. * @return boolean hook return
  478. */
  479. function onEndInitializeQueueManager($manager)
  480. {
  481. // If we don't require CLI mode, or if we do and GNUSOCIAL_CLI _is_ set, then connect the transports
  482. // This check is made mostly because some IM plugins can't deliver to transports unless they
  483. // have continuously running daemons (such as XMPP) and we can't have that over HTTP requests.
  484. if (!$this->requires_cli || defined('GNUSOCIAL_CLI')) {
  485. $manager->connect($this->transport . '-in', new ImReceiverQueueHandler($this), 'im');
  486. $manager->connect($this->transport, new ImQueueHandler($this));
  487. $manager->connect($this->transport . '-out', new ImSenderQueueHandler($this), 'im');
  488. }
  489. return true;
  490. }
  491. function onStartImDaemonIoManagers(&$classes)
  492. {
  493. //$classes[] = new ImManager($this); // handles sending/receiving/pings/reconnects
  494. return true;
  495. }
  496. function onStartEnqueueNotice($notice, &$transports)
  497. {
  498. $profile = Profile::getKV($notice->profile_id);
  499. if (!$profile) {
  500. common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
  501. 'unknown profile ' . common_log_objstring($notice),
  502. __FILE__);
  503. }else{
  504. $transports[] = $this->transport;
  505. }
  506. return true;
  507. }
  508. function onEndShowHeadElements(Action $action)
  509. {
  510. if ($action instanceof ShownoticeAction) {
  511. $user_im_prefs = new User_im_prefs();
  512. $user_im_prefs->user_id = $action->notice->getProfile()->getID();
  513. $user_im_prefs->transport = $this->transport;
  514. } elseif ($action instanceof ShowstreamAction) {
  515. $user_im_prefs = new User_im_prefs();
  516. $user_im_prefs->user_id = $action->getTarget()->getID();
  517. $user_im_prefs->transport = $this->transport;
  518. }
  519. }
  520. function onNormalizeImScreenname($transport, &$screenname)
  521. {
  522. if($transport == $this->transport)
  523. {
  524. $screenname = $this->normalize($screenname);
  525. return false;
  526. }
  527. }
  528. function onValidateImScreenname($transport, $screenname, &$valid)
  529. {
  530. if($transport == $this->transport)
  531. {
  532. $valid = $this->validate($screenname);
  533. return false;
  534. }
  535. }
  536. function onGetImTransports(&$transports)
  537. {
  538. $transports[$this->transport] = array(
  539. 'display' => $this->getDisplayName(),
  540. 'daemonScreenname' => $this->daemonScreenname());
  541. }
  542. function onSendImConfirmationCode($transport, $screenname, $code, Profile $target)
  543. {
  544. if($transport == $this->transport)
  545. {
  546. $this->sendConfirmationCode($screenname, $code, $target);
  547. return false;
  548. }
  549. }
  550. function onUserDeleteRelated($user, &$tables)
  551. {
  552. $tables[] = 'User_im_prefs';
  553. return true;
  554. }
  555. function onHaveImPlugin(&$haveImPlugin) {
  556. $haveImPlugin = true; // set flag true (we're loaded, after all!)
  557. return false; // stop looking
  558. }
  559. function initialize()
  560. {
  561. if( ! common_config('queue', 'enabled'))
  562. {
  563. // TRANS: Server exception thrown trying to initialise an IM plugin without meeting all prerequisites.
  564. throw new ServerException(_('Queueing must be enabled to use IM plugins.'));
  565. }
  566. if(is_null($this->transport)){
  567. // TRANS: Server exception thrown trying to initialise an IM plugin without a transport method.
  568. throw new ServerException(_('Transport cannot be null.'));
  569. }
  570. }
  571. }