1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the NNTP library. |
5
|
|
|
* |
6
|
|
|
* (c) Robin van der Vleuten <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Rvdv\Nntp; |
13
|
|
|
|
14
|
|
|
use Rvdv\Nntp\Command\CommandInterface; |
15
|
|
|
use Rvdv\Nntp\Connection\ConnectionInterface; |
16
|
|
|
use Rvdv\Nntp\Exception\RuntimeException; |
17
|
|
|
use Rvdv\Nntp\Response\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Robin van der Vleuten <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client implements ClientInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ConnectionInterface |
26
|
|
|
*/ |
27
|
|
|
private $connection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param ConnectionInterface $connection |
33
|
|
|
*/ |
34
|
8 |
|
public function __construct(ConnectionInterface $connection) |
35
|
|
|
{ |
36
|
8 |
|
$this->connection = $connection; |
37
|
8 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the connection instance. |
41
|
|
|
* |
42
|
|
|
* @return ConnectionInterface |
43
|
|
|
*/ |
44
|
1 |
|
public function getConnection() |
45
|
|
|
{ |
46
|
1 |
|
return $this->connection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function connect() |
53
|
|
|
{ |
54
|
1 |
|
return $this->connection->connect(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function disconnect() |
61
|
|
|
{ |
62
|
1 |
|
$this->connection->disconnect(); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
4 |
|
public function authenticate($username, $password = null) |
69
|
|
|
{ |
70
|
4 |
|
$response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_USER, (string) $username); |
71
|
|
|
|
72
|
4 |
|
if ($response->getStatusCode() === Response::$codes['PasswordRequired']) { |
|
|
|
|
73
|
2 |
|
if (null === $password) { |
74
|
1 |
|
throw new RuntimeException('NNTP server asks for further authentication but no password is given'); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_PASS, (string) $password); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
3 |
|
if ($response->getStatusCode() !== Response::$codes['AuthenticationAccepted']) { |
|
|
|
|
81
|
1 |
|
throw new RuntimeException(sprintf('Could not authenticate with given username/password: %s', (string) $response)); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $response; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function connectAndAuthenticate($username = null, $password = null) |
91
|
|
|
{ |
92
|
|
|
$response = $this->connect(); |
93
|
|
|
|
94
|
|
|
if (!in_array($response->getStatusCode(), [Response::$codes['PostingAllowed'], Response::$code['PostingProhibited']])) { |
95
|
|
|
throw new RuntimeException(sprintf('Unsuccessful response received: %s', (string) $response)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($username === null) { |
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->authenticate($username, $password); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
5 |
|
public function authInfo($type, $value) |
109
|
|
|
{ |
110
|
5 |
|
return $this->sendCommand(new Command\AuthInfoCommand($type, $value)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function body($article) |
117
|
|
|
{ |
118
|
|
|
return $this->sendCommand(new Command\BodyCommand($article)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function listGroups($keyword = null, $arguments = null) |
125
|
|
|
{ |
126
|
|
|
return $this->sendCommand(new Command\ListCommand($keyword, $arguments)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function group($name) |
133
|
|
|
{ |
134
|
1 |
|
return $this->sendCommand(new Command\GroupCommand($name)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function help() |
141
|
|
|
{ |
142
|
1 |
|
return $this->sendCommand(new Command\HelpCommand()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
1 |
|
public function overviewFormat() |
149
|
|
|
{ |
150
|
1 |
|
return $this->sendCommand(new Command\OverviewFormatCommand()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function post($groups, $subject, $body, $from, $headers = null) |
157
|
|
|
{ |
158
|
|
|
$command = $this->sendCommand(new Command\PostCommand()); |
159
|
|
|
$response = $command->getResponse(); |
160
|
|
|
|
161
|
|
|
if ($response->getStatusCode() === Response::$codes['SendArticle']) { |
162
|
|
|
$response = $this->postArticle($groups, $subject, $body, $from, $headers); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($response->getStatusCode() !== Response::$codes['ArticleReceived']) { |
166
|
|
|
throw new RuntimeException(sprintf('Posting failed: %s', (string) $response)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $response; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function postArticle($groups, $subject, $body, $from, $headers = null) |
176
|
|
|
{ |
177
|
|
|
return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
1 |
|
public function quit() |
184
|
|
|
{ |
185
|
1 |
|
return $this->sendCommand(new Command\QuitCommand()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
1 |
|
public function xfeature($feature) |
192
|
|
|
{ |
193
|
1 |
|
return $this->sendCommand(new Command\XFeatureCommand($feature)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
1 |
|
public function xover($from, $to, array $format) |
200
|
|
|
{ |
201
|
1 |
|
return $this->sendCommand(new Command\XoverCommand($from, $to, $format)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
1 |
|
public function xzver($from, $to, array $format) |
208
|
|
|
{ |
209
|
1 |
|
return $this->sendCommand(new Command\XzverCommand($from, $to, $format)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
5 |
|
public function sendCommand(CommandInterface $command) |
216
|
|
|
{ |
217
|
5 |
|
return $this->connection->sendCommand($command); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function sendArticle(CommandInterface $command) |
224
|
|
|
{ |
225
|
|
|
return $this->connection->sendArticle($command); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.