Added more imapsync CLI options and help

This commit is contained in:
the-djmaze 2022-12-16 09:32:10 +01:00
parent b3fa14f017
commit 26706acf6a

View file

@ -13,7 +13,71 @@ imapsync.php \
use MailSo\Net\Enumerations\ConnectionSecurityType as SecurityType;
if (4 > \count($_SERVER['argv']) || \in_array('--help', $_SERVER['argv'])) {
echo 'OPTIONS
usage: imapsync.php [options]
The standard options are the six values forming the credentials. Three
values on each side are needed to login into the IMAP servers. These six
values are a hostname, a username, and a password, two times.
OPTIONS/credentials
--host1 : Source or "from" imap server.
--port1 : Port to connect on host1.
Optional since default ports are the
well known ports imap/143 or imaps/993.
--user1 : User to login on host1.
--password1 : Password of user1.
--host2 : "destination" imap server.
--port2 : Port to connect on host2. Optional
--user2 : User to login on host2.
--password2 : Password of user2.
If you don\'t pass the user1 password via --password1 then imapsync will
prompt to enter the password on the terminal. Same thing for user2 password.
OPTIONS/encryption
--nossl1 : Do not use a SSL connection on host1.
--ssl1 : Use a SSL connection on host1. On by default if possible.
--nossl2 : Do not use a SSL connection on host2.
--ssl2 : Use a SSL connection on host2. On by default if possible.
--notls1 : Do not use a TLS connection on host1.
--tls1 : Use a TLS connection on host1. On by default if possible.
--notls2 : Do not use a TLS connection on host2.
--tls2 : Use a TLS connection on host2. On by default if possible.
OPTIONS/folders
--rootfolder : prepend this name to each target folder.
Like "INBOX" becomes "test.INBOX"
OPTIONS/behavior
--timeout1 : Connection timeout in seconds for host1.
Default is 300 and 0 means no timeout at all.
--timeout2 : Connection timeout in seconds for host2.
Default is 300 and 0 means no timeout at all.
--justconnect : Just connect to both servers and print useful information.
--justlogin : Just login to both host1 and host2 with users
credentials, then exit.
--help : print this help.
';
exit;
}
// php.net/getopt doesn't allow spaced values like `--host1 example.com`
// and we also ask on the command line when value is empty
function get_opt(string $short_options, array $long_options) {
$opts = [];
foreach ($long_options as $option) {
@ -23,12 +87,27 @@ function get_opt(string $short_options, array $long_options) {
$required = 1 === \substr_count($option, ':');
foreach ($_SERVER['argv'] as $i => $v) {
if ($v === $arg) {
$opts[$key] = ($required || $optional) ? $_SERVER['argv'][$i+1] : true;
}
if (($required || $optional) && 0 === \strpos($v, "{$arg}=")) {
if ($required || $optional) {
$opts[$key] = '';
if (!empty($_SERVER['argv'][$i+1]) && '--' != \substr($_SERVER['argv'][$i+1], 0, 2)) {
$opts[$key] = $_SERVER['argv'][$i+1];
}
} else {
$opts[$key] = true;
}
} else if (($required || $optional) && 0 === \strpos($v, "{$arg}=")) {
$opts[$key] = \substr($v, 1+\strlen($arg));
}
}
// When empty, prompt on command line for value
if (($required || $optional) && isset($opts[$key]) && !\strlen($opts[$key])) {
if (\is_callable('readline')) {
$opts[$key] = \readline("{$key}: ");
} else {
echo "{$key}: ";
$opts[$key] = \stream_get_line(STDIN, 1024, PHP_EOL);
}
}
if ($required && !isset($opts[$key])) {
exit("Missing required argument {$arg}");
}
@ -41,21 +120,27 @@ $options = get_opt('', [
'port1::',
'user1:',
'password1:',
'nossl1', // Do not use a SSL connection on host1.
'ssl1', // Use a SSL connection on host1. On by default if possible.
'notls1', // Do not use a TLS connection on host1.
'tls1', // Use a TLS connection on host1. On by default if possible.
'nossl1',
'ssl1',
'notls1',
'tls1',
'timeout1::',
// "destination" imap server.
'host2::',
'port2::',
'user2:',
'password2:',
'nossl2', // Do not use a SSL connection on host2.
'ssl2', // Use a SSL connection on host2. On by default if possible.
'notls2', // Do not use a TLS connection on host2.
'tls2', // Use a TLS connection on host2. On by default if possible.
'rootfolder::' // Added as prefix to destination folders
'nossl2',
'ssl2',
'notls2',
'tls2',
'timeout2::',
'rootfolder::',
// global
'justconnect',
'justlogin'
]);
chdir(__DIR__);
@ -88,6 +173,9 @@ function getImapClient(int $host)
} else if (143 === $ImapSettings->port && SecurityType::SSL == $ImapSettings->type) {
$ImapSettings->port = 993;
}
if (isset($options["timeout{$host}"])) {
$ImapSettings->timeout = (int) $options["timeout{$host}"];
}
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');
}
@ -109,10 +197,11 @@ function getImapClient(int $host)
// $oPlugins->RunHook('imap.before-connect', array($oAccount, $oImapClient, $ImapSettings));
$oImapClient->Connect($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($oAccount, $oImapClient, $bResult, $ImapSettings));
if (!isset($options['justconnect'])) {
// $oPlugins->RunHook('imap.before-login', array($oAccount, $oImapClient, $ImapSettings));
$oImapClient->Login($ImapSettings);
// $oPlugins->RunHook('imap.after-login', array($oAccount, $oImapClient, $bResult, $ImapSettings));
}
return $oImapClient;
}
@ -120,4 +209,6 @@ $oSync = new \SnappyMail\Imap\Sync;
$oSync->oImapSource = getImapClient(1);
$oSync->oImapTarget = getImapClient(2);
$oSync->import($options['rootfolder'] ?? '');
if (!isset($options['justconnect']) && !isset($options['justlogin'])) {
$oSync->import($options['rootfolder'] ?? '');
}