#!/usr/bin/env perl use 5.016; use strict; use warnings; use POSIX 'mkfifo'; my ($pw_pipe, $pid); sub cleanup { unlink($pw_pipe) if $pw_pipe; kill KILL => $pid if $pid; } $SIG{INT} = $SIG{TERM} = sub { cleanup; exit }; END { cleanup } $pw_pipe = "/run/user/$>/mbsyncloop"; mkfifo($pw_pipe, 0700) or die "couldn't create $pw_pipe"; my $pw = `rbw get mail.tozt.net doy\@tozt.net`; $pid = fork; die unless defined $pid; if (!$pid) { $SIG{PIPE} = sub {}; while (1) { open my $fh, '>', $pw_pipe or die "couldn't open $pw_pipe"; $fh->print("$pw\n"); close $fh; } 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; if (($now - $last_all) >= 14 * 60) { sync("all"); $last_all = $now; } if (idle(15 * 60 - (time - $now))) { sync("priority"); } } 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; } return 0; }