From 980603eb6733a1724d30e9150e942399300baff2 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 2 Oct 2025 18:04:41 +0200 Subject: up --- .local/bin/fetch_mail | 67 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 18 deletions(-) (limited to '.local') diff --git a/.local/bin/fetch_mail b/.local/bin/fetch_mail index 9f1ca5b..2002ce3 100755 --- a/.local/bin/fetch_mail +++ b/.local/bin/fetch_mail @@ -2,26 +2,31 @@ use strict; use warnings; +use threads; +use Fcntl; use Env qw(HOME); use constant MC_FILE_PATH => '/tmp/mc_'; -sub get_new_mail +sub notify { + my ($str) = @_; my $pid; $pid = fork(); - exec('herbe', ' failed to fork for killall') if ($pid < 0); - if (not $pid) { - exec('killall', 'mbsync'); - } - while (wait() != -1) {} - $pid = fork(); - exec('herbe', ' failed to fork for mbsync') if ($pid < 0); if (not $pid) { - exec('mbsync', '-a'); + exec('herbe', $str); } - while (wait() != -1) {} + return; +} + +sub get_new_mail +{ + my ($acc) = @_; + my $ret; + + system('killall mbsync >/dev/null 2>&1'); + system('mbsync', $acc); return; } @@ -49,22 +54,48 @@ sub count_new_mails return $count; } +sub fetch_thread +{ + my $pre_count = -1; + my $mc_file = MC_FILE_PATH . $_; + 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($_); # error if fail + my $post_count = count_new_mails($_, $maildir); + if ($post_count > $pre_count && $post_count > 0) { + my $notify_str = ' ' . $_ . ': ' . $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); + 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 $maildir = $ENV{'MAIL'} . '/'; + # my $notify_str = " new mails\n"; + my $og_length = length($notify_str); + # test gpg my @accs = get_all_accounts(); - # my $mc_file = MC_FILE_PATH . $accs[0]; - my $pre_count = count_new_mails($accs[1], $maildir); - print $pre_count . "\n"; - get_new_mail(); - # get new mail - # my $post_count = count_new_mails($accs[0], $maildir); - # push str - # notify + foreach (@accs) { + } + # notify($notify_str) if (length($notify_str) > $og_length); return; } -- cgit v1.2.3