-
Notifications
You must be signed in to change notification settings - Fork 4
/
XmppClient.php
431 lines (374 loc) · 12.8 KB
/
XmppClient.php
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2016, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Xmpp;
use DI\Container;
use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Kadet\Xmpp\Component\Roster;
use Kadet\Xmpp\Exception\InvalidArgumentException;
use Kadet\Xmpp\Exception\WriteOnlyException;
use Kadet\Xmpp\Component\Authenticator;
use Kadet\Xmpp\Component\Binding;
use Kadet\Xmpp\Component\Component;
use Kadet\Xmpp\Component\ComponentInterface;
use Kadet\Xmpp\Component\SaslAuthenticator;
use Kadet\Xmpp\Component\TlsEnabler;
use Kadet\Xmpp\Network\Connector;
use Kadet\Xmpp\Stanza\Stanza;
use Kadet\Xmpp\Stream\Features;
use Kadet\Xmpp\Utils\Accessors;
use Kadet\Xmpp\Utils\filter as with;
use Kadet\Xmpp\Utils\ServiceManager;
use Kadet\Xmpp\Xml\XmlElementFactory;
use Kadet\Xmpp\Xml\XmlParser;
use Kadet\Xmpp\Xml\XmlStream;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
/**
* Class XmppClient
* @package Kadet\Xmpp
*
* @property Features $features Features provided by that stream
* @property XmlParser $parser XmlParser instance used to process data from stream, can be exchanged only
* when client is not connected to server.
* @property string $resource Client's jid resource
* @property Jid $jid Client's jid (Jabber Identifier) address.
* @property ContainerInterface $container Dependency container used for module management.
* @property string $language Stream language (reflects xml:language attribute)
* @property string $state Current client state
* `disconnected` - not connected to any server,
* `connected` - connected to server, but nothing happened yet,
* `secured` - [optional] TLS negotiation succeeded, after stream restart
* `authenticated` - authentication succeeded,
* `bound` - resource binding succeeded,
* `ready` - client is ready to operate
*
* However modules can add custom states.
*
* @property-read Roster $roster Clients roster.
*
* @property Connector $connector Connector used for obtaining stream
* @property-write string $password Password used for client authentication
*
* @event stanza(Stanza $stanza) Emitted on every incoming stanza regardless of it's kind.
* Equivalent of element event with only instances of Stanza class allowed.
* @event iq(Iq $iq) Emitted on every incoming iq stanza.
* @event message(Message $message) Emitted on every incoming message stanza.
* @event presence(Presence $presence) Emitted on every incoming presence stanza.
*
* @event init(ArrayObject $queue) Emitted when connection is accomplished, after binding process.
*
* @event state(string $state) Emitted on state change.
* @event bind(Jid $jid) Emitted after successful bind.
*/
class XmppClient extends XmlStream implements ContainerInterface
{
use ServiceManager, Accessors;
/**
* Connector used to instantiate stream connection to server.
*
* @var Connector
*/
private $_connector;
/**
* Client's jid (Jabber Identifier) address.
*
* @var Jid
*/
private $_jid;
/**
* Dependency container used as service manager.
*
* @var Container
*/
private $_container;
/**
* Features provided by that stream
*
* @var Features
*/
private $_features;
/**
* Current client state.
*
* @var string
*/
private $_state = 'disconnected';
private $_lang;
/**
* XmppClient constructor.
* @param Jid $jid
* @param array $options {
* @var XmlParser $parser Parser used for interpreting streams.
* @var Component[] $modules Additional modules registered when creating client.
* @var string $language Stream language (reflects xml:language attribute)
* @var ContainerInterface $container Dependency container used for module management.
* @var bool $default-modules Load default modules or not
* }
*/
public function __construct(Jid $jid, array $options = [])
{
$container = new ContainerBuilder();
$container->useAutowiring(false);
$options = array_replace([
'parser' => new XmlParser(new XmlElementFactory()),
'language' => 'en',
'container' => $container->build(),
'connector' => $options['connector'] ?? new Connector\TcpXmppConnector($jid->domain, $options['loop']),
'jid' => $jid,
'modules' => [],
'default-modules' => true,
], $options);
parent::__construct($options['parser'], null);
unset($options['parser']);
$this->applyOptions($options);
$this->on('element', function (Features $element) {
$this->_features = $element;
$this->emit('features', [$element]);
}, Features::class);
$this->on('element', function (Stanza $stanza) {
$this->emit('stanza', [ $stanza ]);
$this->emit($stanza->localName, [ $stanza ]);
}, Stanza::class);
$this->on('close', function () {
$this->state = 'disconnected';
});
}
public function applyOptions(array $options)
{
$options = \Kadet\Xmpp\Utils\helper\rearrange($options, [
'container' => 6,
'jid' => 5,
'connector' => 4,
'modules' => 3,
'password' => -1
]);
if ($options['default-modules']) {
$options['modules'] = array_merge([
TlsEnabler::class => new TlsEnabler(),
Binding::class => new Binding(),
Authenticator::class => new SaslAuthenticator(),
Roster::class => new Roster()
], $options['modules']);
}
foreach ($options as $name => $value) {
$this->$name = $value;
}
}
public function start(array $attributes = [])
{
parent::start(array_merge([
'xmlns' => 'jabber:client',
'version' => '1.0',
'language' => $this->_lang
], $attributes));
}
public function connect()
{
$this->getLogger()->debug("Connecting to {$this->_jid->domain}");
$this->_connector->connect();
}
public function bind($jid)
{
$this->jid = new Jid($jid);
$this->emit('bind', [$jid]);
$this->state = 'bound';
$queue = new \SplQueue();
$this->emit('init', [ $queue ]);
\React\Promise\all(iterator_to_array($queue))->then(function() {
$this->state = 'ready';
});
}
/**
* Registers module in client's dependency container.
*
* @param ComponentInterface $module Module to be registered
* @param bool|string|array $alias Module alias, class name by default.
* `true` for aliasing interfaces and parents too,
* `false` for aliasing as class name only
* array for multiple aliases,
* and any string for alias name
*/
public function register(ComponentInterface $module, $alias = true)
{
$module->setClient($this);
$this->_container->set(get_class($module), $module);
if ($alias === true) {
$this->_addToContainer($module, array_merge(class_implements($module), array_slice(class_parents($module), 1)));
} elseif(is_array($alias)) {
$this->_addToContainer($module, $alias);
} else {
$this->_addToContainer($module, [ $alias === false ? get_class($module) : $alias ]);
}
}
private function _addToContainer(ComponentInterface $module, array $aliases) {
foreach ($aliases as $name) {
if (!$this->has($name)) {
$this->_container->set($name, $module);
}
}
}
/**
* Sends stanza to server and returns promise with server response.
*
* @param Stanza $stanza
* @return ExtendedPromiseInterface
*/
public function send(Stanza $stanza) : ExtendedPromiseInterface
{
$deferred = new Deferred();
$this->once('element', function(Stanza $stanza) use ($deferred) {
if($stanza->type === "error") {
$deferred->reject($stanza);
} else {
$deferred->resolve($stanza);
}
}, with\stanza\id($stanza->id));
$this->write($stanza);
return $deferred->promise();
}
private function handleConnect($stream)
{
$this->exchangeStream($stream);
$this->getLogger()->info("Connected to {$this->_jid->domain}");
$this->start([
'from' => (string)$this->_jid,
'to' => $this->_jid->domain
]);
$this->state = 'connected';
return $this->emit('connect');
}
//region Features
public function getFeatures()
{
return $this->_features;
}
//endregion
//region Parser
public function setParser(XmlParser $parser)
{
if($this->state !== "disconnected") {
throw new \BadMethodCallException('Parser can be changed only when client is disconnected.');
}
parent::setParser($parser);
$this->_parser->factory->load(require __DIR__ . '/XmlElementLookup.php');
}
public function getParser()
{
return $this->_parser;
}
//endregion
//region Connector
protected function setConnector($connector)
{
if ($connector instanceof LoopInterface) {
$this->_connector = new Connector\TcpXmppConnector($this->_jid->domain, $connector);
} elseif ($connector instanceof Connector) {
$this->_connector = $connector;
} else {
throw new InvalidArgumentException(sprintf(
'$connector must be either %s or %s instance, %s given.',
LoopInterface::class, Connector::class, \Kadet\Xmpp\Utils\helper\typeof($connector)
));
}
$this->_connector->on('connect', function ($stream) {
return $this->handleConnect($stream);
});
}
public function getConnector()
{
return $this->_connector;
}
//endregion
//region Resource
public function setResource(string $resource)
{
$this->_jid = new Jid($this->_jid->domain, $this->_jid->local, $resource);
}
public function getResource()
{
return $this->_jid->resource;
}
//endregion
//region Password
public function setPassword(string $password)
{
$this->get(Authenticator::class)->setPassword($password);
}
public function getPassword()
{
throw new WriteOnlyException("Password can't be obtained.");
}
//endregion
//region Modules
public function setModules(array $modules)
{
foreach ($modules as $name => $module) {
$this->register($module, is_string($name) ? $name : true);
}
}
//endregion
//region State
public function setState($state)
{
$this->_state = $state;
$this->emit('state', [$state]);
}
public function getState()
{
return $this->_state;
}
//endregion
//region Container
protected function getContainer() : ContainerInterface
{
return $this->_container;
}
protected function setContainer(Container $container)
{
$this->_container = $container;
}
//endregion
//region Language
public function getLanguage(): string
{
return $this->_lang;
}
public function setLanguage(string $language)
{
$this->_lang = $language;
}
//endregion
//region JID
public function getJid()
{
return $this->_jid;
}
protected function setJid(Jid $jid)
{
$this->_jid = $jid;
}
//endregion
//region Roster
/**
* @return Roster
*/
public function getRoster(): Roster
{
return $this->get(Roster::class);
}
//endregion
}