summaryrefslogtreecommitdiff
path: root/plugins/Xmpp/xmppmanager.php
blob: 87d8186684d28a3f6119feba76c24b7f1e53ce05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2008, 2009, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }

/**
 * XMPP background connection manager for XMPP-using queue handlers,
 * allowing them to send outgoing messages on the right connection.
 *
 * Input is handled during socket select loop, keepalive pings during idle.
 * Any incoming messages will be handled.
 *
 * In a multi-site queuedaemon.php run, one connection will be instantiated
 * for each site being handled by the current process that has XMPP enabled.
 */

class XmppManager extends ImManager
{
    protected $lastping = null;
    protected $pingid = null;

    public $conn = null;
    
    const PING_INTERVAL = 120;
    

    /**
     * Initialize connection to server.
     * @return boolean true on success
     */
    public function start($master)
    {
        if(parent::start($master))
        {
            $this->connect();
            return true;
        }else{
            return false;
        }
    }

    function send_raw_message($data)
    {
        $this->connect();
        if (!$this->conn || $this->conn->isDisconnected()) {
            return false;
        }
        $this->conn->send($data);
        return true;
    }

    /**
     * Message pump is triggered on socket input, so we only need an idle()
     * call often enough to trigger our outgoing pings.
     */
    function timeout()
    {
        return self::PING_INTERVAL;
    }

    /**
     * Process XMPP events that have come in over the wire.
     * @fixme may kill process on XMPP error
     * @param resource $socket
     */
    public function handleInput($socket)
    {
        # Process the queue for as long as needed
        try {
            common_log(LOG_DEBUG, "Servicing the XMPP queue.");
            $this->stats('xmpp_process');
            $this->conn->processTime(0);
        } catch (XMPPHP_Exception $e) {
            common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
            die($e->getMessage());
        }
    }

    /**
     * Lists the IM connection socket to allow i/o master to wake
     * when input comes in here as well as from the queue source.
     *
     * @return array of resources
     */
    public function getSockets()
    {
        $this->connect();
        if($this->conn){
            return array($this->conn->getSocket());
        }else{
            return array();
        }
    }

    /**
     * Idle processing for io manager's execution loop.
     * Send keepalive pings to server.
     *
     * Side effect: kills process on exception from XMPP library.
     *
     * @fixme non-dying error handling
     */
    public function idle($timeout=0)
    {
        $now = time();
        if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
            try {
                $this->send_ping();
            } catch (XMPPHP_Exception $e) {
                common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
                die($e->getMessage());
            }
        }
    }

    function connect()
    {
        if (!$this->conn || $this->conn->isDisconnected()) {
            $resource = 'queue' . posix_getpid();
            $this->conn = new Sharing_XMPP($this->plugin->host ?
                                    $this->plugin->host :
                                    $this->plugin->server,
                                    $this->plugin->port,
                                    $this->plugin->user,
                                    $this->plugin->password,
                                    $this->plugin->resource,
                                    $this->plugin->server,
                                    $this->plugin->debug ?
                                    true : false,
                                    $this->plugin->debug ?
                                    XMPPHP_Log::LEVEL_VERBOSE :  null
                                    );

            if (!$this->conn) {
                return false;
            }
            $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
            $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
            $this->conn->setReconnectTimeout(600);

            $this->conn->autoSubscribe();
            $this->conn->useEncryption($this->plugin->encryption);

            try {
                $this->conn->connect(true); // true = persistent connection
            } catch (XMPPHP_Exception $e) {
                common_log(LOG_ERR, $e->getMessage());
                return false;
            }

            $this->conn->processUntil('session_start');
            $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
        }
        return $this->conn;
    }

    function send_ping()
    {
        $this->connect();
        if (!$this->conn || $this->conn->isDisconnected()) {
            return false;
        }
        $now = time();
        if (!isset($this->pingid)) {
            $this->pingid = 0;
        } else {
            $this->pingid++;
        }

        common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
		$this->conn->send("<iq from='{" . $this->plugin->daemon_screenname() . "}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
        $this->lastping = $now;
        return true;
    }

    function handle_xmpp_message(&$pl)
    {
        $this->plugin->enqueue_incoming_raw($pl);
        return true;
    }

    /**
     * Callback for Jabber reconnect event
     * @param $pl
     */
    function handle_xmpp_reconnect(&$pl)
    {
        common_log(LOG_NOTICE, 'XMPP reconnected');

        $this->conn->processUntil('session_start');
        $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
    }

    /**
     * sends a presence stanza on the XMPP network
     *
     * @param string $status   current status, free-form string
     * @param string $show     structured status value
     * @param string $to       recipient of presence, null for general
     * @param string $type     type of status message, related to $show
     * @param int    $priority priority of the presence
     *
     * @return boolean success value
     */

    function send_presence($status, $show='available', $to=null,
                                  $type = 'available', $priority=null)
    {
        $this->connect();
        if (!$this->conn || $this->conn->isDisconnected()) {
            return false;
        }
        $this->conn->presence($status, $show, $to, $type, $priority);
        return true;
    }

    /**
     * sends a "special" presence stanza on the XMPP network
     *
     * @param string $type   Type of presence
     * @param string $to     JID to send presence to
     * @param string $show   show value for presence
     * @param string $status status value for presence
     *
     * @return boolean success flag
     *
     * @see send_presence()
     */

    function special_presence($type, $to=null, $show=null, $status=null)
    {
        // FIXME: why use this instead of send_presence()?
        $this->connect();
        if (!$this->conn || $this->conn->isDisconnected()) {
            return false;
        }

        $to     = htmlspecialchars($to);
        $status = htmlspecialchars($status);

        $out = "<presence";
        if ($to) {
            $out .= " to='$to'";
        }
        if ($type) {
            $out .= " type='$type'";
        }
        if ($show == 'available' and !$status) {
            $out .= "/>";
        } else {
            $out .= ">";
            if ($show && ($show != 'available')) {
                $out .= "<show>$show</show>";
            }
            if ($status) {
                $out .= "<status>$status</status>";
            }
            $out .= "</presence>";
        }
        $this->conn->send($out);
        return true;
    }
}