forked from jerodev/php-irc-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IrcMessage.php
94 lines (80 loc) · 2.46 KB
/
IrcMessage.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
<?php
declare(strict_types=1);
namespace Jerodev\PhpIrcClient\Messages;
use Jerodev\PhpIrcClient\Helpers\Event;
use Jerodev\PhpIrcClient\IrcChannel;
use Jerodev\PhpIrcClient\IrcClient;
class IrcMessage
{
public ?IrcChannel $channel = null;
protected ?string $commandsuffix = null;
protected bool $handled = false;
protected string $payload = '';
protected ?string $source = null;
public ?string $target = null;
public function __construct(protected string $command)
{
$this->parse($this->command);
}
/**
* This function is always called after the message is parsed.
* The handle will only be executed once unless forced.
*
* @param IrcClient $client A reference to the irc client object
* @param bool $force Force handling this message even if already handled
*/
public function handle(IrcClient $client, bool $force = false): void
{
if ($this->handled && !$force) {
return;
}
}
/**
* Get the events that should be invoked for this message.
* @return array<int, Event>
*/
public function getEvents(): array
{
return [];
}
/**
* Inject the list of IRC channels.
* The messages can use this to gather information of the channel if needed.
* @param array<string, IrcChannel> $channels
*/
public function injectChannel(array $channels): void
{
if (array_key_exists($this->target, $channels)) {
$this->channel = $channels[$this->target];
}
}
/**
* Parse the IRC command string to local properties.
*/
protected function parse(string $command): void
{
$command = trim($command);
$i = 0;
if (':' === $command[0] && false !== strpos($command, ' ')) {
$i = (int)strpos($command, ' ');
$this->source = substr($command, 1, $i - 1);
$i++;
}
$j = strpos($command, ' ', $i);
if (false !== $j) {
$this->command = substr($command, $i, $j - $i);
} else {
$this->command = substr($command, $i);
return;
}
$i = strpos($command, ':', $j);
if (false !== $i) {
if ($i !== $j + 1) {
$this->commandsuffix = substr($command, $j + 1, $i - $j - 2);
}
$this->payload = substr($command, $i + 1);
} else {
$this->commandsuffix = substr($command, $j + 1);
}
}
}