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