|
@@ -7,6 +7,8 @@ use Data::Dumper qw(Dumper);
|
|
|
use IPC::Run 'run';
|
|
|
use String::Util 'trim';
|
|
|
use File::Temp;
|
|
|
+use Try::Tiny;
|
|
|
+use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
|
|
|
|
|
|
my $t = Proc::ProcessTable->new;
|
|
|
my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
|
|
@@ -16,6 +18,8 @@ if ($imapsync_running eq 1)
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
+sub qqw($) { split /\s+/, $_[0] }
|
|
|
+
|
|
|
$DBNAME = '';
|
|
|
$DBUSER = '';
|
|
|
$DBPASS = '';
|
|
@@ -29,11 +33,45 @@ $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
|
|
|
mysql_auto_reconnect => 1,
|
|
|
mysql_enable_utf8mb4 => 1
|
|
|
});
|
|
|
+sub sig_handler {
|
|
|
+ # Send die to force exception in "run"
|
|
|
+ die "sig_handler received signal, preparing to exit...\n";
|
|
|
+};
|
|
|
+
|
|
|
open my $file, '<', "/etc/sogo/sieve.creds";
|
|
|
my $creds = <$file>;
|
|
|
close $file;
|
|
|
my ($master_user, $master_pass) = split /:/, $creds;
|
|
|
-my $sth = $dbh->prepare("SELECT id, user1, user2, host1, authmech1, password1, exclude, port1, enc1, delete2duplicates, maxage, subfolder2, delete1, delete2, automap, skipcrossduplicates, maxbytespersecond 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");
|
|
|
+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");
|
|
|
+
|
|
|
$sth->execute();
|
|
|
my $row;
|
|
|
|
|
@@ -56,6 +94,10 @@ while ($row = $sth->fetchrow_arrayref()) {
|
|
|
$automap = @$row[14];
|
|
|
$skipcrossduplicates = @$row[15];
|
|
|
$maxbytespersecond = @$row[16];
|
|
|
+ $custom_params = @$row[17];
|
|
|
+ $subscribeall = @$row[18];
|
|
|
+ $timeout1 = @$row[19];
|
|
|
+ $timeout2 = @$row[20];
|
|
|
|
|
|
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
|
|
|
$is_running->bind_param( 1, ${id} );
|
|
@@ -70,22 +112,20 @@ while ($row = $sth->fetchrow_arrayref()) {
|
|
|
print $passfile1 "$password1\n";
|
|
|
print $passfile2 trim($master_pass) . "\n";
|
|
|
|
|
|
- run [ "/usr/local/bin/imapsync",
|
|
|
- "--timeout1", "600",
|
|
|
+ my @custom_params_a = qqw($custom_params);
|
|
|
+ my $custom_params_ref = \@custom_params_a;
|
|
|
+
|
|
|
+ my $generated_cmds = [ "/usr/local/bin/imapsync",
|
|
|
"--tmpdir", "/tmp",
|
|
|
- "--subscribeall",
|
|
|
"--nofoldersizes",
|
|
|
- "--skipsize",
|
|
|
- "--buffersize", "8192000",
|
|
|
- "--split1", "3000",
|
|
|
- "--split2", "3000",
|
|
|
- "--fastio1",
|
|
|
- "--fastio2",
|
|
|
+ ($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')),
|
|
@@ -98,12 +138,21 @@ while ($row = $sth->fetchrow_arrayref()) {
|
|
|
"--host2", "localhost",
|
|
|
"--user2", $user2 . '*' . trim($master_user),
|
|
|
"--passfile2", $passfile2->filename,
|
|
|
- '--no-modulesversion'], ">", \my $stdout;
|
|
|
+ '--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);
|
|
|
+ };
|
|
|
|
|
|
- $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();
|
|
|
}
|
|
|
|
|
|
$sth->finish();
|