#!/bin/bash set -e [ -n "$DEBUG" ] && set -x maildir="${HOME}/Mail" cachefile=${XDG_CACHE_HOME}/mailsync.lastrun accounts=(gnzler hu) list= verbose= while getopts a:l:vh opt; do case "${opt}" in a) accounts=("${OPTARG}") ;; l) list="${OPTARG}" ;; v) verbose=1 ;; h | ?) printf "usage: %s [-h]\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 " -v be verbose" printf "\n" exit 2 ;; esac done shift $((OPTIND - 1)) notifysend() { local summary=$1 local body=$2 notify-send \ -i /usr/share/icons/mdi/scalable/email-plus.svg \ -a mailsync \ -c audible \ "${summary}" "${body}" } 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()))))))' } handle_msg() { local msg=$1 local from local subject local listid 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) if [ -n "${listid}" ]; then subject="${subject}\n~> $(echo "${listid}" | sed -e 's|<|\<|g' -e 's|>|\>|g')" fi notifysend "${from}" "${subject}" } notify() { local acct=$1 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 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." return } mbsync_args= [ -n "${verbose}" ] && mbsync_args="--verbose" mbsync $mbsync_args --config="$HOME/.config/mbsyncrc" "$acct" # create cachefile touch "${cachefile}.${acct}" } # only list new mail, don't sync if [ -z "${accounts[*]}" ]; then echo "no account specified" >&2 exit 1 fi for acct in "${accounts[@]}"; do ( if [ -n "${list}" ]; then list_msg "${list}" "${acct}" exit 0 # exit ()& subshell fi sync "${acct}" notify "${acct}" ) & done wait