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