2017-03-02 12:23:23 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use LockFile::Simple qw(lock trylock unlock);
|
2017-07-31 08:17:56 +02:00
|
|
|
use Proc::ProcessTable;
|
2017-03-02 12:23:23 +02:00
|
|
|
use Data::Dumper qw(Dumper);
|
|
|
|
use IPC::Run 'run';
|
2017-06-19 11:40:50 +02:00
|
|
|
use File::Temp;
|
2018-07-27 22:19:14 +02:00
|
|
|
use Try::Tiny;
|
|
|
|
use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
|
2017-03-02 12:23:23 +02:00
|
|
|
|
2019-05-05 22:28:55 +02:00
|
|
|
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
|
2017-07-31 08:17:56 +02:00
|
|
|
my $t = Proc::ProcessTable->new;
|
2020-02-12 09:32:58 +02:00
|
|
|
my $imapsync_running = grep { $_->{cmndline} =~ /imapsync\s/i } @{$t->table};
|
2020-03-20 19:11:24 +02:00
|
|
|
if ($imapsync_running ge 1)
|
2017-07-31 08:17:56 +02:00
|
|
|
{
|
|
|
|
print "imapsync is active, exiting...";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2019-05-04 23:08:35 +02:00
|
|
|
sub qqw($) {
|
2019-05-05 13:07:17 +02:00
|
|
|
my @params = ();
|
|
|
|
my @values = split(/(?=--)/, $_[0]);
|
2019-05-04 23:08:35 +02:00
|
|
|
foreach my $val (@values) {
|
2019-05-05 13:07:17 +02:00
|
|
|
my @tmpparam = split(/ /, $val, 2);
|
|
|
|
foreach my $tmpval (@tmpparam) {
|
|
|
|
if ($tmpval ne '') {
|
|
|
|
push @params, $tmpval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach my $val (@params) {
|
2019-05-04 23:08:35 +02:00
|
|
|
$val=trim($val);
|
|
|
|
}
|
2019-05-05 13:07:17 +02:00
|
|
|
return @params;
|
2019-05-04 23:08:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 12:23:23 +02:00
|
|
|
$run_dir="/tmp";
|
2021-04-16 20:38:09 +02:00
|
|
|
$dsn = 'DBI:mysql:database=' . $ENV{'DBNAME'} . ';mysql_socket=/var/run/mysqld/mysqld.sock';
|
2017-03-02 12:23:23 +02:00
|
|
|
$lock_file = $run_dir . "/imapsync_busy";
|
|
|
|
$lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
|
|
|
|
$lockmgr->lock($lock_file) || die "can't lock ${lock_file}";
|
2021-04-16 20:38:09 +02:00
|
|
|
$dbh = DBI->connect($dsn, $ENV{'DBUSER'}, $ENV{'DBPASS'}, {
|
2017-07-31 08:17:56 +02:00
|
|
|
mysql_auto_reconnect => 1,
|
|
|
|
mysql_enable_utf8mb4 => 1
|
|
|
|
});
|
2020-03-20 19:11:24 +02:00
|
|
|
$dbh->do("UPDATE imapsync SET is_running = 0");
|
|
|
|
|
2018-07-27 22:19:14 +02:00
|
|
|
sub sig_handler {
|
|
|
|
# Send die to force exception in "run"
|
|
|
|
die "sig_handler received signal, preparing to exit...\n";
|
|
|
|
};
|
|
|
|
|
2022-12-12 12:41:30 +02:00
|
|
|
open my $file, '<', "/etc/sogo/sieve.creds";
|
|
|
|
my $creds = <$file>;
|
2017-03-02 12:23:23 +02:00
|
|
|
close $file;
|
|
|
|
my ($master_user, $master_pass) = split /:/, $creds;
|
2018-07-27 22:19:14 +02:00
|
|
|
my $sth = $dbh->prepare("SELECT id,
|
|
|
|
user1,
|
|
|
|
user2,
|
|
|
|
host1,
|
|
|
|
authmech1,
|
|
|
|
password1,
|
|
|
|
exclude,
|
|
|
|
port1,
|
|
|
|
enc1,
|
|
|
|
delete2duplicates,
|
|
|
|
maxage,
|
|
|
|
subfolder2,
|
|
|
|
delete1,
|
|
|
|
delete2,
|
|
|
|
automap,
|
|
|
|
skipcrossduplicates,
|
|
|
|
maxbytespersecond,
|
|
|
|
custom_params,
|
|
|
|
subscribeall,
|
|
|
|
timeout1,
|
2023-11-15 17:18:18 +02:00
|
|
|
timeout2,
|
|
|
|
dry
|
2018-07-27 22:19:14 +02:00
|
|
|
FROM imapsync
|
|
|
|
WHERE active = 1
|
|
|
|
AND is_running = 0
|
|
|
|
AND (
|
|
|
|
UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_run) > mins_interval * 60
|
|
|
|
OR
|
|
|
|
last_run IS NULL)
|
|
|
|
ORDER BY last_run");
|
|
|
|
|
2017-03-02 12:23:23 +02:00
|
|
|
$sth->execute();
|
|
|
|
my $row;
|
|
|
|
|
|
|
|
while ($row = $sth->fetchrow_arrayref()) {
|
|
|
|
|
2018-01-24 13:59:04 +02:00
|
|
|
$id = @$row[0];
|
|
|
|
$user1 = @$row[1];
|
|
|
|
$user2 = @$row[2];
|
|
|
|
$host1 = @$row[3];
|
|
|
|
$authmech1 = @$row[4];
|
|
|
|
$password1 = @$row[5];
|
|
|
|
$exclude = @$row[6];
|
|
|
|
$port1 = @$row[7];
|
|
|
|
$enc1 = @$row[8];
|
|
|
|
$delete2duplicates = @$row[9];
|
|
|
|
$maxage = @$row[10];
|
|
|
|
$subfolder2 = @$row[11];
|
|
|
|
$delete1 = @$row[12];
|
|
|
|
$delete2 = @$row[13];
|
|
|
|
$automap = @$row[14];
|
|
|
|
$skipcrossduplicates = @$row[15];
|
|
|
|
$maxbytespersecond = @$row[16];
|
2018-07-27 22:19:14 +02:00
|
|
|
$custom_params = @$row[17];
|
|
|
|
$subscribeall = @$row[18];
|
|
|
|
$timeout1 = @$row[19];
|
|
|
|
$timeout2 = @$row[20];
|
2023-11-15 17:18:18 +02:00
|
|
|
$dry = @$row[21];
|
2017-03-02 12:23:23 +02:00
|
|
|
|
|
|
|
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
|
|
|
|
|
2017-06-19 11:40:50 +02:00
|
|
|
my $template = $run_dir . '/imapsync.XXXXXXX';
|
|
|
|
my $passfile1 = File::Temp->new(TEMPLATE => $template);
|
|
|
|
my $passfile2 = File::Temp->new(TEMPLATE => $template);
|
2023-11-14 11:11:25 +02:00
|
|
|
|
|
|
|
binmode( $passfile1, ":utf8" );
|
|
|
|
|
2017-06-19 11:40:50 +02:00
|
|
|
print $passfile1 "$password1\n";
|
|
|
|
print $passfile2 trim($master_pass) . "\n";
|
|
|
|
|
2019-05-04 23:08:35 +02:00
|
|
|
my @custom_params_a = qqw($custom_params);
|
|
|
|
my $custom_params_ref = \@custom_params_a;
|
|
|
|
|
|
|
|
my $generated_cmds = [ "/usr/local/bin/imapsync",
|
|
|
|
"--tmpdir", "/tmp",
|
|
|
|
"--nofoldersizes",
|
2021-03-11 20:21:50 +02:00
|
|
|
"--addheader",
|
2019-05-04 23:08:35 +02:00
|
|
|
($timeout1 gt "0" ? () : ('--timeout1', $timeout1)),
|
|
|
|
($timeout2 gt "0" ? () : ('--timeout2', $timeout2)),
|
|
|
|
($exclude eq "" ? () : ("--exclude", $exclude)),
|
|
|
|
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
|
|
|
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
|
|
|
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
|
|
|
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
|
|
|
($subscribeall ne "1" ? () : ('--subscribeall')),
|
|
|
|
($delete1 ne "1" ? () : ('--delete')),
|
|
|
|
($delete2 ne "1" ? () : ('--delete2')),
|
|
|
|
($automap ne "1" ? () : ('--automap')),
|
|
|
|
($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
|
|
|
|
(!defined($enc1) ? () : ($enc1)),
|
|
|
|
"--host1", $host1,
|
|
|
|
"--user1", $user1,
|
|
|
|
"--passfile1", $passfile1->filename,
|
|
|
|
"--port1", $port1,
|
|
|
|
"--host2", "localhost",
|
|
|
|
"--user2", $user2 . '*' . trim($master_user),
|
|
|
|
"--passfile2", $passfile2->filename,
|
2023-11-15 17:18:18 +02:00
|
|
|
($dry eq "1" ? ('--dry') : ()),
|
2019-05-05 14:35:34 +02:00
|
|
|
'--no-modulesversion',
|
|
|
|
'--noreleasecheck'];
|
2018-07-27 22:19:14 +02:00
|
|
|
|
|
|
|
try {
|
2021-09-01 16:29:11 +02:00
|
|
|
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1, success = NULL, exit_status = NULL WHERE id = ?");
|
2019-05-05 13:12:09 +02:00
|
|
|
$is_running->bind_param( 1, ${id} );
|
|
|
|
$is_running->execute();
|
2021-09-01 16:29:11 +02:00
|
|
|
|
2019-05-04 23:08:35 +02:00
|
|
|
run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout;
|
2021-09-01 16:29:11 +02:00
|
|
|
|
|
|
|
# check exit code and status
|
|
|
|
($exit_code, $exit_status) = ($stdout =~ m/Exiting\swith\sreturn\svalue\s(\d+)\s\(([^:)]+)/);
|
|
|
|
|
|
|
|
$success = 0;
|
|
|
|
if (defined $exit_code && $exit_code == 0) {
|
|
|
|
$success = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:41:30 +02:00
|
|
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, success = ?, exit_status = ? WHERE id = ?");
|
2018-07-27 22:19:14 +02:00
|
|
|
$update->bind_param( 1, ${stdout} );
|
2021-09-01 16:29:11 +02:00
|
|
|
$update->bind_param( 2, ${success} );
|
|
|
|
$update->bind_param( 3, ${exit_status} );
|
2022-12-12 12:41:30 +02:00
|
|
|
$update->bind_param( 4, ${id} );
|
2018-07-27 22:19:14 +02:00
|
|
|
$update->execute();
|
|
|
|
} catch {
|
2021-09-01 16:29:11 +02:00
|
|
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = 'Could not start or finish imapsync', success = 0 WHERE id = ?");
|
2019-05-05 13:15:34 +02:00
|
|
|
$update->bind_param( 1, ${id} );
|
|
|
|
$update->execute();
|
|
|
|
} finally {
|
|
|
|
$update = $dbh->prepare("UPDATE imapsync SET last_run = NOW(), is_running = 0 WHERE id = ?");
|
2018-07-27 22:19:14 +02:00
|
|
|
$update->bind_param( 1, ${id} );
|
|
|
|
$update->execute();
|
|
|
|
};
|
2017-03-02 12:23:23 +02:00
|
|
|
|
2019-05-05 13:15:34 +02:00
|
|
|
|
2017-03-02 12:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
|
|
|
$dbh->disconnect();
|
|
|
|
|
|
|
|
$lockmgr->unlock($lock_file);
|