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