stripes/cartero

46 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env sh
pgrep -x mbsync >/dev/null && exit
ping -q -c 1 1.1.1.1 >/dev/null || exit
DATA="${XDG_DATA_HOME:-$HOME/.local/share}"
CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}"
ICON="$HOME/img/.icn/App_Mail.svg"
syncandnotify() {
acc="$(echo "$account" | sed "s/.*\///")"
mbsync -c "$CONFIG"/isync/mbsyncrc "$acc"
new=$(find "$DATA/mail/$acc/INBOX/new/" "$DATA/mail/$acc/Inbox/new/" "$DATA/mail/$acc/inbox/new/" -type f -newer "$CONFIG/neomutt/.mailsynclastrun" 2>/dev/null)
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
if [ "$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')
if [ -n "$DISPLAY" ]; then
notify-send --app-name="neomutt" -i "$ICON" "Mensaje nuevo para $acc" "$from: $subject" &
fi
done
fi
}
if [ "$#" -eq "0" ]; then
accounts="$(awk '/^Channel/ {print $2}' "$CONFIG/isync/mbsyncrc")"
else
for arg in "$@"; do
[ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
done
accounts=$*
fi
for account in $accounts; do
syncandnotify &
done
wait
notmuch new 2>/dev/null
touch "$CONFIG/neomutt/.mailsynclastrun"