If post/send fail, save the draft in dead.letter, the same as
[mmh] / uip / mhmail.in
1 #! /bin/sh
2 #
3 # mhmail.c -- 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) using nmh post(8) or send(1).
10 # Differences from compiled mhmail:
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 # * Adds -debug option for debugging (sending, not incorporating new mail).
16 #
17 # To do:
18 # * update mhmail man page
19 # * add mhmail test
20 # * integrate into autoconf
21 # * support undocumented mhmail options?
22
23 usage='Usage: mhmail [addrs ... [switches]]
24   switches are:
25   -b(ody) text
26   -c(c) addrs ...
27   -f(rom) addr
28   -s(ubject) text
29   -pr(ofile)
30   -v(ersion)
31   -h(elp)
32   -d(ebug)
33   and all post(8)/send(1) switches'
34
35 #### Use autoconf to fill in bindir.
36 nmhbindir=@bindir@
37
38 #### Or if configure hasn't been run yet, figure out nmhbindir at runtime.
39 case ${nmhbindir} in
40   @*@) nmhbindir=`dirname $0` ;;
41 esac
42
43 if [ $# -eq 0 ]; then
44   #### Emulate mhmail for reading mail.
45   exec ${nmhbindir}/inc
46 else
47   #### Go through all the switches so we can build the draft.
48   tolist=
49   body=
50   bodyarg=0
51   cclist=
52   ccarg=0
53   havefrom=0
54   header=
55   otherarg=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 or -c.  For the new switches that compiled mhmail didn't
69       #### have:  let -p indicate mhmail -profile, not send -port, and
70       #### 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) header="${header}From:"; otherarg=1; havefrom=1 ;;
75       -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
76       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
77       -s|-su|-sub|-subj|-subje|-subjec|-subject)
78           header="${header}Subject:"; otherarg=1 ;;
79       -v|-ve|-ver|-vers|-versi|-versio|-version)
80           #### Cheat instead of using autoconf and make to fill in the version.
81           ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
82       -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
83       *) if [ ${ccarg} -eq 1 ]; then
84            cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
85          elif [ ${bodyarg} -eq 1 ]; then
86            body=${arg}; bodyarg=0
87            #### Allow -body "" by using just a newline for the body.
88            [ "${body}"x = x ]  &&  body="
89 "
90          elif [ ${otherarg} -eq 1 ]; then
91            #### Always end ${header} with a newline.
92            header="${header:+${header} }${arg}
93 "; otherarg=0
94          elif [ ${switcharg} -eq 1 ]; then
95            postsendargs="${postsendargs:+${postsendargs} }${arg}"
96          else
97            #### An address.
98            tolist="${tolist:+${tolist}, }${arg}"
99          fi
100     esac
101   done
102
103   #### Check for at least one address and -from.
104   if [ "${tolist}"x = x ]; then
105     printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
106   fi
107   if [ "${havefrom}" = 0 ]; then
108     nmhlibdir=`${nmhbindir}/mhparam libdir`
109     header="${header:+${header}}From: "\
110 `${nmhlibdir}/ap -format '%(localmbox)' 0`"
111 "
112   fi
113
114   #### If no -body, read message from stdin the easy way.
115   [ "${body}"x = x ]  &&  body=`cat`
116   #### Don't allow an empty body (from stdin).
117   [ "${body}"x = x ]  &&  exit 1
118
119   #### Set up a tmpfil and trap to remove it.  send moves the file to a backup.
120   tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
121   tmpfil=${tmpdir}/mhmail$$
122   tmpfilbackup=${tmpdir}/[,#]mhmail$$
123   trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
124
125   #### Put message header and body in file to supply as draft to
126   #### send/post.  ${header} always ends with a newline, so this adds
127   #### the blank that separates the body.
128   umask 077
129   printf "%s" "To: ${tolist}
130 Cc: ${cclist}
131 ${header}
132 ${body}" > ${tmpfil} || exit 1
133
134   if [ "${debug}" ]; then
135     printf "%s:\n" `ls -1 ${tmpfil}`
136     cat "${tmpfil}"
137   fi
138
139   if [ "$use_send" -eq 0 ]; then
140     post_or_send="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
141   else
142     post_or_send="${nmhbindir}/send"
143   fi
144
145   if $debug ${post_or_send} ${tmpfil} ${postsendargs}; then
146     :
147   else
148     #### exec disables the trap set above.
149     printf "Letter saved in dead.letter\n"
150     exec mv ${tmpfil} dead.letter
151   fi
152 fi