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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/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', 'qwe');
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, $maildir) = @_;
my $count;
my $dir;
my $path = $maildir . $acc . '/INBOX/new';
$count = 0;
opendir(my $dh, $path) or die $!;
$count = grep { -f "$path/$_"} readdir($dh);
closedir($dh);
return $count;
}
sub fetch_thread
{
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";
# test gpg
my @accs = get_all_accounts();
foreach (@accs) {
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);
}
# notify($notify_str) if (length($notify_str) > $og_length);
return;
}
fetch_mail();
__END__
|