]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Meteor/MeteorPlugin.php
Allow Meteor plugin to be configurable via configuration database or file.
[quix0rs-gnu-social.git] / plugins / Meteor / MeteorPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to do "real time" updates using Comet/Bayeux
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
35
36 /**
37  * Plugin to do realtime updates using Meteor
38  *
39  * @category Plugin
40  * @package  StatusNet
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/
44  */
45
46 class MeteorPlugin extends RealtimePlugin
47 {
48     public $webserver     = null;
49     public $webport       = null;
50     public $controlport   = null;
51     public $controlserver = null;
52     public $channelbase   = null;
53     protected $_socket    = null;
54
55     function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='')
56     {
57         global $config;
58
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;
64
65         parent::__construct();
66     }
67
68     /**
69      * Pull settings from config file/database if set.
70      */
71     function initialize()
72     {
73         $settings = array('webserver',
74                           'webport',
75                           'controlport',
76                           'controlserver',
77                           'channelbase');
78         foreach ($settings as $name) {
79             $val = common_config('meteor', $name);
80             if ($val !== false) {
81                 $this->$name = $val;
82             }
83         }
84
85         return parent::initialize();
86     }
87
88     function _getScripts()
89     {
90         $scripts = parent::_getScripts();
91         $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
92         $scripts[] = common_path('plugins/Meteor/meteorupdater.js');
93         return $scripts;
94     }
95
96     function _updateInitialize($timeline, $user_id)
97     {
98         $script = parent::_updateInitialize($timeline, $user_id);
99         return $script." MeteorUpdater.init(\"$this->webserver\", $this->webport, \"{$timeline}\");";
100     }
101
102     function _connect()
103     {
104         $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
105         // May throw an exception.
106         $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}");
107         if (!$this->_socket) {
108             throw new Exception("Couldn't connect to {$controlserver} on {$this->controlport}");
109         }
110     }
111
112     function _publish($channel, $message)
113     {
114         $message = json_encode($message);
115         $message = addslashes($message);
116         $cmd = "ADDMESSAGE $channel $message\n";
117         $cnt = fwrite($this->_socket, $cmd);
118         $result = fgets($this->_socket);
119         if (preg_match('/^ERR (.*)$/', $result, $matches)) {
120             throw new Exception('Error adding meteor message "'.$matches[1].'"');
121         }
122         // TODO: parse and deal with result
123     }
124
125     function _disconnect()
126     {
127         $cnt = fwrite($this->_socket, "QUIT\n");
128         @fclose($this->_socket);
129     }
130
131     // Meteord flips out with default '/' separator
132
133     function _pathToChannel($path)
134     {
135         if (!empty($this->channelbase)) {
136             array_unshift($path, $this->channelbase);
137         }
138         return implode('-', $path);
139     }
140 }