#!/bin/sh # populate maildirs and give notification if there is new mail. # run only if all of the following is true: # user logged in (prevent cron errors) # mbsync isn't already running # there is an internet connection. if ! pgrep -u "${USER:=$LOGNAME}" >/dev/null; then printf '%s not logged in. quitting...\n' "$USER" >/dev/stderr exit elif pgrep -x mbsync >/dev/null; then printf 'mbsync is already running. quitting...\n' >/dev/stderr exit elif ! ping -q -c 1 1.1.1.1 >/dev/null; then printf 'no internet connection, quitting...\n' >/dev/stderr fi export DISPLAY=:0 # ensure touch file's directory exists if ! [ -d "$XDG_DATA_HOME/neomutt" ]; then mkdir "$XDG_DATA_HOME/neomutt" fi # check account for new mail, notify for new content syncandnotify() { # sanitize $account for use in function acc="$(printf '%s\n' "$account" | sed "s/.*\///")" mbsync "$acc">/dev/null 2>&1 # $new holds an index of all mail since the last time popmail ran new=$(find "$XDG_DATA_HOME/mail/$acc/inbox/new/" -type f -newer "$XDG_DATA_HOME/neomutt/.popmail-last" 2> /dev/null) # $newcount is the number of new mails newcount=$(printf '%s\n' "$new" | sed '/^\s*$/d' | wc -l | tr -d " ") # send $newcount to a file specific to $acc # the preceding space makes generating a status bar string easier. printf ' %s' "$newcount" > "$XDG_DATA_HOME/mail/$acc/note" # don't notify if there isn't new mail if [ "$newcount" -gt "0" ]; then notify-send --app-name="popmail" "mail fetcher" "new mail for profile $acc! [$newcount]" & for file in $new; do # broken lines, not sure what they're supposed to do #from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//') #subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | sed '$D' | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n') # Extract subject and sender from mail. # modified version of broken $from variable, only prints the first from (so forwards are slightly unclear) from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed '1!d'| sed '1,/From/s/From: //') # modified version of broken $subject variable, only prints the first subject line (so forwards are slightly unclear) subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | sed '$D' | grep -m 1 '^Subject: ' | sed 's/Subject: //') notify-send --app-name="popmail" "$from:" "$subject" & # sleep for half a second to avoid notification clog sleep .5 done fi } # if popmail is given arguments, treat them as accounts to get mail from. # otherwise, scan the mbsync config to get account names if [ "$#" -eq "0" ]; then accounts="$(awk '/^Group/ {print $2}' "$HOME/.mbsyncrc")" else accounts=$* fi # call syncandnotify() for every account and background so it does all of them at once for account in $accounts do syncandnotify & done wait # collect all previously created note files and collate them into a file that can be read by a status bar to notify the user of new mails printf "$(cat "$XDG_DATA_HOME"/mail/*/note|tr " " "|"|sed 's/|//')\n">"$XDG_DATA_HOME/mail/note" #create/update modification time on an empty file that indicates the time of the last run of popmail touch "$XDG_DATA_HOME/neomutt/.popmail-last"