Added support to mhmail.in to read message from stdin.
[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 send(1) or post(8).
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 send(1)/post(8) 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   sendpostargs=
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       #### Send and post wouldn'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       -*) sendpostargs="${sendpostargs:+${sendpostargs} }${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          elif [ ${otherarg} -eq 1 ]; then
88            #### Always end ${header} with a newline.
89            header="${header:+${header} }${arg}
90 "; otherarg=0
91          elif [ ${switcharg} -eq 1 ]; then
92            sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"
93          else
94            #### An address.
95            tolist="${tolist:+${tolist}, }${arg}"
96          fi
97     esac
98   done
99
100   #### Check for at least one address and -from.
101   if [ "${tolist}"x = x ]; then
102     printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
103   fi
104   if [ "${havefrom}" = 0 ]; then
105     nmhlibdir=`${nmhbindir}/mhparam libdir`
106     header="${header:+${header}}From: "\
107 `${nmhlibdir}/ap -format '%(localmbox)' 0`"
108 "
109   fi
110
111   #### If no -body, read message from stdin the easy way.
112   if [ "${body}"x = x ]; then
113     body=`cat`
114   fi
115
116   #### Set up a tmpfil and trap to remove it.  send moves the file to a backup.
117   tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
118   tmpfil=${tmpdir}/mhmail$$
119   tmpfilbackup=${tmpdir}/[,#]mhmail$$
120   trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
121
122   #### Put message header and body in file to supply as draft to
123   #### send/post.  ${header} always ends with a newline, so this adds
124   #### the blank that separates the body.
125   umask 077
126   printf "%s\n" "To: ${tolist}
127 Cc: ${cclist}
128 ${header}
129 ${body}" > ${tmpfil} || exit 1
130
131   if [ "${debug}" ]; then
132     printf "%s:\n" `ls -1 ${tmpfil}`
133     cat ${tmpfil}
134   fi
135
136   if [ "$use_send" -eq 1 ]; then
137     send_or_post="${nmhbindir}/send"
138   else
139     send_or_post="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
140   fi
141
142   $debug ${send_or_post} ${tmpfil} ${sendpostargs}
143 fi