|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Imap; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Protocol\AppendCrlfConnection; |
|
7
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
|
8
|
|
|
use Genkgo\Mail\Protocol\Imap\Negotiation\ReceiveWelcomeNegotiation; |
|
9
|
|
|
use Genkgo\Mail\Protocol\Imap\Response\AggregateResponse; |
|
10
|
|
|
use Genkgo\Mail\Stream\LineIterator; |
|
11
|
|
|
|
|
12
|
|
|
final class Client |
|
13
|
|
|
{ |
|
14
|
|
|
public const AUTH_NONE = 0; |
|
15
|
|
|
|
|
16
|
|
|
public const AUTH_PLAIN = 1; |
|
17
|
|
|
|
|
18
|
|
|
public const AUTH_LOGIN = 2; |
|
19
|
|
|
|
|
20
|
|
|
public const AUTH_AUTO = 3; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConnectionInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $connection; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var NegotiationInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $negotiators = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TagFactoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $tagFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ConnectionInterface $connection |
|
39
|
|
|
* @param TagFactoryInterface $tagFactory |
|
40
|
|
|
* @param iterable $negotiators |
|
41
|
|
|
*/ |
|
42
|
23 |
|
public function __construct( |
|
43
|
|
|
ConnectionInterface $connection, |
|
44
|
|
|
TagFactoryInterface $tagFactory, |
|
45
|
|
|
iterable $negotiators = [] |
|
46
|
|
|
) { |
|
47
|
23 |
|
$this->connection = new AppendCrlfConnection($connection); |
|
48
|
|
|
|
|
49
|
23 |
|
$this->addNegotiator(new ReceiveWelcomeNegotiation($this->connection)); |
|
50
|
|
|
|
|
51
|
23 |
|
foreach ($negotiators as $negotiator) { |
|
52
|
7 |
|
$this->addNegotiator($negotiator); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
23 |
|
$this->connection->addListener('connect', function () { |
|
56
|
4 |
|
foreach ($this->negotiators as $negotiator) { |
|
57
|
4 |
|
$negotiator->negotiate($this); |
|
58
|
|
|
} |
|
59
|
23 |
|
}); |
|
60
|
|
|
|
|
61
|
23 |
|
$this->tagFactory = $tagFactory; |
|
62
|
23 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param NegotiationInterface $negotiation |
|
66
|
|
|
*/ |
|
67
|
23 |
|
private function addNegotiator(NegotiationInterface $negotiation) |
|
68
|
|
|
{ |
|
69
|
23 |
|
$this->negotiators[] = $negotiation; |
|
70
|
23 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param RequestInterface $request |
|
74
|
|
|
* @return AggregateResponse |
|
75
|
|
|
*/ |
|
76
|
17 |
|
public function emit(RequestInterface $request): AggregateResponse |
|
77
|
|
|
{ |
|
78
|
17 |
|
$stream = $request->toStream(); |
|
79
|
|
|
|
|
80
|
17 |
|
foreach (new LineIterator($stream) as $line) { |
|
81
|
17 |
|
$this->connection->send($line); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
13 |
|
$response = new AggregateResponse($request->getTag()); |
|
85
|
|
|
|
|
86
|
13 |
|
while (!$response->hasCompleted()) { |
|
87
|
13 |
|
$response = $response->withLine($this->connection->receive()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
13 |
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Tag |
|
95
|
|
|
*/ |
|
96
|
17 |
|
public function newTag(): Tag |
|
97
|
|
|
{ |
|
98
|
17 |
|
return $this->tagFactory->newTag(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|