-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_send_mail
executable file
·94 lines (77 loc) · 2.04 KB
/
fetch_send_mail
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
#!/bin/zsh
LOGDIR="${HOME}/.log"
LOGFILE="${LOGDIR}/fetch_send_mail.log"
CACHEDIR="${HOME}/.cache/fetch_send_mail"
LOCKFILE="${CACHEDIR}/fetch_send_mail.lock"
ACCOUNTS=
OIMAP_UI="quiet"
VERBOSE=
[[ -d $LOGDIR ]] || mkdir $LOGDIR
[[ -d $CACHEDIR ]] || mkdir -p $CACHEDIR
help() {
echo
echo "Sends stored mail, performs onetime offlineimap run"
echo
echo "Options:"
echo "-a ACCOUNTS Comma-separated list of IMAP accounts to sync"
echo "-v Verbose mode"
echo
exit 1
}
[[ $1 == '--help' ]] && help
while getopts a:vh arg; do
case ${arg} in
a) ACCOUNTS=("${(@s:,:)OPTARG}") ;;
v)
OIMAP_UI="basic"
VERBOSE="-v"
;;
*|h) help ;;
esac
done
set -e
msg() {
[[ -n $VERBOSE ]] && echo $1
if echo "$1" | grep -v 'INFO: ' > /dev/null; then
echo "$(date '+%Y-%m-%d %H:%M:%S %Y') $1" >> $LOGFILE
fi
}
run() {
if [[ -n $VERBOSE ]]; then
"$@"
else
"$@" > /dev/null
fi
}
if [[ -e "${LOCKFILE}" ]]; then
if pgrep -f "(reachdomain|imapfilter|offlineimap)" > /dev/null; then
msg "ERROR: Lock file already present, exiting."
exit 1
else
msg "WARNING: No stuck process found, removing lock file."
rm -f "${LOCKFILE}"
fi
fi
touch "${LOCKFILE}"
trap "rm -f ${LOCKFILE}" EXIT
for account in ${ACCOUNTS[@]}; do
case $account in
gmail) DOMAIN="imap.gmail.com" ;;
seznam) DOMAIN="imap.seznam.cz" ;;
*)
msg "WARNING: Unknown account $account, removing from the list"
ACCOUNTS=("${(@)ACCOUNTS:#$account}")
continue
;;
esac
#if _msg=$(reachdomain -v -d $DOMAIN); then
# msg "INFO: $_msg"
#else
# msg "ERROR: ${${(@f)_msg}[2]}"
# ACCOUNTS=("${(@)ACCOUNTS:#$account}")
# continue
#fi
getmailpass.py $account > /dev/null
#run imapfilter -c ~/.imapfilter/$account.lua -l $LOGFILE $VERBOSE
done
[[ -n $ACCOUNTS ]] && run offlineimap -o -u ${OIMAP_UI} -a ${(j:,:)ACCOUNTS} -l $LOGFILE