-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmailcheck
executable file
·95 lines (81 loc) · 1.88 KB
/
mailcheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/ksh
# simple mailcheck command to leave running in a tmux pane
# configurable via ~/.mblaze/profile
# by default runs fdm to check mail and mairix to index it
# ymmv
MBLAZE=${MBLAZE-$HOME/.mblaze}
profile="$MBLAZE/profile"
cfglook () {
mhdr -h $* | envsubst
}
sleep=$1
[ -z "$sleep" ] && sleep=$(cfglook MailCheckSleep $profile)
[ -z "$sleep" ] && sleep=30
check_cmd=$(cfglook MailCheckCommand $profile)
[ -z "$check_cmd" ] && check_cmd="fdm -q fetch"
check_lockf=$(cfglook MailCheckLockFile $profile)
[ -z "$check_lockf" ] && check_lockf=$HOME/.fdm.lock
index_cmd=$(cfglook MailCheckIndexCommand $profile)
[ -z "$index_cmd" ] && index_cmd="mairix -p"
[ "$index_cmd" = "-" ] && index_cmd=""
date_fmt=$(cfglook MailCheckDateFmt $profile)
[ -z "$date_fmt" ] && date_fmt="%Y-%m-%d %H:%M"
sleeper_pid=0
siginfo=""
# bsd polyfill
case $(uname) in
*BSD) netstat="netstat -na -f inet"
[ -z "$siginfo" ] && siginfo="INFO"
stty_size_field () {
stty -a | perl -lne '/(\d+)\s+'$1'/ && print $1'
}
;;
*) netstat="netstat -na4"
stty_size_field () {
stty -a | perl -lne '/'$1'\s+(\d+);/ && print $1'
}
;;
esac
ts () {
date +"$date_fmt"
}
shleep () {
sleep $1 >/dev/null 2>&1 & sleeper_pid=$!
echo $(ts) sleeping ...
wait $sleeper_pid 2>/dev/null
[ $? -ne 0 ] && echo $(ts) wakeup
sleeper_pid=0
}
wakeup () {
[ $sleeper_pid -ne 0 ] && \
kill $sleeper_pid >/dev/null 2>&1 && \
wait $sleeper_pid
}
mailcheck () {
echo $(ts) checking ...
${check_cmd}
if [ -n "${index_cmd}" ]; then
echo $(ts) indexing ...
${index_cmd}
fi
}
clear_lockfile () {
[ -n "${check_lockf}" ] && rm -f ${check_lockf}
}
interrupted () {
wakeup
clear_lockfile
exit 1
}
trap 'interrupted' INT TERM
if [ -n "$siginfo" ]; then
trap 'wakeup' QUIT
else
trap 'wakeup' ${siginfo}
fi
clear_lockfile
[ -z "${index_cmd}" ] && echo "[automatic indexing is OFF]"
while true; do
mailcheck
shleep ${sleep}
done