dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

mail.sh (2064B)


      1 #!/bin/sh
      2 #
      3 # mail
      4 
      5 ICON=''
      6 GET_ICON=''
      7 ISYNCRC="${XDG_CONFIG_HOME:-${HOME}/.config/}/isync/isyncrc"
      8 # both personal and school mail
      9 PERSONAL_MAIL_DIR="${MAIL_DIR}/matt/INBOX/new/"
     10 SCHOOL_MAIL_DIR="${MAIL_DIR}/algomau/INBOX/new/"
     11 TMP='/tmp/get-mail'
     12 
     13 # count mail files in dir
     14 count_mail() { [ -e "${1}" ] && printf '%s\n' "${#}" || printf '%s\n' 0 ; }
     15 
     16 # use isync to get mail from server
     17 get_mail() {
     18     printf '%s\n' "${GET_ICON}" > "${TMP}"
     19     mbsync -c "${ISYNCRC}" -aq
     20     rm -f "${TMP}"
     21 
     22     # check if gpg key is cached
     23     cached=$(gpg-connect-agent 'keyinfo --list' /bye 2>/dev/null | awk 'BEGIN { CACHED=0 } /^S/ { if ($7==1) { CACHED=1 } } END { if ($0 != "") { print CACHED } else { print "none" } }')
     24 
     25     # kill gpg agent if not cached; else user wont be able to enter key later
     26     [ "${cached}" -eq 0 ] && gpgconf --kill gpg-agent
     27 }
     28 
     29 open() {
     30     wd="$(pwd)"
     31     cd "${DOWNLOADS_DIR}" || return 1
     32     "${TERMINAL}" -c "${MAIL_CLIENT}" -e "${MAIL_CLIENT}"
     33     cd "${wd}" || return 1
     34     get_mail &
     35 }
     36 
     37 bar() {
     38     # count personal and school mail then sum together
     39     personal_mail_count=$(count_mail "${PERSONAL_MAIL_DIR}"/*)
     40     school_mail_count=$(count_mail "${SCHOOL_MAIL_DIR}"/*)
     41     total_mail_count=$((personal_mail_count + school_mail_count))
     42 
     43     # if getting mail show get icon
     44     [ -f "${TMP}" ] && printf '%s\n' "${GET_ICON}" && return
     45 
     46     # if mail open show just mail icon
     47     #shellcheck disable=SC2009
     48     if ps -ef | grep -i "\<${MAIL_CLIENT}\>" | grep -iqv '\<grep\>'; then printf '%s\n' "${ICON}"
     49     # otherwise print both icon and count
     50     else printf '%s\n' "${ICON} ${total_mail_count}"
     51     fi
     52 }
     53 
     54 main() {
     55     # called from bar
     56     [ ${#} -eq 0 ] && bar
     57 
     58     # bar usage
     59     case ${BLOCK_BUTTON} in
     60         1) open     ;;
     61         2) get_mail ;;
     62     esac
     63 
     64     while getopts 'go' opt; do
     65         case "${opt}" in
     66             # get mail if called with g flag
     67             g) get_mail ;;
     68             # open mail if o flag used
     69             o) open     ;;
     70             *) return   ;;
     71         esac
     72     done
     73 }
     74 
     75 main "${@}"