summaryrefslogtreecommitdiffstats
path: root/.local/bin/fetch_mail
blob: 9f1ca5b3f81f080baac0284d872904e9dd725553 (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Env qw(HOME);

use constant MC_FILE_PATH => '/tmp/mc_';

sub get_new_mail
{
	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');
	}
	while (wait() != -1) {}
	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_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 @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
	return;
}

fetch_mail();

__END__