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