| #!/bin/sh | |
| # | |
| # mailnotify (GNU cvs-utils) version 0.2 | |
| # Written by Gary V. Vaughan <gary@gnu.org> | |
| # Copyright (C) 2004, 2006 Free Software Foundation, Inc. | |
| # This is free software; see the source for copying conditions. There is NO | |
| # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| # 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 2 of the License, or | |
| # (at your option) any later version. | |
| # | |
| # This program is distributed in the hope that it will be useful, but | |
| # WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| # General Public License for more details. | |
| # | |
| # You should have received a copy of the GNU General Public License | |
| # along with this program; if not, a copy can be downloaded from | |
| # http://www.gnu.org/copyleft/gpl.html, or by writing to the Free | |
| # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
| # MA 02110-1301, USA. | |
| # Usage: $progname [OPTION]... [--] to-address... | |
| # | |
| # -C ADDR --carbon-copy=ADDR send a carbon-copy to ADDR | |
| # -F ADDR --from=ADDR override default from address with ADDR | |
| # -f FILE --filename=FILE content of this part | |
| # -m TYPE --mime-type=TYPE mime-type of this part | |
| # -n another mime part (-f, -m) to follow | |
| # -o FILE --output-file=FILE output to FILE instead of sending | |
| # -s TEXT --subject=TEXT set subject header | |
| # -v --verbose run in verbose mode | |
| # --version print version information | |
| # -h,-? --help print short or long help message | |
| # Assemble a (possibly multi-part) mime message and hand it to the local | |
| # sendmail for onward delivery. MUAs tend to mangle patch attachments in | |
| # various ways: not setting the mime-type correctly, line wrapping the | |
| # patch itself, escaping certain values, etc. This script is designed to | |
| # make it easier to send a patch as a MIME attachment, though it is general | |
| # enough that it might be useful otherwise. | |
| # For example to send a patch as an attachment, assuming the patch itself | |
| # is in PATCHFILE: | |
| # | |
| # echo 'Applied to HEAD' > body | |
| # $progname -f body -m text/plain -n -f PATCHFILE -m text/x-patch \ | |
| # -s 'FYI: PATCHFILE' patch-list@foo.org | |
| # You will probably find using this script in conjunction with clcommit | |
| # or cvsapply will save you an awful lot of typing. | |
| # Report bugs to <gary@gnu.org> | |
| : ${TMPDIR=/tmp} | |
| : ${HOST=`hostname`} | |
| : ${MV="mv -f"} | |
| : ${RM="rm -f"} | |
| : ${SED="sed"} | |
| dirname="s,/[^/]*$,," | |
| basename="s,^.*/,,g" | |
| # Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh | |
| # is ksh but when the shell is invoked as "sh" and the current value of | |
| # the _XPG environment variable is not equal to 1 (one), the special | |
| # positional parameter $0, within a function call, is the name of the | |
| # function. | |
| progpath="$0" | |
| # The name of this program: | |
| progname=`echo "$progpath" | $SED $basename` | |
| PROGRAM=mailnotify | |
| # Global variables: | |
| EXIT_SUCCESS=0 | |
| EXIT_FAILURE=1 | |
| multipart=1 | |
| outputfile="" | |
| exit_cmd=: | |
| sed_mail_address='s,^.*<\(.*\)>.*$,\1,' | |
| # func_echo arg... | |
| # Echo program name prefixed message. | |
| func_echo () | |
| { | |
| echo $progname: ${1+"$@"} | |
| } | |
| # func_error arg... | |
| # Echo program name prefixed message to standard error. | |
| func_error () | |
| { | |
| echo $progname: ${1+"$@"} 1>&2 | |
| } | |
| # func_fatal_error arg... | |
| # Echo program name prefixed message to standard error, and exit. | |
| func_fatal_error () | |
| { | |
| func_error ${1+"$@"} | |
| exit $EXIT_FAILURE | |
| } | |
| # func_verbose arg... | |
| # Echo program name prefixed message in verbose mode only. | |
| func_verbose () | |
| { | |
| $opt_verbose && func_error ${1+"$@"} | |
| } | |
| # func_fatal_help arg... | |
| # Echo program name prefixed message to standard error, followed by | |
| # a help hint, and exit. | |
| func_fatal_help () | |
| { | |
| func_error ${1+"$@"} | |
| func_fatal_error "Try \`$progname --help' for more information." | |
| } | |
| # func_missing_arg argname | |
| # Echo program name prefixed message to standard error and set global | |
| # exit_cmd. | |
| func_missing_arg () | |
| { | |
| func_error "missing argument for $1" | |
| exit_cmd=exit | |
| } | |
| # Echo short help message to standard output and exit. | |
| func_usage () | |
| { | |
| $SED '/^# Usage:/,/# -h/ { | |
| s/^# // | |
| s/^# *$// | |
| s/\$progname/'$progname'/ | |
| p | |
| }; d' < "$progpath" | |
| echo | |
| echo "run \`$progname --help | more' for full usage" | |
| exit $EXIT_SUCCESS | |
| } | |
| # func_help | |
| # Echo long help message to standard output and exit. | |
| func_help () | |
| { | |
| $SED '/^# Usage:/,/# Report bugs to/ { | |
| s/^# // | |
| s/^# *$// | |
| s/\$progname/'$progname'/ | |
| p | |
| }; d' < "$progpath" | |
| exit $EXIT_SUCCESS | |
| } | |
| # func_version | |
| # Echo version message to standard output and exit. | |
| func_version () | |
| { | |
| $SED '/^# '$PROGRAM' (GNU /,/# warranty; / { | |
| s/^# // | |
| s/^# *$// | |
| s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/ | |
| p | |
| }; d' < "$progpath" | |
| exit $EXIT_SUCCESS | |
| } | |
| # Parse options once, thoroughly. This comes as soon as possible in | |
| # the script to make things like `mailnotify --version' happen quickly. | |
| { | |
| # sed scripts: | |
| my_sed_single_opt='1s/^\(..\).*$/\1/;q' | |
| my_sed_single_rest='1s/^..\(.*\)$/\1/;q' | |
| my_sed_long_opt='1s/^\(--[^=]*\)=.*/\1/;q' | |
| my_sed_long_arg='1s/^--[^=]*=//' | |
| while test $# -gt 0; do | |
| opt="$1" | |
| shift | |
| case $opt in | |
| -C|--carbon-copy) test $# -eq 0 && func_missing_arg "$opt" && break | |
| cc="$1" | |
| shift | |
| ;; | |
| -F|--from) test $# -eq 0 && func_missing_arg "$opt" && break | |
| from="$1" | |
| shift | |
| ;; | |
| -f|--filename) test $# -eq 0 && func_missing_arg "$opt" && break | |
| if test -f "$1"; then :; else | |
| func_error "\`$1' does not exist" | |
| exit_cmd=exit | |
| break | |
| fi | |
| eval datafile$multipart=\"$1\" | |
| shift | |
| ;; | |
| -m|--mime-type) test $# -eq 0 && func_missing_arg "$opt" && break | |
| case $1 in | |
| text/*) ;; | |
| */*) func_error "only text/* mime-types supported" | |
| ;; | |
| *) func_error "invalid mime-type, \`$1'" | |
| exit_cmd=exit | |
| ;; | |
| esac | |
| eval ctype$multipart=\"$1\" | |
| shift | |
| ;; | |
| -n) if eval test -z \"\$ctype$multipart\" || | |
| eval test -z \"\$datafile$multipart\"; then | |
| func_fatal_error "One part is incomplete -- each part needs a filename and a mime-type" | |
| fi | |
| multipart=`expr 1 + $multipart` | |
| ;; | |
| -o|--output-file) test $# -eq 0 && func_missing_arg "$opt" && break | |
| outputfile="$1" | |
| shift | |
| ;; | |
| -s|--subject) test $# -eq 0 && func_missing_arg "$opt" && break | |
| subject="$1" | |
| shift | |
| ;; | |
| -v|--verbose) opt_verbose=: ;; | |
| # Separate optargs to long options: | |
| --carbon-copy=*|--from=*|--filename=*|--mime-type=*|--output-file=*|--subject=*) | |
| arg=`echo "$opt" | $SED "$my_sed_long_arg"` | |
| opt=`echo "$opt" | $SED "$my_sed_long_opt"` | |
| set -- "$opt" "$arg" ${1+"$@"} | |
| ;; | |
| # Separate optargs to short options: | |
| -C*|-F*|-f*|-m*|-o*|-s*) | |
| arg=`echo "$opt" |$SED "$my_sed_single_rest"` | |
| opt=`echo "$opt" |$SED "$my_sed_single_opt"` | |
| set -- "$opt" "$arg" ${1+"$@"} | |
| ;; | |
| # Separate non-argument short options: | |
| -n*|-v*) | |
| rest=`echo "$opt" |$SED "$my_sed_single_rest"` | |
| opt=`echo "$opt" |$SED "$my_sed_single_opt"` | |
| set -- "$opt" "-$rest" ${1+"$@"} | |
| ;; | |
| -\?|-h) func_usage ;; | |
| --help) func_help ;; | |
| --version) func_version ;; | |
| --) break ;; | |
| -*) func_fatal_help "unrecognized option \`$opt'" ;; | |
| *) set -- "$opt" ${1+"$@"}; break ;; | |
| esac | |
| done | |
| test $# -gt 0 || | |
| func_fatal_help "no destination address" | |
| if test -z "$outputfile"; then | |
| if test -z "$subject" || | |
| eval test -z \"\$ctype$multipart\" || | |
| eval test -z \"\$datafile$multipart\"; then | |
| func_fatal_error "if output is not directed to a file -s, -f, and -m are all required" | |
| fi | |
| else | |
| eval test -n \"\$datafile$multipart\" || | |
| func_fatal_error "-f is required." | |
| eval test -n \"\$ctype$multipart\" || | |
| func_fatal_error "with output directed to a file, -m is required" | |
| fi | |
| eval test -f \"\$datafile$multipart\" || | |
| eval func_fatal_error \"\$datafile$multipart: file not found\" | |
| } | |
| # func_headers outfile destination | |
| # Generate common headers to OUTFILE, where DESTINATION is a comma | |
| # separated list of fully qualified destination addresses. | |
| func_headers () | |
| { | |
| my_outfile="$1" | |
| my_destination="$2" | |
| my_sed_version_no='/^# '$PROGRAM' (GNU / { | |
| s/^# .*version // | |
| p | |
| }; d' | |
| { | |
| echo "User-Agent: $PROGRAM/`$SED \"$my_sed_version_no\" < $progpath`" | |
| echo "MIME-Version: 1.0" | |
| test -n "$from" && echo "From: $from" | |
| echo "To: $my_destination" | |
| test -n "$cc" && echo "CC: $cc" | |
| test -n "$subject" && echo "Subject: $subject" | |
| } > "$my_outfile" | |
| } | |
| # func_single_content outfile | |
| # Send the only message part as a single mime mail part. | |
| func_single_content () | |
| { | |
| my_outfile="$1" | |
| cat >> "$my_outfile" <<EOF | |
| Content-Type: $ctype1 | |
| Content-Transfer-Encoding: 7bit | |
| `cat $datafile1` | |
| EOF | |
| } | |
| # func_multipart_content outfile | |
| # Send the various message parts to OUTFILE as a multipart mime mail. | |
| func_multipart_content () | |
| { | |
| my_outfile="$1" | |
| boundary="Boundary-${HOST}-$$-`date | tr ' :' -`" | |
| cat <<EOF >> "$my_outfile" | |
| Content-Type: Multipart/Mixed; | |
| boundary="$boundary" | |
| This is a multimedia message in MIME format. If you are reading | |
| this prefix, your mail reader does not understand MIME. You may | |
| wish to look into upgrading to a mime-aware mail reader. | |
| EOF | |
| i=0 | |
| while test $i -lt $multipart | |
| do | |
| i=`expr 1 + $i` | |
| { | |
| echo "" | |
| echo "--$boundary" | |
| eval echo \"Content-Type: \$ctype$i\" | |
| echo "Content-Transfer-Encoding: 7bit" | |
| echo "" | |
| eval cat \"\$datafile$i\" | |
| } >> "$my_outfile" | |
| done | |
| { | |
| echo "" | |
| echo "--${boundary}--" | |
| echo "" | |
| } >> "$my_outfile" | |
| } | |
| # func_sendmail infile destination [from] | |
| # Send the message in INFILE to the space delimited list of destination | |
| # addresses in DESTINATION. If FROM is given, it is the address the | |
| # mail purports to be from. | |
| # BEWARE: Many MTAs will refuse mail where FROM does not match the actual | |
| # sending domain. | |
| func_sendmail () | |
| { | |
| my_infile="$1" | |
| my_destination="$2" | |
| my_from="$3" | |
| from_name=`echo "$my_from" | sed 's, *<.*> *$,,;s,",,g'` | |
| from_addr=`echo "$my_from" | sed "$sed_mail_address"` | |
| SENDMAIL=sendmail | |
| for try_sendmail in sendmail /usr/lib/sendmail /usr/sbin/sendmail; do | |
| if which $try_sendmail >/dev/null; then | |
| SENDMAIL=$try_sendmail | |
| break | |
| fi | |
| done | |
| func_verbose "Delivering mail, please wait..." | |
| if test -n "$from_name"; then | |
| $SENDMAIL -F "$from_name" -f "$from_addr" $my_destination < "$my_infile" | |
| elif test -n "$from_addr"; then | |
| $SENDMAIL -f "$from_addr" $my_destination < "$my_infile" | |
| else | |
| $SENDMAIL $my_destination < "$my_infile" | |
| fi | |
| if test $? -eq 0; then | |
| func_verbose "...succeeded." | |
| rm $my_infile | |
| else | |
| func_fatal_error "Mail delivery failed, draft mail is in $my_infile" | |
| fi | |
| } | |
| ## ----- ## | |
| ## main. ## | |
| ## ----- ## | |
| { | |
| fname="${TMPDIR}/$PROGRAM$RANDOM-$RANDOM.$$" | |
| trap 'rm -f "$fname"; exit 1' 1 2 15 | |
| destination="" | |
| for to; | |
| do | |
| case $destination in | |
| "") destination="$to" ;; | |
| *) destination="$destination, $to" ;; | |
| esac | |
| done | |
| func_headers "$fname" "$destination" | |
| if test $multipart -gt 1; then | |
| func_multipart_content "$fname" | |
| else | |
| func_single_content "$fname" | |
| fi | |
| if test -z "$outputfile"; then | |
| destination="" | |
| for to; do | |
| to_addr=`echo "$to" | sed "$sed_mail_address"` | |
| test -n "$to_addr" || to_addr="$to" | |
| destination="$destination $to_addr" | |
| done | |
| func_sendmail "$fname" "$destination" "$from" | |
| else | |
| mv $fname $outputfile || exit $EXIT_FAILURE | |
| fi | |
| } | |
| exit $EXIT_SUCCESS | |
| # Local Variables: | |
| # mode:shell-script | |
| # sh-indentation:2 | |
| # End: |