mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

commit 0fecc5ebef6e6ccb7cbfd692137489c8de2213c5
parent fd0b7ee46f0f767fa4e32389af32e01c1701a4ba
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Mon, 16 Sep 2013 10:50:04 +0200

fix

Diffstat:
msmtpQ | 197+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 197 insertions(+), 0 deletions(-)

diff --git a/msmtpQ b/msmtpQ @@ -0,0 +1,197 @@ +#!/bin/bash + +#-------------------------------------------------------------- +# +# msmtpQ : queue funtions to use the msmtp queue, +# as it was defined by Martin Lambers +# Copyright (C) 2008 Chris Gianniotis +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +#-------------------------------------------------------------- + +#-------------------------------------------------------------- +# +# This version has tiny modifications by Marianne Promberger to the original +# found here: +# http://www.mail-archive.com/msmtp-users@lists.sourceforge.net/msg00005.html +# +# -- cleaned up accidental linebreaks in comments +# -- changed shell from /bin/sh to /bin/bash +# -- changed logfile location +# +# marianne@promberger.info +#-------------------------------------------------------------- + +#-------------------------------------------------------------- +# the msmtp queue contains unique filenames of the following form : +# two for each mail in the queue +# +# create new unique filenames of the form : +# MLF: ccyy-mm-dd-hh.mm.ss[-x].mail -- mail file +# MSF: ccyy-mm-dd-hh.mm.ss[-x].msmtp -- msmtp command line file +# where x is a consecutive number only appended for uniqueness +# if you send more than one mail per second +#-------------------------------------------------------------- + + +# msmtpQ is meant to be used directly by an email client - in 'sendmail' mode +# there is a separate log file for all events & operations on the msmtp +# queue that is defined below + + +## !! please define the following two vars before using the msmtpq & msmtpQ +## !! routines + +# set the queue var to the location of the msmtp queue directory +# if the queue dir doesn't yet exist, better to create it (0700) +# before using this routine (it will only complain ...) +# +Q=~/msmtp.queue # the queue - modify this to reflect where + # you'd like it to be + +# set the queue log var to the location of the msmtp queue log file +# where it is or where you'd like it to be +# ( note that the LOG setting could be the same as the ) +# ( 'logfile' setting in .msmtprc - but there may be ) +# ( some advantage in keeping the two logs separate ) +# if you don't want the log at all please unset (comment out) this var +# +LOG=~/logs/msmtp-queue # the log - modify to taste ... + +umask 077 # set secure permissions on created + # directories and files + +usage() { + echo + echo ' usage : msmtpQ' + echo ' msmtpQ -h this helpful blurt' + echo + echo ' any/all args on the msmtpQ command' + echo ' line are passed through to msmtp' + echo ' (not to mention the mail body text' + echo ' passed via standard in ...)' + echo + exit 0 +} + +# log a message, possibly an error +# usage : log_msg [ -e ] msg [ msg ] ... +# opts : -e <exit code> an error ; log msg & terminate w/prejudice +# +log_msg() { + local ERR ARG RC PFX + + if [ "$1" == '-e' ] ; then + ERR='t' # set error flag + RC="$2" # take error exit code + shift 2 # shift opt & its arg off + fi + + if [ -n "$LOG" ] ; then # logging not suppressed + PFX="$(/bin/date +'%Y %b %d %H:%M:%S')" # time stamp prefix - "2008 Mar 13 + # 03:59:45 " + for ARG ; do # each msg line out + echo "$PFX : $ARG" >> "$LOG" # send it to log + done + fi + + if [ -n "$ERR" ] ; then # an error ; leave w/error return + if [ "$RC" != 0 ] ; then # exit code != 0 => display the exit code + [ -n "$LOG" ] && \ + echo " exit code = $RC" >> "$LOG" # logging ok ; send exit code to log + exit $RC # exit w/exit code + else # exit code == 0 => don't display an exit code + exit 1 + fi + fi +} + +# verify that the msmtp queue is present + # the first version can be used if you'd like to create the queue dir + # if it's not found ; I'd rather just be warned if it's not there +check_queue() { # make certain queue dir is present + #if [ ! -d "$Q" ] ; then # queue dir not present ; create it + # /bin/mkdir -p "$Q" || log_msg -e "could not create msmtp queue dir [ $Q ]" + # log_msg "created msmtp queue dir [ $Q ]" + #fi + [ -d "$Q" ] || \ + log_msg -e 0 "can't find msmtp queue [ $Q ]" +} # queue dir not present - complain + +# enqueue a mail +enqueue_mail() { # <-- all mail args ; mail text via TMP + local BASE="${Q}/$(/bin/date +%Y-%m-%d-%H.%M.%S)" # make base filename for queue + local -i INC RC # increment counter for basename collision + + if [ -f "$BASE.*" ] ; then # ensure base filename is unique + INC=1 # initial increment + while [ -f "${BASE}-${INC}.*" ] ; do # base filename exists + (( ++INC )) # filename exists ; bump increment + done + BASE="${BASE}-${INC}" # unique ; set base filename + fi + + # write msmtp command line to .msmtp queue file + echo "$@" > "${BASE}.msmtp" + RC=$? # take exit code + [ $RC != 0 ] && \ + log_msg -e $RC "queueing - writing msmtp cmd line [ $* ] to [ ${BASE}.msmtp ] : failed" + + # mv temp fil to be mail body fil + /bin/mv "$TMP" "${BASE}.mail" + RC=$? # take exit code + if [ $RC == 0 ] ; then # queueing was successful + log_msg "enqueued mail as : $BASE [ $* ] : successful" + else # write failed + log_msg -e $RC "queueing - creating mail body file [ ${BASE}.mail ] : failed" + fi +} + +# send a mail (if possible, otherwise enqueue it) +# if send is successful, msmtp itself will log it (if enabled in .msmtprc) +send_mail() { # <-- all mail args ; mail text via TMP + local -i RC # msmtp return code + + #if /bin/ping -qnc 1 -w 2 64.233.183.147 &> /dev/null ; then # ping ip addr of + # # www.google.com + # /bin/cat "$TMP" | /usr/bin/msmtp "$@" > /dev/null # send mail using temp file + # RC=$? # take exit code + # if [ $RC == 0 ] ; then # send was successful + # log_msg "mail sent for [ $* ] : successful" + # exit 0 # msmtp will log it + # else # send not ok - log msg + # log_msg "mail sent for [ $* ] : unsuccessful ; msmtp exit code was $RC" + # enqueue_mail "$@" # enqueue the mail + # fi + #else # not connected to net ; log msg + log_msg "mail sent for [ $* ] : enqueued" + enqueue_mail "$@" # enqueue the mail + log_msg "running msmtpq -r in background" + nohup ~/bin/msmtpq -r & + log_msg "done!" + #fi +} + +cleanup() { # remove temporary message file + [ -e "$TMP" ] && /bin/rm -f "$TMP" +} + +# +## -- entry point +# + +[ -z "$1" ] && log_msg 'msmtpQ was invoked with no cmd line args ; why was that ?' + +check_queue # check that queue directory is present ... + +trap cleanup 0 1 2 3 6 9 14 15 +TMP="$(/bin/mktemp -qt msmtpQ.tmp.XXXXXXXXXX)" || \ + log_msg -e 'could not create temp file' # make temp file +/bin/cat > "$TMP" # take piped mail into temp fil + +send_mail "$@" # send the mail if possible, queue it if not +