about summary refs log blame commit diff stats
path: root/bin/mbsync-cron
blob: fc611c2d80c58676940b47f720778a61bd3f55c4 (plain) (tree)
1
2
3
4
5
6
7
8
9







                                                                     
               

                                                                      
               

                                                                   
               








                                                    


                                                                                                                         

                                                                         
                                                   
                                                                      
                                                                  

                                          
                                                                                                          

                                                     
                            




                                                                                                                                                     
                                                                      


                                                                
          


      
                                                                             

                                                        
                                                                              










                                                                                      







                                                                                                                                               

                                                                                                        
#!/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 1;
elif pgrep -x mbsync >/dev/null; then
	printf 'mbsync is already running. quitting...\n' >/dev/stderr
	exit 1;
elif ! ping -q -c 1 1.1.1.1 >/dev/null; then
	printf 'no internet connection, quitting...\n' >/dev/stderr
	exit 1;
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() {
    mbsync -c "$XDG_CONFIG_HOME/mbsync/mbsyncrc" "$account">/dev/null 2>&1
    # $new holds an index of all mail since the last time mbsync-cron ran
    new=$(find "$XDG_DATA_HOME/mail/$account/inbox/new/" -type f -newer "$XDG_DATA_HOME/neomutt/.mbsc-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 $account
    # the preceding space makes generating a status bar string easier.
    printf ' %s' "$newcount" > "$XDG_DATA_HOME/mail/$account/note"
    # don't notify if there isn't new mail
    if [ "$newcount" -gt "0" ]; then
        notify-send --app-name="mbsync-cron" "mail fetcher" "new mail for profile $account! [$newcount]" &
	# reduce output if you have a lot of new mail
	if [ "$newcount" -lt "16" ]; then
        for file in $new; do
            # 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="mbsync-cron" "$from:" "$subject" &
	    # sleep for half a second to avoid notification clog
	    sleep .5
        done
	fi
    fi
}

# if mbsync-cron 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}' "$XDG_CONFIG_HOME/mbsync/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
## OLD: 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"
# use a single symbol to indicate new mail in any maildir
if cat "$XDG_DATA_HOME"/mail/*/note|grep -q '[1-9]'; then
	printf ' \n'>"$XDG_DATA_HOME/mail/note"
else
	printf '\n'>"$XDG_DATA_HOME/mail/note"
fi
#create/update modification time on an empty file that indicates the time of the last run of mbsync-cron
touch "$XDG_DATA_HOME/neomutt/.mbsc-last"