]> diplodocus.org Git - nmh/blob - uip/mhmail
More setup_test() and test-ali attempts on Solaris.
[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
54
55 #### Checks for missing mandatory arguments.
56 checkforargs() {
57 if [ $attacharg -eq 1 ]; then
58 printf 'mhmail: missing argument to -attach\n' >&2; exit 1
59 elif [ $bodyarg -eq 1 ]; then
60 printf 'mhmail: missing argument to -body\n' >&2; exit 1
61 elif [ $ccarg -eq 1 -a "$cclist"x = x ]; then
62 printf 'mhmail: missing argument to -cc\n' >&2; exit 1
63 elif [ $fromarg -eq 1 ]; then
64 printf 'mhmail: missing argument to -from\n' >&2; exit 1
65 elif [ $headerfieldarg -eq 1 ]; then
66 printf 'mhmail: missing argument to -headerfield\n' >&2; exit 1
67 elif [ $subjectarg -eq 1 ]; then
68 printf 'mhmail: missing argument to -subject\n' >&2; exit 1
69 elif [ $toarg -eq 1 ]; then
70 printf 'mhmail: missing argument to -to\n' >&2; exit 1
71 fi
72 }
73
74 if [ $# -eq 0 ]; then
75 #### Emulate mhmail for reading mail.
76 exec "$nmhbindir"/inc
77 fi
78
79 #### Go through all the switches so we can build the draft.
80 tolist= ## To: addresses
81 toarg=0 ## whether currently handling -to
82 attacharg=0 ## whether currently handling -attach
83 attach_send_switch_added=0 ## whether added "-attach Nmh-Attachment" switch
84 attachind=Nmh-Attachment ## attachment indicator
85 body= ## contents of the message body
86 bodyarg=0 ## whether currently handling -body
87 cclist= ## Cc: addresses
88 ccarg=0 ## whether currently handling -cc
89 from= ## From: contents
90 fromarg=0 ## whether currently handling -from
91 headerfieldlist= ## header fields to be added to draft
92 headerfieldarg=0 ## whether currently handling -headerfield
93 mhmailswitch=0 ## whether currently handling any mhmail switch
94 subject= ## Subject: contents
95 subjectarg=0 ## whether currently handling -subject
96 resent=0 ## whether resending
97 postsendargs= ## switches to pass on to post or send
98 post_send_switch_arg=0 ## whether currently handling a post/send switch
99 use_send=0 ## use post (default) or send (-profile)
100 sendsw=1 ## to send (default) or not to send
101 for arg in "$@"; do
102 case $arg in
103 #### Post and send won't accept -f -or -s because they'd be
104 #### ambiguous, so no conflicts with them. And they don't have
105 #### -b, -c, -r, -t. For the new switches that compiled mhmail
106 #### didn't have: let -p indicate mhmail -profile, not send
107 #### -port. -send masks the send(1) -send switch. -attach
108 #### masks the send(1) -attach switch.
109 -at|-att|-atta|-attac|-attach)
110 mhmailswitch=1
111 attacharg=1
112 use_send=1
113 if [ $attach_send_switch_added -eq 0 ]; then
114 #### Override any send -attach switch in user's profile.
115 postsendargs="${postsendargs:+$postsendargs }-attach $attachind"
116 attach_send_switch_added=1
117 fi ;;
118 -b|-bo|-bod|-body) mhmailswitch=1; bodyarg=1 ;;
119 -c|-cc) mhmailswitch=1; ccarg=1 ;;
120 -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;;
121 -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\
122 -headerfield) mhmailswitch=1; headerfieldarg=1 ;;
123 -hel|-help) printf '%s\n' "$usage"; exit ;;
124 -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;;
125 -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;;
126 -resend) printf 'mhmail: did you mean -resent instead of -resend?\n' >&2
127 exit 1 ;;
128 -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;;
129 -se|-sen|-send) mhmailswitch=1; sendsw=1 ;;
130 -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;;
131 -t|-to) toarg=1; ccarg=0 ;;
132 -v|-ve|-ver|-vers|-versi|-versio|-version)
133 #### Cheat instead of using autoconf and make to fill in the version.
134 "$nmhbindir"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
135 -*) if [ $mhmailswitch -eq 1 ]; then
136 checkforargs
137 mhmailswitch=0
138 fi
139 post_send_switch_arg=1
140 postsendargs="${postsendargs:+$postsendargs }$arg" ;;
141 *) mhmailswitch=0
142 if [ $bodyarg -eq 1 ]; then
143 body="$arg
144 "
145 bodyarg=0
146 #### Allow -body "" by using just a newline for the body.
147 [ "$body"x = x ] && body='
148 '
149 elif [ $fromarg -eq 1 ]; then
150 from="$arg"
151 fromarg=0
152 elif [ $subjectarg -eq 1 ]; then
153 subject="$arg"
154 subjectarg=0
155 elif [ $attacharg -eq 1 ]; then
156 headerfieldlist="${headerfieldlist:+$headerfieldlist}$attachind: $arg
157 "
158 attacharg=0
159 elif [ $headerfieldarg -eq 1 ]; then
160 #### It's not strictly necessary to have one space after
161 #### the : that separates the header field name from the
162 #### body, but do it to avoid surprising someone.
163 add=`printf %s "$arg" | sed -e 's/:/: /' -e 's/: /: /'`
164 headerfieldlist="${headerfieldlist:+$headerfieldlist}$add
165 "
166 headerfieldarg=0
167 elif [ $post_send_switch_arg -eq 1 ]; then
168 postsendargs="${postsendargs:+$postsendargs }$arg"
169 elif [ $ccarg -eq 1 ]; then
170 #### ccarg can only be reset to 0 by -to.
171 cclist="${cclist:+$cclist, }$arg"
172 else
173 #### An address.
174 tolist="${tolist:+$tolist, }$arg"
175 toarg=0
176 fi ;;
177 esac
178 done
179
180 #### Check for at least one address and -from.
181 if [ "$tolist"x = x ]; then
182 printf 'Usage: mhmail [-t(o)] addrs ... [switches]\n' >&2; exit 1
183 fi
184 if [ "$from"x = x ]; then
185 nmhlibdir=`$nmhbindir/mhparam libdir`/
186 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
187 fi
188
189 #### Check for missing mandatory arguments.
190 checkforargs
191
192 #### Build header.
193 [ $resent -eq 0 ] && prefix= || prefix='Resent-'
194 header="${prefix}To: $tolist
195 "
196 [ "$cclist"x = x ] || header="$header${prefix}Cc: $cclist
197 "
198 [ "$subject"x = x ] || header="$header${prefix}Subject: $subject
199 "
200 [ "$from"x = x ] || header="$header${prefix}From: $from
201 "
202
203 if [ "$headerfieldlist" ]; then
204 header="$header$headerfieldlist"
205 fi
206
207 #### Set up a file to supply as a draft to post/send. And set a
208 #### trap to remove it. send moves the file to a backup, so it will
209 #### remove that, too.
210 umask 077
211 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`$nmhbindir/mhpath +`}}}"
212 tmpfile="$tmpdir/mhmail$$"
213 tmpfilebackup="'$tmpdir'/,mhmail$$ '$tmpdir'/#mhmail$$"
214 tmpfileresent=
215
216 message_file=
217 if [ $resent -eq 0 ]; then
218 #### Add blank line after header if not resending.
219 header="$header
220 "
221 message_file="$tmpfile"
222 else
223 if [ $use_send -eq 0 ]; then
224 postsendargs="${postsendargs:+$postsendargs }-dist"
225 message_file="$tmpfile"
226 else
227 #### When resending with send, tmpfile will just contain the
228 #### Resent- header fields. "$tmpfileresent" will contain
229 #### the message that is being resent.
230 tmpfileresent="$tmpdir/mhmail-resent$$"
231 mhdist=1; export mhdist
232 mhaltmsg=$tmpfileresent; export mhaltmsg
233 message_file="$tmpfileresent"
234 printf '' >"$message_file" || exit 2
235 fi
236 fi
237
238 trap "rm -f '$tmpfile' $tmpfilebackup ${tmpfileresent:+'$tmpfileresent'}" 0
239
240 if [ "$body"x = x ]; then
241 #### First put message header in the file.
242 printf %s "$header" >"$tmpfile" || exit 2
243
244 tmpfile_size_before=`wc -c "$message_file"`
245 #### Now grab the body from stdin. cat >> handles blank lines
246 #### better than body=`cat`.
247 cat >>"$message_file" || exit 2
248 tmpfile_size_after=`wc -c "$message_file"`
249
250 #### Don't allow an empty body (from stdin). Use string
251 #### comparison so we don't have to strip the filename, etc.
252 if [ "$tmpfile_size_before" = "$tmpfile_size_after" ]; then
253 printf 'mhmail: empty message not sent, use -body '"''"' to force.\n' >&2
254 exit 1
255 fi
256
257 #### Add trailing newline to body if it doesn't have one.
258 if [ `tail -n 1 "$message_file" | wc -l` -ne 1 ]; then
259 printf '\n' >>"$message_file" || exit 2
260 fi
261 else
262 #### Add trailing newline to body if it doesn't have one.
263 [ `printf %s "$body" | tail -n 1 | wc -l` -ne 1 ] && body="$body
264 "
265
266 if [ "$tmpfileresent" ]; then
267 #### Put just the new message header in the file.
268 printf %s "$header" >"$tmpfile" || exit 2
269 #### and the body in the file to resend.
270 printf %s "$body" >"$tmpfileresent" || exit 2
271 else
272 #### Put message header and body in the file.
273 printf %s "$header$body" >"$tmpfile" || exit 2
274 fi
275 fi
276
277 if [ $sendsw -eq 0 ]; then
278 cat "$tmpfile"
279 else
280 if [ $use_send -eq 0 ]; then
281 post_or_send=`$nmhbindir/mhparam postproc`
282 else
283 post_or_send="$nmhbindir/send"
284 fi
285
286 if "$post_or_send" "$tmpfile" $postsendargs; then
287 exit
288 else
289 status=$?
290 mv -f "$tmpfile" dead.letter
291 printf 'Letter saved in dead.letter\n' >&2
292 exit $status
293 fi
294 fi