X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=getmail;h=e7a4ee2a3a3e02905429751165b44fa388ee8f8f;hb=789f31d2b70d4fdd868403f23423ff825e6cef26;hp=b824fab4e5b13b7643e12c3748c9ab3be4a9cd85;hpb=8141a4df5c77faa99dd7b7357d6520069b32a213;p=bin.git diff --git a/getmail b/getmail index b824fab..e7a4ee2 100755 --- a/getmail +++ b/getmail @@ -1,23 +1,61 @@ #!/bin/sh # Configure the two things below for your MUA and the host being run. -IMAPHOST=rzlab.ucr.edu +IMAPHOST=$( awk -F= '/remotehost/{print $2}' ~/.offlineimaprc | head -n 1 ) MUA=mutt #set -x # Don't check mail if ~/.nocheck exists and we're not running # interactively. -if [[ -z "$CRON" && -e ~/.nocheck ]]; then +if [ -z "$CRON" ] && [ -e ~/.nocheck ]; then exit 0; fi; -# See if the server exists -if [ -z "$CRON" ] || kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then - if ping -q -c 3 $IMAPHOST >/dev/null 2>&1; then - nice -n 19 offlineimap -o -u Noninteractive.Basic 2>&1 |grep validity| \ - perl -ne 'my ($account,$list) = /UID validity problem for folder ([\w-]+)\.([\w-]+)/; - next unless defined $account and length $account; - qx(/home/don/bin/deletemailbox -a $account $list > /dev/null);'; +if [ -z "$HOSTNAME" ]; then + HOSTNAME="$(hostname)" +fi; + +IONICE="" +if which ionice >/dev/null 2>&1; then + IONICE="ionice -c 3" +fi; + +# if we are running under cron +if [ -n "$CRON" ]; then + # and there is no MUA open, exit + if ! kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then + exit 0; + fi; + # if there is not a working ssh master, exit. + if ! ssh -O check $IMAPHOST 2>&1|grep -q running; then + exit 0; + fi; +fi; + +# source the ssh info if it exists +if [ -e ~/.ssh/"ssh_agent_info_$HOSTNAME" ]; then + . ~/.ssh/"ssh_agent_info_$HOSTNAME" > /dev/null; +fi; +env; + +PID="$(pgrep -U $(id -u) offlineimap)"; +if [ -n "$PID" ]; then + # if offlineimap has been running for more than 20 minutes + if [ $(( $(date +'%s') - $(stat -c '%Y' /proc/$PID/cmdline) )) -gt 1200 ]; then + kill "$PID" >/dev/null 2>&1 || true; + sleep 5 + kill -0 "$PID" >/dev/null 2>&1 && kill -9 "$PID" >/dev/null 2>&1; + PID="$(pgrep -U $(id -u) offlineimap)"; + fi; +fi; + +if [ -z "$PID" ]; then + nice -n 19 ${IONICE} offlineimap -o -u basic 2>&1 |grep validity| \ + perl -ne 'my ($account,$list) = /UID validity problem for folder ([\w-]+)\.([\w-]+)/; + next unless defined $account and length $account; + qx(/home/don/bin/deletemailbox -a Inbox $account.$list > /dev/null);'; + if which notmuch >/dev/null 2>&1; then + nice -n 19 ${IONICE} notmuch new >/dev/null 2>&1; fi; fi;