forked from haqistan/mblaze-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailcheck
executable file
·85 lines (67 loc) · 1.54 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
#!/bin/sh
# 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"
sleep=$1
sleeper_pid=0
ts () {
date +"$date_fmt"
}
getmblazeopt () {
[[ -n $1 ]] && mhdr -h $1 $profile | envsubst && return 0
return 1
}
check_cmd=$(getmblazeopt MailCheckCommand)
check_cmd=${check_cmd:-"fdm -q fetch"}
sleep=${1:-$(getmblazeopt MailCheckSleep)}
sleep=${sleep:-30}
check_lockf=$(getmblazeopt MailCheckLockFile)
check_lockf=${check_lockf:-"$HOME/.fdm.lock"}
index_cmd=$(getmblazeopt MailCheckIndexCommand)
index_cmd=${index_cmd:--}
if [[ $index_cmd == "-" ]]; then
index_cmd=""
else
index_cmd=${index_cmd:-"mairix -p"}
fi
date_fmt=$(getmblazeopt MailCheckDateFmt)
date_fmt=${date_fmt:-"%Y-%m-%d %H:%M"}
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 QUIT
trap 'wakeup' INFO
clear_lockfile
[ -z "${index_cmd}" ] && echo "[automatic indexing is OFF]"
while true; do
mailcheck
shleep ${sleep}
done