1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Clients\IRC; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
5
|
|
|
use PHPDaemon\Network\ClientConnection; |
6
|
|
|
use PHPDaemon\Utils\IRC; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package NetworkClients |
10
|
|
|
* @subpackage IRCClient |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Connection extends ClientConnection |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string Username |
18
|
|
|
*/ |
19
|
|
|
protected $user = 'Guest'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string Password |
23
|
|
|
*/ |
24
|
|
|
protected $password = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $EOL = "\r\n"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $nick; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $realname; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $mode = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
public $buffers = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $servername; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
public $channels = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
public $latency; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var integer |
68
|
|
|
*/ |
69
|
|
|
public $lastPingTS; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var int |
73
|
|
|
*/ |
74
|
|
|
public $timeout = 300; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var bool To get local port number |
78
|
|
|
*/ |
79
|
|
|
protected $bevConnectEnabled = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function onReady() |
86
|
|
|
{ |
87
|
|
|
if ($this->pool->identd) { |
88
|
|
|
/** @noinspection PhpParamsInspection */ |
89
|
|
|
$this->getSocketName(); |
|
|
|
|
90
|
|
|
$this->pool->identd->registerPair($this->locAddr, $this->locPort, ['UNIX', $this->user]); |
91
|
|
|
} |
92
|
|
|
list($this->nick, $this->realname) = explode('/', $this->path . '/John Doe'); |
93
|
|
|
$this->command('USER', $this->user, 0, '*', $this->realname); |
94
|
|
|
$this->command('NICK', $this->nick); |
95
|
|
|
if (mb_orig_strlen($this->password)) { |
96
|
|
|
$this->message('NickServ', 'IDENTIFY ' . $this->password); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @TODO DESCR |
102
|
|
|
* @param string $cmd |
103
|
|
|
* @param mixed ...$args Arguments |
104
|
|
|
*/ |
105
|
|
|
public function command($cmd) |
106
|
|
|
{ |
107
|
|
|
if (ctype_digit($cmd)) { |
108
|
|
|
$cmd = IRC::getCommandByCode((int)$cmd); |
109
|
|
|
} |
110
|
|
|
$line = $cmd; |
111
|
|
View Code Duplication |
for ($i = 1, $s = func_num_args(); $i < $s; ++$i) { |
|
|
|
|
112
|
|
|
$arg = func_get_arg($i); |
113
|
|
|
if (($i + 1 === $s) && (mb_orig_strpos($arg, "\x20") !== false)) { |
114
|
|
|
$line .= ' :'; |
115
|
|
|
} else { |
116
|
|
|
$line .= ' '; |
117
|
|
|
} |
118
|
|
|
$line .= $arg; |
119
|
|
|
} |
120
|
|
|
$this->writeln($line); |
121
|
|
|
if ($this->pool->protologging) { |
122
|
|
|
Daemon::log('->->->-> ' . $line); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @TODO DESCR |
128
|
|
|
* @param integer|string $cmd |
129
|
|
|
* @param array $args |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function commandArr($cmd, $args = []) |
133
|
|
|
{ |
134
|
|
|
if (!is_array($args)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
if (ctype_digit($cmd)) { |
138
|
|
|
$cmd = IRC::getCommandByCode((int)$cmd); |
139
|
|
|
} |
140
|
|
|
$line = $cmd; |
141
|
|
View Code Duplication |
for ($i = 0, $s = sizeof($args); $i < $s; ++$i) { |
|
|
|
|
142
|
|
|
if (($i + 1 === $s) && (mb_orig_strpos($args[$i], "\x20") !== false)) { |
143
|
|
|
$line .= ' :'; |
144
|
|
|
} else { |
145
|
|
|
$line .= ' '; |
146
|
|
|
} |
147
|
|
|
$line .= $args[$i]; |
148
|
|
|
} |
149
|
|
|
$this->writeln($line); |
150
|
|
|
if ($this->pool->protologging && $cmd !== 'PONG') { |
151
|
|
|
Daemon::log('->->->-> ' . $line); |
152
|
|
|
} |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @TODO DESCR |
158
|
|
|
* @param array $channels |
159
|
|
|
*/ |
160
|
|
|
public function join($channels) |
161
|
|
|
{ |
162
|
|
|
if (!is_array($channels)) { |
163
|
|
|
$channels = array_map('trim', explode(',', $channels)); |
164
|
|
|
} |
165
|
|
|
foreach ($channels as $chan) { |
166
|
|
|
$this->command('JOIN', $chan); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @TODO DESCR |
172
|
|
|
* @param array $channels |
173
|
|
|
* @param mixed $msg |
174
|
|
|
*/ |
175
|
|
|
public function part($channels, $msg = null) |
176
|
|
|
{ |
177
|
|
|
$this->command('PART', $channels, $msg); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Called when connection finishes |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
public function onFinish() |
185
|
|
|
{ |
186
|
|
|
if ($this->pool->identd) { |
187
|
|
|
$this->pool->identd->unregisterPair($this->locPort, $this->port); |
188
|
|
|
} |
189
|
|
|
$this->event('disconnect'); |
190
|
|
|
parent::onFinish(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @TODO DESCR |
195
|
|
|
*/ |
196
|
|
|
public function ping() |
197
|
|
|
{ |
198
|
|
|
$this->lastPingTS = microtime(true); |
|
|
|
|
199
|
|
|
$this->writeln('PING :' . $this->servername); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @TODO DESCR |
204
|
|
|
* @param string $to |
205
|
|
|
* @param string $msg |
206
|
|
|
*/ |
207
|
|
|
public function message($to, $msg) |
208
|
|
|
{ |
209
|
|
|
$this->command('PRIVMSG', $to, $msg); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @TODO DESCR |
214
|
|
|
* @param string $channel |
215
|
|
|
* @param string $target |
216
|
|
|
* @param string $mode |
217
|
|
|
*/ |
218
|
|
|
public function addMode($channel, $target, $mode) |
219
|
|
|
{ |
220
|
|
|
if ($channel) { |
221
|
|
|
$this->channel($channel)->addMode($target, $mode); |
222
|
|
|
} else { |
223
|
|
|
if (mb_orig_strpos($this->mode, $mode) === false) { |
224
|
|
|
$this->mode .= $mode; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @TODO DESCR |
231
|
|
|
* @param string $channel |
232
|
|
|
* @param string $target |
233
|
|
|
* @param string $mode |
234
|
|
|
*/ |
235
|
|
|
public function removeMode($channel, $target, $mode) |
236
|
|
|
{ |
237
|
|
|
if ($channel) { |
238
|
|
|
$this->channel($channel)->removeMode($target, $mode); |
239
|
|
|
} |
240
|
|
|
$this->mode = str_replace($mode, '', $this->mode); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @TODO DESCR |
245
|
|
|
* @param string $from |
246
|
|
|
* @param string $cmd |
247
|
|
|
* @param array $args |
248
|
|
|
*/ |
249
|
|
|
public function onCommand($from, $cmd, $args) |
250
|
|
|
{ |
251
|
|
|
$this->event('command', $from, $cmd, $args); |
252
|
|
|
if ($cmd === 'RPL_WELCOME') { |
253
|
|
|
$this->servername = $from['orig']; |
254
|
|
|
if ($this->onConnected) { |
255
|
|
|
$this->connected = true; |
256
|
|
|
$this->onConnected->executeAll($this); |
257
|
|
|
$this->onConnected = null; |
258
|
|
|
} |
259
|
|
|
} elseif ($cmd === 'NOTICE') { |
260
|
|
|
list($target, $text) = $args; |
261
|
|
|
$this->event('notice', $target, $text); |
262
|
|
|
} elseif ($cmd === 'RPL_YOURHOST') { |
263
|
|
|
} elseif ($cmd === 'RPL_MOTDSTART') { |
264
|
|
|
$this->motd = $args[1]; |
|
|
|
|
265
|
|
|
return; |
266
|
|
|
} elseif ($cmd === 'RPL_MOTD') { |
267
|
|
|
$this->motd .= "\r\n" . $args[1]; |
|
|
|
|
268
|
|
|
return; |
269
|
|
|
} elseif ($cmd === 'RPL_ENDOFMOTD') { |
270
|
|
|
$this->motd .= "\r\n"; // . $args[1]; |
|
|
|
|
271
|
|
|
$this->event('motd', $this->motd); |
|
|
|
|
272
|
|
|
return; |
273
|
|
|
} elseif ($cmd === 'JOIN') { |
274
|
|
|
list($channel) = $args; |
275
|
|
|
$chan = $this->channel($channel); |
276
|
|
|
ChannelParticipant::instance($chan, $from['nick'])->setUsermask($from); |
|
|
|
|
277
|
|
|
} elseif ($cmd === 'NICK') { |
278
|
|
|
list($newNick) = $args; |
279
|
|
|
foreach ($this->channels as $channel) { |
280
|
|
|
if (isset($channel->nicknames[$from['nick']])) { |
281
|
|
|
$channel->nicknames[$from['nick']]->setNick($newNick); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} elseif ($cmd === 'QUIT') { |
285
|
|
|
$args[] = null; |
286
|
|
|
list($msg) = $args; |
|
|
|
|
287
|
|
|
foreach ($this->channels as $channel) { |
288
|
|
|
if (isset($channel->nicknames[$from['nick']])) { |
289
|
|
|
$channel->nicknames[$from['nick']]->destroy(); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} elseif ($cmd === 'PART') { |
293
|
|
|
$args[] = null; |
294
|
|
|
list($channel, $msg) = $args; |
295
|
|
|
if ($chan = $this->channelIfExists($channel)) { |
296
|
|
|
$chan->onPart($from, $msg); |
|
|
|
|
297
|
|
|
} |
298
|
|
|
} elseif ($cmd === 'RPL_NAMREPLY') { |
299
|
|
|
$bufName = 'RPL_NAMREPLY'; |
300
|
|
|
list(/*$myNick*/, $chanType, $channelName) = $args; |
301
|
|
|
$this->channel($channelName)->setChanType($chanType); |
302
|
|
|
if (!isset($this->buffers[$bufName])) { |
303
|
|
|
$this->buffers[$bufName] = []; |
304
|
|
|
} |
305
|
|
|
if (!isset($this->buffers[$bufName][$channelName])) { |
306
|
|
|
$this->buffers[$bufName][$channelName] = new \SplStack; |
307
|
|
|
} |
308
|
|
|
$this->buffers[$bufName][$channelName]->push($args); |
309
|
|
|
} elseif ($cmd === 'RPL_ENDOFNAMES') { |
310
|
|
|
$bufName = 'RPL_NAMREPLY'; |
311
|
|
|
list(/*$nick*/, $channelName, /*$text*/) = $args; |
312
|
|
|
if (!isset($this->buffers[$bufName][$channelName])) { |
313
|
|
|
return; |
314
|
|
|
} |
315
|
|
|
$buf = $this->buffers[$bufName][$channelName]; |
316
|
|
|
$chan = null; |
317
|
|
|
while (!$buf->isEmpty()) { |
318
|
|
|
$shift = $buf->shift(); |
319
|
|
|
list($nick, $chantype, $channelName, $participants) = $shift; |
|
|
|
|
320
|
|
|
if (!$chan) { |
321
|
|
|
$chan = $this->channel($channelName)->setType($chantype); |
322
|
|
|
$chan->each('destroy'); |
323
|
|
|
} |
324
|
|
|
preg_match_all('~([\+%@]?)(\S+)~', $participants, $matches, PREG_SET_ORDER); |
325
|
|
|
|
326
|
|
|
foreach ($matches as $m) { |
|
|
|
|
327
|
|
|
list(, $flag, $nickname) = $m; |
328
|
|
|
ChannelParticipant::instance($chan, $nickname)->setFlag($flag); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} elseif ($cmd === 'RPL_WHOREPLY') { |
332
|
|
|
if (sizeof($args) < 7) { |
333
|
|
|
} |
334
|
|
|
list(/*$myNick*/, $channelName, $user, /*$host*/, $server, $nick, $mode, $hopCountRealName) = $args; |
335
|
|
|
list($hopCount, $realName) = explode("\x20", $hopCountRealName); |
|
|
|
|
336
|
|
|
if ($channel = $this->channelIfExists($channelName)) { |
337
|
|
|
ChannelParticipant::instance($channel, $nick) |
|
|
|
|
338
|
|
|
->setUsermask($nick . '!' . $user . '@' . $server) |
339
|
|
|
->setFlag($mode); |
340
|
|
|
} |
341
|
|
|
} elseif ($cmd === 'RPL_TOPIC') { |
342
|
|
|
list(/*$myNick*/, $channelName, $text) = $args; |
343
|
|
|
if ($channel = $this->channelIfExists($channelName)) { |
344
|
|
|
$channel->setTopic($text); |
|
|
|
|
345
|
|
|
} |
346
|
|
|
} elseif ($cmd === 'RPL_ENDOFWHO') { |
347
|
|
|
/*list($myNick, $channelName, $text) = $args;*/ |
348
|
|
|
} elseif ($cmd === 'MODE') { |
349
|
|
|
if (sizeof($args) === 3) { |
350
|
|
|
list($channel, $mode, $target) = $args; |
351
|
|
|
} else { |
352
|
|
|
$channel = null; |
353
|
|
|
list($target, $mode) = $args; |
354
|
|
|
} |
355
|
|
|
if (mb_orig_strlen($mode)) { |
356
|
|
|
if ($mode[0] === '+') { |
357
|
|
|
$this->addMode($channel, $target, mb_orig_substr($mode, 1)); |
358
|
|
|
} elseif ($mode[0] === '-') { |
359
|
|
|
$this->removeMode($channel, $target, mb_orig_substr($mode, 1)); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} elseif ($cmd === 'RPL_CREATED') { |
363
|
|
|
list(/*$to*/, $this->created) = $args; |
|
|
|
|
364
|
|
|
} elseif ($cmd === 'RPL_MYINFO') { |
365
|
|
|
list(/*$to*/, $this->servername, $this->serverver, $this->availUserModes, $this->availChanModes) = $args; |
|
|
|
|
366
|
|
|
} elseif ($cmd === 'PRIVMSG') { |
367
|
|
|
list($target, $body) = $args; |
368
|
|
|
$msg = [ |
369
|
|
|
'from' => $from, |
370
|
|
|
'to' => $target, |
371
|
|
|
'body' => $body, |
372
|
|
|
'private' => mb_orig_substr($target, 0, 1) !== '#', |
373
|
|
|
]; |
374
|
|
|
$this->event($msg['private'] ? 'privateMsg' : 'channelMsg', $msg); |
375
|
|
|
$this->event('msg', $msg); |
376
|
|
|
if (!$msg['private']) { |
377
|
|
|
$this->channel($target)->event('msg', $msg); |
378
|
|
|
} |
379
|
|
|
} elseif ($cmd === 'PING') { |
380
|
|
|
$this->writeln(isset($args[0]) ? 'PONG :' . $args[0] : 'PONG'); |
381
|
|
View Code Duplication |
} elseif ($cmd === 'PONG') { |
|
|
|
|
382
|
|
|
if ($this->lastPingTS) { |
383
|
|
|
$this->latency = microtime(true) - $this->lastPingTS; |
|
|
|
|
384
|
|
|
$this->lastPingTS = null; |
385
|
|
|
} |
386
|
|
|
return; |
387
|
|
|
} |
388
|
|
|
if ($this->pool->protologging) { |
389
|
|
|
Daemon::$process->log('<-<-<-< ' . $cmd . ': ' . json_encode($args) . ' (' . json_encode($from['orig']) . ') (' . json_encode($this->lastLine) . ')'); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Called when new data received |
395
|
|
|
* @return void |
396
|
|
|
*/ |
397
|
|
|
public function onRead() |
398
|
|
|
{ |
399
|
|
|
while (($line = $this->readline()) !== null) { |
400
|
|
|
if ($line === '') { |
401
|
|
|
continue; |
402
|
|
|
} |
403
|
|
|
if (mb_orig_strlen($line) > 512) { |
404
|
|
|
Daemon::$process->log('IRCClientConnection error: buffer overflow.'); |
405
|
|
|
$this->finish(); |
406
|
|
|
return; |
407
|
|
|
} |
408
|
|
|
$line = mb_orig_substr($line, 0, -mb_orig_strlen($this->EOL)); |
409
|
|
|
$p = mb_orig_strpos($line, ' :', 1); |
410
|
|
|
$max = $p !== false ? substr_count($line, "\x20", 0, $p + 1) + 1 : 18; |
411
|
|
|
$e = explode("\x20", $line, $max); |
412
|
|
|
$i = 0; |
413
|
|
|
$from = IRC::parseUsermask($e[$i][0] === ':' ? mb_orig_substr($e[$i++], 1) : null); |
414
|
|
|
$cmd = $e[$i++]; |
415
|
|
|
$args = []; |
416
|
|
|
|
417
|
|
View Code Duplication |
for ($s = min(sizeof($e), 14); $i < $s; ++$i) { |
|
|
|
|
418
|
|
|
if ($e[$i][0] === ':') { |
419
|
|
|
$args[] = mb_orig_substr($e[$i], 1); |
420
|
|
|
break; |
421
|
|
|
} |
422
|
|
|
$args[] = $e[$i]; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
View Code Duplication |
if (ctype_digit($cmd)) { |
|
|
|
|
426
|
|
|
$code = (int)$cmd; |
427
|
|
|
$cmd = isset(IRC::$codes[$code]) ? IRC::$codes[$code] : $code; |
428
|
|
|
} |
429
|
|
|
$this->lastLine = $line; |
|
|
|
|
430
|
|
|
$this->onCommand($from, $cmd, $args); |
|
|
|
|
431
|
|
|
} |
432
|
|
|
if (mb_orig_strlen($this->buf) > 512) { |
|
|
|
|
433
|
|
|
Daemon::$process->log('IRCClientConnection error: buffer overflow.'); |
434
|
|
|
$this->finish(); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @TODO DESCR |
440
|
|
|
* @param string $chan |
441
|
|
|
* @return Channel |
442
|
|
|
*/ |
443
|
|
|
public function channel($chan) |
444
|
|
|
{ |
445
|
|
|
if (isset($this->channels[$chan])) { |
446
|
|
|
return $this->channels[$chan]; |
447
|
|
|
} |
448
|
|
|
return $this->channels[$chan] = new Channel($this, $chan); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @TODO DESCR |
453
|
|
|
* @param string $chan |
454
|
|
|
* @return bool |
455
|
|
|
*/ |
456
|
|
|
public function channelIfExists($chan) |
457
|
|
|
{ |
458
|
|
|
if (isset($this->channels[$chan])) { |
459
|
|
|
return $this->channels[$chan]; |
460
|
|
|
} |
461
|
|
|
return false; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|