Skip to content

Commit bb818fa

Browse files
sazanofMikhail Sazanov
andauthored
Add attributes and special flags (Webklex#428)
Signed-off-by: Mikhail Sazanov <[email protected]> Co-authored-by: Mikhail Sazanov <[email protected]>
1 parent 0966e53 commit bb818fa

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/Folder.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ class Folder {
121121
/** @var array */
122122
public array $status;
123123

124+
/** @var array */
125+
public array $attributes = [];
126+
127+
128+
const SPECIAL_ATTRIBUTES = [
129+
'haschildren' => ['\haschildren'],
130+
'hasnochildren' => ['\hasnochildren'],
131+
'template' => ['\template', '\templates'],
132+
'inbox' => ['\inbox'],
133+
'sent' => ['\sent'],
134+
'drafts' => ['\draft', '\drafts'],
135+
'archive' => ['\archive', '\archives'],
136+
'trash' => ['\trash'],
137+
'junk' => ['\junk', '\spam'],
138+
];
139+
124140
/**
125141
* Folder constructor.
126142
* @param Client $client
@@ -235,7 +251,7 @@ public function getChildren(): FolderCollection {
235251
*/
236252
protected function decodeName($name): string|array|bool|null {
237253
$parts = [];
238-
foreach (explode($this->delimiter, $name) as $item) {
254+
foreach(explode($this->delimiter, $name) as $item) {
239255
$parts[] = EncodingAliases::convert($item, "UTF7-IMAP", "UTF-8");
240256
}
241257

@@ -264,6 +280,14 @@ protected function parseAttributes($attributes): void {
264280
$this->marked = in_array('\Marked', $attributes);
265281
$this->referral = in_array('\Referral', $attributes);
266282
$this->has_children = in_array('\HasChildren', $attributes);
283+
284+
array_map(function($el) {
285+
foreach(self::SPECIAL_ATTRIBUTES as $key => $attribute) {
286+
if(in_array(strtolower($el), $attribute)){
287+
$this->attributes[] = $key;
288+
}
289+
}
290+
}, $attributes);
267291
}
268292

269293
/**
@@ -284,7 +308,7 @@ protected function parseAttributes($attributes): void {
284308
public function move(string $new_name, bool $expunge = true): array {
285309
$this->client->checkConnection();
286310
$status = $this->client->getConnection()->renameFolder($this->full_name, $new_name)->validatedData();
287-
if ($expunge) $this->client->expunge();
311+
if($expunge) $this->client->expunge();
288312

289313
$folder = $this->client->getFolder($new_name);
290314
$event = $this->getEvent("folder", "moved");
@@ -336,7 +360,7 @@ public function appendMessage(string $message, array $options = null, Carbon|str
336360
* date string that conforms to the rfc2060 specifications for a date_time value or be a Carbon object.
337361
*/
338362

339-
if ($internal_date instanceof Carbon) {
363+
if($internal_date instanceof Carbon){
340364
$internal_date = $internal_date->format('d-M-Y H:i:s O');
341365
}
342366

@@ -377,11 +401,11 @@ public function rename(string $new_name, bool $expunge = true): array {
377401
*/
378402
public function delete(bool $expunge = true): array {
379403
$status = $this->client->getConnection()->deleteFolder($this->path)->validatedData();
380-
if ($this->client->getActiveFolder() == $this->path){
404+
if($this->client->getActiveFolder() == $this->path){
381405
$this->client->setActiveFolder(null);
382406
}
383407

384-
if ($expunge) $this->client->expunge();
408+
if($expunge) $this->client->expunge();
385409

386410
$event = $this->getEvent("folder", "deleted");
387411
$event::dispatch($this);
@@ -437,7 +461,7 @@ public function unsubscribe(): array {
437461
public function idle(callable $callback, int $timeout = 300): void {
438462
$this->client->setTimeout($timeout);
439463

440-
if (!in_array("IDLE", $this->client->getConnection()->getCapabilities()->validatedData())) {
464+
if(!in_array("IDLE", $this->client->getConnection()->getCapabilities()->validatedData())){
441465
throw new Exceptions\NotSupportedCapabilityException("IMAP server does not support IDLE");
442466
}
443467

@@ -450,15 +474,15 @@ public function idle(callable $callback, int $timeout = 300): void {
450474

451475
$sequence = ClientManager::get('options.sequence', IMAP::ST_MSGN);
452476

453-
while (true) {
477+
while(true) {
454478
// This polymorphic call is fine - Protocol::idle() will throw an exception beforehand
455479
$line = $idle_client->getConnection()->nextLine(Response::empty());
456480

457-
if (($pos = strpos($line, "EXISTS")) !== false) {
481+
if(($pos = strpos($line, "EXISTS")) !== false){
458482
$msgn = (int)substr($line, 2, $pos - 2);
459483

460484
// Check if the stream is still alive or should be considered stale
461-
if (!$this->client->isConnected() || $last_action->isBefore(Carbon::now())) {
485+
if(!$this->client->isConnected() || $last_action->isBefore(Carbon::now())){
462486
// Reset the connection before interacting with it. Otherwise, the resource might be stale which
463487
// would result in a stuck interaction. If you know of a way of detecting a stale resource, please
464488
// feel free to improve this logic. I tried a lot but nothing seem to work reliably...
@@ -582,7 +606,7 @@ public function getClient(): Client {
582606
* @param $delimiter
583607
*/
584608
public function setDelimiter($delimiter): void {
585-
if (in_array($delimiter, [null, '', ' ', false]) === true) {
609+
if(in_array($delimiter, [null, '', ' ', false]) === true){
586610
$delimiter = ClientManager::get('options.delimiter', '/');
587611
}
588612

0 commit comments

Comments
 (0)