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 public $webserver = null;
48 public $webport = null;
49 public $controlport = null;
50 public $controlserver = null;
51 public $channelbase = null;
52 public $protocol = null;
53 public $persistent = true;
54 protected $_socket = null;
56 function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $protocol='http')
60 $this->webserver = (empty($webserver)) ? $config['site']['server'] : $webserver;
61 $this->webport = $webport;
62 $this->controlport = $controlport;
63 $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
64 $this->channelbase = $channelbase;
65 $this->protocol = $protocol;
67 parent::__construct();
71 * Pull settings from config file/database if set.
75 $settings = array('webserver',
81 foreach ($settings as $name) {
82 $val = common_config('meteor', $name);
88 return parent::initialize();
91 function _getScripts()
93 $scripts = parent::_getScripts();
94 if ($this->protocol == 'https') {
95 $scripts[] = 'https://'.$this->webserver.(($this->webport == 443) ? '':':'.$this->webport).'/meteor.js';
97 $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
99 $scripts[] = $this->path('js/meteorupdater.js');
103 function _updateInitialize($timeline, $user_id)
105 $script = parent::_updateInitialize($timeline, $user_id);
106 $ours = sprintf("MeteorUpdater.init(%s, %s, %s, %s);",
107 json_encode($this->webserver),
108 json_encode($this->webport),
109 json_encode($this->protocol),
110 json_encode($timeline));
111 return $script." ".$ours;
116 $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
118 $errno = $errstr = null;
120 $flags = STREAM_CLIENT_CONNECT;
121 if ($this->persistent) $flags |= STREAM_CLIENT_PERSISTENT;
123 // May throw an exception.
124 $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}", $errno, $errstr, $timeout, $flags);
125 if (!$this->_socket) {
126 // TRANS: Exception. %1$s is the control server, %2$s is the control port.
127 throw new Exception(sprintf(_m('Could not connect to %1$s on %2$s.'),$controlserver,$this->controlport));
131 function _publish($channel, $message)
133 $message = json_encode($message);
134 $message = addslashes($message);
135 $cmd = "ADDMESSAGE $channel $message\n";
136 $cnt = fwrite($this->_socket, $cmd);
137 $result = fgets($this->_socket);
138 if (preg_match('/^ERR (.*)$/', $result, $matches)) {
139 // TRANS: Exception. %s is the Meteor message that could not be added.
140 throw new Exception(sprintf(_m('Error adding meteor message "%s".'),$matches[1]));
142 // TODO: parse and deal with result
145 function _disconnect()
147 if (!$this->persistent) {
148 $cnt = fwrite($this->_socket, "QUIT\n");
149 @fclose($this->_socket);
153 // Meteord flips out with default '/' separator
155 function _pathToChannel($path)
157 if (!empty($this->channelbase)) {
158 array_unshift($path, $this->channelbase);
160 return implode('-', $path);
163 function onPluginVersion(array &$versions)
165 $versions[] = array('name' => 'Meteor',
166 'version' => GNUSOCIAL_VERSION,
167 'author' => 'Evan Prodromou',
168 'homepage' => 'http://status.net/wiki/Plugin:Meteor',
170 // TRANS: Plugin description.
171 _m('Plugin to do "real time" updates using Meteor.'));