aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-30 17:48:02 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-30 17:48:02 -0500
commit206101f51e57b5ce128177f6a8be1f4e12f1dce8 (patch)
tree8ebded34fb303ed95d33aab29e6e0dad2fdba752
parent19e944011d0d60e2c1c5d90e659c99b842383be4 (diff)
downloadmbsyncloop-206101f51e57b5ce128177f6a8be1f4e12f1dce8.tar.gz
mbsyncloop-206101f51e57b5ce128177f6a8be1f4e12f1dce8.zip
use imap idle to get more up to date mail checking
-rwxr-xr-xmbsyncloop46
1 files 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;
}