Gnulib has changed again. Reimport. Adjust. Rinse. Repeat.
[m4/ericb.git] / ltdl / config / mailnotify
bloba362f1b8e2cd4551a142eb5b86c5eda3ddd6a4e0
1 #!/bin/sh
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,
24 # MA 02110-1301, USA.
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
47 # is in PATCHFILE:
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>
58 : ${TMPDIR=/tmp}
59 : ${HOST=`hostname`}
60 : ${MV="mv -f"}
61 : ${RM="rm -f"}
62 : ${SED="sed"}
64 dirname="s,/[^/]*$,,"
65 basename="s,^.*/,,g"
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
71 # function.
72 progpath="$0"
74 # The name of this program:
75 progname=`echo "$progpath" | $SED "$basename"`
76 PROGRAM=mailnotify
78 # Global variables:
79 EXIT_SUCCESS=0
80 EXIT_FAILURE=1
82 multipart=1
83 outputfile=""
85 exit_cmd=:
87 sed_mail_address='s,^.*<\(.*\)>.*$,\1,'
89 # func_echo arg...
90 # Echo program name prefixed message.
91 func_echo ()
93 echo $progname: ${1+"$@"}
96 # func_error arg...
97 # Echo program name prefixed message to standard error.
98 func_error ()
100 echo $progname: ${1+"$@"} 1>&2
103 # func_fatal_error arg...
104 # Echo program name prefixed message to standard error, and exit.
105 func_fatal_error ()
107 func_error ${1+"$@"}
108 exit $EXIT_FAILURE
111 # func_verbose arg...
112 # Echo program name prefixed message in verbose mode only.
113 func_verbose ()
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.
121 func_fatal_help ()
123 func_error ${1+"$@"}
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
129 # exit_cmd.
130 func_missing_arg ()
132 func_error "missing argument for $1"
133 exit_cmd=exit
136 # Echo short help message to standard output and exit.
137 func_usage ()
139 $SED '/^# Usage:/,/# -h/ {
140 s/^# //; s/^# *$//;
141 s/\$progname/'$progname'/;
143 }; d' < "$progpath"
144 echo
145 echo "run \`$progname --help | more' for full usage"
146 exit $EXIT_SUCCESS
149 # func_help
150 # Echo long help message to standard output and exit.
151 func_help ()
153 $SED '/^# Usage:/,/# Report bugs to/ {
154 s/^# //; s/^# *$//;
155 s/\$progname/'$progname'/;
157 }; d' < "$progpath"
158 exit $EXIT_SUCCESS
161 # func_version
162 # Echo version message to standard output and exit.
163 func_version ()
165 $SED '/^# '$PROGRAM' (GNU /,/# warranty; / {
166 s/^# //; s/^# *$//;
167 s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/;
169 }; d' < "$progpath"
170 exit $EXIT_SUCCESS
173 # Parse options once, thoroughly. This comes as soon as possible in
174 # the script to make things like `mailnotify --version' happen quickly.
176 # sed scripts:
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
183 opt="$1"
184 shift
185 case $opt in
186 -C|--carbon-copy) test $# -eq 0 && func_missing_arg "$opt" && break
187 cc="$1"
188 shift
191 -F|--from) test $# -eq 0 && func_missing_arg "$opt" && break
192 from="$1"
193 shift
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"
199 exit_cmd=exit
200 break
202 eval datafile$multipart=\"$1\"
203 shift
206 -m|--mime-type) test $# -eq 0 && func_missing_arg "$opt" && break
207 case $1 in
208 text/*) ;;
209 */*) func_error "only text/* mime-types supported"
211 *) func_error "invalid mime-type, \`$1'"
212 exit_cmd=exit
214 esac
215 eval ctype$multipart=\"$1\"
216 shift
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
227 outputfile="$1"
228 shift
231 -s|--subject) test $# -eq 0 && func_missing_arg "$opt" && break
232 subject="$1"
233 shift
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:
253 -n*|-v*)
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 ;;
260 --help) func_help ;;
261 --version) func_version ;;
262 --) break ;;
263 -*) func_fatal_help "unrecognized option \`$opt'" ;;
264 *) set -- "$opt" ${1+"$@"}; break ;;
265 esac
266 done
268 test $# -gt 0 ||
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"
277 else
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.
291 func_headers ()
293 my_outfile="$1"
294 my_destination="$2"
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"
304 } > "$my_outfile"
308 # func_single_content outfile
309 # Send the only message part as a single mime mail part.
310 func_single_content ()
312 my_outfile="$1"
314 cat >> "$my_outfile" <<EOF
315 Content-Type: $ctype1
316 Content-Transfer-Encoding: 7bit
318 `cat $datafile1`
323 # func_multipart_content outfile
324 # Send the various message parts to OUTFILE as a multipart mime mail.
325 func_multipart_content ()
327 my_outfile="$1"
328 boundary="Boundary-${HOST}-$$-`date | tr ' :' -`"
329 cat <<EOF >> "$my_outfile"
330 Content-Type: Multipart/Mixed;
331 boundary="$boundary"
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
340 i=`expr 1 + $i`
342 echo ""
343 echo "--$boundary"
344 eval echo \"Content-Type: \$ctype$i\"
345 echo "Content-Transfer-Encoding: 7bit"
346 echo ""
347 eval cat \"\$datafile$i\"
348 } >> "$my_outfile"
349 done
351 echo ""
352 echo "--${boundary}--"
353 echo ""
354 } >> "$my_outfile"
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
363 # sending domain.
364 func_sendmail ()
366 my_infile="$1"
367 my_destination="$2"
368 my_from="$3"
370 from_name=`echo "$my_from" | sed 's, *<.*> *$,,;s,",,g'`
371 from_addr=`echo "$my_from" | sed "$sed_mail_address"`
373 SENDMAIL=sendmail
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
377 break
379 done
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"
386 else
387 $SENDMAIL $my_destination < "$my_infile"
389 if test $? -eq 0; then
390 func_verbose "...succeeded."
391 rm $my_infile
392 else
393 func_fatal_error "Mail delivery failed, draft mail is in $my_infile"
399 ## ----- ##
400 ## main. ##
401 ## ----- ##
404 fname="${TMPDIR}/$PROGRAM$RANDOM-$RANDOM.$$"
405 trap 'rm -f "$fname"; exit 1' 1 2 15
407 destination=""
408 for to;
410 case $destination in
411 "") destination="$to" ;;
412 *) destination="$destination, $to" ;;
413 esac
414 done
416 func_headers "$fname" "$destination"
417 if test $multipart -gt 1; then
418 func_multipart_content "$fname"
419 else
420 func_single_content "$fname"
423 if test -z "$outputfile"; then
424 destination=""
425 for to; do
426 to_addr=`echo "$to" | sed "$sed_mail_address"`
427 test -n "$to_addr" || to_addr="$to"
428 destination="$destination $to_addr"
429 done
430 func_sendmail "$fname" "$destination" "$from"
431 else
432 mv $fname $outputfile || exit $EXIT_FAILURE
436 exit $EXIT_SUCCESS
438 # Local Variables:
439 # mode:shell-script
440 # sh-indentation:2
441 # End: