123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- abstract class ImPlugin extends Plugin
- {
-
- public $transport = null;
-
- public $public = array();
-
- abstract function normalize($screenname);
-
- abstract function validate($screenname);
-
- abstract function getDisplayName();
-
- function sendNotice($screenname, Notice $notice)
- {
- return $this->sendMessage($screenname, $this->formatNotice($notice));
- }
-
- abstract function sendMessage($screenname, $body);
-
- abstract function receiveRawMessage($data);
-
- abstract function daemonScreenname();
-
- function microiduri($screenname)
- {
- return $this->transport . ':' . $screenname;
- }
-
-
- function enqueueOutgoingRaw($data)
- {
- $qm = QueueManager::get();
- $qm->enqueue($data, $this->transport . '-out');
- }
-
- function enqueueIncomingRaw($data)
- {
- $qm = QueueManager::get();
- $qm->enqueue($data, $this->transport . '-in');
- }
-
- function getUser($screenname)
- {
- $user_im_prefs = $this->getUserImPrefsFromScreenname($screenname);
- if($user_im_prefs){
- $user = User::getKV('id', $user_im_prefs->user_id);
- $user_im_prefs->free();
- return $user;
- }else{
- return false;
- }
- }
-
- function getUserImPrefsFromScreenname($screenname)
- {
- $user_im_prefs = User_im_prefs::pkeyGet(
- array('transport' => $this->transport,
- 'screenname' => $this->normalize($screenname)));
- if ($user_im_prefs) {
- return $user_im_prefs;
- } else {
- return false;
- }
- }
-
- function getScreenname($user)
- {
- $user_im_prefs = $this->getUserImPrefsFromUser($user);
- if ($user_im_prefs) {
- return $user_im_prefs->screenname;
- } else {
- return false;
- }
- }
-
- function getUserImPrefsFromUser($user)
- {
- $user_im_prefs = User_im_prefs::pkeyGet(
- array('transport' => $this->transport,
- 'user_id' => $user->id));
- if ($user_im_prefs){
- return $user_im_prefs;
- } else {
- return false;
- }
- }
-
-
- protected function sendFromSite($screenname, $msg)
- {
- $text = '['.common_config('site', 'name') . '] ' . $msg;
- $this->sendMessage($screenname, $text);
- }
-
- function sendConfirmationCode($screenname, $code, $user)
- {
-
-
-
- $body = sprintf(_('User "%1$s" on %2$s has said that your %3$s screenname belongs to them. ' .
- 'If that is true, you can confirm by clicking on this URL: ' .
- '%4$s' .
- ' . (If you cannot click it, copy-and-paste it into the ' .
- 'address bar of your browser). If that user is not you, ' .
- 'or if you did not request this confirmation, just ignore this message.'),
- $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', null, array('code' => $code)));
- return $this->sendMessage($screenname, $body);
- }
-
- function publicNotice($notice)
- {
-
-
-
-
- foreach ($this->public as $screenname) {
- common_log(LOG_INFO,
- 'Sending notice ' . $notice->id .
- ' to public listener ' . $screenname,
- __FILE__);
- $this->sendNotice($screenname, $notice);
- }
- return true;
- }
-
- function broadcastNotice($notice)
- {
- $ni = $notice->whoGets();
- foreach ($ni as $user_id => $reason) {
- $user = User::getKV($user_id);
- if (empty($user)) {
-
- continue;
- }
- $user_im_prefs = $this->getUserImPrefsFromUser($user);
- if(!$user_im_prefs || !$user_im_prefs->notify){
- continue;
- }
- switch ($reason) {
- case NOTICE_INBOX_SOURCE_REPLY:
- if (!$user_im_prefs->replies) {
- continue 2;
- }
- break;
- case NOTICE_INBOX_SOURCE_SUB:
- $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
- 'subscribed' => $notice->profile_id));
- if (empty($sub) || !$sub->jabber) {
- continue 2;
- }
- break;
- case NOTICE_INBOX_SOURCE_GROUP:
- break;
- default:
-
-
- throw new Exception(sprintf(_('Unknown inbox source %d.'), $reason));
- }
- common_log(LOG_INFO,
- 'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname,
- __FILE__);
- $this->sendNotice($user_im_prefs->screenname, $notice);
- $user_im_prefs->free();
- }
- return true;
- }
-
- protected function formatNotice(Notice $notice)
- {
- $profile = $notice->getProfile();
- try {
- $parent = $notice->getParent();
- $orig_profile = $parent->getProfile();
- $nicknames = sprintf('%1$s => %2$s', $profile->nickname, $orig_profile->nickname);
- } catch (Exception $e) {
- $nicknames = $profile->nickname;
- }
- return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id);
- }
-
-
- protected function handleCommand($user, $body)
- {
- $inter = new CommandInterpreter();
- $cmd = $inter->handle_command($user, $body);
- if ($cmd) {
- $chan = new IMChannel($this);
- $cmd->execute($chan);
- return true;
- } else {
- return false;
- }
- }
-
- protected function isAutoreply($txt)
- {
- if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
- return true;
- } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
- return true;
- } else {
- return false;
- }
- }
-
- protected function isOtr($txt)
- {
- if (preg_match('/^\?OTR/', $txt)) {
- return true;
- } else {
- return false;
- }
- }
-
- protected function handleIncoming($from, $notice_text)
- {
- $user = $this->getUser($from);
-
- global $_cur;
- $_cur = $user;
- if (!$user) {
- $this->sendFromSite($from, 'Unknown user; go to ' .
- common_local_url('imsettings') .
- ' to add your address to your account');
- common_log(LOG_WARNING, 'Message from unknown user ' . $from);
- return;
- }
- if ($this->handleCommand($user, $notice_text)) {
- common_log(LOG_INFO, "Command message by $from handled.");
- return;
- } else if ($this->isAutoreply($notice_text)) {
- common_log(LOG_INFO, 'Ignoring auto reply from ' . $from);
- return;
- } else if ($this->isOtr($notice_text)) {
- common_log(LOG_INFO, 'Ignoring OTR from ' . $from);
- return;
- } else {
- common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
- $this->addNotice($from, $user, $notice_text);
- }
- $user->free();
- unset($user);
- unset($_cur);
- unset($message);
- }
-
- protected function addNotice($screenname, $user, $body)
- {
- $body = trim(strip_tags($body));
- $content_shortened = common_shorten_links($body);
- if (Notice::contentTooLong($content_shortened)) {
- $this->sendFromSite($screenname,
-
-
- sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
- 'Message too long - maximum is %1$d characters, you sent %2$d.',
- Notice::maxContent()),
- Notice::maxContent(),
- mb_strlen($content_shortened)));
- return;
- }
- try {
- $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
- } catch (Exception $e) {
- common_log(LOG_ERR, $e->getMessage());
- $this->sendFromSite($from, $e->getMessage());
- return;
- }
- common_log(LOG_INFO,
- 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
- $notice->free();
- unset($notice);
- }
-
-
- function onEndInitializeQueueManager($manager)
- {
- $manager->connect($this->transport . '-in', new ImReceiverQueueHandler($this), 'im');
- $manager->connect($this->transport, new ImQueueHandler($this));
- $manager->connect($this->transport . '-out', new ImSenderQueueHandler($this), 'im');
- return true;
- }
- function onStartImDaemonIoManagers(&$classes)
- {
-
- return true;
- }
- function onStartEnqueueNotice($notice, &$transports)
- {
- $profile = Profile::getKV($notice->profile_id);
- if (!$profile) {
- common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
- 'unknown profile ' . common_log_objstring($notice),
- __FILE__);
- }else{
- $transports[] = $this->transport;
- }
- return true;
- }
- function onEndShowHeadElements($action)
- {
- $aname = $action->trimmed('action');
- if ($aname == 'shownotice') {
- $user_im_prefs = new User_im_prefs();
- $user_im_prefs->user_id = $action->profile->id;
- $user_im_prefs->transport = $this->transport;
- if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) {
- $id = new Microid($this->microiduri($user_im_prefs->screenname),
- $action->notice->uri);
- $action->element('meta', array('name' => 'microid',
- 'content' => $id->toString()));
- }
- } else if ($aname == 'showstream') {
- $user_im_prefs = new User_im_prefs();
- $user_im_prefs->user_id = $action->user->id;
- $user_im_prefs->transport = $this->transport;
- if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->profile->profileurl) {
- $id = new Microid($this->microiduri($user_im_prefs->screenname),
- $action->selfUrl());
- $action->element('meta', array('name' => 'microid',
- 'content' => $id->toString()));
- }
- }
- }
- function onNormalizeImScreenname($transport, &$screenname)
- {
- if($transport == $this->transport)
- {
- $screenname = $this->normalize($screenname);
- return false;
- }
- }
- function onValidateImScreenname($transport, $screenname, &$valid)
- {
- if($transport == $this->transport)
- {
- $valid = $this->validate($screenname);
- return false;
- }
- }
- function onGetImTransports(&$transports)
- {
- $transports[$this->transport] = array(
- 'display' => $this->getDisplayName(),
- 'daemonScreenname' => $this->daemonScreenname());
- }
- function onSendImConfirmationCode($transport, $screenname, $code, $user)
- {
- if($transport == $this->transport)
- {
- $this->sendConfirmationCode($screenname, $code, $user);
- return false;
- }
- }
- function onUserDeleteRelated($user, &$tables)
- {
- $tables[] = 'User_im_prefs';
- return true;
- }
- function onHaveImPlugin(&$haveImPlugin) {
- $haveImPlugin = true;
- return false;
- }
- function initialize()
- {
- if( ! common_config('queue', 'enabled'))
- {
-
- throw new ServerException(_('Queueing must be enabled to use IM plugins.'));
- }
- if(is_null($this->transport)){
-
- throw new ServerException(_('Transport cannot be null.'));
- }
- }
- }
|