summaryrefslogtreecommitdiffstats
path: root/.local/bin/fetch_mail
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-x.local/bin/fetch_mail125
-rwxr-xr-x.local/bin/fetch_mail.sh42
2 files changed, 125 insertions, 42 deletions
diff --git a/.local/bin/fetch_mail b/.local/bin/fetch_mail
new file mode 100755
index 0000000..04df4b3
--- /dev/null
+++ b/.local/bin/fetch_mail
@@ -0,0 +1,125 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Fcntl;
+use Env qw(HOME);
+
+use constant MC_FILE_PATH => '/tmp/mc_';
+
+sub test_gpg
+{
+ my $ret;
+
+ $ret = system("echo test | gpg2 --sign --batch --no-tty --pinentry-mode error -o /dev/null >/dev/null 2>&1");
+ if ($ret != 0) {
+ notify("[!] gpg locked | can't fetch mails");
+ }
+ return $ret;
+}
+
+sub notify
+{
+ my ($str) = @_;
+ my $pid;
+
+ $pid = fork();
+ if (not $pid) {
+ exec('herbe', $str);
+ }
+ return;
+}
+
+sub get_new_mail
+{
+ my ($acc) = @_;
+ my $ret;
+
+ $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;
+ }
+ get_new_mail($acc);
+ my $post_count = count_new_mails($acc);
+ if ($post_count > $pre_count && $post_count > 0) {
+ my $notify_str = '[mail] ' . $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);
+ 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 @pids;
+ my $ret;
+
+ $ret = test_gpg();
+ if ($ret != 0) {
+ return;
+ }
+ system('killall mbsync >/dev/null 2>&1');
+ my @accs = get_all_accounts();
+ for (@accs) {
+ my $pid = fork();
+ if (not $pid) {
+ fetch_thread($_);
+ return;
+ }
+ push(@pids, $pid);
+
+ }
+ while (wait() != -1) {}
+ return;
+}
+
+fetch_mail();
+
+__END__
diff --git a/.local/bin/fetch_mail.sh b/.local/bin/fetch_mail.sh
deleted file mode 100755
index ed3da4a..0000000
--- a/.local/bin/fetch_mail.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/sh
-
-set -f
-set +f
-set -m
-
-export GNUPGHOME=$HOME/.local/share/gnupg
-export PASSWORD_STORE_DIR=$HOME/.local/share/pass
-
-mc_file="/tmp/newmailcount"
-if [ -f $mc_file ]; then
- pre_count=$(cat $mc_file)
-else
- pre_count="$(ls "${MAIL:-/var/mail/jozan}"/gmail_br/INBOX/new | wc -l 2>/dev/null)"
- mb1="$(ls "${MAIL:-/var/mail/jozan}"/gmail_oon/INBOX/new | wc -l 2>/dev/null)"
- mb2="$(ls "${MAIL:-/var/mail/jozan}"/gmail_tos/INBOX/new | wc -l 2>/dev/null)"
- pre_count="$((pre_count + mb1 + mb2))"
-fi
-
-if ! echo test | gpg2 --sign --batch --no-tty --pinentry-mode error -o /dev/null >/dev/null 2>&1; then
- notify-send -u low -t 6000 'mbsync' ' GPG locked'
- exit 1
-fi
-killall mbsync >/dev/null 2>&1
-notify-send -u low -t 3000 'mbsync' ' fetching mail...' >/dev/null 2>&1
-# {
-mbsync -a -c /usr/home/jozan/.config/mbsync/mbsyncrc ||
- notify-send -u low -t 6000 'mbsync' ' failed to fetch mail' >/dev/null 2>&1
-# }&
-# gsleep 0.2
-# kill -74 $(pidof dwmblocks) >/dev/null 2>&1
-# fg
-pre_count="$(ls "${MAIL:-/var/mail/jozan}"/gmail_br/INBOX/new | wc -l 2>/dev/null)"
-mb1="$(ls "${MAIL:-/var/mail/jozan}"/gmail_oon/INBOX/new | wc -l 2>/dev/null)"
-mb2="$(ls "${MAIL:-/var/mail/jozan}"/gmail_tos/INBOX/new | wc -l 2>/dev/null)"
-post_count="$((post_count + mb1 + mb2))"
-if [ $post_count -gt $pre_count ]; then
- notify-send -u normal 'NeoMutt' ' '$post_count' new mail(s)'
-fi
-echo $post_count >$mc_file
-# sleep 1
-# kill -74 $(pidof dwmblocks) >/dev/null 2>&1