3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to do "real time" updates using Meteor
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.
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.
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) {
34 require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
37 * Plugin to do realtime updates using Meteor
41 * @author Evan Prodromou <evan@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
45 class MeteorPlugin extends RealtimePlugin
47 const PLUGIN_VERSION = '2.0.0';
49 public $webserver = null;
50 public $webport = null;
51 public $controlport = null;
52 public $controlserver = null;
53 public $channelbase = null;
54 public $protocol = null;
55 public $persistent = true;
56 protected $_socket = null;
58 function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $protocol='http')
62 $this->webserver = (empty($webserver)) ? $config['site']['server'] : $webserver;
63 $this->webport = $webport;
64 $this->controlport = $controlport;
65 $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
66 $this->channelbase = $channelbase;
67 $this->protocol = $protocol;
69 parent::__construct();
73 * Pull settings from config file/database if set.
77 $settings = array('webserver',
83 foreach ($settings as $name) {
84 $val = common_config('meteor', $name);
90 return parent::initialize();
93 function _getScripts()
95 $scripts = parent::_getScripts();
96 if ($this->protocol == 'https') {
97 $scripts[] = 'https://'.$this->webserver.(($this->webport == 443) ? '':':'.$this->webport).'/meteor.js';
99 $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
101 $scripts[] = $this->path('js/meteorupdater.js');
105 function _updateInitialize($timeline, $user_id)
107 $script = parent::_updateInitialize($timeline, $user_id);
108 $ours = sprintf("MeteorUpdater.init(%s, %s, %s, %s);",
109 json_encode($this->webserver),
110 json_encode($this->webport),
111 json_encode($this->protocol),
112 json_encode($timeline));
113 return $script." ".$ours;
118 $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
120 $errno = $errstr = null;
122 $flags = STREAM_CLIENT_CONNECT;
123 if ($this->persistent) $flags |= STREAM_CLIENT_PERSISTENT;
125 // May throw an exception.
126 $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}", $errno, $errstr, $timeout, $flags);
127 if (!$this->_socket) {
128 // TRANS: Exception. %1$s is the control server, %2$s is the control port.
129 throw new Exception(sprintf(_m('Could not connect to %1$s on %2$s.'),$controlserver,$this->controlport));
133 function _publish($channel, $message)
135 $message = json_encode($message);
136 $message = addslashes($message);
137 $cmd = "ADDMESSAGE $channel $message\n";
138 $cnt = fwrite($this->_socket, $cmd);
139 $result = fgets($this->_socket);
140 if (preg_match('/^ERR (.*)$/', $result, $matches)) {
141 // TRANS: Exception. %s is the Meteor message that could not be added.
142 throw new Exception(sprintf(_m('Error adding meteor message "%s".'),$matches[1]));
144 // TODO: parse and deal with result
147 function _disconnect()
149 if (!$this->persistent) {
150 $cnt = fwrite($this->_socket, "QUIT\n");
151 @fclose($this->_socket);
155 // Meteord flips out with default '/' separator
157 function _pathToChannel($path)
159 if (!empty($this->channelbase)) {
160 array_unshift($path, $this->channelbase);
162 return implode('-', $path);
165 function onPluginVersion(array &$versions)
167 $versions[] = array('name' => 'Meteor',
168 'version' => self::PLUGIN_VERSION,
169 'author' => 'Evan Prodromou',
170 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Meteor',
172 // TRANS: Plugin description.
173 _m('Plugin to do "real time" updates using Meteor.'));