forked from clue/reactphp-quassel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.php
More file actions
213 lines (182 loc) · 8.07 KB
/
Copy pathFactory.php
File metadata and controls
213 lines (182 loc) · 8.07 KB
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
<?php
namespace Clue\React\Quassel;
use Clue\React\Quassel\Io\Prober;
use Clue\React\Quassel\Io\Protocol;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise;
use React\Socket\ConnectorInterface;
use React\Socket\Connector;
use React\Stream\DuplexStreamInterface;
use InvalidArgumentException;
class Factory
{
/** @var LoopInterface */
private $loop;
/** @var ConnectorInterface */
private $connector;
/** @var Prober */
private $prober;
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, Prober $prober = null)
{
$this->loop = $loop ?: Loop::get();
if ($connector === null) {
$connector = new Connector($loop);
}
if ($prober === null) {
$prober = new Prober();
}
$this->connector = $connector;
$this->prober = $prober;
}
public function createClient($uri)
{
if (strpos($uri, '://') === false) {
$uri= 'quassel://' . $uri;
}
$parts = parse_url($uri);
if (!$parts || !isset($parts['scheme'], $parts['host']) || $parts['scheme'] !== 'quassel') {
return Promise\reject(new InvalidArgumentException('Given argument "' . $uri. '" is not a valid Quassel URI'));
}
if (!isset($parts['port'])) {
$parts['port'] = 4242;
}
$args = array();
if (isset($parts['query'])) {
parse_str($parts['query'], $args);
}
// establish low-level TCP/IP connection to Quassel IRC core
$promise = $this->connector->connect($parts['host'] . ':' . $parts['port']);
// probe protocol once connected
$probe = 0;
$connector = $this->connector;
$prober = $this->prober;
$promise = $promise->then(function (DuplexStreamInterface $stream) use ($prober, &$probe, $connector, $parts) {
return $prober->probe($stream)->then(
function ($ret) use (&$probe, $stream) {
// probe returned successfully, create new client for this stream
$probe = $ret;
return $stream;
},
function ($e) use ($connector, $parts) {
// probing failed
if ($e->getCode() === Prober::ERROR_CLOSED) {
// legacy servers will terminate connection while probing
// let's just open a new connection and assume default probe
return $connector->connect($parts['host'] . ':' . $parts['port']);
}
throw $e;
}
);
});
// decorate client once probing is finished
$promise = $promise->then(
function (DuplexStreamInterface $stream) use (&$probe) {
return new Client($stream, Protocol::createFromProbe($probe));
}
);
// automatic login if username/password is given as part of URI
if (isset($parts['user']) || isset($parts['pass'])) {
$that = $this;
$promise = $promise->then(function (Client $client) use ($that, $parts) {
return $that->awaitLogin(
$client,
isset($parts['user']) ? urldecode($parts['user']) : '',
isset($parts['pass']) ? urldecode($parts['pass']) : ''
);
});
}
// automatically send ping requests and await pong replies unless "?ping=0" is given
// automatically reply to incoming ping requests with a pong unless "?pong=0" is given
$ping = (!isset($args['ping'])) ? 60 : (float)$args['ping'];
$pong = (!isset($args['pong']) || $args['pong']) ? true : false;
if ($ping !== 0.0 || $pong) {
$loop = $this->loop;
$await = false;
$promise = $promise->then(function (Client $client) use ($loop, $ping, $pong) {
$timer = null;
if ($ping !== 0.0) {
// send heartbeat message every X seconds to check dropped connection
$timer = $loop->addPeriodicTimer($ping, function () use ($client, &$await) {
if ($await) {
$client->emit('error', array(
new \RuntimeException('Connection to Quassel core timed out')
));
$client->close();
} else {
$client->writeHeartBeatRequest();
$await = true;
}
});
// stop heartbeat timer once connection closes
$client->on('close', function () use ($loop, &$timer) {
$loop->cancelTimer($timer);
$timer = null;
});
}
$client->on('data', function ($message) use ($client, $pong, &$timer, &$await, $loop) {
// reply to incoming ping messages with pong
if (is_array($message) && isset($message[0]) && $message[0] === Protocol::REQUEST_HEARTBEAT && $pong) {
$client->writeHeartBeatReply($message[1]);
}
// restart heartbeat timer once data comes in
if ($timer !== null) {
$loop->cancelTimer($timer);
$timer = $loop->addPeriodicTimer($timer->getInterval(), $timer->getCallback());
$await = false;
}
});
return $client;
});
}
return $promise;
}
/** @internal */
public function awaitLogin(Client $client, $user, $pass)
{
return new Promise\Promise(function ($resolve, $reject) use ($client, $user, $pass) {
// handle incoming response messages
$client->on('data', $handler = function ($data) use ($resolve, $reject, $client, $user, $pass, &$handler) {
$type = isset($data->MsgType) ? $data->MsgType : null;
// continue to login if connection is initialized
if ($type === 'ClientInitAck') {
if (!isset($data->Configured) || !$data->Configured) {
$reject(new \RuntimeException('Unable to log in to unconfigured Quassel IRC core'));
return $client->close();
}
$client->writeClientLogin($user, $pass);
return;
}
// reject if core rejects initialization
if ($type === 'ClientInitReject') {
$reject(new \RuntimeException('Connection rejected by Quassel core: ' . $data->Error));
return $client->close();
}
// reject promise if login is rejected
if ($type === 'ClientLoginReject') {
$reject(new \RuntimeException('Unable to log in: ' . $data->Error));
return $client->close();
}
// resolve promise if login is successful
if ($type === 'ClientLoginAck') {
$client->removeListener('data', $handler);
$handler = null;
$resolve($client);
return;
}
// otherwise reject if we receive an unexpected message
$reject(new \RuntimeException('Received unexpected "' . $type . '" message during login'));
$client->close();
});
// reject promise if client emits error
$client->on('error', function ($error) use ($reject) {
$reject($error);
});
// reject promise if client closes while waiting for login
$client->on('close', function () use ($reject) {
$reject(new \RuntimeException('Unexpected close'));
});
$client->writeClientInit();
});
}
}