3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to do "real time" updates using Comet/Bayeux
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('STATUSNET') && !defined('LACONICA')) {
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 $persistent = true;
53 protected $_socket = null;
55 function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='')
59 $this->webserver = (empty($webserver)) ? $config['site']['server'] : $webserver;
60 $this->webport = $webport;
61 $this->controlport = $controlport;
62 $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
63 $this->channelbase = $channelbase;
65 parent::__construct();
69 * Pull settings from config file/database if set.
73 $settings = array('webserver',
78 foreach ($settings as $name) {
79 $val = common_config('meteor', $name);
85 return parent::initialize();
88 function _getScripts()
90 $scripts = parent::_getScripts();
91 $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
92 $scripts[] = $this->path('meteorupdater.min.js');
96 function _updateInitialize($timeline, $user_id)
98 $script = parent::_updateInitialize($timeline, $user_id);
99 return $script." MeteorUpdater.init(\"$this->webserver\", $this->webport, \"{$timeline}\");";
104 $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
106 $errno = $errstr = null;
108 $flags = STREAM_CLIENT_CONNECT;
109 if ($this->persistent) $flags |= STREAM_CLIENT_PERSISTENT;
111 // May throw an exception.
112 $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}", $errno, $errstr, $timeout, $flags);
113 if (!$this->_socket) {
114 // TRANS: Exception. %1$s is the control server, %2$s is the control port.
115 throw new Exception(sprintf(_m('Couldn\'t connect to %1$s on %2$s.'),$controlserver,$this->controlport));
119 function _publish($channel, $message)
121 $message = json_encode($message);
122 $message = addslashes($message);
123 $cmd = "ADDMESSAGE $channel $message\n";
124 $cnt = fwrite($this->_socket, $cmd);
125 $result = fgets($this->_socket);
126 if (preg_match('/^ERR (.*)$/', $result, $matches)) {
127 // TRANS: Exception. %s is the Meteor message that could not be added.
128 throw new Exception(sprintf(_m('Error adding meteor message "%s"'),$matches[1]));
130 // TODO: parse and deal with result
133 function _disconnect()
135 if (!$this->persistent) {
136 $cnt = fwrite($this->_socket, "QUIT\n");
137 @fclose($this->_socket);
141 // Meteord flips out with default '/' separator
143 function _pathToChannel($path)
145 if (!empty($this->channelbase)) {
146 array_unshift($path, $this->channelbase);
148 return implode('-', $path);
151 function onPluginVersion(&$versions)
153 $versions[] = array('name' => 'Meteor',
154 'version' => STATUSNET_VERSION,
155 'author' => 'Evan Prodromou',
156 'homepage' => 'http://status.net/wiki/Plugin:Meteor',
158 _m('Plugin to do "real time" updates using Comet/Bayeux.'));