Fix sync-jobs with legacy-encoded passwords by implementing UTF-8/Latin-1 retry logic

Co-authored-by: DerLinkman <62480600+DerLinkman@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-15 08:15:52 +00:00
co-authored by DerLinkman
parent 9b1540697f
commit 4e12ba99a0
+31 -5
View File
@@ -117,17 +117,28 @@ while ($row = $sth->fetchrow_arrayref()) {
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; } if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
my $template = $run_dir . '/imapsync.XXXXXXX'; my $template = $run_dir . '/imapsync.XXXXXXX';
my $passfile1 = File::Temp->new(TEMPLATE => $template);
my $passfile2 = File::Temp->new(TEMPLATE => $template); my $passfile2 = File::Temp->new(TEMPLATE => $template);
binmode( $passfile1, ":utf8" );
print $passfile1 "$password1\n";
print $passfile2 trim($master_pass) . "\n"; print $passfile2 trim($master_pass) . "\n";
my @custom_params_a = qqw($custom_params); my @custom_params_a = qqw($custom_params);
my $custom_params_ref = \@custom_params_a; my $custom_params_ref = \@custom_params_a;
# Helper function to run imapsync with a specific encoding for password1
my $run_imapsync = sub {
my ($encoding) = @_;
my $passfile1 = File::Temp->new(TEMPLATE => $template);
if ($encoding eq 'utf8') {
binmode( $passfile1, ":utf8" );
} elsif ($encoding eq 'latin1') {
binmode( $passfile1, ":encoding(iso-8859-1)" );
}
print $passfile1 "$password1\n";
$passfile1->flush();
my $generated_cmds = [ "/usr/local/bin/imapsync", my $generated_cmds = [ "/usr/local/bin/imapsync",
"--tmpdir", "/tmp", "--tmpdir", "/tmp",
"--nofoldersizes", "--nofoldersizes",
@@ -156,12 +167,27 @@ while ($row = $sth->fetchrow_arrayref()) {
'--no-modulesversion', '--no-modulesversion',
'--noreleasecheck']; '--noreleasecheck'];
my $stdout;
run [@$generated_cmds, @$custom_params_ref], '&>', \$stdout;
return $stdout;
};
try { try {
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1, success = NULL, exit_status = NULL WHERE id = ?"); $is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1, success = NULL, exit_status = NULL WHERE id = ?");
$is_running->bind_param( 1, ${id} ); $is_running->bind_param( 1, ${id} );
$is_running->execute(); $is_running->execute();
run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout; # First attempt with UTF-8 encoding (default behavior)
my $stdout = $run_imapsync->('utf8');
# Check if authentication failed
my $auth_failed = ($stdout =~ /LOGIN failed|authentication failed|AUTHENTICATIONFAILED/i);
# If authentication failed with UTF-8, retry with Latin-1 encoding for legacy passwords
if ($auth_failed) {
$stdout = $run_imapsync->('latin1');
}
# check exit code and status # check exit code and status
($exit_code, $exit_status) = ($stdout =~ m/Exiting\swith\sreturn\svalue\s(\d+)\s\(([^:)]+)/); ($exit_code, $exit_status) = ($stdout =~ m/Exiting\swith\sreturn\svalue\s(\d+)\s\(([^:)]+)/);