diff options
author | joe <rbo@gmx.us> | 2025-10-02 18:04:41 +0200 |
---|---|---|
committer | joe <rbo@gmx.us> | 2025-10-02 18:04:41 +0200 |
commit | 980603eb6733a1724d30e9150e942399300baff2 (patch) | |
tree | 9974c14d7565aa1230a0471192a87b6e6495eb9f /.local/bin | |
parent | up (diff) | |
download | dotfiles-bsd-980603eb6733a1724d30e9150e942399300baff2.tar.gz dotfiles-bsd-980603eb6733a1724d30e9150e942399300baff2.tar.bz2 dotfiles-bsd-980603eb6733a1724d30e9150e942399300baff2.tar.xz dotfiles-bsd-980603eb6733a1724d30e9150e942399300baff2.tar.zst dotfiles-bsd-980603eb6733a1724d30e9150e942399300baff2.zip |
up
Diffstat (limited to '.local/bin')
-rwxr-xr-x | .local/bin/fetch_mail | 67 |
1 files changed, 49 insertions, 18 deletions
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; } |