123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- defined('GNUSOCIAL') || die();
- class XmppPlugin extends ImPlugin
- {
- const PLUGIN_VERSION = '2.0.0';
- public $server = null;
- public $port = 5222;
- public $user = 'update';
- public $resource = 'gnusocial';
- public $encryption = true;
- public $password = null;
- public $host = null;
- public $debug = false;
- public $transport = 'xmpp';
- public function getDisplayName()
- {
-
- return _m('XMPP/Jabber');
- }
- public function daemonScreenname()
- {
- $ret = $this->user . '@' . $this->server;
- if ($this->resource) {
- return $ret . '/' . $this->resource;
- } else {
- return $ret;
- }
- }
- public function validate($screenname)
- {
- return $this->validateBaseJid($screenname, common_config('email', 'check_domain'));
- }
-
- protected function validateBaseJid($jid, $check_domain = false)
- {
- try {
- $parts = $this->splitJid($jid);
- if ($check_domain) {
- if (!$this->checkDomain($parts['domain'])) {
- return false;
- }
- }
-
- return is_null($parts['resource']);
- } catch (UnexpectedValueException $e) {
- return false;
- }
- }
-
- protected function splitJid($jid)
- {
- $chars = [];
-
-
- $chars['1.1'] = "\x{20}";
-
- $chars['1.2'] = "\x{a0}\x{1680}\x{2000}-\x{200b}\x{202f}\x{205f}\x{3000a}";
-
- $chars['2.1'] = "\x{00}-\x{1f}\x{7f}";
-
- $chars['2.2'] = "\x{80}-\x{9f}\x{6dd}\x{70f}\x{180e}\x{200c}\x{200d}"
- . "\x{2028}\x{2029}\x{2060}-\x{2063}\x{206a}-\x{206f}\x{feff}"
- . "\x{fff9}-\x{fffc}\x{1d173}-\x{1d17a}";
-
- $chars['3'] = "\x{e000}-\x{f8ff}\x{f0000}-\x{ffffd}\x{100000}-\x{10fffd}";
-
- $chars['4'] = "\x{fdd0}-\x{fdef}\x{fffe}\x{ffff}\x{1fffe}\x{1ffff}"
- . "\x{2fffe}\x{2ffff}\x{3fffe}\x{3ffff}\x{4fffe}\x{4ffff}\x{5fffe}"
- . "\x{5ffff}\x{6fffe}\x{6ffff}\x{7fffe}\x{7ffff}\x{8fffe}\x{8ffff}"
- . "\x{9fffe}\x{9ffff}\x{afffe}\x{affff}\x{bfffe}\x{bffff}\x{cfffe}"
- . "\x{cffff}\x{dfffe}\x{dffff}\x{efffe}\x{effff}\x{ffffe}\x{fffff}"
- . "\x{10fffe}\x{10ffff}";
-
-
-
-
- $chars['6'] = "\x{fff9}-\x{fffd}";
-
- $chars['7'] = "\x{2ff0}-\x{2ffb}";
-
- $chars['8'] = "\x{340}\x{341}\x{200e}\x{200f}\x{202a}-\x{202e}\x{206a}-\x{206f}";
-
- $chars['9'] = "\x{e0001}\x{e0020}-\x{e007f}";
- $nodeprep_chars = implode('', $chars);
-
- $nodeprep_chars .= "\x{22}\x{26}\x{27}\x{2f}\x{3a}\x{3c}\x{3e}\x{40}";
-
- $resprep_chars = implode('', array_slice($chars, 1));
- $parts = explode('/', $jid, 2);
- if (count($parts) > 1) {
- $resource = $parts[1];
-
-
-
- } else {
- $resource = null;
- }
- $node = explode("@", $parts[0]);
- if ((count($node) > 2) || (count($node) == 0)) {
-
- throw new UnexpectedValueException(_m('Invalid JID: too many @s.'));
- } elseif (count($node) == 1) {
- $domain = $node[0];
- $node = null;
- } else {
- $domain = $node[1];
- $node = $node[0];
- if ($node == '') {
-
- throw new UnexpectedValueException(_m('Invalid JID: @ but no node'));
- }
- }
- if (!is_null($node)) {
-
- if (mb_strlen($node, '8bit') > 1023) {
-
- throw new UnexpectedValueException(_m('Invalid JID: node too long.'));
- }
-
- if (preg_match("/[{$nodeprep_chars}]/u", $node)
- || mb_detect_encoding($node, 'UTF-8', true) !== 'UTF-8') {
-
-
- throw new UnexpectedValueException(sprintf(_m('Invalid JID node "%s".'), $node));
- }
- }
- if (mb_strlen($domain, '8bit') > 1023) {
-
- throw new UnexpectedValueException(_m('Invalid JID: domain too long.'));
- }
- if (!common_valid_domain($domain)) {
-
-
- throw new UnexpectedValueException(sprintf(_m('Invalid JID domain name "%s".'), $domain));
- }
- if (!is_null($resource)) {
- if (mb_strlen($resource, '8bit') > 1023) {
-
- throw new UnexpectedValueException('Invalid JID: resource too long.');
- }
- if (preg_match("/[{$resprep_chars}]/u", $resource)) {
-
-
- throw new UnexpectedValueException(sprintf(_m('Invalid JID resource "%s".'), $resource));
- }
- }
- return array('node' => is_null($node) ? null : mb_strtolower($node),
- 'domain' => is_null($domain) ? null : mb_strtolower($domain),
- 'resource' => $resource);
- }
-
- protected function checkDomain($domain)
- {
- if (checkdnsrr("_xmpp-server._tcp." . $domain, "SRV")) {
- return true;
- }
- if (checkdnsrr($domain, "ANY")) {
- return true;
- }
- return false;
- }
- public function onStartImDaemonIoManagers(&$classes)
- {
- parent::onStartImDaemonIoManagers($classes);
- $classes[] = new XmppManager($this);
- return true;
- }
- public function sendMessage($screenname, $body)
- {
- $this->queuedConnection()->message($screenname, $body, 'chat');
- }
-
- public function queuedConnection()
- {
- if (!isset($this->server)) {
-
- throw new Exception(_m('You must specify a server in the configuration.'));
- }
- if (!isset($this->port)) {
-
- throw new Exception(_m('You must specify a port in the configuration.'));
- }
- if (!isset($this->user)) {
-
- throw new Exception(_m('You must specify a user in the configuration.'));
- }
- if (!isset($this->password)) {
-
- throw new Exception(_m('You must specify a password in the configuration.'));
- }
- return new QueuedXMPP(
- $this,
- ($this->host ?: $this->server),
- $this->port,
- $this->user,
- $this->password,
- $this->resource,
- $this->server,
- ($this->debug ? true : false),
- ($this->debug ? \XMPPHP\Log::LEVEL_VERBOSE : null)
- );
- }
- public function sendNotice($screenname, Notice $notice)
- {
- try {
- $msg = $this->formatNotice($notice);
- $entry = $this->format_entry($notice);
- } catch (Exception $e) {
- common_log(LOG_ERR, __METHOD__ . ": Discarding outgoing stanza because of exception: {$e->getMessage()}");
- return false;
- }
- $this->queuedConnection()->message($screenname, $msg, 'chat', null, $entry);
- return true;
- }
-
- protected function format_entry(Notice $notice)
- {
- $profile = $notice->getProfile();
- $entry = $notice->asAtomEntry(true, true);
- $xs = new XMLStringer();
- $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
- $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
- $xs->element('a', array('href' => $profile->profileurl), $profile->nickname);
- try {
- $parent = $notice->getParent();
- $orig_profile = $parent->getProfile();
- $orig_profurl = $orig_profile->getUrl();
- $xs->text(" => ");
- $xs->element('a', array('href' => $orig_profurl), $orig_profile->nickname);
- $xs->text(": ");
- } catch (NoParentNoticeException $e) {
- $xs->text(": ");
- }
-
- $xs->raw(str_replace("\t", "", $notice->getRendered()));
- $xs->text(" ");
- $xs->element(
- 'a',
- [
- 'href' => common_local_url(
- 'conversation',
- ['id' => $notice->conversation]
- ) . '#notice-' . $notice->id,
- ],
-
-
- sprintf(_m('[%u]'), $notice->id)
- );
- $xs->elementEnd('body');
- $xs->elementEnd('html');
- $html = $xs->getString();
- return $html . ' ' . $entry;
- }
- public function receiveRawMessage($pl)
- {
- $from = $this->normalize($pl['from']);
- if (is_null($from)) {
- $this->log(LOG_WARNING, 'Ignoring message from invalid JID: ' . $pl['xml']->toString());
- return true;
- }
- if ($pl['type'] !== 'chat') {
- $this->log(LOG_WARNING, "Ignoring message of type " . $pl['type'] . " from $from: " . $pl['xml']->toString());
- return true;
- }
- if (mb_strlen($pl['body']) == 0) {
- $this->log(LOG_WARNING, "Ignoring message with empty body from $from: " . $pl['xml']->toString());
- return true;
- }
- $this->handleIncoming($from, $pl['body']);
- return true;
- }
-
- public function normalize($jid)
- {
- try {
- $parts = $this->splitJid($jid);
- if (!is_null($parts['node'])) {
- return $parts['node'] . '@' . $parts['domain'];
- } else {
- return $parts['domain'];
- }
- } catch (UnexpectedValueException $e) {
- return null;
- }
- }
-
- public function onGetValidDaemons(&$daemons)
- {
- if (isset($this->server) &&
- isset($this->port) &&
- isset($this->user) &&
- isset($this->password)) {
- array_push(
- $daemons,
- INSTALLDIR . '/scripts/imdaemon.php'
- );
- }
- return true;
- }
-
- public function onNodeInfoProtocols(array &$protocols)
- {
- $protocols[] = "xmpp";
- return true;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'XMPP',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Craig Andrews, Evan Prodromou',
- 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/XMPP',
- 'rawdescription' =>
-
- _m('The XMPP plugin allows users to send and receive notices over the XMPP/Jabber network.'));
- return true;
- }
-
- protected function validateFullJid($jid, $check_domain = false)
- {
- try {
- $parts = $this->splitJid($jid);
- if ($check_domain) {
- if (!$this->checkDomain($parts['domain'])) {
- return false;
- }
- }
- return $parts['resource'] !== '';
- } catch (UnexpectedValueException $e) {
- return false;
- }
- }
- }
|