1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Spires\Irc; |
5
|
|
|
|
6
|
|
|
use Spires\Contracts\Core\Core; |
7
|
|
|
use Spires\Core\Dispatcher; |
8
|
|
|
use Spires\Irc\Message\Inbound\RawMessage; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Core |
14
|
|
|
*/ |
15
|
|
|
private $core; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Connection |
19
|
|
|
*/ |
20
|
|
|
private $connection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var User |
24
|
|
|
*/ |
25
|
|
|
private $user; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var resource |
29
|
|
|
*/ |
30
|
|
|
private $socket; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Dispatcher |
34
|
|
|
*/ |
35
|
|
|
private $dispatcher; |
36
|
|
|
|
37
|
|
|
public function __construct(Core $core, Connection $connection, User $user, Dispatcher $dispatcher) |
38
|
|
|
{ |
39
|
|
|
$this->core = $core; |
40
|
|
|
$this->connection = $connection; |
41
|
|
|
$this->user = $user; |
42
|
|
|
$this->dispatcher = $dispatcher; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function connection() : Connection |
46
|
|
|
{ |
47
|
|
|
return $this->connection; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function channel() : string |
51
|
|
|
{ |
52
|
|
|
return $this->connection()->channel(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function user() : User |
56
|
|
|
{ |
57
|
|
|
return $this->user; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function socket() : resource |
61
|
|
|
{ |
62
|
|
|
return $this->socket; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function connect() |
66
|
|
|
{ |
67
|
|
|
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
68
|
|
|
|
69
|
|
|
$isConnected = socket_connect( |
|
|
|
|
70
|
|
|
$this->socket, |
71
|
|
|
$this->connection()->server(), |
72
|
|
|
$this->connection()->port() |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->write("NICK {$this->user()->nickname()}"); |
76
|
|
|
$this->write("USER {$this->user()->username()} {$this->user()->usermode()} * :{$this->user()->realname()}"); |
77
|
|
|
$this->write("JOIN {$this->connection()->channel()}"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function read() |
81
|
|
|
{ |
82
|
|
|
return socket_read($this->socket, 2048, PHP_NORMAL_READ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function write(string $response) |
86
|
|
|
{ |
87
|
|
|
$response = trim($response); |
88
|
|
|
|
89
|
|
|
$this->logWrite($response); |
90
|
|
|
return socket_write($this->socket, $response . "\r\n"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function log(string $title, string $string) |
94
|
|
|
{ |
95
|
|
|
$time = date('H:i:s'); |
96
|
|
|
$title = str_pad($title, 8, ' ', STR_PAD_RIGHT); |
97
|
|
|
|
98
|
|
|
fwrite(STDOUT, "[{$time}|{$title}]: " . $string . "\n"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function logNewLine() |
102
|
|
|
{ |
103
|
|
|
fwrite(STDOUT, "\n"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function logLine() |
107
|
|
|
{ |
108
|
|
|
fwrite(STDOUT, str_repeat('_', 80) . "\n"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function logHeading(string $title) |
112
|
|
|
{ |
113
|
|
|
$title = str_repeat('=', 5) . ' ' . $title . ' '; |
114
|
|
|
$title = str_pad($title, 60, '=', STR_PAD_RIGHT); |
115
|
|
|
$line = str_repeat('=', 60); |
116
|
|
|
|
117
|
|
|
$this->logNewLine(); |
118
|
|
|
$this->log('debug', $line); |
119
|
|
|
$this->log('debug', $title); |
120
|
|
|
$this->log('debug', $line); |
121
|
|
|
$this->logNewLine(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function logDebug(string $string) |
125
|
|
|
{ |
126
|
|
|
$this->log('debug', $string); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function logRead(string $string) |
130
|
|
|
{ |
131
|
|
|
$this->log('read', $string); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function logWrite(string $string) |
135
|
|
|
{ |
136
|
|
|
$this->log('write', $string); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function run() |
140
|
|
|
{ |
141
|
|
|
$parser = new Parser(); |
142
|
|
|
|
143
|
|
|
while ($raw = $this->read()) { |
144
|
|
|
if (!$raw = trim($raw)) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
$this->logLine(); |
148
|
|
|
$this->logRead($raw); |
149
|
|
|
$messageArray = $parser->parse($raw . "\r\n"); |
150
|
|
|
|
151
|
|
|
$message = new RawMessage( |
152
|
|
|
$messageArray['nickname'], |
153
|
|
|
$messageArray['username'], |
154
|
|
|
$messageArray['hostname'], |
155
|
|
|
$messageArray['serverName'], |
156
|
|
|
$messageArray['command'], |
157
|
|
|
$messageArray['params'] |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$this->dispatcher->dispatch($message); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.