feat: speed improvements

Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit is contained in:
Sebastian Krupinski
2026-02-20 23:34:30 -05:00
parent e51c65bf19
commit 7446edced3
37 changed files with 648 additions and 1086 deletions

View File

@@ -16,6 +16,7 @@ use Gricob\IMAP\Mime\Part\MultiPart;
use Gricob\IMAP\Mime\Part\Part;
use Gricob\IMAP\Mime\Part\SinglePart;
use Gricob\IMAP\Protocol\Command\AppendCommand;
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
use Gricob\IMAP\Protocol\Command\Argument\Search\Criteria;
use Gricob\IMAP\Protocol\Command\Argument\SequenceSet;
use Gricob\IMAP\Protocol\Command\Argument\Store\Flags;
@@ -27,6 +28,7 @@ use Gricob\IMAP\Protocol\Command\ExpungeCommand;
use Gricob\IMAP\Protocol\Command\FetchCommand;
use Gricob\IMAP\Protocol\Command\ListCommand;
use Gricob\IMAP\Protocol\Command\LogInCommand;
use Gricob\IMAP\Protocol\Command\SearchCommand;
use Gricob\IMAP\Protocol\Command\SelectCommand;
use Gricob\IMAP\Protocol\Command\StoreCommand;
use Gricob\IMAP\Protocol\Imap;
@@ -238,6 +240,43 @@ class Client
}
}
/**
* Stream every message in the currently-selected mailbox using a 1:*
* sequence set, yielding uid (or sequence number) => FetchData as each
* FETCH response arrives off the socket.
*
* @param string $mailbox Mailbox to select before fetching
* @param string[] $items IMAP FETCH data items
* @return Generator<int, FetchData>
*/
public function streamAll(
string $mailbox,
array $items = ['FLAGS', 'ENVELOPE', 'INTERNALDATE', 'RFC822.SIZE', 'BODYSTRUCTURE', 'UID'],
): Generator {
$this->select($mailbox);
$gen = $this->imap->sendStreaming(
new FetchCommand(
$this->configuration->useUid,
SequenceSet::all(),
$items,
)
);
foreach ($gen as $line) {
if (!$line instanceof FetchData) {
continue;
}
$id = $line->id;
if ($this->configuration->useUid) {
$id = $line->uid ?? throw new RuntimeException('Unable to get uid from message ' . $line->id);
}
yield $id => $line;
}
}
/**
* Stream messages from a sequence range as a Generator, yielding each
* LazyMessage as soon as its FETCH response line arrives off the socket —
@@ -378,6 +417,75 @@ class Client
$this->send(new CreateCommand($name));
}
/** Delete a mailbox by name. */
public function deleteMailbox(string $name): void
{
$this->send(new Command('DELETE', new QuotedString($name)));
}
/** Rename a mailbox. */
public function renameMailbox(string $oldName, string $newName): void
{
$this->send(new Command('RENAME', new QuotedString($oldName), new QuotedString($newName)));
}
/**
* Copy messages to a destination mailbox.
*
* @param int[] $uids
*/
public function copyMessages(string $mailbox, array $uids, string $destination): void
{
$this->select($mailbox);
$this->send(new Command('UID COPY', new SequenceSet(...$uids), new QuotedString($destination)));
}
/**
* Set, add, or remove flags on a set of messages in a single round-trip.
*
* @param string $action '+' to add, '-' to remove, '' to replace
* @param string[] $flags e.g. ['\\Seen', '\\Flagged']
* @param int[] $uids
*/
public function storeFlags(string $mailbox, array $uids, string $action, array $flags): void
{
$this->select($mailbox);
$this->send(new StoreCommand(
$this->configuration->useUid,
new SequenceSet(...$uids),
new Flags($flags, $action),
));
}
/**
* Permanently delete messages by UID (marks \\Deleted then EXPUNGEs).
*
* @param int[] $uids
*/
public function deleteMessages(string $mailbox, array $uids): void
{
$this->storeFlags($mailbox, $uids, '+', ['\\Deleted']);
$this->send(new ExpungeCommand());
}
/**
* Search a mailbox with the given criteria and return matching UIDs (or
* sequence numbers when useUid is false).
*
* @param Criteria[] $criteria Pass no criteria to match ALL messages.
* @return int[]
*/
public function searchMessages(string $mailbox, array $criteria = []): array
{
$this->select($mailbox);
$response = $this->send(new SearchCommand($this->configuration->useUid, ...$criteria));
$ids = [];
foreach ($response->getData(SearchData::class) as $searchData) {
array_push($ids, ...$searchData->numbers);
}
return $ids;
}
/**
* @param list<string>|null $flags
*/
@@ -412,7 +520,7 @@ class Client
public function doSearch(array $criteria, ?PreFetchOptions $preFetchOptions = null): array
{
$response = $this->send(
new Protocol\Command\SearchCommand(
new SearchCommand(
$this->configuration->useUid,
...$criteria
)