]>
diplodocus.org Git - nmh/blob - uip/mhmail
3 # mhmail -- simple mail program
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.
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.
21 usage
='Usage: mhmail [-t(o)] addrs ... [switches]
23 -at(tach) file [-at(tach) file] ...
27 -hea(derfield) name:value [-hea(derfield) name:value] ...
35 and all post(8)/send(1) switches
36 mhmail with no arguments is equivalent to inc'
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.
46 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s
"$d" && break
51 #### Convenience function to allow concise code below.
53 [ $# -gt 0 ] && printf '%s\n' "$1" >&2
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 ' ;;
67 #### Checks for missing mandatory arguments.
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'
87 #### Emulate mhmail for reading mail.
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
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)
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
145 post_send_switch_arg
=1
146 postsendargs
="${postsendargs:+$postsendargs }$arg" ;;
148 if [ $bodyarg -eq 1 ]; then
152 #### Allow -body "" by using just a newline for the body.
153 [ "$body"x
= x
] && body
='
155 elif [ $fromarg -eq 1 ]; then
158 elif [ $subjectarg -eq 1 ]; then
161 elif [ $attacharg -eq 1 ]; then
162 headerfieldlist
="${headerfieldlist:+$headerfieldlist}$attachind: $arg
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
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"
181 tolist
="${tolist:+$tolist, }$arg"
187 #### Check for at least one address and -from.
188 if [ "$tolist"x
= x
]; then
189 die
'Usage: mhmail [-t(o)] addrs ... [switches]'
191 if [ "$from"x
= x
]; then
192 from
=`${nmhlibexecdir}/ap -format '%(localmbox)' 0`
195 #### Check for missing mandatory arguments.
199 [ $resent -eq 0 ] && prefix
= || prefix
='Resent-'
200 header
="${prefix}To: $tolist
202 [ "$cclist"x
= x
] || header
="$header${prefix}Cc: $cclist
204 [ "$subject"x
= x
] || header
="$header${prefix}Subject: $subject
206 [ "$from"x
= x
] || header
="$header${prefix}From: $from
209 if [ "$headerfieldlist" ]; then
210 header
="$header$headerfieldlist"
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.
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}*"
226 if [ $resent -eq 0 ]; then
227 #### Add blank line after header if not resending.
230 message_file
="$tmpfile"
232 if [ $use_send -eq 0 ]; then
233 postsendargs
="${postsendargs:+$postsendargs }-dist"
234 message_file
="$tmpfile"
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
248 trap "rm -f '$tmpfile' $tmpfilebackup ${tmpfileresent:+'$tmpfileresent'}" 0
250 if [ "$body"x
= x
]; then
251 #### First put message header in the file.
252 printf %s
"$header" >"$tmpfile" || exit 2
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"`
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.'
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
271 #### Add trailing newline to body if it doesn't have one.
272 [ `printf %s "$body" | ${tail}1 | wc -l` -ne 1 ] && body
="$body
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
281 #### Put message header and body in the file.
282 printf %s
"$header$body" >"$tmpfile" || exit 2
286 if [ $sendsw -eq 0 ]; then
289 if [ $use_send -eq 0 ]; then
290 post_or_send
=`$nmhbindir/mhparam postproc`
292 post_or_send
="$nmhbindir/send"
295 if "$post_or_send" $postsendargs "$tmpfile"; then
299 mv -f "$tmpfile" dead.letter
300 printf 'Letter saved in dead.letter\n' >&2