]> diplodocus.org Git - nmh/blob - uip/mhmail
Added mention of filename completion at whatnow prompt if nmh
[nmh] / uip / mhmail
1 #! /bin/sh
2 #
3 # mhmail -- simple mail program
4 #
5 # This code is Copyright (c) 2012, by the authors of nmh. See the
6 # COPYRIGHT file in the root directory of the nmh distribution for
7 # complete copyright information.
8 #
9 # Emulation of compiled mhmail(1), with these differences:
10 # * Adds -send/-nosend, -headerfield, and -attach options.
11 # * Adds optional -to switch for recipient addresses.
12 # * Supports all post(8) (by default, without -profile) or send(1)
13 # (with -profile) options.
14 # * Optionally (with -profile) obeys the users profile, including
15 # AliasFile and send entries.
16 # * Instead of silently not sending an empty message, notifies user
17 # "mhmail: empty message not sent, use -body '' to force."
18 # * The compiled mhmail dropped a trailing newline from the -body argument.
19 # * Drops support for undocumented -queue option.
20
21 usage='Usage: mhmail [-t(o)] addrs ... [switches]
22 switches are:
23 -at(tach) file [-at(tach) file] ...
24 -b(ody) text
25 -c(c) addrs ...
26 -f(rom) addr
27 -hea(derfield) name:value [-hea(derfield) name:value] ...
28 -su(bject) text
29 -r(esent)
30 -pr(ofile)
31 -se(nd)
32 -nose(nd)
33 -v(ersion)
34 -hel(p)
35 and all post(8)/send(1) switches
36 mhmail with no arguments is equivalent to inc'
37
38
39 #### Find location of a program. Bourne shell just puts the name in
40 #### $0 if it's found from the PATH, so search that if necessary.
41 finddir() {
42 case $1 in
43 */*) dirname "$1" ;;
44 * ) IFS=:
45 for d in $PATH; do
46 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
47 done ;;
48 esac
49 }
50
51 bindir=`finddir $0`
52 nmhbindir=`cd "$bindir" && pwd`
53 if printf 'OK\n' | tail -n 1 2>&1 | grep OK >/dev/null; then
54 tail='tail -n'
55 else
56 tail='tail -'
57 fi
58
59
60 #### Checks for missing mandatory arguments.
61 checkforargs() {
62 if [ $attacharg -eq 1 ]; then
63 printf 'mhmail: missing argument to -attach\n' >&2; exit 1
64 elif [ $bodyarg -eq 1 ]; then
65 printf 'mhmail: missing argument to -body\n' >&2; exit 1
66 elif [ $ccarg -eq 1 -a "$cclist"x = x ]; then
67 printf 'mhmail: missing argument to -cc\n' >&2; exit 1
68 elif [ $fromarg -eq 1 ]; then
69 printf 'mhmail: missing argument to -from\n' >&2; exit 1
70 elif [ $headerfieldarg -eq 1 ]; then
71 printf 'mhmail: missing argument to -headerfield\n' >&2; exit 1
72 elif [ $subjectarg -eq 1 ]; then
73 printf 'mhmail: missing argument to -subject\n' >&2; exit 1
74 elif [ $toarg -eq 1 ]; then
75 printf 'mhmail: missing argument to -to\n' >&2; exit 1
76 fi
77 }
78
79 if [ $# -eq 0 ]; then
80 #### Emulate mhmail for reading mail.
81 exec "$nmhbindir"/inc
82 fi
83
84 #### Go through all the switches so we can build the draft.
85 tolist= ## To: addresses
86 toarg=0 ## whether currently handling -to
87 attacharg=0 ## whether currently handling -attach
88 attach_send_switch_added=0 ## whether added "-attach Nmh-Attachment" switch
89 attachind=Nmh-Attachment ## attachment indicator
90 body= ## contents of the message body
91 bodyarg=0 ## whether currently handling -body
92 cclist= ## Cc: addresses
93 ccarg=0 ## whether currently handling -cc
94 from= ## From: contents
95 fromarg=0 ## whether currently handling -from
96 headerfieldlist= ## header fields to be added to draft
97 headerfieldarg=0 ## whether currently handling -headerfield
98 mhmailswitch=0 ## whether currently handling any mhmail switch
99 subject= ## Subject: contents
100 subjectarg=0 ## whether currently handling -subject
101 resent=0 ## whether resending
102 postsendargs= ## switches to pass on to post or send
103 post_send_switch_arg=0 ## whether currently handling a post/send switch
104 use_send=0 ## use post (default) or send (-profile)
105 sendsw=1 ## to send (default) or not to send
106 for arg in "$@"; do
107 case $arg in
108 #### Post and send won't accept -f -or -s because they'd be
109 #### ambiguous, so no conflicts with them. And they don't have
110 #### -b, -c, -r, -t. For the new switches that compiled mhmail
111 #### didn't have: let -p indicate mhmail -profile, not send
112 #### -port. -send masks the send(1) -send switch. -attach
113 #### masks the send(1) -attach switch.
114 -at|-att|-atta|-attac|-attach)
115 mhmailswitch=1
116 attacharg=1
117 use_send=1
118 if [ $attach_send_switch_added -eq 0 ]; then
119 #### Override any send -attach switch in user's profile.
120 postsendargs="${postsendargs:+$postsendargs }-attach $attachind"
121 attach_send_switch_added=1
122 fi ;;
123 -b|-bo|-bod|-body) mhmailswitch=1; bodyarg=1 ;;
124 -c|-cc) mhmailswitch=1; ccarg=1 ;;
125 -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;;
126 -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\
127 -headerfield) mhmailswitch=1; headerfieldarg=1 ;;
128 -hel|-help) printf '%s\n' "$usage"; exit ;;
129 -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;;
130 -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;;
131 -resend) printf 'mhmail: did you mean -resent instead of -resend?\n' >&2
132 exit 1 ;;
133 -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;;
134 -se|-sen|-send) mhmailswitch=1; sendsw=1 ;;
135 -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;;
136 -t|-to) toarg=1; ccarg=0 ;;
137 -v|-ve|-ver|-vers|-versi|-versio|-version)
138 #### Cheat instead of using autoconf and make to fill in the version.
139 "$nmhbindir"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
140 -*) if [ $mhmailswitch -eq 1 ]; then
141 checkforargs
142 mhmailswitch=0
143 fi
144 post_send_switch_arg=1
145 postsendargs="${postsendargs:+$postsendargs }$arg" ;;
146 *) mhmailswitch=0
147 if [ $bodyarg -eq 1 ]; then
148 body="$arg
149 "
150 bodyarg=0
151 #### Allow -body "" by using just a newline for the body.
152 [ "$body"x = x ] && body='
153 '
154 elif [ $fromarg -eq 1 ]; then
155 from="$arg"
156 fromarg=0
157 elif [ $subjectarg -eq 1 ]; then
158 subject="$arg"
159 subjectarg=0
160 elif [ $attacharg -eq 1 ]; then
161 headerfieldlist="${headerfieldlist:+$headerfieldlist}$attachind: $arg
162 "
163 attacharg=0
164 elif [ $headerfieldarg -eq 1 ]; then
165 #### It's not strictly necessary to have one space after
166 #### the : that separates the header field name from the
167 #### body, but do it to avoid surprising someone.
168 #### Solaris sed wants the trailing newline in its input.
169 add=`printf '%s\n' "$arg" | sed -e 's/:/: /' -e 's/: /: /'`
170 headerfieldlist="${headerfieldlist:+$headerfieldlist}$add
171 "
172 headerfieldarg=0
173 elif [ $post_send_switch_arg -eq 1 ]; then
174 postsendargs="${postsendargs:+$postsendargs }$arg"
175 elif [ $ccarg -eq 1 ]; then
176 #### ccarg can only be reset to 0 by -to.
177 cclist="${cclist:+$cclist, }$arg"
178 else
179 #### An address.
180 tolist="${tolist:+$tolist, }$arg"
181 toarg=0
182 fi ;;
183 esac
184 done
185
186 #### Check for at least one address and -from.
187 if [ "$tolist"x = x ]; then
188 printf 'Usage: mhmail [-t(o)] addrs ... [switches]\n' >&2; exit 1
189 fi
190 if [ "$from"x = x ]; then
191 nmhlibdir=`$nmhbindir/mhparam libdir`/
192 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
193 fi
194
195 #### Check for missing mandatory arguments.
196 checkforargs
197
198 #### Build header.
199 [ $resent -eq 0 ] && prefix= || prefix='Resent-'
200 header="${prefix}To: $tolist
201 "
202 [ "$cclist"x = x ] || header="$header${prefix}Cc: $cclist
203 "
204 [ "$subject"x = x ] || header="$header${prefix}Subject: $subject
205 "
206 [ "$from"x = x ] || header="$header${prefix}From: $from
207 "
208
209 if [ "$headerfieldlist" ]; then
210 header="$header$headerfieldlist"
211 fi
212
213 #### Set up a file to supply as a draft to post/send. And set a
214 #### trap to remove it. send moves the file to a backup, so it will
215 #### remove that, too.
216 umask 077
217 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`$nmhbindir/mhpath +`}}}"
218 tmpfile="$tmpdir/mhmail$$"
219 tmpfilebackup="'$tmpdir'/,mhmail$$ '$tmpdir'/#mhmail$$"
220 tmpfileresent=
221
222 message_file=
223 if [ $resent -eq 0 ]; then
224 #### Add blank line after header if not resending.
225 header="$header
226 "
227 message_file="$tmpfile"
228 else
229 if [ $use_send -eq 0 ]; then
230 postsendargs="${postsendargs:+$postsendargs }-dist"
231 message_file="$tmpfile"
232 else
233 #### When resending with send, tmpfile will just contain the
234 #### Resent- header fields. "$tmpfileresent" will contain
235 #### the message that is being resent.
236 tmpfileresent="$tmpdir/mhmail-resent$$"
237 mhdist=1; export mhdist
238 mhaltmsg=$tmpfileresent; export mhaltmsg
239 message_file="$tmpfileresent"
240 printf '' >"$message_file" || exit 2
241 fi
242 fi
243
244 trap "rm -f '$tmpfile' $tmpfilebackup ${tmpfileresent:+'$tmpfileresent'}" 0
245
246 if [ "$body"x = x ]; then
247 #### First put message header in the file.
248 printf %s "$header" >"$tmpfile" || exit 2
249
250 tmpfile_size_before=`wc -c "$message_file"`
251 #### Now grab the body from stdin. cat >> handles blank lines
252 #### better than body=`cat`.
253 cat >>"$message_file" || exit 2
254 tmpfile_size_after=`wc -c "$message_file"`
255
256 #### Don't allow an empty body (from stdin). Use string
257 #### comparison so we don't have to strip the filename, etc.
258 if [ "$tmpfile_size_before" = "$tmpfile_size_after" ]; then
259 printf 'mhmail: empty message not sent, use -body '"''"' to force.\n' >&2
260 exit 1
261 fi
262
263 #### Add trailing newline to body if it doesn't have one.
264 if [ `${tail}1 "$message_file" | wc -l` -ne 1 ]; then
265 printf '\n' >>"$message_file" || exit 2
266 fi
267 else
268 #### Add trailing newline to body if it doesn't have one.
269 [ `printf %s "$body" | ${tail}1 | wc -l` -ne 1 ] && body="$body
270 "
271
272 if [ "$tmpfileresent" ]; then
273 #### Put just the new message header in the file.
274 printf %s "$header" >"$tmpfile" || exit 2
275 #### and the body in the file to resend.
276 printf %s "$body" >"$tmpfileresent" || exit 2
277 else
278 #### Put message header and body in the file.
279 printf %s "$header$body" >"$tmpfile" || exit 2
280 fi
281 fi
282
283 if [ $sendsw -eq 0 ]; then
284 cat "$tmpfile"
285 else
286 if [ $use_send -eq 0 ]; then
287 post_or_send=`$nmhbindir/mhparam postproc`
288 else
289 post_or_send="$nmhbindir/send"
290 fi
291
292 if "$post_or_send" "$tmpfile" $postsendargs; then
293 exit
294 else
295 status=$?
296 mv -f "$tmpfile" dead.letter
297 printf 'Letter saved in dead.letter\n' >&2
298 exit $status
299 fi
300 fi