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