Removed support for zip from sendfiles. I added it as part of the
[mmh] / etc / sendfiles
1 #!/bin/sh
2 #
3 # Sends multiple files and/or directories in a MIME message.
4 # Requires tar and any specified compression program.
5 #
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.
9
10 usage='Usage: sendfiles [switches] -to recipient -subject subject '"\
11 "'file1 [file2 ...]
12   or
13        sendfiles [switches] recipient subject file1 [file2 ...]
14   switches are:
15   -compress [bzip2 | compress | gzip | lzma | none]
16   -from <sender>
17   -[delay] <delay> (expressed in seconds)
18   -version
19   -help
20   Can use PERSON environment variable instead of -from switch.'
21
22
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.
25 finddir() {
26   case $1 in
27     */*) dirname "$1" ;;
28     *  ) IFS=:
29          for d in $PATH; do
30            [ -f "${d:=.}/$1"  -a  -x "$d/$1" ]  &&  printf %s "$d"  &&  break
31          done ;;
32   esac
33 }
34
35 die() {
36   printf '%s\n' "$usage"; exit ${1:-1}
37 }
38
39 bindir=`finddir $0`
40 nmhbindir=`cd "$bindir" && pwd`
41 nmhlibdir=`$nmhbindir/mhparam libdir`
42
43
44 #### Process switches.
45 compress=                   ## compress method
46 compressarg=0               ## whether currently handling -compress
47 delay=                      ## delay value
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
53 to=                         ## To: address
54 toarg=0                     ## whether currently handling -to
55 for arg in "$@"; do
56   case $arg in
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 ;;
68     -t|-to) toarg=1 ;;
69     -v|-ve|-ver|-vers|-versi|-versio|-version)
70        "$nmhlibdir/viamail" -version | sed 's/viamail/sendfiles/'; exit ;;
71     -*) die ;;
72     *) if [ $compressarg -eq 1 ]; then
73          compress="$arg"
74          compressarg=0
75        elif [ $delayarg -eq 1 ]; then
76          delay="$arg"
77          delayarg=0
78        elif [ $fromarg -eq 1 ]; then
79          from="$arg"
80          fromarg=0
81        elif [ $subjectarg -eq 1 ]; then
82          subject="$arg"
83          subjectarg=0
84        elif [ $toarg -eq 1 ]; then
85          to="$arg"
86          toarg=0
87        else
88          #### Argument doesn't apply to a switch, so we're done with switches.
89          break
90        fi ;;
91   esac
92   shift
93 done
94
95 #### Check for switch after non-switch argument.
96 for arg in "$@"; do
97   case $arg in
98     -*) die ;;
99   esac
100 done
101
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
106       to="$1"; shift
107       subject="$1"; shift
108     else
109       die
110     fi
111   else
112     die
113   fi
114 else
115   [ x"$subject" = x  -o  $# -lt 1 ]  &&  die
116 fi
117
118 #### Check for missing mandatory arguments.
119 checkforargs() {
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
130   fi
131 }
132
133 checkforargs
134 [ $# -eq 0 ]  &&  die
135
136
137 if [ x"$from" = x ]; then
138   if [ x"$PERSON" = x ]; then
139     from=`"$nmhlibdir/ap" -format '%(localmbox)' 0`
140   else
141     from="$PERSON"
142   fi
143 fi
144
145
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"
151       break
152     fi
153   done
154 fi
155
156 case $compress in
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' \
166                    "$compress" >&2
167             die ;;
168 esac
169
170
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