Improved MailClient::FolderDelete and MailClient::MessageSetFlag

This commit is contained in:
djmaze 2022-01-07 12:14:50 +01:00
parent 69ac75959c
commit 81bcc99426
2 changed files with 39 additions and 111 deletions

View file

@ -12,6 +12,7 @@
namespace MailSo\Mail; namespace MailSo\Mail;
use MailSo\Imap\Enumerations\FolderResponseStatus; use MailSo\Imap\Enumerations\FolderResponseStatus;
use MailSo\Imap\Enumerations\MessageFlag;
use MailSo\Imap\Enumerations\StoreAction; use MailSo\Imap\Enumerations\StoreAction;
use MailSo\Imap\SequenceSet; use MailSo\Imap\SequenceSet;
@ -116,41 +117,6 @@ class MailClient
// return \MailSo\Imap\Enumerations\FetchType::ENVELOPE; // return \MailSo\Imap\Enumerations\FetchType::ENVELOPE;
} }
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
* @throws \MailSo\Mail\Exceptions\Exception
*/
public function MessageSetFlagToAll(string $sFolderName, string $sMessageFlag, bool $bSetAction = true, bool $bSkipUnsupportedFlag = false, ?array $aCustomUids = null) : void
{
$oFolderInfo = $this->oImapClient->FolderSelect($sFolderName);
if (!$oFolderInfo->IsFlagSupported($sMessageFlag))
{
if (!$bSkipUnsupportedFlag)
{
throw new Exceptions\RuntimeException('Message flag "'.$sMessageFlag.'" is not supported.');
}
}
if ($oFolderInfo && 0 < $oFolderInfo->MESSAGES)
{
$oRange = null;
if (\is_array($aCustomUids)) {
if (\count($aCustomUids)) {
$oRange = new SequenceSet($aCustomUids);
}
} else {
$oRange = new SequenceSet('1:*', false);
}
if ($oRange) {
$sStoreAction = $bSetAction ? StoreAction::ADD_FLAGS_SILENT : StoreAction::REMOVE_FLAGS_SILENT;
$this->oImapClient->MessageStoreFlag($oRange, array($sMessageFlag), $sStoreAction);
}
}
}
/** /**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
@ -159,51 +125,15 @@ class MailClient
*/ */
public function MessageSetFlag(string $sFolderName, SequenceSet $oRange, string $sMessageFlag, bool $bSetAction = true, bool $bSkipUnsupportedFlag = false) : void public function MessageSetFlag(string $sFolderName, SequenceSet $oRange, string $sMessageFlag, bool $bSetAction = true, bool $bSkipUnsupportedFlag = false) : void
{ {
$oFolderInfo = $this->oImapClient->FolderSelect($sFolderName); if (\count($oRange)) {
if (!$oFolderInfo->IsFlagSupported($sMessageFlag)) $oFolderInfo = $this->oImapClient->FolderSelect($sFolderName);
{ if ($oFolderInfo->IsFlagSupported($sMessageFlag)) {
if (!$bSkipUnsupportedFlag) $sStoreAction = $bSetAction ? StoreAction::ADD_FLAGS_SILENT : StoreAction::REMOVE_FLAGS_SILENT;
{ $this->oImapClient->MessageStoreFlag($oRange, array($sMessageFlag), $sStoreAction);
} else if (!$bSkipUnsupportedFlag) {
throw new Exceptions\RuntimeException('Message flag "'.$sMessageFlag.'" is not supported.'); throw new Exceptions\RuntimeException('Message flag "'.$sMessageFlag.'" is not supported.');
} }
} }
else
{
$sStoreAction = $bSetAction ? StoreAction::ADD_FLAGS_SILENT : StoreAction::REMOVE_FLAGS_SILENT;
$this->oImapClient->MessageStoreFlag($oRange, array($sMessageFlag), $sStoreAction);
}
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageSetFlagged(string $sFolderName, SequenceSet $oRange, bool $bSetAction = true, bool $bSkipUnsupportedFlag = false) : void
{
$this->MessageSetFlag($sFolderName, $oRange,
\MailSo\Imap\Enumerations\MessageFlag::FLAGGED, $bSetAction, $bSkipUnsupportedFlag);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageSetSeenToAll(string $sFolderName, bool $bSetAction = true, array $aCustomUids = null) : void
{
$this->MessageSetFlagToAll($sFolderName, \MailSo\Imap\Enumerations\MessageFlag::SEEN, $bSetAction, true, $aCustomUids);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageSetSeen(string $sFolderName, SequenceSet $oRange, bool $bSetAction = true) : void
{
$this->MessageSetFlag($sFolderName, $oRange,
\MailSo\Imap\Enumerations\MessageFlag::SEEN, $bSetAction, true);
} }
/** /**
@ -382,7 +312,7 @@ class MailClient
$this->oImapClient->FolderSelect($sFolder); $this->oImapClient->FolderSelect($sFolder);
$this->oImapClient->MessageStoreFlag($oRange, $this->oImapClient->MessageStoreFlag($oRange,
array(\MailSo\Imap\Enumerations\MessageFlag::DELETED), array(MessageFlag::DELETED),
StoreAction::ADD_FLAGS_SILENT StoreAction::ADD_FLAGS_SILENT
); );
@ -571,7 +501,7 @@ class MailClient
$aFlags = \array_map('strtolower', $oFetchResponse->GetFetchValue( $aFlags = \array_map('strtolower', $oFetchResponse->GetFetchValue(
\MailSo\Imap\Enumerations\FetchType::FLAGS)); \MailSo\Imap\Enumerations\FetchType::FLAGS));
if (!\in_array(\strtolower(\MailSo\Imap\Enumerations\MessageFlag::SEEN), $aFlags)) if (!\in_array(\strtolower(MessageFlag::SEEN), $aFlags))
{ {
$iUid = (int) $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::UID); $iUid = (int) $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::UID);
$sHeaders = $oFetchResponse->GetHeaderFieldsValue(); $sHeaders = $oFetchResponse->GetHeaderFieldsValue();
@ -1344,26 +1274,24 @@ class MailClient
*/ */
public function FolderDelete(string $sFolderFullName, bool $bUnsubscribeOnDeletion = true) : self public function FolderDelete(string $sFolderFullName, bool $bUnsubscribeOnDeletion = true) : self
{ {
if (!\strlen($sFolderFullName) || 'INBOX' === $sFolderFullName) if (!\strlen($sFolderFullName) || 'INBOX' === $sFolderFullName) {
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException; throw new \MailSo\Base\Exceptions\InvalidArgumentException;
} }
$this->oImapClient->FolderExamine($sFolderFullName); if ($this->IsSupported('IMAP4rev2')) {
$oInfo = $this->oImapClient->FolderExamine($sFolderFullName);
$aIndexOrUids = $this->oImapClient->MessageSimpleSearch('ALL'); } else {
if (\count($aIndexOrUids)) $oInfo = $this->oImapClient->FolderStatus($sFolderFullName);
{ }
if ($oInfo->MESSAGES) {
throw new Exceptions\NonEmptyFolder; throw new Exceptions\NonEmptyFolder;
} }
$this->oImapClient->FolderExamine('INBOX'); if ($bUnsubscribeOnDeletion) {
if ($bUnsubscribeOnDeletion)
{
$this->oImapClient->FolderUnsubscribe($sFolderFullName); $this->oImapClient->FolderUnsubscribe($sFolderFullName);
} }
$this->oImapClient->FolderUnselect();
$this->oImapClient->FolderDelete($sFolderFullName); $this->oImapClient->FolderDelete($sFolderFullName);
return $this; return $this;
@ -1378,7 +1306,7 @@ class MailClient
if (0 < $oFolderInformation->MESSAGES) if (0 < $oFolderInformation->MESSAGES)
{ {
$this->oImapClient->MessageStoreFlag(new SequenceSet('1:*', false), $this->oImapClient->MessageStoreFlag(new SequenceSet('1:*', false),
array(\MailSo\Imap\Enumerations\MessageFlag::DELETED), array(MessageFlag::DELETED),
StoreAction::ADD_FLAGS_SILENT StoreAction::ADD_FLAGS_SILENT
); );

View file

@ -7,6 +7,7 @@ use RainLoop\Exceptions\ClientException;
use RainLoop\Model\Account; use RainLoop\Model\Account;
use RainLoop\Notifications; use RainLoop\Notifications;
use MailSo\Imap\SequenceSet; use MailSo\Imap\SequenceSet;
use MailSo\Imap\Enumerations\MessageFlag;
trait Messages trait Messages
{ {
@ -114,9 +115,8 @@ trait Messages
$iNewUid = 0; $iNewUid = 0;
$this->MailClient()->MessageAppendStream( $this->MailClient()->MessageAppendStream(
$rMessageStream, $iMessageStreamSize, $sDraftFolder, array( $rMessageStream, $iMessageStreamSize, $sDraftFolder, array(MessageFlag::SEEN), $iNewUid
\MailSo\Imap\Enumerations\MessageFlag::SEEN );
), $iNewUid);
if (!empty($sMessageId) && (null === $iNewUid || 0 === $iNewUid)) if (!empty($sMessageId) && (null === $iNewUid || 0 === $iNewUid))
{ {
@ -186,7 +186,7 @@ trait Messages
case 'reply': case 'reply':
case 'reply-all': case 'reply-all':
$this->MailClient()->MessageSetFlag($sDraftInfoFolder, new SequenceSet($iDraftInfoUid), $this->MailClient()->MessageSetFlag($sDraftInfoFolder, new SequenceSet($iDraftInfoUid),
\MailSo\Imap\Enumerations\MessageFlag::ANSWERED); MessageFlag::ANSWERED);
break; break;
case 'forward': case 'forward':
$sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', ''); $sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', '');
@ -219,9 +219,7 @@ trait Messages
array($oAccount, &$rMessageStream, &$iMessageStreamSize)); array($oAccount, &$rMessageStream, &$iMessageStreamSize));
$this->MailClient()->MessageAppendStream( $this->MailClient()->MessageAppendStream(
$rMessageStream, $iMessageStreamSize, $sSentFolder, array( $rMessageStream, $iMessageStreamSize, $sSentFolder, array(MessageFlag::SEEN)
\MailSo\Imap\Enumerations\MessageFlag::SEEN
)
); );
} }
else else
@ -235,9 +233,8 @@ trait Messages
array($oAccount, &$rAppendMessageStream, &$iAppendMessageStreamSize)); array($oAccount, &$rAppendMessageStream, &$iAppendMessageStreamSize));
$this->MailClient()->MessageAppendStream( $this->MailClient()->MessageAppendStream(
$rAppendMessageStream, $iAppendMessageStreamSize, $sSentFolder, array( $rAppendMessageStream, $iAppendMessageStreamSize, $sSentFolder, array(MessageFlag::SEEN)
\MailSo\Imap\Enumerations\MessageFlag::SEEN );
));
if (\is_resource($rAppendMessageStream)) if (\is_resource($rAppendMessageStream))
{ {
@ -391,21 +388,23 @@ trait Messages
public function DoMessageSetSeen() : array public function DoMessageSetSeen() : array
{ {
return $this->messageSetFlag('MessageSetSeen', __FUNCTION__); return $this->messageSetFlag(MessageFlag::SEEN, __FUNCTION__);
} }
public function DoMessageSetSeenToAll() : array public function DoMessageSetSeenToAll() : array
{ {
$this->initMailClientConnection(); $this->initMailClientConnection();
$sFolder = $this->GetActionParam('Folder', '');
$bSetAction = '1' === (string) $this->GetActionParam('SetAction', '0');
$sThreadUids = \trim($this->GetActionParam('ThreadUids', '')); $sThreadUids = \trim($this->GetActionParam('ThreadUids', ''));
try try
{ {
$this->MailClient()->MessageSetSeenToAll($sFolder, $bSetAction, $this->MessageSetFlag(
!empty($sThreadUids) ? explode(',', $sThreadUids) : null); $this->GetActionParam('Folder', ''),
empty($sThreadUids) ? new SequenceSet('1:*', false) : new SequenceSet(\explode(',', $sThreadUids)),
MessageFlag::SEEN,
!empty($this->GetActionParam('SetAction', '0'))
);
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
@ -417,7 +416,7 @@ trait Messages
public function DoMessageSetFlagged() : array public function DoMessageSetFlagged() : array
{ {
return $this->messageSetFlag('MessageSetFlagged', __FUNCTION__); return $this->messageSetFlag(MessageFlag::FLAGGED, __FUNCTION__, true);
} }
/** /**
@ -530,7 +529,7 @@ trait Messages
{ {
try try
{ {
$this->MailClient()->MessageSetSeen($sFromFolder, $oUids); $this->MailClient()->MessageSetFlag($sFromFolder, $oUids, MessageFlag::SEEN);
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
@ -799,17 +798,18 @@ trait Messages
} }
} }
private function messageSetFlag(string $sActionFunction, string $sResponseFunction) : array private function messageSetFlag(string $sMessageFlag, string $sResponseFunction, bool $bSkipUnsupportedFlag = false) : array
{ {
$this->initMailClientConnection(); $this->initMailClientConnection();
try try
{ {
$this->MailClient()->{$sActionFunction}( $this->MailClient()->MessageSetFlag(
$this->GetActionParam('Folder', ''), $this->GetActionParam('Folder', ''),
new SequenceSet(\explode(',', (string) $this->GetActionParam('Uids', ''))), new SequenceSet(\explode(',', (string) $this->GetActionParam('Uids', ''))),
$sMessageFlag,
!empty($this->GetActionParam('SetAction', '0')), !empty($this->GetActionParam('SetAction', '0')),
true $bSkipUnsupportedFlag
); );
} }
catch (\Throwable $oException) catch (\Throwable $oException)