Removed newline before generated From:.
[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 send(1) (with -profile) or post(8) (without -profile) options.
12 # * Optionally (with -profile) obeys the users profile, including
13 #   AliasFile and send entries.
14 # * Adds -debug option for debugging (sending, not incorporating new mail).
15 #
16 # To do:
17 # * update mhmail man page
18 # * add mhmail test
19 # * integrate into autoconf
20 # * support undocumented mhmail options?
21
22 usage='Usage: mhmail [addrs ... [switches]]
23   switches are:
24   -b(ody) text
25   -c(c) addrs ...
26   -f(rom) addr
27   -s(ubject) text
28   -pr(ofile)
29   -v(ersion)
30   -h(elp)
31   -d(ebug)
32   and all send(1)/post(8) switches'
33
34 #### Use autoconf to fill in bindir.
35 nmhbindir=@bindir@
36
37 #### Or if configure hasn't been run yet, figure out nmhbindir at runtime.
38 case ${nmhbindir} in
39   @*@) nmhbindir=`dirname $0` ;;
40 esac
41
42 if [ $# -eq 0 ]; then
43   #### Emulate mhmail for reading mail.
44   exec ${nmhbindir}/inc
45 else
46   #### Go through all the switches so we can build the draft.
47   tolist=
48   body=
49   bodyarg=0
50   cclist=
51   ccarg=0
52   havefrom=0
53   header=
54   otherarg=0
55   sendpostargs=
56   switcharg=0
57   use_send=0
58   debug=
59   for arg in "$@"; do
60     case ${arg} in
61       -*) switcharg=0
62     esac
63
64     case ${arg} in
65       #### Send and post wouldn't accept -f -or -s because they'd be
66       #### ambiguous, so no conflicts with them.  And they don't have
67       #### -b or -c.  For the new switches that compiled mhmail didn't
68       #### have:  let -p indicate mhmail -profile, not send -port, and
69       #### let -d indicate mhmail -debug, not send -draft.
70       -b|-bo|-bod|-body) bodyarg=1 ;;
71       -c|-cc) ccarg=1 ;;
72       -d|-de|-deb|-debu|-debug) debug=echo ;;
73       -f|-fr|-fro|-from) header="${header}From:"; otherarg=1; havefrom=1 ;;
74       -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
75       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
76       -s|-su|-sub|-subj|-subje|-subjec|-subject)
77           header="${header}Subject:"; otherarg=1 ;;
78       -v|-ve|-ver|-vers|-versi|-versio|-version)
79           #### Cheat instead of using autoconf and make to fill in the version.
80           ${nmhbindir}/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
81       -*) sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"; switcharg=1 ;;
82       *) if [ ${ccarg} -eq 1 ]; then
83            cclist="${cclist:+${cclist}, }${arg}"; ccarg=0
84          elif [ ${bodyarg} -eq 1 ]; then
85            body=${arg}; bodyarg=0
86          elif [ ${otherarg} -eq 1 ]; then
87            #### Always end ${header} with a newline.
88            header="${header:+${header} }${arg}
89 "; otherarg=0
90          elif [ ${switcharg} -eq 1 ]; then
91            sendpostargs="${sendpostargs:+${sendpostargs} }${arg}"
92          else
93            #### An address.
94            tolist="${tolist:+${tolist}, }${arg}"
95          fi
96     esac
97   done
98
99   #### Check for at least one address and -from.
100   if [ "${tolist}"x = x ]; then
101     printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
102   fi
103   if [ "${havefrom}" = 0 ]; then
104     nmhlibdir=`${nmhbindir}/mhparam libdir`
105     header="${header:+${header}}From: "\
106 `${nmhlibdir}/ap -format '%(localmbox)' 0`"
107 "
108   fi
109
110   #### Set up a tmpfil and trap to remove it.  send moves the file to a backup.
111   tmpdir=${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}
112   tmpfil=${tmpdir}/mhmail$$
113   tmpfilbackup=${tmpdir}/[,#]mhmail$$
114   trap "rm -f ${tmpfil} ${tmpfilbackup}" EXIT
115
116   #### Put message header and body in file to supply as draft to
117   #### send/post.  ${header} always ends with a newline, so this adds
118   #### the blank that separates the body.
119   umask 077
120   printf "%s\n" "To: ${tolist}
121 Cc: ${cclist}
122 ${header}
123 ${body}" > ${tmpfil} || exit 1
124
125   if [ "${debug}" ]; then
126     printf "%s:\n" `ls -1 ${tmpfil}`
127     cat ${tmpfil}
128   fi
129
130   if [ "$use_send" -eq 1 ]; then
131     send_or_post="${nmhbindir}/send"
132   else
133     send_or_post="${nmhlibdir:=`${nmhbindir}/mhparam libdir`}/post"
134   fi
135
136   $debug ${send_or_post} ${tmpfil} ${sendpostargs}
137 fi