implugin.php 21 KB

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