X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=getmail;h=5d739a9609d9ddde25157eb82d67d73250e6f381;hb=dcbcee937ad71e91417e60b4288f7396ee7a93f6;hp=44c2020c117c613295cd3e1b858cf94b004d2b69;hpb=f7bbe0b6230a1ef2279cea678c8b17e0b6f0ec20;p=bin.git diff --git a/getmail b/getmail index 44c2020..5d739a9 100755 --- a/getmail +++ b/getmail @@ -1,22 +1,72 @@ #!/bin/sh -# Configure the two things below for your MUA and the host being run. -IMAPHOST=rzlab.ucr.edu -MUA=mutt +if [ -n "$VERBOSE" ]; then + set -x +fi; + +cmd_exists () { + if which "$1" >/dev/null 2>&1; then + return 0; + else + return 1; + fi; +} -#set -x +# Configure the two things below for your MUA and the host being run. +IMAPHOST=$( awk -F= '/remotehost/{print $2}' ~/.offlineimaprc | head -n 1 ) +MUAS="neomutt mutt mutt-ng" # 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; 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-]+)/; - qx(/home/don/bin/deletemailbox -a $account $list > /dev/null);'; +if [ -z "$HOSTNAME" ]; then + HOSTNAME="$(hostname)" +fi; + +IONICE="" +if cmd_exists "ionice"; then + IONICE="ionice -c 3" +fi; + +# if we are running under cron and there is no running MUA, exit +if [ -n "$CRON" ]; then + RUNNING_MUA=0 + for MUA in $MUAS; do + if kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then + RUNNING_MUA=1 + break; + fi; + done; + if [ "x$RUNNING_MUA" = "x0" ]; 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; + +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 cmd_exists "notmuch"; then + nice -n 19 ${IONICE} notmuch new >/dev/null 2>&1; fi; fi;