3 # Sends multiple files and/or directories in a MIME message.
4 # Requires tar and any specified compression program.
6 # This code is Copyright (c) 2012, by the authors of nmh. See the
7 # COPYRIGHT file in the root directory of the nmh distribution for
8 # complete copyright information.
10 usage='Usage: sendfiles [switches] -to recipient -subject subject '"\
13 sendfiles [switches] recipient subject file1 [file2 ...]
15 -compress [bzip2 | compress | gzip | lzma | none]
17 -[delay] <delay> (expressed in seconds)
20 Can use PERSON environment variable instead of -from switch.'
23 #### Find location of a program. Bourne shell just puts the name in
24 #### $0 if it's found from the PATH, so search that if necessary.
30 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
36 printf '%s\n' "$usage"; exit ${1:-1}
40 nmhbindir=`cd "$bindir" && pwd`
41 nmhlibdir=`$nmhbindir/mhparam libdir`
44 #### Process switches.
45 compress= ## compress method
46 compressarg=0 ## whether currently handling -compress
48 delayarg=0 ## whether currently handling -delay
49 from= ## From: contents
50 fromarg=0 ## whether currently handling -from
51 subject= ## Subject: contents
52 subjectarg=0 ## whether currently handling -subject
54 toarg=0 ## whether currently handling -to
57 -c|-co|-com|-comp|-compr|-compre|-compres|-compress) compressarg=1 ;;
58 -d|-de|-del|-dela|-delay) delayarg=1 ;;
59 -[0-9]|-[0-9][0-9]|-[0-9][0-9][0-9]|-[0-9][0-9][0-9][0-9])
60 delay=`printf '%s\n' "$arg" | sed -e 's%-%%'` ;;
61 -f|-fr|-fro|-from) fromarg=1 ;;
62 #### Support -gzip for backward compatibility.
63 -gzip) compress=gzip ;;
64 -h|-he|-hel|-help) die 0 ;;
65 #### Support -none for backward compatibility.
66 -none) compress=none ;;
67 -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
69 -v|-ve|-ver|-vers|-versi|-versio|-version)
70 "$nmhlibdir/viamail" -version | sed 's/viamail/sendfiles/'; exit ;;
72 *) if [ $compressarg -eq 1 ]; then
75 elif [ $delayarg -eq 1 ]; then
78 elif [ $fromarg -eq 1 ]; then
81 elif [ $subjectarg -eq 1 ]; then
84 elif [ $toarg -eq 1 ]; then
88 #### Argument doesn't apply to a switch, so we're done with switches.
95 #### Check for switch after non-switch argument.
102 #### Check for required arguments (to, subject, file(s)).
103 if [ x"$to" = x ]; then
104 if [ x"$subject" = x ]; then
105 if [ $# -ge 3 ]; then
115 [ x"$subject" = x -o $# -lt 1 ] && die
118 #### Check for missing mandatory arguments.
120 if [ $compressarg -eq 1 ]; then
121 printf 'sendfiles: missing argument to -compress\n' >&2; exit 1
122 elif [ $delayarg -eq 1 ]; then
123 printf 'sendfiles: missing argument to -delay\n' >&2; exit 1
124 elif [ $fromarg -eq 1 ]; then
125 printf 'sendfiles: missing argument to -from\n' >&2; exit 1
126 elif [ $subjectarg -eq 1 ]; then
127 printf 'sendfiles: missing argument to -subject\n' >&2; exit 1
128 elif [ $toarg -eq 1 ]; then
129 printf 'sendfiles: missing argument to -to\n' >&2; exit 1
137 if [ x"$from" = x ]; then
138 if [ x"$PERSON" = x ]; then
139 from=`"$nmhlibdir/ap" -format '%(localmbox)' 0`
146 #### Determine compression method and descriptive info.
147 if [ x"$compress" = x ]; then
148 for compressor in gzip bzip2 lzma compress none; do
149 if [ x"`finddir $compressor`" = x ]; then :; else
150 compress="$compressor"
157 bzip2) uncompress=bzcat; conversion='; x-conversions=bzip2' ;;
158 compress) compress='compress -c'; uncompress='uncompress -c';
159 conversion='; x-conversions=compress' ;;
160 gzip) compress='gzip -c'; uncompress='gzip -cd'
161 conversion='; x-conversions=gzip' ;;
162 lzma) compress='lzma -c'; uncompress='lzma -cd'
163 conversion='; x-conversions=lzma' ;;
164 none) compress=cat uncompress=cat; conversion= ;;
165 *) printf 'sendfiles: unknown compression method "%s"\n' \
171 #### Send using viamail.
172 tar cvf - "$@" | $compress | \
173 "$nmhlibdir/viamail" -to "$to" -subject "$subject" \
174 -from "$from" -parameters "type=tar$conversion" \
175 -comment "extract with $uncompress | tar xvpf -" \
176 -delay "$delay" -verbose