3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
35 * Plugin to do realtime updates using Comet
39 * @author Evan Prodromou <evan@controlyourself.ca>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://laconi.ca/
44 class MeteorPlugin extends Plugin
46 var $webserver = null;
48 var $controlport = null;
49 var $controlserver = null;
50 var $channelbase = null;
53 function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='')
57 $this->webserver = (empty($webserver)) ? $config['site']['server'] : $webserver;
58 $this->webport = $webport;
59 $this->controlport = $controlport;
60 $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
61 $this->channelbase = $channelbase;
63 parent::__construct();
66 function onEndShowScripts($action)
70 $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
72 switch ($action->trimmed('action')) {
74 $timeline = '/timelines/public';
77 $tag = $action->trimmed('tag');
79 $timeline = '/timelines/tag/'.$tag;
88 $action->element('script', array('type' => 'text/javascript',
89 'src' => 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js'),
92 foreach (array('meteorupdater.js', 'json2.js') as $script) {
93 $action->element('script', array('type' => 'text/javascript',
94 'src' => common_path('plugins/Meteor/'.$script)),
98 $user = common_current_user();
100 if (!empty($user->id)) {
101 $user_id = $user->id;
106 $replyurl = common_local_url('newnotice');
107 $favorurl = common_local_url('favor');
108 // FIXME: need to find a better way to pass this pattern in
109 $deleteurl = common_local_url('deletenotice',
110 array('notice' => '0000000000'));
112 $action->elementStart('script', array('type' => 'text/javascript'));
113 $action->raw("$(document).ready(function() { MeteorUpdater.init(\"$this->webserver\", $this->webport, \"{$this->channelbase}$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
114 $action->elementEnd('script');
119 function onEndNoticeSave($notice)
121 $this->log(LOG_INFO, "Called for save notice.");
123 $timelines = array();
125 // XXX: Add other timelines; this is just for the public one
127 if ($notice->is_local ||
128 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
129 $timelines[] = '/timelines/public';
132 $tags = $this->getNoticeTags($notice);
135 foreach ($tags as $tag) {
136 $timelines[] = '/timelines/tag/' . $tag;
140 if (count($timelines) > 0) {
142 $json = json_encode($this->noticeAsJson($notice));
144 $this->log(LOG_DEBUG, $json);
148 foreach ($timelines as $timeline) {
149 $this->_addMessage($timeline, $json);
152 $this->_disconnect();
160 // May throw an exception.
161 $this->_socket = @stream_socket_client("tcp://{$this->controlserver}:{$this->controlport}",
162 $errno, $errstr, $timeout, $conflag);
163 if (!$this->_socket) {
164 throw new Exception("Couldn't connect to {$this->controlserver} on {$this->controlport}");
168 function _addMessage($channel, $message)
170 $cmd = "ADDMESSAGE {$this->channelbase}$channel $message\n";
171 $this->log(LOG_DEBUG, $cmd);
172 @fwrite($this->_socket, "COMMAND: $cmd");
173 $result = fgets($this->_socket);
174 $this->log(LOG_DEBUG, "RESULT: $result");
175 // TODO: parse and deal with result
178 function _disconnect()
180 @fclose($this->_socket);
183 function noticeAsJson($notice)
185 // FIXME: this code should be abstracted to a neutral third
186 // party, like Notice::asJson(). I'm not sure of the ethics
187 // of refactoring from within a plugin, so I'm just abusing
188 // the TwitterApiAction method. Don't do this unless you're me!
190 require_once(INSTALLDIR.'/lib/twitterapi.php');
192 $act = new TwitterApiAction('/dev/null');
194 $arr = $act->twitter_status_array($notice, true);
195 $arr['url'] = $notice->bestUrl();
196 $arr['html'] = htmlspecialchars($notice->rendered);
197 $arr['source'] = htmlspecialchars($arr['source']);
199 if (!empty($notice->reply_to)) {
200 $reply_to = Notice::staticGet('id', $notice->reply_to);
201 if (!empty($reply_to)) {
202 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
207 $profile = $notice->getProfile();
208 $arr['user']['profile_url'] = $profile->profileurl;
213 function getNoticeTags($notice)
217 $nt = new Notice_tag();
218 $nt->notice_id = $notice->id;
222 while ($nt->fetch()) {
233 // Push this up to Plugin
235 function log($level, $msg)
237 common_log($level, get_class($this) . ': '.$msg);