imapsync_cron.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/perl
  2. use DBI;
  3. use LockFile::Simple qw(lock trylock unlock);
  4. use Proc::ProcessTable;
  5. use Data::Dumper qw(Dumper);
  6. use IPC::Run 'run';
  7. use String::Util 'trim';
  8. use File::Temp;
  9. my $t = Proc::ProcessTable->new;
  10. my $imapsync_running = grep { $_->{cmndline} =~ /^\/usr\/bin\/perl \/usr\/local\/bin\/imapsync\s/ } @{$t->table};
  11. if ($imapsync_running eq 1)
  12. {
  13. print "imapsync is active, exiting...";
  14. exit;
  15. }
  16. $DBNAME = '';
  17. $DBUSER = '';
  18. $DBPASS = '';
  19. $run_dir="/tmp";
  20. $dsn = "DBI:mysql:database=" . $DBNAME . ";host=mysql";
  21. $lock_file = $run_dir . "/imapsync_busy";
  22. $lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
  23. $lockmgr->lock($lock_file) || die "can't lock ${lock_file}";
  24. $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {
  25. mysql_auto_reconnect => 1,
  26. mysql_enable_utf8mb4 => 1
  27. });
  28. open my $file, '<', "/etc/sogo/sieve.creds";
  29. my $creds = <$file>;
  30. close $file;
  31. my ($master_user, $master_pass) = split /:/, $creds;
  32. my $sth = $dbh->prepare("SELECT id, user1, user2, host1, authmech1, password1, exclude, port1, enc1, delete2duplicates, maxage, subfolder2, delete1, delete2 FROM imapsync WHERE active = 1 AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_run) > mins_interval * 60 OR last_run IS NULL) ORDER BY last_run");
  33. $sth->execute();
  34. my $row;
  35. while ($row = $sth->fetchrow_arrayref()) {
  36. $id = @$row[0];
  37. $user1 = @$row[1];
  38. $user2 = @$row[2];
  39. $host1 = @$row[3];
  40. $authmech1 = @$row[4];
  41. $password1 = @$row[5];
  42. $exclude = @$row[6];
  43. $port1 = @$row[7];
  44. $enc1 = @$row[8];
  45. $delete2duplicates = @$row[9];
  46. $maxage = @$row[10];
  47. $subfolder2 = @$row[11];
  48. $delete1 = @$row[12];
  49. $delete2 = @$row[13];
  50. $is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1 WHERE id = ?");
  51. $is_running->bind_param( 1, ${id} );
  52. $is_running->execute();
  53. if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
  54. my $template = $run_dir . '/imapsync.XXXXXXX';
  55. my $passfile1 = File::Temp->new(TEMPLATE => $template);
  56. my $passfile2 = File::Temp->new(TEMPLATE => $template);
  57. print $passfile1 "$password1\n";
  58. print $passfile2 trim($master_pass) . "\n";
  59. run [ "/usr/local/bin/imapsync",
  60. "--timeout1", "10",
  61. "--tmpdir", "/tmp",
  62. "--subscribeall",
  63. ($exclude eq "" ? () : ("--exclude", $exclude)),
  64. ($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
  65. ($maxage eq "0" ? () : ('--maxage', $maxage)),
  66. ($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
  67. ($delete1 ne "1" ? () : ('--delete')),
  68. ($delete2 ne "1" ? () : ('--delete2')),
  69. (!defined($enc1) ? () : ($enc1)),
  70. "--host1", $host1,
  71. "--user1", $user1,
  72. "--passfile1", $passfile1->filename,
  73. "--port1", $port1,
  74. "--host2", "localhost",
  75. "--user2", $user2 . '*' . trim($master_user),
  76. "--passfile2", $passfile2->filename,
  77. '--no-modulesversion'], ">", \my $stdout;
  78. $update = $dbh->prepare("UPDATE imapsync SET returned_text = ?, last_run = NOW(), is_running = 0 WHERE id = ?");
  79. $update->bind_param( 1, ${stdout} );
  80. $update->bind_param( 2, ${id} );
  81. $update->execute();
  82. }
  83. $sth->finish();
  84. $dbh->disconnect();
  85. $lockmgr->unlock($lock_file);