summaryrefslogtreecommitdiffstats
path: root/bin/mbsyncloop
blob: 6b682bdb3157f7ee1ada4b39ff132654df4e3cb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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;
}

my $last = 0;
while (1) {
    my $now = time;
    my $channel = ($now - $last) > 15 * 60 ? "all" : "priority";
    $last = $now;

    system("mbsync -c ~/.mbsyncloop $channel") and last;
    system("notmuch new") and last;
    sleep 60 - (time - $now);
}