|
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
|
24 |
|
public function __construct(ConnectionInterface $connection) |
|
35
|
|
|
{ |
|
36
|
24 |
|
$this->connection = $connection; |
|
37
|
24 |
|
} |
|
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
|
4 |
|
public function connect() |
|
53
|
|
|
{ |
|
54
|
4 |
|
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
|
6 |
|
public function authenticate($username, $password = null) |
|
69
|
|
|
{ |
|
70
|
6 |
|
$response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_USER, (string) $username); |
|
71
|
|
|
|
|
72
|
6 |
|
if ($response->getStatusCode() === Response::$codes['PasswordRequired']) { |
|
73
|
3 |
|
if (null === $password) { |
|
74
|
1 |
|
throw new RuntimeException('NNTP server asks for further authentication but no password is given'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
$response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_PASS, (string) $password); |
|
78
|
2 |
|
} |
|
79
|
|
|
|
|
80
|
5 |
|
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
|
4 |
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function connectAndAuthenticate($username, $password = null) |
|
91
|
|
|
{ |
|
92
|
3 |
|
$response = $this->connect(); |
|
93
|
|
|
|
|
94
|
3 |
|
if (!in_array($response->getStatusCode(), [Response::$codes['PostingAllowed'], Response::$codes['PostingProhibited']])) { |
|
95
|
1 |
|
throw new RuntimeException(sprintf('Unsuccessful response received: %s', (string) $response)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return $this->authenticate($username, $password); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
7 |
|
public function authInfo($type, $value) |
|
105
|
|
|
{ |
|
106
|
7 |
|
return $this->sendCommand(new Command\AuthInfoCommand($type, $value)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function body($article) |
|
113
|
|
|
{ |
|
114
|
1 |
|
return $this->sendCommand(new Command\BodyCommand($article)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function head($article) |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->sendCommand(new Command\HeadCommand($article)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function listGroups($keyword = null, $arguments = null) |
|
129
|
|
|
{ |
|
130
|
1 |
|
return $this->sendCommand(new Command\ListCommand($keyword, $arguments)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function group($name) |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->sendCommand(new Command\GroupCommand($name)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function help() |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->sendCommand(new Command\HelpCommand()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function overviewFormat() |
|
153
|
|
|
{ |
|
154
|
1 |
|
return $this->sendCommand(new Command\OverviewFormatCommand()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function post($groups, $subject, $body, $from, $headers = null) |
|
161
|
|
|
{ |
|
162
|
2 |
|
$response = $this->sendCommand(new Command\PostCommand()); |
|
163
|
|
|
|
|
164
|
2 |
|
if ($response->getStatusCode() === Response::$codes['SendArticle']) { |
|
165
|
2 |
|
$response = $this->postArticle($groups, $subject, $body, $from, $headers); |
|
166
|
2 |
|
} |
|
167
|
|
|
|
|
168
|
2 |
|
if ($response->getStatusCode() !== Response::$codes['ArticleReceived']) { |
|
169
|
1 |
|
throw new RuntimeException(sprintf('Posting failed: %s', (string) $response)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
return $response; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
3 |
|
public function postArticle($groups, $subject, $body, $from, $headers = null) |
|
179
|
|
|
{ |
|
180
|
3 |
|
return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* {@inheritdoc} |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function quit() |
|
187
|
|
|
{ |
|
188
|
1 |
|
return $this->sendCommand(new Command\QuitCommand()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritdoc} |
|
193
|
|
|
*/ |
|
194
|
1 |
|
public function xfeature($feature) |
|
195
|
|
|
{ |
|
196
|
1 |
|
return $this->sendCommand(new Command\XFeatureCommand($feature)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritdoc} |
|
201
|
|
|
*/ |
|
202
|
1 |
|
public function xover($from, $to, array $format) |
|
203
|
|
|
{ |
|
204
|
1 |
|
return $this->sendCommand(new Command\XoverCommand($from, $to, $format)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* {@inheritdoc} |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function xzver($from, $to, array $format) |
|
211
|
|
|
{ |
|
212
|
1 |
|
return $this->sendCommand(new Command\XzverCommand($from, $to, $format)); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* {@inheritdoc} |
|
217
|
|
|
*/ |
|
218
|
19 |
|
public function sendCommand(CommandInterface $command) |
|
219
|
|
|
{ |
|
220
|
19 |
|
return $this->connection->sendCommand($command); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
3 |
|
public function sendArticle(CommandInterface $command) |
|
227
|
|
|
{ |
|
228
|
3 |
|
return $this->connection->sendArticle($command); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|