imapsync echo progress to CLI #744

This commit is contained in:
the-djmaze 2022-12-12 09:40:39 +01:00
parent 48712ac13f
commit 9c3fe7d4e5
2 changed files with 53 additions and 27 deletions

View file

@ -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";
}
}
}

View file

@ -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;
}