nix-config/home/mail/mailsync.nix

133 lines
3.9 KiB
Nix
Raw Normal View History

2022-01-18 09:32:55 +01:00
{ config, pkgs, ... }:
let
2022-04-24 17:28:46 +02:00
shell = "${pkgs.dash}/bin/dash";
pidof = "${pkgs.procps}/bin/pidof";
pgrep = "${pkgs.procps}/bin/pgrep";
grep = "${pkgs.gnugrep}/bin/grep";
sed = "${pkgs.gnused}/bin/sed";
awk = "${pkgs.gawk}/bin/awk";
perl = "${pkgs.perl}/bin/perl";
find = "${pkgs.findutils}/bin/find";
2022-07-11 15:21:47 +02:00
2022-01-18 09:32:55 +01:00
notifySend = "${pkgs.libnotify}/bin/notify-send";
2022-04-24 17:28:46 +02:00
notmuch = "${pkgs.notmuch}/bin/notmuch";
head = "${pkgs.coreutils}/bin/head";
touch = "${pkgs.coreutils}/bin/touch";
tr = "${pkgs.coreutils}/bin/tr";
2022-01-18 09:32:55 +01:00
mbsyncrc = "${config.home.homeDirectory}/.mbsyncrc";
2022-04-24 17:28:46 +02:00
mbsync = "${pkgs.isync}/bin/mbsync -c ${mbsyncrc}";
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
maildir = "${config.home.homeDirectory}/Maildir";
2022-01-18 09:32:55 +01:00
passwordStoreDir = "${config.home.homeDirectory}/.password-store";
2022-04-24 17:28:46 +02:00
notmuchConfig = "${config.home.homeDirectory}/.config/notmuch/default/config";
gnupghome = "${config.home.homeDirectory}/.gnupg";
lastrun = "${config.home.homeDirectory}/.mailsynclastrun";
in pkgs.writeScriptBin "mailsync" ''
#!${shell}
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
# Run only if not already running in other instance
${pidof} mbsync >/dev/null && {
echo "mbsync is already running."
exit
}
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
export PASSWORD_STORE_DIR="${passwordStoreDir}"
export NOTMUCH_CONFIG="${notmuchConfig}"
export GNUPGHOME="${gnupghome}"
export GPG_TTY=$TTY
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
notify() {
pgrepoutput="$(${pgrep} -a X\(org\|wayland\))"
displays="$(echo "$pgrepoutput" | ${grep} -wo "[0-9]*:[0-9]\+" | sort -u)"
[ -n "$pgrepoutput" ] && for x in ''${displays:-0:}; do
export DISPLAY=$x
${notifySend} \
--app-name="email" \
"email" \
"📬 $2 new mail(s) in \`$1\` account."
done
}
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
messageinfo() {
from="$1"
subject="$2"
pgrepoutput="$(${pgrep} -a X\(org\|wayland\))"
displays="$(echo "$pgrepoutput" | ${grep} -wo "[0-9]*:[0-9]\+" | sort -u)"
[ -n "$pgrepoutput" ] && for x in ''${displays:-0:}; do
export DISPLAY=$x
${notifySend} \
--app-name="email" \
"📧$from:" \
"$subject"
done
}
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
# Check account for new mail. Notify if there is new content.
syncandnotify() {
accounts="$1"
acc="$(echo "$account" | ${sed} "s/.*\///")"
if [ -z "$opts" ]; then
${mbsync} "$acc"
2022-01-18 09:32:55 +01:00
else
2022-04-24 17:28:46 +02:00
${mbsync} "$opts" "$acc"
fi
new=$(
${find} \
"${maildir}/$acc/INBOX/new/" \
"${maildir}/$acc/Inbox/new/" \
"${maildir}/mail/$acc/inbox/new/" \
-type f \
-newer "${lastrun}" \
2> /dev/null
)
newcount=$(echo "$new" | ${sed} '/^\s*$/d' | wc -l)
if [ "$newcount" -gt 5 ]; then
notify "$acc" "$newcount"
elif [ "$newcount" -gt 0 ]; then
for file in $new; do
# Extract subject and sender from mail.
from=$(
${awk} '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | \
${perl} -CS -MEncode -ne 'print decode("MIME-Header", $_)' | \
${awk} '{ $1=""; if (NF>=3)$NF=""; print $0 }' | \
${sed} 's/^[[:blank:]]*[\"'\'''\<]*//;s/[\"'\'''\>]*[[:blank:]]*$//'
)
subject=$(
${awk} '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | \
${head} -n 1 | ${perl} -CS -MEncode -ne 'print decode("MIME-Header", $_)' | \
${sed} 's/^Subject: //' | \
${sed} 's/^{[[:blank:]]*[\"'\'''\<]*//;s/[\"'\'''\>]*[[:blank:]]*$//' | \
${tr} -d '\n'
)
messageinfo "$from" "$subject" &
2022-01-18 09:32:55 +01:00
done
fi
2022-04-24 17:28:46 +02:00
}
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
# Sync accounts passed as argument or all.
if [ "$#" -eq "0" ]; then
accounts="$(${awk} '/^Channel/ {print $2}' "${mbsyncrc}")"
else
for arg in "$@"; do
[ "''${arg%''${arg#?}}" = '-' ] && \
opts="''${opts:+''${opts} }''${arg}" && \
shift 1
2022-01-18 09:32:55 +01:00
done
2022-04-24 17:28:46 +02:00
accounts=$*
fi
# Parallelize multiple accounts
for account in $accounts; do
syncandnotify "''${account}" &
done
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
wait
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
${notmuch} new 2>/dev/null
2022-01-18 09:32:55 +01:00
2022-04-24 17:28:46 +02:00
#Create a touch file that indicates the time of the last run of mailsync
${touch} "${lastrun}"
''