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'; $sSourceINBOX = 'INBOX';
\SnappyMail\HTTP\Stream::start(); $isCli = false !== \stripos(\php_sapi_name(), 'cli');
\SnappyMail\HTTP\Stream::JSON([ if ($isCli) {
'folders' => \count($aSourceFolders) echo 'folders: ' . \count($aSourceFolders) . "\n";
]); } else {
\SnappyMail\HTTP\Stream::start();
\SnappyMail\HTTP\Stream::JSON([
'folders' => \count($aSourceFolders)
]);
}
$fi = 0; $fi = 0;
\ignore_user_abort(true); \ignore_user_abort(true);
foreach ($aSourceFolders as $sSourceFolderName => $oImapFolder) { foreach ($aSourceFolders as $sSourceFolderName => $oImapFolder) {
++$fi; ++$fi;
\SnappyMail\HTTP\Stream::JSON([ if ($isCli) {
'index' => $fi, echo \str_pad($fi, 3, ' ', STR_PAD_LEFT) . " folder: {$sSourceFolderName}\n";
'folder' => $sSourceFolderName } else {
]); \SnappyMail\HTTP\Stream::JSON([
'index' => $fi,
'folder' => $sSourceFolderName
]);
}
if ($oImapFolder->IsSelectable()) { if ($oImapFolder->IsSelectable()) {
if ($oImapFolder->IsInbox()) { if ($oImapFolder->IsInbox()) {
$sSourceINBOX = $sSourceFolderName; $sSourceINBOX = $sSourceFolderName;
@ -90,10 +99,15 @@ class Sync
$oSourceInfo = $this->oImapSource->FolderSelect($sSourceFolderName); $oSourceInfo = $this->oImapSource->FolderSelect($sSourceFolderName);
if ($oSourceInfo->MESSAGES) { if ($oSourceInfo->MESSAGES) {
\SnappyMail\HTTP\Stream::JSON([ if ($isCli) {
'index' => $fi, echo \str_pad($fi, 3, ' ', STR_PAD_LEFT)
'messages' => $oSourceInfo->MESSAGES . " messages: [" . \str_repeat(' ', 50) . "] 0/{$oSourceInfo->MESSAGES}";
]); } else {
\SnappyMail\HTTP\Stream::JSON([
'index' => $fi,
'messages' => $oSourceInfo->MESSAGES
]);
}
// All id's to skip from source // All id's to skip from source
$oTargetInfo = $this->oImapTarget->FolderSelect($sTargetFolderName); $oTargetInfo = $this->oImapTarget->FolderSelect($sTargetFolderName);
if ($oTargetInfo->MESSAGES) { if ($oTargetInfo->MESSAGES) {
@ -175,10 +189,23 @@ class Sync
*/ */
} }
\SnappyMail\HTTP\Stream::JSON([ if ($isCli) {
'index' => $fi, // Clear line
'message' => $i 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; $type = SecurityType::AUTO_DETECT;
if (isset($options["tls{$host}"])) { if (isset($options["tls{$host}"])) {
$type = SecurityType::STARTTLS; $type = SecurityType::STARTTLS;
} } else if (isset($options["ssl{$host}"])) {
else if (isset($options["ssl{$host}"])) {
$type = SecurityType::SSL; $type = SecurityType::SSL;
} } else if (isset($options["notls{$host}"])) {
else if (isset($options["notls{$host}"])) {
$type = SecurityType::NONE; $type = SecurityType::NONE;
} }
$ImapSettings = \MailSo\Imap\Settings::fromArray([ $ImapSettings = \MailSo\Imap\Settings::fromArray([
@ -87,6 +85,8 @@ function getImapClient(int $host)
]); ]);
if (993 === $ImapSettings->port) { if (993 === $ImapSettings->port) {
$ImapSettings->type = SecurityType::SSL; $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)) { 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'); \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; $ImapSettings->useAuth = true;
$oImapClient = new \MailSo\Imap\ImapClient; $oImapClient = new \MailSo\Imap\ImapClient;
//$oImapTarget->SetLogger($this->Logger()); // $oAccount = new \RainLoop\Model\Account;
$oImapClient->__FORCE_SELECT_ON_EXAMINE__ = !!$oConfig->Get('imap', 'use_force_selection'); $oImapClient->SetLogger(\RainLoop\API::Logger());
$oImapClient->__DISABLE_METADATA = !!$oConfig->Get('imap', 'disable_metadata'); // $oPlugins->RunHook('imap.before-connect', array($oAccount, $oImapClient, $ImapSettings));
//$oPlugins->RunHook('imap.before-connect', array($this, $oImapClient, $ImapSettings));
$oImapClient->Connect($ImapSettings); $oImapClient->Connect($ImapSettings);
//$oPlugins->RunHook('imap.after-connect', array($this, $oImapClient, $ImapSettings)); // $oPlugins->RunHook('imap.after-connect', array($oAccount, $oImapClient, $ImapSettings));
//$oPlugins->RunHook('imap.before-login', array($this, $oImapClient, $ImapSettings)); // $oPlugins->RunHook('imap.before-login', array($oAccount, $oImapClient, $ImapSettings));
$oImapClient->Login($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; return $oImapClient;
} }