2017-03-02 18:23:23 +08:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use LockFile::Simple qw(lock trylock unlock);
|
2017-07-31 14:17:56 +08:00
|
|
|
use Proc::ProcessTable;
|
2017-03-02 18:23:23 +08:00
|
|
|
use Data::Dumper qw(Dumper);
|
|
|
|
use IPC::Run 'run';
|
|
|
|
use String::Util 'trim';
|
2017-06-19 17:40:50 +08:00
|
|
|
use File::Temp;
|
2018-07-28 04:19:14 +08:00
|
|
|
use Try::Tiny;
|
|
|
|
use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
|
2017-03-02 18:23:23 +08:00
|
|
|
|
2017-07-31 14:17:56 +08:00
|
|
|
my $t = Proc::ProcessTable->new;
|
|
|
|
my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
|
|
|
|
if ($imapsync_running eq 1)
|
|
|
|
{
|
|
|
|
print "imapsync is active, exiting...";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:24:51 +08:00
|
|
|
sub qqw($) {
|
|
|
|
my @values = split('(?=--)', $_[0]);
|
|
|
|
foreach my $val (@values) {
|
|
|
|
$val=trim($val);
|
|
|
|
}
|
|
|
|
return @values
|
|
|
|
}
|
2018-07-28 04:19:14 +08:00
|
|
|
|
2017-03-02 18:23:23 +08:00
|
|
|
$DBNAME = '';
|
|
|
|
$DBUSER = '';
|
|
|
|
$DBPASS = '';
|
|
|
|
|
|
|
|
$run_dir="/tmp";
|
2019-01-19 00:36:30 +08:00
|
|
|
$dsn = "DBI:mysql:database=" . $DBNAME . ";mysql_socket=/var/run/mysqld/mysqld.sock";
|
2017-03-02 18:23:23 +08: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}";
|
2017-07-31 14:17:56 +08:00
|
|
|
$dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
|
|
|
|
mysql_auto_reconnect => 1,
|
|
|
|
mysql_enable_utf8mb4 => 1
|
|
|
|
});
|
2018-07-28 04:19:14 +08:00
|
|
|
sub sig_handler {
|
|
|
|
# Send die to force exception in "run"
|
|
|
|
die "sig_handler received signal, preparing to exit...\n";
|
|
|
|
};
|
|
|
|
|
2017-03-02 18:23:23 +08:00
|
|
|
open my $file, '<', "/etc/sogo/sieve.creds";
|
|
|
|
my $creds = <$file>;
|
|
|
|
close $file;
|
|
|
|
my ($master_user, $master_pass) = split /:/, $creds;
|
2018-07-28 04:19:14 +08: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,
|
|
|
|
timeout2
|
|
|
|
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 18:23:23 +08:00
|
|
|
$sth->execute();
|
|
|
|
my $row;
|
|
|
|
|
|
|
|
while ($row = $sth->fetchrow_arrayref()) {
|
|
|
|
|
2018-01-24 19:59:04 +08: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-28 04:19:14 +08:00
|
|
|
$custom_params = @$row[17];
|
|
|
|
$subscribeall = @$row[18];
|
|
|
|
$timeout1 = @$row[19];
|
|
|
|
$timeout2 = @$row[20];
|
2017-03-02 18:23:23 +08:00
|
|
|
|
2017-11-08 18:07:32 +08:00
|
|
|
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
|
|
|
|
$is_running->bind_param( 1, ${id} );
|
|
|
|
$is_running->execute();
|
|
|
|
|
2017-03-02 18:23:23 +08:00
|
|
|
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
|
|
|
|
|
2017-06-19 17:40:50 +08:00
|
|
|
my $template = $run_dir . '/imapsync.XXXXXXX';
|
|
|
|
my $passfile1 = File::Temp->new(TEMPLATE => $template);
|
|
|
|
my $passfile2 = File::Temp->new(TEMPLATE => $template);
|
|
|
|
|
|
|
|
print $passfile1 "$password1\n";
|
|
|
|
print $passfile2 trim($master_pass) . "\n";
|
|
|
|
|
2018-07-28 04:19:14 +08:00
|
|
|
my @custom_params_a = qqw($custom_params);
|
|
|
|
my $custom_params_ref = \@custom_params_a;
|
|
|
|
|
|
|
|
my $generated_cmds = [ "/usr/local/bin/imapsync",
|
2017-03-02 18:23:23 +08:00
|
|
|
"--tmpdir", "/tmp",
|
2018-02-14 19:27:30 +08:00
|
|
|
"--nofoldersizes",
|
2018-07-28 04:19:14 +08:00
|
|
|
($timeout1 gt "0" ? () : ('--timeout1', $timeout1)),
|
|
|
|
($timeout2 gt "0" ? () : ('--timeout2', $timeout2)),
|
2017-04-10 19:08:02 +08:00
|
|
|
($exclude eq "" ? () : ("--exclude", $exclude)),
|
2018-01-24 19:59:04 +08:00
|
|
|
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
|
|
|
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
2018-02-27 22:12:21 +08:00
|
|
|
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
2018-01-24 19:59:04 +08:00
|
|
|
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
2018-07-28 04:19:14 +08:00
|
|
|
($subscribeall ne "1" ? () : ('--subscribeall')),
|
2018-01-24 19:59:04 +08:00
|
|
|
($delete1 ne "1" ? () : ('--delete')),
|
|
|
|
($delete2 ne "1" ? () : ('--delete2')),
|
|
|
|
($automap ne "1" ? () : ('--automap')),
|
|
|
|
($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
|
2017-04-10 19:08:02 +08:00
|
|
|
(!defined($enc1) ? () : ($enc1)),
|
2017-03-02 18:23:23 +08:00
|
|
|
"--host1", $host1,
|
|
|
|
"--user1", $user1,
|
2017-06-19 17:40:50 +08:00
|
|
|
"--passfile1", $passfile1->filename,
|
2017-03-02 18:23:23 +08:00
|
|
|
"--port1", $port1,
|
|
|
|
"--host2", "localhost",
|
|
|
|
"--user2", $user2 . '*' . trim($master_user),
|
2017-06-19 17:40:50 +08:00
|
|
|
"--passfile2", $passfile2->filename,
|
2018-07-28 04:19:14 +08:00
|
|
|
'--no-modulesversion'];
|
|
|
|
|
|
|
|
try {
|
|
|
|
run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout;
|
|
|
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
|
|
|
|
$update->bind_param( 1, ${stdout} );
|
|
|
|
$update->bind_param( 2, ${id} );
|
|
|
|
$update->execute();
|
|
|
|
} catch {
|
|
|
|
$update = $dbh->prepare("UPDATE imapsync SET returned_text = 'Could not start or finish imapsync', last_run = NOW(), is_running = 0 WHERE id = ?");
|
|
|
|
$update->bind_param( 1, ${id} );
|
|
|
|
$update->execute();
|
|
|
|
$lockmgr->unlock($lock_file);
|
|
|
|
};
|
2017-03-02 18:23:23 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
|
|
|
$dbh->disconnect();
|
|
|
|
|
|
|
|
$lockmgr->unlock($lock_file);
|