Added -header-field switch to mhmail.
[mmh] / 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 # * Instead of silently not sending an empty message, notifies user
11 #   "mhmail: empty message not sent, use -body '' to force."
12 # * The compiled mhmail dropped a trailing newline from the -body argument.
13 # * Supports all post(8) (by default, without -profile) or send(1)
14 #   (with -profile) options.
15 # * Optionally (with -profile) obeys the users profile, including
16 #   AliasFile and send entries.
17 # * Adds -send/-nosend and -header-field options.
18 # * Drops support for undocumented -queue option.
19 #
20 # To do:
21 # * add -attach file ... switch
22
23 usage='Usage: mhmail [addrs ... [switches]]
24   switches are:
25   -b(ody) text
26   -c(c) addrs ...
27   -f(rom) addr
28   -hea(der-field) name:field [-hea(der-field) name:field] ...
29   -su(bject) text
30   -r(esent)
31   -pr(ofile)
32   -se(nd)
33   -nose(nd)
34   -v(ersion)
35   -hel(p)
36   and all post(8)/send(1) switches'
37
38 bindir=`dirname $0`
39 nmhbindir=`cd "${bindir}" && pwd`
40
41 if [ $# -eq 0 ]; then
42   #### Emulate mhmail for reading mail.
43   exec "${nmhbindir}"/inc
44 else
45   #### Go through all the switches so we can build the draft.
46   tolist=
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.
73       -b|-bo|-bod|-body) bodyarg=1 ;;
74       -c|-cc) ccarg=1 ;;
75       -f|-fr|-fro|-from) fromarg=1 ;;
76       -hea|-head|-heade|-header|-header-|-header-f|-header-fi|-header-fie|-header-fiel|-header-field) headerfieldarg=1 ;;
77       -hel|-help) printf "%s\n" "${usage}"; exit ;;
78       -nose|-nosen|-nosend) sendsw=0 ;;
79       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
80       -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
81          exit 1 ;;
82       -r|-re|-res|-rese|-resen|-resent) resent=1 ;;
83       -se|-sen|-send) sendsw=1 ;;
84       -su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
85       -v|-ve|-ver|-vers|-versi|-versio|-version)
86          #### Cheat instead of using autoconf and make to fill in the version.
87          "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
88       -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
89       *) if [ ${bodyarg} -eq 1 ]; then
90            body="${arg}
91 "
92            bodyarg=0
93            #### Allow -body "" by using just a newline for the body.
94            [ "${body}"x = x ]  &&  body='
95 '
96          elif [ ${fromarg} -eq 1 ]; then
97            from="${arg}"
98            fromarg=0
99          elif [ ${subjectarg} -eq 1 ]; then
100            subject="${arg}"
101            subjectarg=0
102          elif [ ${headerfieldarg} -eq 1 ]; then
103            #### It's not strictly necessary to have one space after
104            #### the : that separates the header field name from the
105            #### body, but do it to avoid surprising someone.
106            add=`printf "${arg}" | sed -e 's/:/: /' -e 's/:  /: /'`
107            headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
108 "
109            headerfieldarg=0
110          elif [ ${switcharg} -eq 1 ]; then
111            postsendargs="${postsendargs:+${postsendargs} }${arg}"
112          elif [ ${ccarg} -eq 1 ]; then
113            #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
114            cclist="${cclist:+${cclist}, }${arg}"
115          else
116            #### An address.
117            tolist="${tolist:+${tolist}, }${arg}"
118          fi ;;
119     esac
120   done
121
122   #### Check for at least one address and -from.
123   if [ "${tolist}"x = x ]; then
124     printf "mhmail: usage: mhmail addrs ... [switches]\n"
125     exit 1
126   fi
127   if [ "${from}"x = x ]; then
128     nmhlibdir=`${nmhbindir}/mhparam libdir`/
129     from=`${nmhlibdir}ap -format '%(localmbox)' 0`
130   fi
131
132   #### Build header.
133   [ ${resent} -eq 0 ]  &&  prefix=  ||  prefix='Resent-'
134   header="${prefix}To: ${tolist}
135 "
136   [ "${cclist}"x = x ]  ||  header="${header}${prefix}Cc: ${cclist}
137 "
138   [ "${subject}"x = x ]  ||  header="${header}${prefix}Subject: ${subject}
139 ";
140   [ "${from}"x = x ]  ||  header="${header}${prefix}From: ${from}
141 ";
142
143   if [ "${headerfieldlist}" ]; then
144     header="${header}${headerfieldlist}";
145   fi
146
147   #### Set up a file to supply as a draft to send/post.  And set a
148   #### trap to remove it.  send moves the file to a backup, so it will
149   #### remove that, too.
150   umask 077
151   tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
152   tmpfile="${tmpdir}/mhmail$$"
153   tmpfilebackup="${tmpdir}/[,#]mhmail$$"
154   tmpfileresent=
155
156   message_file=
157   if [ ${resent} -eq 0 ]; then
158     #### Add blank line after header if not resending.
159     header="${header}
160 "
161     message_file="${tmpfile}"
162   else
163     if [ ${use_send} -eq 0 ]; then
164       postsendargs="${postsendargs:+${postsendargs} }-dist"
165       message_file="${tmpfile}"
166     else
167       #### When resending with send, tmpfile will just contain the
168       #### Resent- header fields.  "${tmpfileresent}" will contain
169       #### the message that is being resent.
170       tmpfileresent="${tmpdir}/mhmail-resent$$"
171       mhdist=1; export mhdist
172       mhaltmsg=${tmpfileresent}; export mhaltmsg
173       message_file="${tmpfileresent}"
174       printf "" >"${message_file}"  || exit 2
175     fi
176   fi
177
178   trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
179
180   if [ "${body}"x = x ]; then
181     #### First put message header in the file.
182     printf "%s" "${header}" >"${tmpfile}" || exit 2
183
184     tmpfile_size_before=`wc -c "${message_file}"`
185     #### Now grab the body from stdin.  cat >> handles blank lines
186     #### better than body=`cat`.
187     cat >>"${message_file}" || exit 2
188     tmpfile_size_after=`wc -c "${message_file}"`
189
190     #### Don't allow an empty body (from stdin).  Use string
191     #### comparison so we don't have to strip the filename, etc.
192     if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
193       printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
194       exit 1
195     fi
196
197     #### Add trailing newline to body if it doesn't have one.
198     if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
199       printf "\n" >>"${message_file}" || exit 2
200     fi
201   else
202     #### Add trailing newline to body if it doesn't have one.
203     [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ]  &&  body="${body}
204 "
205
206     if [ "${tmpfileresent}" ]; then
207       #### Put just the new message header in the file.
208       printf "%s" "${header}" >"${tmpfile}" || exit 2
209       #### and the body in the file to resend.
210       printf "${body}" >"${tmpfileresent}" || exit 2
211     else
212       #### Put message header and body in the file.
213       printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
214     fi
215   fi
216
217   if [ ${sendsw} -eq 0 ]; then
218     cat "${tmpfile}"
219   else
220     if [ ${use_send} -eq 0 ]; then
221       post_or_send=`${nmhbindir}/mhparam postproc`
222     else
223       post_or_send="${nmhbindir}/send"
224     fi
225
226     if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
227       exit
228     else
229       printf "Letter saved in dead.letter\n"
230       #### exec skips the trap set above.
231       [ "${tmpfileresent}" ]  &&  rm -f "${tmpfileresent}"
232       exec mv "${tmpfile}" dead.letter
233     fi
234   fi
235 fi