1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jerodev\PhpIrcClient\Messages; |
4
|
|
|
|
5
|
|
|
use Jerodev\PhpIrcClient\Helpers\Event; |
6
|
|
|
use Jerodev\PhpIrcClient\IrcClient; |
7
|
|
|
|
8
|
|
|
class IrcMessage |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $rawMessage; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $command; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $commandsuffix; |
18
|
|
|
|
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $handled; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $payload; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $source; |
27
|
|
|
|
28
|
|
|
public function __construct(string $command) |
29
|
|
|
{ |
30
|
|
|
$this->handled = false; |
31
|
|
|
$this->parse($command); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This function is always called after the message is parsed. |
36
|
|
|
* The handle will only be executed once unless forced. |
37
|
|
|
* |
38
|
|
|
* @param IrcClient $client A reference to the irc client object |
39
|
|
|
* @param bool $force Force handling this message even if already handled. |
40
|
|
|
*/ |
41
|
|
|
public function handle(IrcClient $client, bool $force = false): void |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if ($this->handled && !$force) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the events that should be invoked for this message. |
50
|
|
|
* |
51
|
|
|
* @return Event[] |
52
|
|
|
*/ |
53
|
|
|
public function getEvents(): array |
54
|
|
|
{ |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Parse the irc command string to local properties |
60
|
|
|
* |
61
|
|
|
* @param string $command |
62
|
|
|
*/ |
63
|
|
|
private function parse(string $command): void |
64
|
|
|
{ |
65
|
|
|
$command = trim($command); |
66
|
|
|
$this->rawMessage = $command; |
67
|
|
|
$i = 0; |
68
|
|
|
|
69
|
|
|
if ($command[0] === ':') { |
70
|
|
|
$i = strpos($command, ' '); |
71
|
|
|
$this->source = substr($command, 1, $i - 1); |
72
|
|
|
|
73
|
|
|
$i++; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$j = strpos($command, ' ', $i); |
77
|
|
|
if (is_numeric($j)) { |
|
|
|
|
78
|
|
|
$this->command = substr($command, $i, $j - $i); |
79
|
|
|
} else { |
80
|
|
|
$this->command = substr($command, $i); |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$i = strpos($command, ':', $j); |
85
|
|
|
if (is_numeric($i)) { |
|
|
|
|
86
|
|
|
if ($i !== $j + 1) { |
87
|
|
|
$this->commandsuffix = substr($command, $j + 1, $i - $j - 2); |
88
|
|
|
} |
89
|
|
|
$this->payload = substr($command, $i + 1); |
90
|
|
|
} else { |
91
|
|
|
$this->commandsuffix = substr($command, $j + 1); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.