Replaced compiled mhmail with script that was formerly named mhmail.in.
[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 -debug option for debugging (sending, not incorporating new mail).
18 # * Drops support for undocumented -queue option.
19 #
20 # To do:
21 # * add support for undocumented -resent compiled mhmail switch
22 # * add -attach file ... switch
23
24 usage='Usage: mhmail [addrs ... [switches]]
25   switches are:
26   -b(ody) text
27   -c(c) addrs ...
28   -f(rom) addr
29   -s(ubject) text
30   -pr(ofile)
31   -v(ersion)
32   -h(elp)
33   -d(ebug)
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   body=
46   bodyarg=0
47   cclist=
48   ccarg=0
49   from=
50   fromarg=0
51   subject=
52   subjectarg=0
53   postsendargs=
54   switcharg=0
55   use_send=0
56   debug=
57   for arg in "$@"; do
58     case "${arg}" in
59       -*) switcharg=0
60     esac
61
62     case "${arg}" in
63       #### Post and send won't accept -f -or -s because they'd be
64       #### ambiguous, so no conflicts with them.  And they don't have
65       #### -b or -c.  For the new switches that compiled mhmail didn't
66       #### have:  let -p indicate mhmail -profile, not send -port, and
67       #### let -d indicate mhmail -debug, not send -draft.
68       -b|-bo|-bod|-body) bodyarg=1 ;;
69       -c|-cc) ccarg=1 ;;
70       -d|-de|-deb|-debu|-debug) debug=echo ;;
71       -f|-fr|-fro|-from) fromarg=1 ;;
72       -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
73       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
74       -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
75       -v|-ve|-ver|-vers|-versi|-versio|-version)
76          #### Cheat instead of using autoconf and make to fill in the version.
77          "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
78       -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
79       *) if [ ${bodyarg} -eq 1 ]; then
80            body="${arg}
81 "; bodyarg=0
82            #### Allow -body "" by using just a newline for the body.
83            [ "${body}"x = x ]  &&  body='
84 '
85          elif [ ${fromarg} -eq 1 ]; then
86            from="${arg}"; fromarg=0
87          elif [ ${subjectarg} -eq 1 ]; then
88            subject="${arg}"; subjectarg=0
89          elif [ ${switcharg} -eq 1 ]; then
90            postsendargs="${postsendargs:+${postsendargs} }${arg}"
91          elif [ ${ccarg} -eq 1 ]; then
92            #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
93            cclist="${cclist:+${cclist}, }${arg}"
94          else
95            #### An address.
96            tolist="${tolist:+${tolist}, }${arg}"
97          fi
98     esac
99   done
100
101   #### Check for at least one address and -from.
102   if [ "${tolist}"x = x ]; then
103     printf "mhmail: usage: mhmail addrs ... [switches]\n"
104     exit 1
105   fi
106   if [ "${from}"x = x ]; then
107     nmhlibdir=`${nmhbindir}/mhparam libdir`/
108     #### If nmhlibdir isn't right, assume that the nmh lib dir is on the PATH.
109     [ -x "${nmhlibdir}ap" ]  ||  nmhlibdir=
110     from=`${nmhlibdir}ap -format '%(localmbox)' 0`
111   fi
112
113   #### Build header.
114   header="To: ${tolist}
115 "
116   [ "${cclist}"x = x ]  ||  header="${header}Cc: ${cclist}
117 "
118   [ "${subject}"x = x ]  ||  header="${header}Subject: ${subject}
119 ";
120   [ "${from}"x = x ]  ||  header="${header}From: ${from}
121 ";
122   header="${header}
123 "
124
125   #### Set up a file to supply as a draft to send/post.  And set a
126   #### trap to remove it.  send moves the file to a backup, so it will
127   #### remove that, too.
128   umask 077
129   tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
130   tmpfil="${tmpdir}/mhmail$$"
131   tmpfilbackup="${tmpdir}/[,#]mhmail$$"
132   trap 'rm -f '"${tmpfil}"' '"${tmpfilbackup}" EXIT
133
134   if [ "${body}"x = x ]; then
135     #### First put message header in the file.  cat >> handles blank
136     #### lines better than body=`cat`.
137     printf "%s" "${header}" > "${tmpfil}" || exit 1
138
139     tmpfile_size_before=`wc -c "${tmpfil}"`
140     #### Now grab the body from stdin.
141     cat >>"${tmpfil}"
142     tmpfile_size_after=`wc -c "${tmpfil}"`
143
144     #### Don't allow an empty body (from stdin).  Use string
145     #### comparison so we don't have to strip the filename, etc.
146     if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
147       printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
148       exit 1
149     fi
150
151     #### Add trailing newline to body if it doesn't have one.
152     [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ]  &&  body="${body}
153 "
154   else
155     #### Put message header and body in the file.
156     printf "%s" "${header}${body}" > "${tmpfil}" || exit 1
157   fi
158
159   if [ "${debug}" ]; then
160     printf "%s:\n" `ls -1 "${tmpfil}"`
161     cat "${tmpfil}"
162   fi
163
164   if [ "$use_send" -eq 0 ]; then
165     post_or_send=`${nmhbindir}/mhparam postproc`
166   else
167     post_or_send="${nmhbindir}/send"
168   fi
169
170   if $debug "${post_or_send}" "${tmpfil}" ${postsendargs}; then
171     :
172   else
173     printf "Letter saved in dead.letter\n"
174     #### exec skips the trap set above.
175     exec mv "${tmpfil}" dead.letter
176   fi
177 fi