Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes and special flags #428

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ class Folder {
/** @var array */
public array $status;

/** @var array */
public array $attributes = [];


const SPECIAL_ATTRIBUTES = [
'haschildren' => ['\haschildren'],
'hasnochildren' => ['\hasnochildren'],
'template' => ['\template', '\templates'],
'inbox' => ['\inbox'],
'sent' => ['\sent'],
'drafts' => ['\draft', '\drafts'],
'archive' => ['\archive', '\archives'],
'trash' => ['\trash'],
'junk' => ['\junk', '\spam'],
];

/**
* Folder constructor.
* @param Client $client
Expand Down Expand Up @@ -235,7 +251,7 @@ public function getChildren(): FolderCollection {
*/
protected function decodeName($name): string|array|bool|null {
$parts = [];
foreach (explode($this->delimiter, $name) as $item) {
foreach(explode($this->delimiter, $name) as $item) {
$parts[] = EncodingAliases::convert($item, "UTF7-IMAP", "UTF-8");
}

Expand Down Expand Up @@ -264,6 +280,14 @@ protected function parseAttributes($attributes): void {
$this->marked = in_array('\Marked', $attributes);
$this->referral = in_array('\Referral', $attributes);
$this->has_children = in_array('\HasChildren', $attributes);

array_map(function($el) {
foreach(self::SPECIAL_ATTRIBUTES as $key => $attribute) {
if(in_array(strtolower($el), $attribute)){
$this->attributes[] = $key;
}
}
}, $attributes);
}

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

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

if ($internal_date instanceof Carbon) {
if($internal_date instanceof Carbon){
$internal_date = $internal_date->format('d-M-Y H:i:s O');
}

Expand Down Expand Up @@ -377,11 +401,11 @@ public function rename(string $new_name, bool $expunge = true): array {
*/
public function delete(bool $expunge = true): array {
$status = $this->client->getConnection()->deleteFolder($this->path)->validatedData();
if ($this->client->getActiveFolder() == $this->path){
if($this->client->getActiveFolder() == $this->path){
$this->client->setActiveFolder(null);
}

if ($expunge) $this->client->expunge();
if($expunge) $this->client->expunge();

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

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

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

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

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

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

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

Expand Down