]> git.donarmstrong.com Git - bin.git/blob - getmail
add mutt alias which executes neomutt if that exists
[bin.git] / getmail
1 #!/bin/sh
2
3 if [ -n "$VERBOSE" ]; then
4     set -x
5 fi;
6
7 # Configure the two things below for your MUA and the host being run.
8 IMAPHOST=$( awk -F= '/remotehost/{print $2}' ~/.offlineimaprc | head -n 1 )
9 MUA=mutt
10
11 # Don't check mail if ~/.nocheck exists and we're not running
12 # interactively.
13 if [ -z "$CRON" ] && [ -e ~/.nocheck ]; then
14     exit 0;
15 fi;
16
17 if [ -z "$HOSTNAME" ]; then
18     HOSTNAME="$(hostname)"
19 fi;
20
21 IONICE=""
22 if which ionice >/dev/null 2>&1; then
23     IONICE="ionice -c 3"
24 fi;
25
26 # if we are running under cron
27 if [ -n "$CRON" ]; then
28     # and there is no MUA open, exit
29     if ! kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then
30         exit 0;
31     fi;
32     # if there is not a working ssh master, exit.
33     if ! ssh -O check $IMAPHOST 2>&1|grep -q running; then
34         exit 0;
35     fi;
36 fi;
37
38 # source the ssh info if it exists
39 if [ -e ~/.ssh/"ssh_agent_info_$HOSTNAME" ]; then
40         . ~/.ssh/"ssh_agent_info_$HOSTNAME" > /dev/null;
41 fi;
42
43 PID="$(pgrep -U $(id -u) offlineimap)";
44 if [ -n "$PID" ]; then
45         # if offlineimap has been running for more than 20 minutes
46         if [ $(( $(date +'%s') - $(stat -c '%Y' /proc/$PID/cmdline) )) -gt 1200 ]; then
47                 kill "$PID" >/dev/null 2>&1 || true;
48                 sleep 5
49                 kill -0 "$PID" >/dev/null 2>&1 && kill -9 "$PID" >/dev/null 2>&1;
50                 PID="$(pgrep -U $(id -u) offlineimap)";
51         fi;
52 fi;
53
54 if [ -z "$PID" ]; then
55         nice -n 19 ${IONICE} offlineimap -o -u basic 2>&1 |grep validity| \
56             perl -ne 'my ($account,$list) = /UID validity problem for folder ([\w-]+)\.([\w-]+)/;
57                   next unless defined $account and length $account;
58                   qx(/home/don/bin/deletemailbox -a Inbox $account.$list > /dev/null);';
59     if which notmuch >/dev/null 2>&1; then
60         nice -n 19 ${IONICE} notmuch new >/dev/null 2>&1;
61     fi;
62 fi;