From 9c3fe7d4e56a82d6a1dad6ee9ef07c00728c2c9a Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 12 Dec 2022 09:40:39 +0100 Subject: [PATCH] imapsync echo progress to CLI #744 --- .../app/libraries/snappymail/imap/sync.php | 59 ++++++++++++++----- snappymail/v/0.0.0/imapsync.php | 21 ++++--- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/imap/sync.php b/snappymail/v/0.0.0/app/libraries/snappymail/imap/sync.php index 4c0e33379..bbf53894b 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/imap/sync.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/imap/sync.php @@ -46,19 +46,28 @@ class Sync } $sSourceINBOX = 'INBOX'; - \SnappyMail\HTTP\Stream::start(); - \SnappyMail\HTTP\Stream::JSON([ - 'folders' => \count($aSourceFolders) - ]); + $isCli = false !== \stripos(\php_sapi_name(), 'cli'); + if ($isCli) { + echo 'folders: ' . \count($aSourceFolders) . "\n"; + } else { + \SnappyMail\HTTP\Stream::start(); + \SnappyMail\HTTP\Stream::JSON([ + 'folders' => \count($aSourceFolders) + ]); + } $fi = 0; \ignore_user_abort(true); foreach ($aSourceFolders as $sSourceFolderName => $oImapFolder) { ++$fi; - \SnappyMail\HTTP\Stream::JSON([ - 'index' => $fi, - 'folder' => $sSourceFolderName - ]); + if ($isCli) { + echo \str_pad($fi, 3, ' ', STR_PAD_LEFT) . " folder: {$sSourceFolderName}\n"; + } else { + \SnappyMail\HTTP\Stream::JSON([ + 'index' => $fi, + 'folder' => $sSourceFolderName + ]); + } if ($oImapFolder->IsSelectable()) { if ($oImapFolder->IsInbox()) { $sSourceINBOX = $sSourceFolderName; @@ -90,10 +99,15 @@ class Sync $oSourceInfo = $this->oImapSource->FolderSelect($sSourceFolderName); if ($oSourceInfo->MESSAGES) { - \SnappyMail\HTTP\Stream::JSON([ - 'index' => $fi, - 'messages' => $oSourceInfo->MESSAGES - ]); + if ($isCli) { + echo \str_pad($fi, 3, ' ', STR_PAD_LEFT) + . " messages: [" . \str_repeat(' ', 50) . "] 0/{$oSourceInfo->MESSAGES}"; + } else { + \SnappyMail\HTTP\Stream::JSON([ + 'index' => $fi, + 'messages' => $oSourceInfo->MESSAGES + ]); + } // All id's to skip from source $oTargetInfo = $this->oImapTarget->FolderSelect($sTargetFolderName); if ($oTargetInfo->MESSAGES) { @@ -175,10 +189,23 @@ class Sync */ } - \SnappyMail\HTTP\Stream::JSON([ - 'index' => $fi, - 'message' => $i - ]); + if ($isCli) { + // Clear line + echo "\x1b[2K\x1b[1G"; + // Echo same line with progress + $p = \floor(50 * $i / $oSourceInfo->MESSAGES); + echo \str_pad($fi, 3, ' ', STR_PAD_LEFT) + . " messages: [" . \str_repeat('=', $p) . \str_repeat(' ', 50 - $p) + . "] {$i}/{$oSourceInfo->MESSAGES}"; + } else { + \SnappyMail\HTTP\Stream::JSON([ + 'index' => $fi, + 'message' => $i + ]); + } + } + if ($isCli) { + echo "\n"; } } } diff --git a/snappymail/v/0.0.0/imapsync.php b/snappymail/v/0.0.0/imapsync.php index eb0e82a5d..52871cb2d 100755 --- a/snappymail/v/0.0.0/imapsync.php +++ b/snappymail/v/0.0.0/imapsync.php @@ -72,11 +72,9 @@ function getImapClient(int $host) $type = SecurityType::AUTO_DETECT; if (isset($options["tls{$host}"])) { $type = SecurityType::STARTTLS; - } - else if (isset($options["ssl{$host}"])) { + } else if (isset($options["ssl{$host}"])) { $type = SecurityType::SSL; - } - else if (isset($options["notls{$host}"])) { + } else if (isset($options["notls{$host}"])) { $type = SecurityType::NONE; } $ImapSettings = \MailSo\Imap\Settings::fromArray([ @@ -87,6 +85,8 @@ function getImapClient(int $host) ]); if (993 === $ImapSettings->port) { $ImapSettings->type = SecurityType::SSL; + } else if (143 === $ImapSettings->port && SecurityType::SSL == $ImapSettings->type) { + $ImapSettings->port = 993; } if ($oConfig->Get('labs', 'sasl_allow_scram_sha', false)) { \array_push($ImapSettings->SASLMechanisms, 'SCRAM-SHA3-512', 'SCRAM-SHA-512', 'SCRAM-SHA-256', 'SCRAM-SHA-1'); @@ -104,15 +104,14 @@ function getImapClient(int $host) $ImapSettings->useAuth = true; $oImapClient = new \MailSo\Imap\ImapClient; - //$oImapTarget->SetLogger($this->Logger()); - $oImapClient->__FORCE_SELECT_ON_EXAMINE__ = !!$oConfig->Get('imap', 'use_force_selection'); - $oImapClient->__DISABLE_METADATA = !!$oConfig->Get('imap', 'disable_metadata'); - //$oPlugins->RunHook('imap.before-connect', array($this, $oImapClient, $ImapSettings)); +// $oAccount = new \RainLoop\Model\Account; + $oImapClient->SetLogger(\RainLoop\API::Logger()); +// $oPlugins->RunHook('imap.before-connect', array($oAccount, $oImapClient, $ImapSettings)); $oImapClient->Connect($ImapSettings); - //$oPlugins->RunHook('imap.after-connect', array($this, $oImapClient, $ImapSettings)); - //$oPlugins->RunHook('imap.before-login', array($this, $oImapClient, $ImapSettings)); +// $oPlugins->RunHook('imap.after-connect', array($oAccount, $oImapClient, $ImapSettings)); +// $oPlugins->RunHook('imap.before-login', array($oAccount, $oImapClient, $ImapSettings)); $oImapClient->Login($ImapSettings); - //$oPlugins->RunHook('imap.after-login', array($this, $oImapClient, $bResult, $ImapSettings)); +// $oPlugins->RunHook('imap.after-login', array($oAccount, $oImapClient, $bResult, $ImapSettings)); return $oImapClient; }