helm/os/+arch/.config/panel/lemons/mail
2017-04-25 23:29:43 -04:00

96 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
set -e
CWD=$(dirname $0)
if [[ ! -f ~/.private/config/email.conf ]]; then
>&2 echo "Couldn't load account settings"
exit 1
fi
. $HOME/.private/config/email.conf
. $CWD/../panel-config
get_mail() {
local maildatatmp=/tmp/mail-data-"$1"
local mailtmp=/tmp/mail-count-"$1"
if curl -u "$1:$2" --silent https://mail.google.com/mail/feed/atom > "$maildatatmp"; then
[[ -f "$mailtmp" ]] || echo "0" >"$mailtmp"
local feed=$(<"$maildatatmp")
if [[ "${feed//\<fullcount\>/}" != "$feed" ]]; then
local lastnum=$(<"$mailtmp")
local total=$(echo "$feed" | grep -o "<entry>" | wc -l)
local new=0
echo "$total" > "$mailtmp"
if [[ $total > $lastnum ]]; then
new=$(( $total - $lastnum ))
NOTIFY=1
fi
parse_xml "$new" <<<"$feed"
echo $total
return
fi
>&2 echo "lemon/mail: malformed input ($1)"
else
>&2 echo "lemon/mail: failed to connect ($1)"
fi
ERROR=!
echo 0
}
# Oh man. I really did this in bash. Don't try this at home.
parse_xml() {
[[ $1 == 0 ]] && return
echo 1 > /tmp/notify
local n="${1:-1}"
tr '<>' '\n' | sed -e '/^\(?xml \|feed \|\/\|link rel=\|$\)/d;s/^entry$/----/;s/^author$/====/' | \
{
while read line; do
[[ $line == "----" ]] && break
done
while [[ $n > 0 ]] && read line; do
case $line in
name) read name ;;
email) read email ;;
title) read title ;;
esac
if [[ $title && $name && $email ]]; then
let n--
notify-send --icon="mail-message-new" "$name\n&lt;${email/-/_}&gt;" "$title"
title=
name=
email=
fi
done
}
}
#
a=$(get_mail "${MAIL1_USER}" "${MAIL1_PASS}")
b=$(get_mail "${MAIL2_USER}" "${MAIL2_PASS}")
c=$(get_mail "${MAIL3_USER}" "${MAIL3_PASS}")
total=$(( $a + $b + $c ))
icon="${ICON_MAIL}"
color="${COLOR_FG}"
case $total in
[1-9]*)
icon="$icon $total"
if [[ -f /tmp/notify ]]; then
aplay -N -q /usr/share/sounds/ding.wav
rm -f /tmp/notify
fi
;;
0) color="${COLOR_DIM}"
;;
esac
[[ $ERROR ]] && color=${COLOR_1}
echo "M%{F$color}$icon%{F-}"
# vim:set ft=sh: