3 # mailnotify (GNU cvs-utils) version 0.2
4 # Written by Gary V. Vaughan <gary@gnu.org>
6 # Copyright (C) 2004 Free Software Foundation, Inc.
7 # This is free software; see the source for copying conditions. There is NO
8 # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, a copy can be downloaded from
22 # http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
23 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # Usage: $progname [OPTION]... [--] to-address...
28 # -C ADDR --carbon-copy=ADDR send a carbon-copy to ADDR
29 # -F ADDR --from=ADDR override default from address with ADDR
30 # -f FILE --filename=FILE content of this part
31 # -m TYPE --mime-type=TYPE mime-type of this part
32 # -n another mime part (-f, -m) to follow
33 # -o FILE --output-file=FILE output to FILE instead of sending
34 # -s TEXT --subject=TEXT set subject header
35 # -v --verbose run in verbose mode
36 # --version print version information
37 # -h,-? --help print short or long help message
39 # Assemble a (possibly multi-part) mime message and hand it to the local
40 # sendmail for onward delivery. MUAs tend to mangle patch attachments in
41 # various ways: not setting the mime-type correctly, line wrapping the
42 # patch itself, escaping certain values, etc. This script is designed to
43 # make it easier to send a patch as a MIME attachment, though it is general
44 # enough that it might be useful otherwise.
46 # For example to send a patch as an attachment, assuming the patch itself
49 # echo 'Applied to HEAD' > body
50 # $progname -f body -m text/plain -n -f PATCHFILE -m text/x-patch \
51 # -s 'FYI: PATCHFILE' patch-list@foo.org
53 # You will probably find using this script in conjunction with clcommit
54 # or cvsapply will save you an awful lot of typing.
56 # Report bugs to <gary@gnu.org>
67 # Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh
68 # is ksh but when the shell is invoked as "sh" and the current value of
69 # the _XPG environment variable is not equal to 1 (one), the special
70 # positional parameter $0, within a function call, is the name of the
74 # The name of this program:
75 progname
=`echo "$progpath" | $SED "$basename"`
87 sed_mail_address
='s,^.*<\(.*\)>.*$,\1,'
90 # Echo program name prefixed message.
93 echo $progname: ${1+"$@"}
97 # Echo program name prefixed message to standard error.
100 echo $progname: ${1+"$@"} 1>&2
103 # func_fatal_error arg...
104 # Echo program name prefixed message to standard error, and exit.
111 # func_verbose arg...
112 # Echo program name prefixed message in verbose mode only.
115 $opt_verbose && func_error
${1+"$@"}
118 # func_fatal_help arg...
119 # Echo program name prefixed message to standard error, followed by
120 # a help hint, and exit.
124 func_fatal_error
"Try \`$progname --help' for more information."
127 # func_missing_arg argname
128 # Echo program name prefixed message to standard error and set global
132 func_error
"missing argument for $1"
136 # Echo short help message to standard output and exit.
139 $SED '/^# Usage:/,/# -h/ {
141 s/\$progname/'$progname'/;
145 echo "run \`$progname --help | more' for full usage"
150 # Echo long help message to standard output and exit.
153 $SED '/^# Usage:/,/# Report bugs to/ {
155 s/\$progname/'$progname'/;
162 # Echo version message to standard output and exit.
165 $SED '/^# '$PROGRAM' (GNU /,/# warranty; / {
167 s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/;
173 # Parse options once, thoroughly. This comes as soon as possible in
174 # the script to make things like `mailnotify --version' happen quickly.
177 my_sed_single_opt
='1s/^\(..\).*$/\1/;q'
178 my_sed_single_rest
='1s/^..\(.*\)$/\1/;q'
179 my_sed_long_opt
='1s/^\(--[^=]*\)=.*/\1/;q'
180 my_sed_long_arg
='1s/^--[^=]*=//'
182 while test $# -gt 0; do
186 -C|
--carbon-copy) test $# -eq 0 && func_missing_arg
"$opt" && break
191 -F|
--from) test $# -eq 0 && func_missing_arg
"$opt" && break
196 -f|
--filename) test $# -eq 0 && func_missing_arg
"$opt" && break
197 if test -f "$1"; then :; else
198 func_error
"\`$1' does not exist"
202 eval datafile
$multipart=\"$1\"
206 -m|
--mime-type) test $# -eq 0 && func_missing_arg
"$opt" && break
209 */*) func_error
"only text/* mime-types supported"
211 *) func_error
"invalid mime-type, \`$1'"
215 eval ctype
$multipart=\"$1\"
219 -n) if eval test -z \"\
$ctype$multipart\" ||
220 eval test -z \"\
$datafile$multipart\"; then
221 func_fatal_error
"One part is incomplete -- each part needs a filename and a mime-type"
223 multipart
=`expr 1 + $multipart`
226 -o|
--output-file) test $# -eq 0 && func_missing_arg
"$opt" && break
231 -s|
--subject) test $# -eq 0 && func_missing_arg
"$opt" && break
236 -v|
--verbose) opt_verbose
=: ;;
238 # Separate optargs to long options:
239 --carbon-copy=*|
--from=*|
--filename=*|
--mime-type=*|
--output-file=*|
--subject=*)
240 arg
=`echo "$opt" | $SED "$my_sed_long_arg"`
241 opt
=`echo "$opt" | $SED "$my_sed_long_opt"`
242 set -- "$opt" "$arg" ${1+"$@"}
245 # Separate optargs to short options:
246 -C*|
-F*|
-f*|
-m*|
-o*|
-s*)
247 arg
=`echo "$opt" |$SED "$my_sed_single_rest"`
248 opt
=`echo "$opt" |$SED "$my_sed_single_opt"`
249 set -- "$opt" "$arg" ${1+"$@"}
252 # Separate non-argument short options:
254 rest
=`echo "$opt" |$SED "$my_sed_single_rest"`
255 opt
=`echo "$opt" |$SED "$my_sed_single_opt"`
256 set -- "$opt" "-$rest" ${1+"$@"}
259 -\?|
-h) func_usage
;;
261 --version) func_version
;;
263 -*) func_fatal_help
"unrecognized option \`$opt'" ;;
264 *) set -- "$opt" ${1+"$@"}; break ;;
269 func_fatal_help
"no destination address"
271 if test -z "$outputfile"; then
272 if test -z "$subject" ||
273 eval test -z \"\
$ctype$multipart\" ||
274 eval test -z \"\
$datafile$multipart\"; then
275 func_fatal_error
"if output is not directed to a file -s, -f, and -m are all required"
278 eval test -n \"\
$datafile$multipart\" ||
279 func_fatal_error
"-f is required."
280 eval test -n \"\
$ctype$multipart\" ||
281 func_fatal_error
"with output directed to a file, -m is required"
283 eval test -f \"\
$datafile$multipart\" ||
284 eval func_fatal_error
\"\
$datafile$multipart: file not found
\"
288 # func_headers outfile destination
289 # Generate common headers to OUTFILE, where DESTINATION is a comma
290 # separated list of fully qualified destination addresses.
295 my_sed_version_no
='/^# '$PROGRAM' (GNU / { s/^# .*version //; p; }; d'
298 echo "User-Agent: $PROGRAM/`$SED \"$my_sed_version_no\" < $progpath`"
299 echo "MIME-Version: 1.0"
300 test -n "$from" && echo "From: $from"
301 echo "To: $my_destination"
302 test -n "$cc" && echo "CC: $cc"
303 test -n "$subject" && echo "Subject: $subject"
308 # func_single_content outfile
309 # Send the only message part as a single mime mail part.
310 func_single_content
()
314 cat >> "$my_outfile" <<EOF
315 Content-Type: $ctype1
316 Content-Transfer-Encoding: 7bit
323 # func_multipart_content outfile
324 # Send the various message parts to OUTFILE as a multipart mime mail.
325 func_multipart_content
()
328 boundary
="Boundary-${HOST}-$$-`date | tr ' :' -`"
329 cat <<EOF >> "$my_outfile"
330 Content-Type: Multipart/Mixed;
333 This is a multimedia message in MIME format. If you are reading
334 this prefix, your mail reader does not understand MIME. You may
335 wish to look into upgrading to a mime-aware mail reader.
338 while test $i -lt $multipart
344 eval echo \"Content-Type
: \
$ctype$i\"
345 echo "Content-Transfer-Encoding: 7bit"
347 eval cat \"\
$datafile$i\"
352 echo "--${boundary}--"
358 # func_sendmail infile destination [from]
359 # Send the message in INFILE to the space delimited list of destination
360 # addresses in DESTINATION. If FROM is given, it is the address the
361 # mail purports to be from.
362 # BEWARE: Many MTAs will refuse mail where FROM does not match the actual
370 from_name
=`echo "$my_from" | sed 's, *<.*> *$,,;s,",,g'`
371 from_addr
=`echo "$my_from" | sed "$sed_mail_address"`
374 for try_sendmail
in sendmail
/usr
/lib
/sendmail
/usr
/sbin
/sendmail
; do
375 if which $try_sendmail >/dev
/null
; then
376 SENDMAIL
=$try_sendmail
381 func_verbose
"Delivering mail, please wait..."
382 if test -n "$from_name"; then
383 $SENDMAIL -F "$from_name" -f "$from_addr" $my_destination < "$my_infile"
384 elif test -n "$from_addr"; then
385 $SENDMAIL -f "$from_addr" $my_destination < "$my_infile"
387 $SENDMAIL $my_destination < "$my_infile"
389 if test $?
-eq 0; then
390 func_verbose
"...succeeded."
393 func_fatal_error
"Mail delivery failed, draft mail is in $my_infile"
404 fname
="${TMPDIR}/$PROGRAM$RANDOM-$RANDOM.$$"
405 trap 'rm -f "$fname"; exit 1' 1 2 15
411 "") destination
="$to" ;;
412 *) destination
="$destination, $to" ;;
416 func_headers
"$fname" "$destination"
417 if test $multipart -gt 1; then
418 func_multipart_content
"$fname"
420 func_single_content
"$fname"
423 if test -z "$outputfile"; then
426 to_addr
=`echo "$to" | sed "$sed_mail_address"`
427 test -n "$to_addr" || to_addr
="$to"
428 destination
="$destination $to_addr"
430 func_sendmail
"$fname" "$destination" "$from"
432 mv $fname $outputfile ||
exit $EXIT_FAILURE