From 206101f51e57b5ce128177f6a8be1f4e12f1dce8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 30 Dec 2021 17:48:02 -0500 Subject: use imap idle to get more up to date mail checking --- mbsyncloop | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/mbsyncloop b/mbsyncloop index a00dc59..b96ddae 100755 --- a/mbsyncloop +++ b/mbsyncloop @@ -32,22 +32,46 @@ if (!$pid) { exit; } +open my $goimapnotify, '-|', "sh", "-c", "goimapnotify --conf ~/.config/imapnotify/tozt.conf 2>&1" or die; + my $last_all = 0; while (1) { my $now = time; - - my $channel; - if ($now - $last_all > 15 * 60) { - $channel = "all"; + if (($now - $last_all) >= 14 * 60) { + sync("all"); + $last_all = $now; } - else { - $channel = "priority"; + if (idle(15 * 60 - (time - $now))) { + sync("priority"); } +} - my $status = system("mbsync -c ~/.mbsyncloop $channel"); - if ($status == 0 && $channel eq "all") { - $last_all = $now; +sub sync { + my ($channel) = @_; + warn "syncing $channel"; + while (1) { + my $status = system("mbsync -c ~/.mbsyncloop $channel"); + if (!$status) { + system("notmuch new | grep -v '^No new mail\.\$'"); + last; + } + sleep 5; + } + warn "done syncing $channel"; +} + +sub idle { + my ($max_delay) = @_; + my $rin = ''; + vec($rin, fileno($goimapnotify), 1) = 1; + my $ready = select(my $rout = $rin, undef, undef, $max_delay); + if ($ready) { + while (1) { + my $ready = select(my $rout = $rin, undef, undef, 0.01); + last unless $ready; + sysread $goimapnotify, my $data, 4096; + } + return 1; } - system("notmuch new | grep -v '^No new mail\.\$'"); - sleep 60 - (time - $now); + return 0; } -- cgit v1.2.3-54-g00ecf