Skip to content

Commit 0e336cd

Browse files
committed
WIP
1 parent b2d2f51 commit 0e336cd

File tree

9 files changed

+255
-273
lines changed

9 files changed

+255
-273
lines changed

src/Connection/Connection.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,16 @@ protected function getDefaultSocketOptions(string $transport): array
271271
*/
272272
public function connect(string $host, ?int $port = null): void
273273
{
274+
$transport = $this->getTransport();
274275

275-
$transport = 'tcp';
276-
$encryption = '';
277-
278-
if ($this->encryption) {
279-
$encryption = strtolower($this->encryption);
280-
281-
if (in_array($encryption, ['ssl', 'tls'])) {
282-
$transport = $encryption;
283-
$port ??= 993;
284-
}
276+
if (in_array($transport, ['ssl', 'tls'])) {
277+
$port ??= 993;
278+
} else {
279+
$port ??= 143;
285280
}
286281

287-
$port ??= 143;
288-
289-
$this->parser = new ImapParser(
290-
new ImapTokenizer($this->stream)
282+
$this->setParser(
283+
$this->newParser($this->stream)
291284
);
292285

293286
$this->stream->open(
@@ -306,43 +299,17 @@ public function connect(string $host, ?int $port = null): void
306299

307300
$this->setStreamTimeout($this->connectionTimeout);
308301

309-
if ($encryption == 'starttls') {
302+
if ($transport === 'starttls') {
310303
$this->enableStartTls();
311304
}
312305
}
313306

314307
/**
315-
* Send an IMAP command.
308+
* Get the transport method for the current connection.
316309
*/
317-
public function send(string $name, array $tokens = [], ?string &$tag = null): void
318-
{
319-
$command = new ImapCommand($name, $tokens);
320-
321-
if (! $tag) {
322-
$this->sequence++;
323-
$tag = 'TAG'.$this->sequence;
324-
}
325-
326-
$command->setTag($tag);
327-
328-
$this->result = new Result;
329-
330-
$this->result->addCommand($command);
331-
332-
foreach ($command->compile() as $line) {
333-
$this->write($line);
334-
}
335-
}
336-
337-
protected function untilTaggedResponse(string $tag, Result $result): void
310+
protected function getTransport(): string
338311
{
339-
while ($response = $this->nextResponse()) {
340-
$result->addResponse($response);
341-
342-
if ($response instanceof TaggedResponse && $response->tag()->is($tag)) {
343-
break;
344-
}
345-
}
312+
return $this->encryption ? strtolower($this->encryption) : 'tcp';
346313
}
347314

348315
/**
@@ -367,6 +334,31 @@ public function nextReply(): Response
367334
return $reply;
368335
}
369336

337+
/**
338+
* Send an IMAP command.
339+
*/
340+
public function send(string $name, array $tokens, ?string &$tag): void
341+
{
342+
$command = new ImapCommand($name, $tokens);
343+
344+
if (! $tag) {
345+
$this->sequence++;
346+
$tag = 'TAG'.$this->sequence;
347+
}
348+
349+
$command->setTag($tag);
350+
351+
$result = new Result;
352+
353+
$this->setResult($result);
354+
355+
$result->addCommand($command);
356+
357+
foreach ($command->compile() as $line) {
358+
$this->write($line);
359+
}
360+
}
361+
370362
/**
371363
* Write data to the stream.
372364
*/

src/Connection/ConnectionInterface.php

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace DirectoryTree\ImapEngine\Connection;
44

5+
use DirectoryTree\ImapEngine\Collections\ResponseCollection;
6+
use DirectoryTree\ImapEngine\Connection\Responses\TaggedResponse;
7+
58
interface ConnectionInterface
69
{
710
/**
@@ -17,17 +20,17 @@ public function connected(): bool;
1720
/**
1821
* Login to a new session.
1922
*/
20-
public function login(string $user, string $password): Response;
23+
public function login(string $user, string $password): TaggedResponse;
2124

2225
/**
2326
* Logout of the current server session.
2427
*/
25-
public function logout(): Response;
28+
public function logout(): ?TaggedResponse;
2629

2730
/**
2831
* Authenticate the current session.
2932
*/
30-
public function authenticate(string $user, string $token): Response;
33+
public function authenticate(string $user, string $token): TaggedResponse;
3134

3235
/**
3336
* Send idle command.
@@ -42,84 +45,84 @@ public function done(): void;
4245
/**
4346
* Send noop command.
4447
*/
45-
public function noop(): Response;
48+
public function noop(): ResponseCollection;
4649

4750
/**
4851
* Apply session saved changes to the server.
4952
*/
50-
public function expunge(): Response;
53+
public function expunge(): ResponseCollection;
5154

5255
/**
5356
* Get an array of available capabilities.
5457
*
55-
* @return Response containing a list of capabilities
58+
* @return ResponseCollection containing a list of capabilities
5659
*/
57-
public function capability(): Response;
60+
public function capability(): ResponseCollection;
5861

5962
/**
6063
* Execute a search request.
6164
*
62-
* @return Response containing the message ids
65+
* @return ResponseCollection containing the message ids
6366
*/
64-
public function search(array $params): Response;
67+
public function search(array $params): ResponseCollection;
6568

6669
/**
6770
* Exchange identification information.
6871
*
6972
* @see https://datatracker.ietf.org/doc/html/rfc2971.
7073
*/
71-
public function id(?array $ids = null): Response;
74+
public function id(?array $ids = null): ResponseCollection;
7275

7376
/**
7477
* Fetch message UIDs using the given message numbers.
7578
*/
76-
public function uids(int|array $msgns): Response;
79+
public function uids(int|array $msgns): ResponseCollection;
7780

7881
/**
7982
* Fetch message contents.
8083
*/
81-
public function contents(int|array $ids): Response;
84+
public function contents(int|array $ids): ResponseCollection;
8285

8386
/**
8487
* Fetch message headers.
8588
*/
86-
public function headers(int|array $ids): Response;
89+
public function headers(int|array $ids): ResponseCollection;
8790

8891
/**
8992
* Fetch message flags.
9093
*/
91-
public function flags(int|array $ids): Response;
94+
public function flags(int|array $ids): ResponseCollection;
9295

9396
/**
9497
* Fetch message sizes.
9598
*/
96-
public function sizes(int|array $ids): Response;
99+
public function sizes(int|array $ids): ResponseCollection;
97100

98101
/**
99102
* Select the given folder.
100103
*/
101-
public function selectFolder(string $folder): Response;
104+
public function selectFolder(string $folder): ResponseCollection;
102105

103106
/**
104107
* Examine a given folder.
105108
*/
106-
public function examineFolder(string $folder): Response;
109+
public function examineFolder(string $folder): ResponseCollection;
107110

108111
/**
109112
* Get a list of available folders.
110113
*
111114
* @param string $reference mailbox reference for list
112115
* @param string $folder mailbox / folder name match with wildcards
113-
* @return Response containing mailboxes that matched $folder as array(globalName => array('delim' => .., 'flags' => ..))
116+
* @return ResponseCollection containing mailboxes that matched $folder as array(globalName => array('delim' => .., 'flags' => ..))
114117
*/
115-
public function folders(string $reference = '', string $folder = '*'): Response;
118+
public function folders(string $reference = '', string $folder = '*'): ResponseCollection;
116119

117120
/**
118121
* Get the status of a given folder.
119122
*
120-
* @return Response list of STATUS items
123+
* @return ResponseCollection list of STATUS items
121124
*/
122-
public function folderStatus(string $folder, array $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response;
125+
public function folderStatus(string $folder, array $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): ResponseCollection;
123126

124127
/**
125128
* Set message flags.
@@ -131,9 +134,9 @@ public function folderStatus(string $folder, array $arguments = ['MESSAGES', 'UN
131134
* @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given
132135
* @param bool $silent if false the return values are the new flags for the wanted messages
133136
* @param string|null $item command used to store a flag
134-
* @return Response containing the new flags if $silent is false, else true or false depending on success
137+
* @return ResponseCollection containing the new flags if $silent is false, else true or false depending on success
135138
*/
136-
public function store(array|string $flags, int $from, ?int $to = null, ?string $mode = null, bool $silent = true, ?string $item = null): Response;
139+
public function store(array|string $flags, int $from, ?int $to = null, ?string $mode = null, bool $silent = true, ?string $item = null): ResponseCollection;
137140

138141
/**
139142
* Append a new message to given folder.
@@ -143,7 +146,7 @@ public function store(array|string $flags, int $from, ?int $to = null, ?string $
143146
* @param array|null $flags flags for new message
144147
* @param string|null $date date for new message
145148
*/
146-
public function appendMessage(string $folder, string $message, ?array $flags = null, ?string $date = null): Response;
149+
public function appendMessage(string $folder, string $message, ?array $flags = null, ?string $date = null): ResponseCollection;
147150

148151
/**
149152
* Copy message set from current folder to other folder.
@@ -152,16 +155,16 @@ public function appendMessage(string $folder, string $message, ?array $flags = n
152155
* @param int|null $to if null only one message ($from) is fetched, else it's the
153156
* last message, INF means last message available
154157
*/
155-
public function copyMessage(string $folder, $from, ?int $to = null): Response;
158+
public function copyMessage(string $folder, $from, ?int $to = null): ResponseCollection;
156159

157160
/**
158161
* Copy multiple messages to the target folder.
159162
*
160163
* @param array<string> $messages List of message identifiers
161164
* @param string $folder Destination folder
162-
* @return Response Tokens if operation successful, false if an error occurred
165+
* @return ResponseCollection Tokens if operation successful, false if an error occurred
163166
*/
164-
public function copyManyMessages(array $messages, string $folder): Response;
167+
public function copyManyMessages(array $messages, string $folder): ResponseCollection;
165168

166169
/**
167170
* Move a message set from current folder to another folder.
@@ -170,50 +173,50 @@ public function copyManyMessages(array $messages, string $folder): Response;
170173
* @param int|null $to if null only one message ($from) is fetched, else it's the
171174
* last message, INF means last message available
172175
*/
173-
public function moveMessage(string $folder, $from, ?int $to = null): Response;
176+
public function moveMessage(string $folder, $from, ?int $to = null): ResponseCollection;
174177

175178
/**
176179
* Move multiple messages to the target folder.
177180
*
178181
* @param array<string> $messages List of message identifiers
179182
* @param string $folder Destination folder
180-
* @return Response Tokens if operation successful, false if an error occurred
183+
* @return ResponseCollection Tokens if operation successful, false if an error occurred
181184
*/
182-
public function moveManyMessages(array $messages, string $folder): Response;
185+
public function moveManyMessages(array $messages, string $folder): ResponseCollection;
183186

184187
/**
185188
* Create a new folder.
186189
*
187190
* @param string $folder folder name
188191
*/
189-
public function createFolder(string $folder): Response;
192+
public function createFolder(string $folder): ResponseCollection;
190193

191194
/**
192195
* Rename an existing folder.
193196
*
194197
* @param string $oldPath old name
195198
* @param string $newPath new name
196199
*/
197-
public function renameFolder(string $oldPath, string $newPath): Response;
200+
public function renameFolder(string $oldPath, string $newPath): ResponseCollection;
198201

199202
/**
200203
* Delete a folder.
201204
*
202205
* @param string $folder folder name
203206
*/
204-
public function deleteFolder(string $folder): Response;
207+
public function deleteFolder(string $folder): ResponseCollection;
205208

206209
/**
207210
* Subscribe to a folder.
208211
*
209212
* @param string $folder folder name
210213
*/
211-
public function subscribeFolder(string $folder): Response;
214+
public function subscribeFolder(string $folder): ResponseCollection;
212215

213216
/**
214217
* Unsubscribe from a folder.
215218
*
216219
* @param string $folder folder name
217220
*/
218-
public function unsubscribeFolder(string $folder): Response;
221+
public function unsubscribeFolder(string $folder): ResponseCollection;
219222
}

src/Connection/Data/Data.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine\Connection\Data;
44

5+
use DirectoryTree\ImapEngine\Connection\Tokens\Token;
56
use Stringable;
67

78
abstract class Data implements Stringable
@@ -20,4 +21,12 @@ public function tokens(): array
2021
{
2122
return $this->tokens;
2223
}
24+
25+
/**
26+
* Get all of the token's values.
27+
*/
28+
public function values(): array
29+
{
30+
return array_map(fn (Token $token) => $token->value, $this->tokens);
31+
}
2332
}

0 commit comments

Comments
 (0)