helm/os/arch/.config/panel/lemons/mail

96 lines
2.4 KiB
Plaintext
Raw Normal View History

2016-11-02 16:58:47 -07:00
#!/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
2017-01-03 00:49:10 -08:00
. $HOME/.private/config/email.conf
. $CWD/../panel-config
2016-11-02 16:58:47 -07:00
get_mail() {
local maildatatmp=/tmp/mail-data-"$1"
local mailtmp=/tmp/mail-count-"$1"
2017-01-16 19:35:15 -08:00
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
}
2016-11-02 16:58:47 -07:00
# 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 ))
2016-11-06 16:40:02 -08:00
icon="${ICON_MAIL}"
color="${COLOR_FG}"
case $total in
2016-11-06 16:40:02 -08:00
[1-9]*)
icon="$icon $total"
if [[ -f /tmp/notify ]]; then
aplay -N -q /usr/share/sounds/ding.wav
rm -f /tmp/notify
fi
2016-11-06 16:40:02 -08:00
;;
0) color="${COLOR_DIM}"
;;
esac
[[ $ERROR ]] && color=${COLOR_1}
2016-11-06 16:40:02 -08:00
echo "M%{F$color}$icon%{F-}"
# vim:set ft=sh: