|
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
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Client |
|
13
|
|
|
* @package Genkgo\Mail\Protocol\Imap |
|
14
|
|
|
*/ |
|
15
|
|
|
final class Client |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
public CONST AUTH_NONE = 0; |
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
public CONST AUTH_PLAIN = 1; |
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
public CONST AUTH_AUTO = 3; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ConnectionInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $connection; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var NegotiationInterface[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $negotiators = []; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var TagFactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $tagFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Client constructor. |
|
44
|
|
|
* @param ConnectionInterface $connection |
|
45
|
|
|
* @param TagFactoryInterface $tagFactory |
|
46
|
|
|
* @param iterable $negotiators |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function __construct( |
|
49
|
|
|
ConnectionInterface $connection, |
|
50
|
|
|
TagFactoryInterface $tagFactory, |
|
51
|
|
|
iterable $negotiators = [] |
|
52
|
|
|
) { |
|
53
|
5 |
|
$this->connection = new AppendCrlfConnection($connection); |
|
54
|
|
|
|
|
55
|
5 |
|
$this->addNegotiator(new ReceiveWelcomeNegotiation($this->connection)); |
|
56
|
|
|
|
|
57
|
5 |
|
foreach ($negotiators as $negotiator) { |
|
58
|
|
|
$this->addNegotiator($negotiator); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$this->connection->addListener('connect', function () { |
|
62
|
|
|
foreach ($this->negotiators as $negotiator) { |
|
63
|
|
|
$negotiator->negotiate($this); |
|
64
|
|
|
} |
|
65
|
5 |
|
}); |
|
66
|
|
|
|
|
67
|
5 |
|
$this->tagFactory = $tagFactory; |
|
68
|
5 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param NegotiationInterface $negotiation |
|
72
|
|
|
*/ |
|
73
|
5 |
|
private function addNegotiator(NegotiationInterface $negotiation) |
|
74
|
|
|
{ |
|
75
|
5 |
|
$this->negotiators[] = $negotiation; |
|
76
|
5 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param RequestInterface $request |
|
80
|
|
|
* @return AggregateResponse |
|
81
|
|
|
*/ |
|
82
|
4 |
|
public function emit(RequestInterface $request): AggregateResponse |
|
83
|
|
|
{ |
|
84
|
4 |
|
$stream = $request->toStream(); |
|
85
|
4 |
|
$stream->rewind(); |
|
86
|
|
|
|
|
87
|
4 |
|
$bytes = ''; |
|
88
|
4 |
|
while (!$stream->eof()) { |
|
89
|
4 |
|
$bytes .= $stream->read(1000); |
|
90
|
|
|
|
|
91
|
4 |
|
$index = 0; |
|
92
|
4 |
|
while (isset($bytes[$index])) { |
|
93
|
4 |
|
if ($bytes[$index] === "\r" && isset($bytes[$index + 1]) && $bytes[$index + 1] === "\n") { |
|
94
|
|
|
$line = substr($bytes, 0, $index); |
|
95
|
|
|
$bytes = substr($bytes, $index + 2); |
|
96
|
|
|
$index = -1; |
|
97
|
|
|
|
|
98
|
|
|
$this->connection->send($line); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
$index++; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
$this->connection->send($bytes); |
|
106
|
|
|
|
|
107
|
4 |
|
$response = new AggregateResponse($request); |
|
108
|
|
|
|
|
109
|
4 |
|
while (!$response->hasCompleted()) { |
|
110
|
4 |
|
$response = $response->withLine($this->connection->receive()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
4 |
|
return $response; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return Tag |
|
118
|
|
|
*/ |
|
119
|
4 |
|
public function newTag(): Tag |
|
120
|
|
|
{ |
|
121
|
4 |
|
return $this->tagFactory->newTag(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
} |