]>
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
52 nmhbindir
=`cd "$bindir" && pwd`
53 if printf 'OK\n' | tail -n 1 2>&1 | grep OK
>/dev
/null
; then
60 #### Checks for missing mandatory arguments.
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
80 #### Emulate mhmail for reading mail.
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
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)
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
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
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
144 post_send_switch_arg
=1
145 postsendargs
="${postsendargs:+$postsendargs }$arg" ;;
147 if [ $bodyarg -eq 1 ]; then
151 #### Allow -body "" by using just a newline for the body.
152 [ "$body"x
= x
] && body
='
154 elif [ $fromarg -eq 1 ]; then
157 elif [ $subjectarg -eq 1 ]; then
160 elif [ $attacharg -eq 1 ]; then
161 headerfieldlist
="${headerfieldlist:+$headerfieldlist}$attachind: $arg
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
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"
180 tolist
="${tolist:+$tolist, }$arg"
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
190 if [ "$from"x
= x
]; then
191 nmhlibdir
=`$nmhbindir/mhparam libdir`/
192 from
=`${nmhlibdir}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
214 #### trap to remove it. send moves the file to a backup, so it will
215 #### remove that, too.
217 tmpdir
="${MHTMPDIR:-${TMPDIR:-${TMP:-`$nmhbindir/mhpath +`}}}"
218 tmpfile
="$tmpdir/mhmail$$"
219 tmpfilebackup
="'$tmpdir'/,mhmail$$ '$tmpdir'/#mhmail$$"
223 if [ $resent -eq 0 ]; then
224 #### Add blank line after header if not resending.
227 message_file
="$tmpfile"
229 if [ $use_send -eq 0 ]; then
230 postsendargs
="${postsendargs:+$postsendargs }-dist"
231 message_file
="$tmpfile"
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
244 trap "rm -f '$tmpfile' $tmpfilebackup ${tmpfileresent:+'$tmpfileresent'}" 0
246 if [ "$body"x
= x
]; then
247 #### First put message header in the file.
248 printf %s
"$header" >"$tmpfile" || exit 2
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"`
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
263 #### Add trailing newline to body if it doesn't have one.
264 if [ `tail -n 1 "$message_file" | wc -l` -ne 1 ]; then
265 printf '\n' >>"$message_file" || exit 2
268 #### Add trailing newline to body if it doesn't have one.
269 [ `printf %s "$body" | tail -n 1 | wc -l` -ne 1 ] && body
="$body
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
278 #### Put message header and body in the file.
279 printf %s
"$header$body" >"$tmpfile" || exit 2
283 if [ $sendsw -eq 0 ]; then
286 if [ $use_send -eq 0 ]; then
287 post_or_send
=`$nmhbindir/mhparam postproc`
289 post_or_send
="$nmhbindir/send"
292 if "$post_or_send" "$tmpfile" $postsendargs; then
296 mv -f "$tmpfile" dead.letter
297 printf 'Letter saved in dead.letter\n' >&2