Added mention of mhmail as a message composition utility.
[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 # * set prefix properly so that it supports distcheck
19 # * support undocumented mhmail options?
20
21 usage='Usage: mhmail [addrs ... [switches]]
22   switches are:
23   -b(ody) text
24   -c(c) addrs ...
25   -f(rom) addr
26   -s(ubject) text
27   -pr(ofile)
28   -v(ersion)
29   -h(elp)
30   -d(ebug)
31   and all post(8)/send(1) switches'
32
33 #### Use autoconf to fill in bindir.
34 # This doesn't work with distcheck.
35 # prefix=@prefix@
36 # exec_prefix=@exec_prefix@
37 # nmhbindir=@bindir@
38 nmhbindir=`dirname $0`
39
40 #### Or if configure/make haven't been run yet, figure out nmhbindir at runtime.
41 case ${nmhbindir} in
42   @*@) nmhbindir=`dirname $0` ;;
43 esac
44
45 if [ $# -eq 0 ]; then
46   #### Emulate mhmail for reading mail.
47   exec ${nmhbindir}/inc
48 else
49   #### Go through all the switches so we can build the draft.
50   tolist=
51   body=
52   bodyarg=0
53   cclist=
54   ccarg=0
55   havefrom=0
56   header=
57   otherarg=0
58   postsendargs=
59   switcharg=0
60   use_send=0
61   debug=
62   for arg in "$@"; do
63     case ${arg} in
64       -*) switcharg=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 or -c.  For the new switches that compiled mhmail didn't
71       #### have:  let -p indicate mhmail -profile, not send -port, and
72       #### let -d indicate mhmail -debug, not send -draft.
73       -b|-bo|-bod|-body) bodyarg=1 ;;
74       -c|-cc) ccarg=1 ;;
75       -d|-de|-deb|-debu|-debug) debug=echo ;;
76       -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
77       -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
78       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
79       -s|-su|-sub|-subj|-subje|-subjec|-subject)
80           header="${header}Subject:"; otherarg=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 [ ${ccarg} -eq 1 ]; then
86            cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
87          elif [ ${bodyarg} -eq 1 ]; then
88            body="${arg}
89 "; bodyarg=0
90            #### Allow -body "" by using just a newline for the body.
91            [ "${body}"x = x ]  &&  body='
92 '
93          elif [ ${otherarg} -eq 1 ]; then
94            #### Always end ${header} with a newline.
95            header="${header:+${header} }${arg}
96 "; otherarg=0
97          elif [ ${switcharg} -eq 1 ]; then
98            postsendargs="${postsendargs:+${postsendargs} }${arg}"
99          else
100            #### An address.
101            tolist="${tolist:+${tolist}, }${arg}"
102          fi
103     esac
104   done
105
106   #### Check for at least one address and -from.
107   if [ "${tolist}"x = x ]; then
108     printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
109   fi
110   if [ "${havefrom}" = 0 ]; then
111     nmhlibdir=`${nmhbindir}/mhparam libdir`
112     header="${header:+${header}}From: "\
113 `${nmhlibdir}/ap -format '%(localmbox)' 0`'
114 '
115   fi
116
117   #### If no -body, read message from stdin the easy way.
118   if [ "${body}"x = x ]; then
119     #### This will lose any trailing newline(s), so we can't
120     #### send the message when stdin contains only newlines.
121     body=`cat`
122
123     #### Don't allow an empty body (from stdin).
124     if [ "${body}"x = x ]; then
125       printf "empty body, message not sent\n"
126       exit 1
127     fi
128
129     #### Add trailing newline to body if it doesn't have one.
130     [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ]  &&  body="${body}
131 "
132   fi
133
134   #### Set up a tmpfil and trap to remove it.  send moves the file to a backup.
135   tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
136   tmpfil="${tmpdir}/mhmail$$"
137   tmpfilbackup="${tmpdir}/[,#]mhmail$$"
138   trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
139
140   #### Put message header and body in file to supply as draft to
141   #### send/post.  ${header} always ends with a newline, so this adds
142   #### the blank that separates the body.
143   umask 077
144   printf "%s" "To: ${tolist}
145 Cc: ${cclist}
146 ${header}
147 ${body}" > ${tmpfil} || exit 1
148
149   if [ "${debug}" ]; then
150     printf "%s:\n" `ls -1 ${tmpfil}`
151     cat "${tmpfil}"
152   fi
153
154   if [ "$use_send" -eq 0 ]; then
155     post_or_send="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
156   else
157     post_or_send="${nmhbindir}/send"
158   fi
159
160   if $debug ${post_or_send} ${tmpfil} ${postsendargs}; then
161     :
162   else
163     printf "Letter saved in dead.letter\n"
164     #### exec skips the trap set above.
165     exec mv ${tmpfil} dead.letter
166   fi
167 fi