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