| 1 |
#!/bin/bash |
| 2 |
# Esmtp-wrapper adds shell based queueing support to esmtp. |
| 3 |
# Copyright (C) 2007 by Phil Sutter <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
qdir="$HOME/.esmtp_queue" |
| 20 |
deliver_lock="$qdir/.deliver_lock" |
| 21 |
esmtp="/usr/bin/esmtp" |
| 22 |
mktemp="/bin/mktemp" |
| 23 |
dotlockfile="/usr/bin/dotlockfile" |
| 24 |
|
| 25 |
__do_check_program() { # (path to program) |
| 26 |
[ -x "$1" ] || { \ |
| 27 |
echo "required program '$1' not found, exiting" |
| 28 |
exit 1 |
| 29 |
} |
| 30 |
} |
| 31 |
check_programs() { # () |
| 32 |
__do_check_program "$esmtp" |
| 33 |
__do_check_program "$mktemp" |
| 34 |
__do_check_program "$dotlockfile" |
| 35 |
} |
| 36 |
|
| 37 |
queue_mail() { # ($@) |
| 38 |
local ret=0 |
| 39 |
mkdir -p "$qdir" || { |
| 40 |
echo "unable to create queue dir $qdir" 2>&1 |
| 41 |
return 1 |
| 42 |
} |
| 43 |
mdir="`$mktemp -d "$qdir/XXXXXXXX"`" |
| 44 |
[ -d "$mdir" ] || { |
| 45 |
echo "unable to create tempdir inside $qdir" 2>&1 |
| 46 |
return 1 |
| 47 |
} |
| 48 |
touch "$mdir/lock" |
| 49 |
echo "$@" >"$mdir/cmd" || ret=1 |
| 50 |
cat </dev/stdin >"$mdir/mail" || ret=1 |
| 51 |
[ $ret -eq 0 ] || { |
| 52 |
echo "could not enqueue mail in dir $mdir" 2>&1 |
| 53 |
return 1 |
| 54 |
} |
| 55 |
|
| 56 |
chmod 0600 $mdir/* || echo "warning setting secure permissions failed!" 2>&1 |
| 57 |
rm "$mdir/lock" |
| 58 |
return 0 |
| 59 |
} |
| 60 |
|
| 61 |
show_mail() { # ($mdir) |
| 62 |
echo "mail in dir $1:" |
| 63 |
printf '\t%s' "`grep ^From: "$1/mail"`" |
| 64 |
printf '\t%s' "`grep ^To: "$1/mail"`" |
| 65 |
[ -f "$1/lock" ] && printf '\t%s' "[LOCKED]" |
| 66 |
printf '\t%s\n' "`grep ^Date: "$1/mail"`" |
| 67 |
} |
| 68 |
|
| 69 |
show_queue() { # () |
| 70 |
local i=0 |
| 71 |
local lockwarn=0 |
| 72 |
for dir in $qdir/*; do |
| 73 |
[ ! -d "$dir" ] && continue |
| 74 |
[ -f "$dir/lock" ] && lockwarn=1 |
| 75 |
show_mail $dir |
| 76 |
let i++ |
| 77 |
done |
| 78 |
echo "$i mails to deliver" |
| 79 |
[ $lockwarn -eq 0 ] || { \ |
| 80 |
printf '\n%s\n' "Warning: locked mails in queue!"; \ |
| 81 |
printf '%s' "This means that either enqueueing is in "; \ |
| 82 |
printf '%s\n' "progress, or it failed for some reason."; \ |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
send_mail() { # ($mdir) |
| 87 |
$esmtp $(<"$1/cmd") <"$1/mail" && \ |
| 88 |
rm -rf "$1" || return 1 |
| 89 |
return $? |
| 90 |
} |
| 91 |
|
| 92 |
deliver_queue() { # ([background]) |
| 93 |
local undelivered=0 |
| 94 |
|
| 95 |
# when delivering in background, there is time to wait |
| 96 |
# for a potential burst run (git-send-email, e.g.) or |
| 97 |
# exiting of other delivery jobs. |
| 98 |
if [[ "$1" = "background" ]]; then |
| 99 |
sleep 5 |
| 100 |
fi |
| 101 |
if ! $dotlockfile -p -l "$deliver_lock"; then |
| 102 |
return 1 |
| 103 |
fi |
| 104 |
for dir in $qdir/*; do |
| 105 |
[ ! -d "$dir" ] && continue |
| 106 |
[ -f "$dir/lock" ] && continue |
| 107 |
send_mail "$dir" |
| 108 |
undelivered=`expr $undelivered + $?` |
| 109 |
done |
| 110 |
$dotlockfile -u "$deliver_lock" |
| 111 |
return $undelivered |
| 112 |
} |
| 113 |
|
| 114 |
ME=`basename "$0"` |
| 115 |
|
| 116 |
check_programs # possible program exit point |
| 117 |
|
| 118 |
case "$ME" in |
| 119 |
sendmail|esmtp) |
| 120 |
queue_mail "$@" || return 1 |
| 121 |
deliver_queue "background" & |
| 122 |
;; |
| 123 |
deliver) |
| 124 |
deliver_queue |
| 125 |
;; |
| 126 |
mailq) |
| 127 |
if [ x"$1" = x"-q" ]; then |
| 128 |
deliver_queue |
| 129 |
else |
| 130 |
show_queue |
| 131 |
fi |
| 132 |
;; |
| 133 |
*) |
| 134 |
exit 1 |
| 135 |
;; |
| 136 |
esac |
| 137 |
|
| 138 |
exit $? |
| 139 |
|