#!/usr/bin/env perl use strict; use warnings; use threads; use Fcntl; use Env qw(HOME); use constant MC_FILE_PATH => '/tmp/mc_'; sub notify { my ($str) = @_; my $pid; $pid = fork(); if (not $pid) { exec('herbe', $str); } return; } sub get_new_mail { my ($acc) = @_; my $ret; system('killall mbsync >/dev/null 2>&1'); $ret = system('mbsync', $acc); if ($ret != 0) { notify(" $acc: failed to sync mails"); } return; } sub get_all_accounts { my @accs; opendir(my $dh, $ENV{'MAIL'}) or die $!; @accs = grep { !/^\./ } readdir($dh); closedir($dh); return @accs; } sub count_new_mails { my ($acc) = @_; my $count; my $dir; my $path = $ENV{'MAIL'} . '/' . $acc . '/INBOX/new'; $count = 0; opendir(my $dh, $path) or die $!; $count = grep { -f "$path/$_"} readdir($dh); closedir($dh); return $count; } sub fetch_thread { my ($acc) = @_; # my $pre_count = -1; # my $mc_file = MC_FILE_PATH . $acc; # my $fh; # if (-f $mc_file) { # open($fh, '+<', $mc_file) or die $!; # $pre_count = <$fh>; # } else { # open($fh, '+>', $mc_file) or die $!; # $pre_count = -1; # } # # if ($pre_count == -1) { # # $pre_count = count_new_mails($_, $maildir); # # } # get_new_mail($acc); # my $post_count = count_new_mails($acc); # if ($post_count > $pre_count && $post_count > 0) { # my $notify_str = ' ' . $acc . ': ' . $post_count . ' new mail'; # $notify_str .= ($post_count > 1 ? "s\n" : "\n"); # notify($notify_str) # } # seek($fh, 0, 0); # print $fh "$post_count"; # close($fh); print "hey $acc\n"; threads->detach(); return; } sub fetch_mail { $ENV{'MAIL'} = $HOME . '/.local/share/mail'; $ENV{'GNUPGHOME'} = $HOME . '/.local/share/gnupg'; $ENV{'PASSWORD_STORE_DIR'} = $HOME . '/.local/share/pass'; my @threads; # test gpg my @accs = get_all_accounts(); push(@threads, threads->new(\&fetch_thread, $_)) for (@accs); # $_->join() for (@threads); print "done\n"; # notify($notify_str) if (length($notify_str) > $og_length); return; } fetch_mail(); __END__