diff options
| -rwxr-xr-x | bin/hibiki/mailsync | 93 |
1 files changed, 67 insertions, 26 deletions
diff --git a/bin/hibiki/mailsync b/bin/hibiki/mailsync index 3fc940f..66d6164 100755 --- a/bin/hibiki/mailsync +++ b/bin/hibiki/mailsync @@ -9,16 +9,16 @@ cachefile=${XDG_CACHE_HOME}/mailsync.lastrun accounts=(gnzler hu) list= verbose= -while getopts a:l:vh opt; do +while getopts a:lvh opt; do case "${opt}" in a) accounts=("${OPTARG}") ;; - l) list="${OPTARG}" ;; + l) list=1 ;; v) verbose=1 ;; h | ?) - printf "usage: %s [-h]\n" "$(basename "$0")" + printf "usage: %s [-h] [ACCOUNT...]\n" "$(basename "$0")" printf "\n" - printf " -a ACCOUNTS, accunts to sync (default: \"%s\")\n" "$(echo "${accounts[@]}")" - printf " -l CUR_OR_NEW, list new or current mails (default: \"%s\")\n" "${list}" + printf " -a ACCOUNTS, accunts to sync (default: \"%s\")\n" "${accounts[@]}" + printf " -l list mail\n" printf " -v be verbose" printf "\n" exit 2 @@ -27,19 +27,31 @@ while getopts a:l:vh opt; do done shift $((OPTIND - 1)) +if [ -n "$1" ]; then + accounts=("$@") +fi + notifysend() { local summary=$1 local body=$2 + local ico + shift 2 - notify-send \ - -i /usr/share/icons/mdi/scalable/email-plus.svg \ + set -x + if ! printf "%s" "$*" | grep -- "-i" >/dev/null; then + ico="📨 " + fi + + # -i /usr/share/icons/mdi/scalable/email-plus.svg \ + notify-send $@ \ -a mailsync \ -c audible \ - "${summary}" "${body}" + "${ico:-}${summary}" "${body}" + set +x } decode() { - python -c 'import sys;import email.header;print(("".join(list(map(lambda s: s[0] if (type(s[0]) == str) else s[0].decode(s[1] or "utf-8"), email.header.decode_header((sys.stdin.readlines()[0]).strip()))))))' + python -c 'import sys;import email.header;print(("".join(list(map(lambda s: s[0] if (type(s[0]) == str) else s[0].decode(s[1] or "utf-8"), email.header.decode_header(((sys.stdin.readlines() or [""])[0]).strip()))))))' } handle_msg() { @@ -47,14 +59,33 @@ handle_msg() { local from local subject local listid + local ico - from=$(awk '/^From:/{print}' "${msg}" | cut -d':' -f2- | decode) - subject=$(awk '/^Subject:/{print}' "${msg}" | cut -d':' -f2- | decode) - listid=$(awk '/^List-ID:/{print}' "${msg}" | cut -d':' -f2- | decode) + from=$(sed ':a; N; $!b a; s/\n\s\{1,\}/ /g' "${msg}" | awk '/^From:/{print}' | cut -d: -f2 | decode) + subject=$(sed ':a; N; $!b a; s/\n\s\{1,\}/ /g' "${msg}" | awk '/^Subject:/{print}' | cut -d: -f2 | decode) + listid=$(sed ':a; N; $!b a; s/\n\s\{1,\}/ /g' "${msg}" | awk '/^List-ID:/{print}' | cut -d: -f2 | decode) if [ -n "${listid}" ]; then subject="${subject}\n<span alpha='90%' size='small'>~> $(echo "${listid}" | sed -e 's|<|\<|g' -e 's|>|\>|g')</span>" + ico="$(iconez -sn email-multiple-outline)" + else + ico="$(iconez -sn email-receive-outline)" fi - notifysend "${from}" "${subject}" + notifysend "${from}" "${subject}" -i "${ico}" -t 2000 +} + +list_msg() { + local acct=$2 + local args=(-type f -not -name '\.*') + local cachefile="${cachefile}.${acct}" + + find "${maildir}"/"${acct}"/{INBOX,lists}/new/ "${args[@]}" 2>/dev/null || true + + # local cur_or_new=${1:-new} + # if [ "${cur_or_new}" = "new" ]; then + # find "${maildir}"/"${acct}"/{INBOX,lists}/"${cur_or_new}"/ "${args[@]}" 2>/dev/null || true + # else + # find "${maildir}"/"${acct}"/*/"${cur_or_new}"/ "${args[@]}" 2>/dev/null || true + # fi } notify() { @@ -62,35 +93,45 @@ notify() { local new_mail new_mail=$(list_msg new "${acct}") - if [ -n "$new_mail" ]; then - printf "found %d new mail\n" "$(echo "${new_mail}" | wc -l)" - for msg in ${new_mail}; do handle_msg "$msg"; done + if [ -n "${new_mail}" ]; then + local num + num="$(echo "${new_mail}" | grep -c INBOX)" + # always print INBOX messages + for msg in $(echo "${new_mail}" | grep INBOX); do handle_msg "$msg"; done + + # everything else, limit to 5, just print the count otherwise + num="$(echo "${new_mail}" | grep -c -v INBOX)" + notifysend "${acct}: new" "Found ${num} new mail" -i "$(iconez -sn email-receive-outline)" + [ "${num}" -gt 5 ] && return + for msg in $(echo "${new_mail}" | grep -v INBOX); do handle_msg "$msg"; done fi } -list_msg() { - local cur_or_new=${1:-new} - local acct=$2 - local args=(-type f) - local cachefile="${cachefile}.${acct}" - - [ "${cur_or_new}" = "new" ] && [ -f "${cachefile}" ] && args+=(-newer "${cachefile}") - find "${maildir}"/"${acct}"/*/"${cur_or_new}"/ "${args[@]}" 2>/dev/null -} - sync() { local acct=$1 # exit if we're already running pgrep mbsync >/dev/null && { echo "mbsync is already running." + notifysend "mailsync stopped" "mbsync is already running" -i "$(iconez -sn email-off-outline)" return } + + # persistent notification + id=$(notifysend "${acct}: fetching new mail..." "running mbsync" -i "$(iconez -sn email-sync-outline)" --print-id --expire-time=0) + mbsync_args= [ -n "${verbose}" ] && mbsync_args="--verbose" mbsync $mbsync_args --config="$HOME/.config/mbsyncrc" "$acct" # create cachefile touch "${cachefile}.${acct}" + + # clear notification + gdbus call --session \ + --dest org.freedesktop.Notifications \ + --object-path /org/freedesktop/Notifications \ + --method org.freedesktop.Notifications.CloseNotification \ + "$id" >/dev/null } # only list new mail, don't sync |