notmuch and alot

feat/kile-v2
Ricard Illa 2022-04-24 17:28:46 +02:00
parent 2827d2f025
commit 0c3cd27f4d
No known key found for this signature in database
GPG Key ID: F69A672B72E54902
3 changed files with 121 additions and 125 deletions

View File

@ -85,6 +85,7 @@ let
tls.enable = true; tls.enable = true;
port = 993; port = 993;
}; };
notmuch.enable = true;
}; };
switchAccountMacro = i: x: { switchAccountMacro = i: x: {
@ -523,12 +524,10 @@ in {
]; ];
}; };
#programs.notmuch = { programs.notmuch = {
# enable = true; enable = true;
# new = { new = { tags = [ "unread" "inbox" ]; };
# tags = ["unread" "inbox"]; };
# };
#};
programs.mbsync.enable = true; programs.mbsync.enable = true;
programs.msmtp.enable = true; programs.msmtp.enable = true;

View File

@ -1,132 +1,131 @@
{ config, pkgs, ... }: { config, pkgs, ... }:
let let
shell = "${pkgs.dash}/bin/dash"; shell = "${pkgs.dash}/bin/dash";
pidof = "${pkgs.procps}/bin/pidof"; pidof = "${pkgs.procps}/bin/pidof";
pgrep = "${pkgs.procps}/bin/pgrep"; pgrep = "${pkgs.procps}/bin/pgrep";
grep = "${pkgs.gnugrep}/bin/grep"; grep = "${pkgs.gnugrep}/bin/grep";
sed = "${pkgs.gnused}/bin/sed"; sed = "${pkgs.gnused}/bin/sed";
awk = "${pkgs.gawk}/bin/awk"; awk = "${pkgs.gawk}/bin/awk";
perl = "${pkgs.perl}/bin/perl"; perl = "${pkgs.perl}/bin/perl";
find = "${pkgs.findutils}/bin/find"; find = "${pkgs.findutils}/bin/find";
notifySend = "${pkgs.libnotify}/bin/notify-send"; notifySend = "${pkgs.libnotify}/bin/notify-send";
notmuch = "${pkgs.notmuch}/bin/notmuch"; notmuch = "${pkgs.notmuch}/bin/notmuch";
head = "${pkgs.coreutils}/bin/head"; head = "${pkgs.coreutils}/bin/head";
touch = "${pkgs.coreutils}/bin/touch"; touch = "${pkgs.coreutils}/bin/touch";
tr = "${pkgs.coreutils}/bin/tr"; tr = "${pkgs.coreutils}/bin/tr";
mbsyncrc = "${config.home.homeDirectory}/.mbsyncrc"; mbsyncrc = "${config.home.homeDirectory}/.mbsyncrc";
mbsync = "${pkgs.isync}/bin/mbsync -c ${mbsyncrc}"; mbsync = "${pkgs.isync}/bin/mbsync -c ${mbsyncrc}";
maildir = "${config.home.homeDirectory}/Maildir"; maildir = "${config.home.homeDirectory}/Maildir";
passwordStoreDir = "${config.home.homeDirectory}/.password-store"; passwordStoreDir = "${config.home.homeDirectory}/.password-store";
notmuchConfig = "${config.home.homeDirectory}/.notmuch-config"; notmuchConfig = "${config.home.homeDirectory}/.config/notmuch/default/config";
gnupghome = "${config.home.homeDirectory}/.gnupg"; gnupghome = "${config.home.homeDirectory}/.gnupg";
lastrun = "${config.home.homeDirectory}/.mailsynclastrun"; lastrun = "${config.home.homeDirectory}/.mailsynclastrun";
in in pkgs.writeScriptBin "mailsync" ''
pkgs.writeScriptBin "mailsync" '' #!${shell}
#!${shell}
# Run only if not already running in other instance # Run only if not already running in other instance
${pidof} mbsync >/dev/null && { ${pidof} mbsync >/dev/null && {
echo "mbsync is already running." echo "mbsync is already running."
exit exit
} }
export PASSWORD_STORE_DIR="${passwordStoreDir}" export PASSWORD_STORE_DIR="${passwordStoreDir}"
export NOTMUCH_CONFIG="${notmuchConfig}" export NOTMUCH_CONFIG="${notmuchConfig}"
export GNUPGHOME="${gnupghome}" export GNUPGHOME="${gnupghome}"
export GPG_TTY=$TTY export GPG_TTY=$TTY
notify() { notify() {
pgrepoutput="$(${pgrep} -a X\(org\|wayland\))" pgrepoutput="$(${pgrep} -a X\(org\|wayland\))"
displays="$(echo "$pgrepoutput" | ${grep} -wo "[0-9]*:[0-9]\+" | sort -u)" displays="$(echo "$pgrepoutput" | ${grep} -wo "[0-9]*:[0-9]\+" | sort -u)"
[ -n "$pgrepoutput" ] && for x in ''${displays:-0:}; do [ -n "$pgrepoutput" ] && for x in ''${displays:-0:}; do
export DISPLAY=$x export DISPLAY=$x
${notifySend} \ ${notifySend} \
--app-name="email" \ --app-name="email" \
"email" \ "email" \
"📬 $2 new mail(s) in \`$1\` account." "📬 $2 new mail(s) in \`$1\` account."
done
}
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
}
# 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"
else
${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" &
done
fi
}
# 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
done
accounts=$*
fi
# Parallelize multiple accounts
for account in $accounts; do
syncandnotify "''${account}" &
done done
}
wait 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
}
${notmuch} new 2>/dev/null # 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"
else
${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" &
done
fi
}
#Create a touch file that indicates the time of the last run of mailsync # Sync accounts passed as argument or all.
${touch} "${lastrun}" if [ "$#" -eq "0" ]; then
'' accounts="$(${awk} '/^Channel/ {print $2}' "${mbsyncrc}")"
else
for arg in "$@"; do
[ "''${arg%''${arg#?}}" = '-' ] && \
opts="''${opts:+''${opts} }''${arg}" && \
shift 1
done
accounts=$*
fi
# Parallelize multiple accounts
for account in $accounts; do
syncandnotify "''${account}" &
done
wait
${notmuch} new 2>/dev/null
#Create a touch file that indicates the time of the last run of mailsync
${touch} "${lastrun}"
''

View File

@ -9,8 +9,6 @@
home.username = "rilla"; home.username = "rilla";
home.homeDirectory = "/home/rilla"; home.homeDirectory = "/home/rilla";
nixpkgs.overlays = [ (self: super: { alot = stablePkgs.alot; }) ];
imports = [ imports = [
./arduino ./arduino
./barrier ./barrier