]> diplodocus.org Git - nmh/blob - etc/sendfiles
Simplified m_strn() per Ralph's suggestions.
[nmh] / 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 help() {
36 printf '%s\n' "$usage"
37 #### Print the nmh intro text.
38 ${nmhbindir}/mhparam -help | sed -n -e '/^$/,$p'
39 exit
40 }
41
42 die() {
43 printf '%s\n' "$usage"
44 exit ${1:-1}
45 }
46
47 bindir=`finddir $0`
48 nmhbindir=`cd "$bindir" && pwd`
49 nmhlibexecdir=`$nmhbindir/mhparam libexecdir`
50
51
52 #### Process switches.
53 compress= ## compress method
54 compressarg=0 ## whether currently handling -compress
55 delay= ## delay value
56 delayarg=0 ## whether currently handling -delay
57 from= ## From: contents
58 fromarg=0 ## whether currently handling -from
59 subject= ## Subject: contents
60 subjectarg=0 ## whether currently handling -subject
61 to= ## To: address
62 toarg=0 ## whether currently handling -to
63 for arg in "$@"; do
64 case $arg in
65 -c|-co|-com|-comp|-compr|-compre|-compres|-compress) compressarg=1 ;;
66 -d|-de|-del|-dela|-delay) delayarg=1 ;;
67 -[0-9]|-[0-9][0-9]|-[0-9][0-9][0-9]|-[0-9][0-9][0-9][0-9])
68 delay=`printf '%s\n' "$arg" | sed -e 's%-%%'` ;;
69 -f|-fr|-fro|-from) fromarg=1 ;;
70 #### Support -gzip for backward compatibility.
71 -gzip) compress=gzip ;;
72 -h|-he|-hel|-help) help ;;
73 #### Support -none for backward compatibility.
74 -none) compress=none ;;
75 -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
76 -t|-to) toarg=1 ;;
77 -v|-ve|-ver|-vers|-versi|-versio|-version)
78 "$nmhlibexecdir/viamail" -version | sed 's/viamail/sendfiles/'; exit ;;
79 -*) die ;;
80 *) if [ $compressarg -eq 1 ]; then
81 compress="$arg"
82 compressarg=0
83 elif [ $delayarg -eq 1 ]; then
84 delay="$arg"
85 delayarg=0
86 elif [ $fromarg -eq 1 ]; then
87 from="$arg"
88 fromarg=0
89 elif [ $subjectarg -eq 1 ]; then
90 subject="$arg"
91 subjectarg=0
92 elif [ $toarg -eq 1 ]; then
93 to="$arg"
94 toarg=0
95 else
96 #### Argument doesn't apply to a switch, so we're done with switches.
97 break
98 fi ;;
99 esac
100 shift
101 done
102
103 #### Check for switch after non-switch argument.
104 for arg in "$@"; do
105 case $arg in
106 -*) die ;;
107 esac
108 done
109
110 #### Check for required arguments (to, subject, file(s)).
111 if [ x"$to" = x ]; then
112 if [ x"$subject" = x ]; then
113 if [ $# -ge 3 ]; then
114 to="$1"; shift
115 subject="$1"; shift
116 else
117 die
118 fi
119 else
120 die
121 fi
122 else
123 [ x"$subject" = x -o $# -lt 1 ] && die
124 fi
125
126 #### Check for missing mandatory arguments.
127 checkforargs() {
128 if [ $compressarg -eq 1 ]; then
129 printf 'sendfiles: missing argument to -compress\n' >&2; exit 1
130 elif [ $delayarg -eq 1 ]; then
131 printf 'sendfiles: missing argument to -delay\n' >&2; exit 1
132 elif [ $fromarg -eq 1 ]; then
133 printf 'sendfiles: missing argument to -from\n' >&2; exit 1
134 elif [ $subjectarg -eq 1 ]; then
135 printf 'sendfiles: missing argument to -subject\n' >&2; exit 1
136 elif [ $toarg -eq 1 ]; then
137 printf 'sendfiles: missing argument to -to\n' >&2; exit 1
138 fi
139 }
140
141 checkforargs
142 [ $# -eq 0 ] && die
143
144
145 if [ x"$from" = x ]; then
146 if [ x"$PERSON" = x ]; then
147 from=`"$nmhlibexecdir/ap" -format '%(localmbox)' 0`
148 else
149 from="$PERSON"
150 fi
151 fi
152
153
154 #### Determine compression method and descriptive info.
155 if [ x"$compress" = x ]; then
156 for compressor in gzip bzip2 lzma compress none; do
157 if [ x"`finddir $compressor`" = x ]; then :; else
158 compress="$compressor"
159 break
160 fi
161 done
162 fi
163
164 case $compress in
165 bzip2) uncompress=bzcat; conversion='; x-conversions=bzip2' ;;
166 compress) compress='compress -c'; uncompress='uncompress -c';
167 conversion='; x-conversions=compress' ;;
168 gzip) compress='gzip -c'; uncompress='gzip -cd'
169 conversion='; x-conversions=gzip' ;;
170 lzma) compress='lzma -c'; uncompress='lzma -cd'
171 conversion='; x-conversions=lzma' ;;
172 none) compress=cat uncompress=cat; conversion= ;;
173 *) printf 'sendfiles: unknown compression method "%s"\n' \
174 "$compress" >&2
175 die ;;
176 esac
177
178
179 #### Send using viamail.
180 tar cvf - "$@" | $compress | \
181 "$nmhlibexecdir/viamail" -to "$to" -subject "$subject" \
182 -from "$from" -parameters "type=tar$conversion" \
183 -comment "extract with $uncompress | tar xvpf -" \
184 -delay "$delay" -verbose