Move ImapClient::Message* functions to trait

This commit is contained in:
djmaze 2022-01-06 11:28:57 +01:00
parent 7dec51a364
commit a228f81ece
2 changed files with 111 additions and 117 deletions

View file

@ -13,6 +13,7 @@
namespace MailSo\Imap\Commands;
use MailSo\Imap\ResponseCollection;
use MailSo\Imap\SequenceSet;
use MailSo\Imap\Enumerations\ResponseType;
/**
@ -21,6 +22,116 @@ use MailSo\Imap\Enumerations\ResponseType;
*/
trait Messages
{
/**
* Appends message to specified folder
*
* @param resource $rMessageAppendStream
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageAppendStream(string $sFolderName, $rMessageAppendStream, int $iStreamSize, array $aAppendFlags = null, int &$iUid = null, int $iDateTime = 0) : ?int
{
$aData = array($this->EscapeFolderName($sFolderName), $aAppendFlags);
if (0 < $iDateTime) {
$aData[] = $this->EscapeString(\gmdate('d-M-Y H:i:s', $iDateTime).' +0000');
}
$aData[] = '{'.$iStreamSize.'}';
$this->SendRequestGetResponse('APPEND', $aData);
$this->writeLog('Write to connection stream', \MailSo\Log\Enumerations\Type::NOTE);
\MailSo\Base\Utils::MultipleStreamWriter($rMessageAppendStream, array($this->ConnectionResource()));
$this->sendRaw('');
$oResponse = $this->getResponse();
if (null !== $iUid) {
$oLast = $oResponse->getLast();
if ($oLast
&& ResponseType::TAGGED === $oLast->ResponseType
&& \is_array($oLast->OptionalResponse)
&& !empty($oLast->OptionalResponse[2])
&& \is_numeric($oLast->OptionalResponse[2])
&& 'APPENDUID' === \strtoupper($oLast->OptionalResponse[0])
) {
$iUid = (int) $oLast->OptionalResponse[2];
}
}
return $iUid;
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageCopy(string $sToFolder, SequenceSet $oRange) : ResponseCollection
{
if (!\count($oRange)) {
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException,
\MailSo\Log\Enumerations\Type::ERROR, true);
}
return $this->SendRequestGetResponse(
$oRange->UID ? 'UID COPY' : 'COPY',
array((string) $oRange, $this->EscapeFolderName($sToFolder))
);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageMove(string $sToFolder, SequenceSet $oRange) : ResponseCollection
{
if (!\count($oRange)) {
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException,
\MailSo\Log\Enumerations\Type::ERROR, true);
}
if (!$this->IsSupported('MOVE')) {
$this->writeLogException(
new Exceptions\RuntimeException('Move is not supported'),
\MailSo\Log\Enumerations\Type::ERROR, true);
}
return $this->SendRequestGetResponse(
$oRange->UID ? 'UID MOVE' : 'MOVE',
array((string) $oRange, $this->EscapeFolderName($sToFolder))
);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageStoreFlag(SequenceSet $oRange, array $aInputStoreItems, string $sStoreAction) : ?ResponseCollection
{
if (!\count($oRange) || !\strlen(\trim($sStoreAction)) || !\count($aInputStoreItems)) {
return null;
}
/**
* TODO:
* https://datatracker.ietf.org/doc/html/rfc4551#section-3.2
* $sStoreAction[] = (UNCHANGEDSINCE $modsequence)
*/
return $this->SendRequestGetResponse(
$oRange->UID ? 'UID STORE' : 'STORE',
array((string) $oRange, $sStoreAction, $aInputStoreItems)
);
}
/**
* See https://tools.ietf.org/html/rfc5256
* @throws \MailSo\Base\Exceptions\InvalidArgumentException

View file

@ -898,51 +898,6 @@ class ImapClient extends \MailSo\Net\NetClient
return $aReturn;
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageCopy(string $sToFolder, SequenceSet $oRange) : self
{
if (!\count($oRange))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException,
\MailSo\Log\Enumerations\Type::ERROR, true);
}
$this->SendRequestGetResponse($oRange->UID ? 'UID COPY' : 'COPY',
array((string) $oRange, $this->EscapeFolderName($sToFolder)));
return $this;
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageMove(string $sToFolder, SequenceSet $oRange) : self
{
if (!\count($oRange))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException,
\MailSo\Log\Enumerations\Type::ERROR, true);
}
if (!$this->IsSupported('MOVE'))
{
$this->writeLogException(
new Exceptions\RuntimeException('Move is not supported'),
\MailSo\Log\Enumerations\Type::ERROR, true);
}
$this->SendRequestGetResponse($oRange->UID ? 'UID MOVE' : 'MOVE',
array((string) $oRange, $this->EscapeFolderName($sToFolder)));
return $this;
}
/**
* The EXPUNGE command permanently removes all messages that have the
* \Deleted flag set from the currently selected mailbox.
@ -964,78 +919,6 @@ class ImapClient extends \MailSo\Net\NetClient
return $this;
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageStoreFlag(SequenceSet $oRange, array $aInputStoreItems, string $sStoreAction) : self
{
if (!\count($oRange) ||
!\strlen(\trim($sStoreAction)) ||
!\count($aInputStoreItems))
{
return false;
}
/**
* TODO:
* https://datatracker.ietf.org/doc/html/rfc4551#section-3.2
* $sStoreAction[] = (UNCHANGEDSINCE $modsequence)
*/
$this->SendRequestGetResponse(
$oRange->UID ? 'UID STORE' : 'STORE',
array((string) $oRange, $sStoreAction, $aInputStoreItems)
);
return $this;
}
/**
* @param resource $rMessageAppendStream
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageAppendStream(string $sFolderName, $rMessageAppendStream, int $iStreamSize, array $aAppendFlags = null, int &$iUid = null, int $iDateTime = 0) : self
{
$aData = array($this->EscapeFolderName($sFolderName), $aAppendFlags);
if (0 < $iDateTime)
{
$aData[] = $this->EscapeString(\gmdate('d-M-Y H:i:s', $iDateTime).' +0000');
}
$aData[] = '{'.$iStreamSize.'}';
$this->SendRequestGetResponse('APPEND', $aData);
$this->writeLog('Write to connection stream', \MailSo\Log\Enumerations\Type::NOTE);
\MailSo\Base\Utils::MultipleStreamWriter($rMessageAppendStream, array($this->ConnectionResource()));
$this->sendRaw('');
$oResponse = $this->getResponse();
if (null !== $iUid)
{
$oLast = $oResponse->getLast();
if ($oLast && Enumerations\ResponseType::TAGGED === $oLast->ResponseType && \is_array($oLast->OptionalResponse))
{
if (\strlen($oLast->OptionalResponse[0]) &&
\strlen($oLast->OptionalResponse[2]) &&
'APPENDUID' === strtoupper($oLast->OptionalResponse[0]) &&
\is_numeric($oLast->OptionalResponse[2])
)
{
$iUid = (int) $oLast->OptionalResponse[2];
}
}
}
return $this;
}
public function FolderCurrentInformation() : ?FolderInformation
{
return $this->oCurrentFolderInfo;