X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fimplugin.php;h=457c9dba5252d9528c3d5eb0568e97820d3d3cd6;hb=56e2b0007c00b85b80c85da072def1f9e832e05b;hp=7125aaee8d8c19ab8489c719d42328e757c886b2;hpb=173778eab145cfd73f597cd75db195e4d47fe323;p=quix0rs-gnu-social.git diff --git a/lib/implugin.php b/lib/implugin.php index 7125aaee8d..457c9dba52 100644 --- a/lib/implugin.php +++ b/lib/implugin.php @@ -42,7 +42,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ - abstract class ImPlugin extends Plugin { //name of this IM transport @@ -50,6 +49,8 @@ abstract class ImPlugin extends Plugin //list of screennames that should get all public notices public $public = array(); + protected $requires_cli = true; + /** * normalize a screenname for comparison * @@ -59,7 +60,6 @@ abstract class ImPlugin extends Plugin */ abstract function normalize($screenname); - /** * validate (ensure the validity of) a screenname * @@ -79,35 +79,35 @@ abstract class ImPlugin extends Plugin /** * send a single notice to a given screenname * The implementation should put raw data, ready to send, into the outgoing - * queue using enqueue_outgoing_raw() + * queue using enqueueOutgoingRaw() * * @param string $screenname screenname to send to * @param Notice $notice notice to send * * @return boolean success value */ - function send_notice($screenname, $notice) + function sendNotice($screenname, Notice $notice) { - return $this->send_message($screenname, $this->format_notice($notice)); + return $this->sendMessage($screenname, $this->formatNotice($notice)); } /** * send a message (text) to a given screenname * The implementation should put raw data, ready to send, into the outgoing - * queue using enqueue_outgoing_raw() + * queue using enqueueOutgoingRaw() * * @param string $screenname screenname to send to * @param Notice $body text to send * * @return boolean success value */ - abstract function send_message($screenname, $body); + abstract function sendMessage($screenname, $body); /** * receive a raw message * Raw IM data is taken from the incoming queue, and passed to this function. - * It should parse the raw message and call handle_incoming() - * + * It should parse the raw message and call handleIncoming() + * * Returning false may CAUSE REPROCESSING OF THE QUEUE ITEM, and should * be used for temporary failures only. For permanent failures such as * unrecognized addresses, return true to indicate your processing has @@ -117,26 +117,15 @@ abstract class ImPlugin extends Plugin * * @return boolean true if processing completed, false for temporary failures */ - abstract function receive_raw_message($data); + abstract function receiveRawMessage($data); /** * get the screenname of the daemon that sends and receives message for this service * * @return string screenname of this plugin */ - abstract function daemon_screenname(); + abstract function daemonScreenname(); - /** - * get the microid uri of a given screenname - * - * @param string $screenname screenname - * - * @return string microid uri - */ - function microiduri($screenname) - { - return $this->transport . ':' . $screenname; - } //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - MISC ========================\ /** @@ -144,7 +133,7 @@ abstract class ImPlugin extends Plugin * * @param object $data */ - function enqueue_outgoing_raw($data) + function enqueueOutgoingRaw($data) { $qm = QueueManager::get(); $qm->enqueue($data, $this->transport . '-out'); @@ -155,7 +144,7 @@ abstract class ImPlugin extends Plugin * * @param object $data */ - function enqueue_incoming_raw($data) + function enqueueIncomingRaw($data) { $qm = QueueManager::get(); $qm->enqueue($data, $this->transport . '-in'); @@ -168,11 +157,11 @@ abstract class ImPlugin extends Plugin * * @return User user */ - function get_user($screenname) + function getUser($screenname) { - $user_im_prefs = $this->get_user_im_prefs_from_screenname($screenname); + $user_im_prefs = $this->getUserImPrefsFromScreenname($screenname); if($user_im_prefs){ - $user = User::staticGet('id', $user_im_prefs->user_id); + $user = User::getKV('id', $user_im_prefs->user_id); $user_im_prefs->free(); return $user; }else{ @@ -180,7 +169,6 @@ abstract class ImPlugin extends Plugin } } - /** * given a screenname, get the User_im_prefs object for this transport * @@ -188,7 +176,7 @@ abstract class ImPlugin extends Plugin * * @return User_im_prefs user_im_prefs */ - function get_user_im_prefs_from_screenname($screenname) + function getUserImPrefsFromScreenname($screenname) { $user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $this->transport, @@ -200,7 +188,6 @@ abstract class ImPlugin extends Plugin } } - /** * given a User, get their screenname * @@ -208,9 +195,9 @@ abstract class ImPlugin extends Plugin * * @return string screenname of that user */ - function get_screenname($user) + function getScreenname($user) { - $user_im_prefs = $this->get_user_im_prefs_from_user($user); + $user_im_prefs = $this->getUserImPrefsFromUser($user); if ($user_im_prefs) { return $user_im_prefs->screenname; } else { @@ -218,7 +205,6 @@ abstract class ImPlugin extends Plugin } } - /** * given a User, get their User_im_prefs * @@ -226,7 +212,7 @@ abstract class ImPlugin extends Plugin * * @return User_im_prefs user_im_prefs of that user */ - function get_user_im_prefs_from_user($user) + function getUserImPrefsFromUser($user) { $user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $this->transport, @@ -246,32 +232,35 @@ abstract class ImPlugin extends Plugin * * @param boolean success */ - protected function send_from_site($screenname, $msg) + protected function sendFromSite($screenname, $msg) { $text = '['.common_config('site', 'name') . '] ' . $msg; - $this->send_message($screenname, $text); + $this->sendMessage($screenname, $text); } /** - * send a confirmation code to a user + * Send a confirmation code to a user * * @param string $screenname screenname sending to * @param string $code the confirmation code - * @param User $user user sending to + * @param Profile $target For whom the code is valid for * * @return boolean success value */ - function send_confirmation_code($screenname, $code, $user) + function sendConfirmationCode($screenname, $code, Profile $target) { - $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' . - 'If that\'s true, you can confirm by clicking on this URL: ' . - '%s' . + // TRANS: Body text for confirmation code e-mail. + // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename, + // TRANS: %3$s is the display name of an IM plugin. + $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 isn\'t you, ' . - 'or if you didn\'t request this confirmation, just ignore this message.'), - $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code))); + 'address bar of your browser). If that user is not you, ' . + 'or if you did not request this confirmation, just ignore this message.'), + $target->getNickname(), common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', null, array('code' => $code))); - return $this->send_message($screenname, $body); + return $this->sendMessage($screenname, $body); } /** @@ -285,7 +274,7 @@ abstract class ImPlugin extends Plugin * @return boolean success flag */ - function public_notice($notice) + function publicNotice($notice) { // Now, users who want everything @@ -298,7 +287,7 @@ abstract class ImPlugin extends Plugin 'Sending notice ' . $notice->id . ' to public listener ' . $screenname, __FILE__); - $this->send_notice($screenname, $notice); + $this->sendNotice($screenname, $notice); } return true; @@ -318,18 +307,17 @@ abstract class ImPlugin extends Plugin * @return boolean success flag */ - function broadcast_notice($notice) + function broadcastNotice($notice) { - $ni = $notice->whoGets(); foreach ($ni as $user_id => $reason) { - $user = User::staticGet($user_id); + $user = User::getKV($user_id); if (empty($user)) { // either not a local user, or just not found continue; } - $user_im_prefs = $this->get_user_im_prefs_from_user($user); + $user_im_prefs = $this->getUserImPrefsFromUser($user); if(!$user_im_prefs || !$user_im_prefs->notify){ continue; } @@ -350,13 +338,15 @@ abstract class ImPlugin extends Plugin case NOTICE_INBOX_SOURCE_GROUP: break; default: - throw new Exception(sprintf(_("Unknown inbox source %d."), $reason)); + // TRANS: Exception thrown when trying to deliver a notice to an unknown inbox. + // TRANS: %d is the unknown inbox ID (number). + throw new Exception(sprintf(_('Unknown inbox source %d.'), $reason)); } common_log(LOG_INFO, 'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname, __FILE__); - $this->send_notice($user_im_prefs->screenname, $notice); + $this->sendNotice($user_im_prefs->screenname, $notice); $user_im_prefs->free(); } @@ -371,10 +361,22 @@ abstract class ImPlugin extends Plugin * @return string plain-text version of the notice, with user nickname prefixed */ - function format_notice($notice) + protected function formatNotice(Notice $notice) { $profile = $notice->getProfile(); - return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']'; + $nicknames = $profile->getNickname(); + + try { + $parent = $notice->getParent(); + $orig_profile = $parent->getProfile(); + $nicknames = sprintf('%1$s => %2$s', $profile->getNickname(), $orig_profile->getNickname()); + } catch (NoParentNoticeException $e) { + // Not a reply, no parent notice stored + } catch (NoResultException $e) { + // Parent notice was probably deleted + } + + return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id); } //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - RECEIVING ========================\ @@ -384,7 +386,7 @@ abstract class ImPlugin extends Plugin * @param string $body message text * @return boolean true if the message was a command and was executed, false if it was not a command */ - protected function handle_command($user, $body) + protected function handleCommand($user, $body) { $inter = new CommandInterpreter(); $cmd = $inter->handle_command($user, $body); @@ -392,9 +394,8 @@ abstract class ImPlugin extends Plugin $chan = new IMChannel($this); $cmd->execute($chan); return true; - } else { - return false; } + return false; } /** @@ -402,7 +403,7 @@ abstract class ImPlugin extends Plugin * @param string $txt message text * @return boolean true if autoreply */ - protected function is_autoreply($txt) + protected function isAutoreply($txt) { if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) { return true; @@ -418,7 +419,7 @@ abstract class ImPlugin extends Plugin * @param string $txt message text * @return boolean true if OTR */ - protected function is_otr($txt) + protected function isOtr($txt) { if (preg_match('/^\?OTR/', $txt)) { return true; @@ -436,34 +437,34 @@ abstract class ImPlugin extends Plugin * * @param boolean success */ - protected function handle_incoming($from, $notice_text) + protected function handleIncoming($from, $notice_text) { - $user = $this->get_user($from); + $user = $this->getUser($from); // For common_current_user to work global $_cur; $_cur = $user; if (!$user) { - $this->send_from_site($from, 'Unknown user; go to ' . + $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->handle_command($user, $notice_text)) { + if ($this->handleCommand($user, $notice_text)) { common_log(LOG_INFO, "Command message by $from handled."); return; - } else if ($this->is_autoreply($notice_text)) { + } else if ($this->isAutoreply($notice_text)) { common_log(LOG_INFO, 'Ignoring auto reply from ' . $from); return; - } else if ($this->is_otr($notice_text)) { + } 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->add_notice($from, $user, $notice_text); + $this->addNotice($from, $user, $notice_text); } $user->free(); @@ -481,14 +482,19 @@ abstract class ImPlugin extends Plugin * * @param boolean success */ - protected function add_notice($screenname, $user, $body) + protected function addNotice($screenname, $user, $body) { $body = trim(strip_tags($body)); $content_shortened = common_shorten_links($body); if (Notice::contentTooLong($content_shortened)) { - $this->send_from_site($screenname, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d.'), - Notice::maxContent(), - mb_strlen($content_shortened))); + $this->sendFromSite($screenname, + // TRANS: Message given when a status is too long. %1$s is the maximum number of characters, + // TRANS: %2$s is the number of characters sent (used for plural). + 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; } @@ -496,11 +502,10 @@ abstract class ImPlugin extends Plugin $notice = Notice::saveNew($user->id, $content_shortened, $this->transport); } catch (Exception $e) { common_log(LOG_ERR, $e->getMessage()); - $this->send_from_site($from, $e->getMessage()); + $this->sendFromSite($from, $e->getMessage()); return; } - common_broadcast_notice($notice); common_log(LOG_INFO, 'Added notice ' . $notice->id . ' from user ' . $user->nickname); $notice->free(); @@ -508,7 +513,7 @@ abstract class ImPlugin extends Plugin } //========================EVENT HANDLERS========================\ - + /** * Register notice queue handler * @@ -518,9 +523,14 @@ abstract class ImPlugin extends Plugin */ 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'); + // If we don't require CLI mode, or if we do and GNUSOCIAL_CLI _is_ set, then connect the transports + // This check is made mostly because some IM plugins can't deliver to transports unless they + // have continously running daemons (such as XMPP) and we can't have that over HTTP requests. + if (!$this->requires_cli || defined('GNUSOCIAL_CLI')) { + $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; } @@ -532,7 +542,7 @@ abstract class ImPlugin extends Plugin function onStartEnqueueNotice($notice, &$transports) { - $profile = Profile::staticGet($notice->profile_id); + $profile = Profile::getKV($notice->profile_id); if (!$profile) { common_log(LOG_WARNING, 'Refusing to broadcast notice with ' . @@ -545,35 +555,20 @@ abstract class ImPlugin extends Plugin return true; } - function onEndShowHeadElements($action) + function onEndShowHeadElements(Action $action) { - $aname = $action->trimmed('action'); - - if ($aname == 'shownotice') { + if ($action instanceof ShownoticeAction) { $user_im_prefs = new User_im_prefs(); - $user_im_prefs->user_id = $action->profile->id; + $user_im_prefs->user_id = $action->notice->getProfile()->getID(); $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') { + } elseif ($action instanceof ShowstreamAction) { $user_im_prefs = new User_im_prefs(); - $user_im_prefs->user_id = $action->user->id; + $user_im_prefs->user_id = $action->getTarget()->getID(); $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())); - } } } @@ -599,14 +594,14 @@ abstract class ImPlugin extends Plugin { $transports[$this->transport] = array( 'display' => $this->getDisplayName(), - 'daemon_screenname' => $this->daemon_screenname()); + 'daemonScreenname' => $this->daemonScreenname()); } - function onSendImConfirmationCode($transport, $screenname, $code, $user) + function onSendImConfirmationCode($transport, $screenname, $code, Profile $target) { if($transport == $this->transport) { - $this->send_confirmation_code($screenname, $code, $user); + $this->sendConfirmationCode($screenname, $code, $target); return false; } } @@ -617,10 +612,22 @@ abstract class ImPlugin extends Plugin return true; } + function onHaveImPlugin(&$haveImPlugin) { + $haveImPlugin = true; // set flag true (we're loaded, after all!) + return false; // stop looking + } + function initialize() { + if( ! common_config('queue', 'enabled')) + { + // TRANS: Server exception thrown trying to initialise an IM plugin without meeting all prerequisites. + throw new ServerException(_('Queueing must be enabled to use IM plugins.')); + } + if(is_null($this->transport)){ - throw new Exception('transport cannot be null'); + // TRANS: Server exception thrown trying to initialise an IM plugin without a transport method. + throw new ServerException(_('Transport cannot be null.')); } } }