dgy
/
hexagons
Archived
1
0
Fork 0
This repository has been archived on 2021-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
hexagons/.local/bin/correo

40 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
pgrep -u "$USER" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
pgrep -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}
ping -q -c 1 1.1.1.1 > /dev/null || { echo "No internet connection detected."; exit ;}
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
export DISPLAY=:0.0
syncandnotify() {
acc="$(echo "$account" | sed "s/.*\///")"
mbsync "$acc"
new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.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')
notify-send --app-name="mutt-wizard" -i ~/Pictures/.icn/correo.png "New message in $acc" "$from: $subject" &
done
fi
}
if [ "$#" -eq "0" ]; then
accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
else
accounts=$*
fi
for account in $accounts
do
syncandnotify &
done
wait
notmuch new 2>/dev/null
touch "$HOME/.config/mutt/.mailsynclastrun"