]> diplodocus.org Git - nmh/blob - uip/mhmail
Replaced mhmail -debug switch with -nosend, and added -send.
[nmh] / uip / mhmail
1 #! /bin/sh
2 #
3 # mhmail -- 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), with these differences:
10 # * Instead of silently not sending an empty message, notifies user
11 # "mhmail: empty message not sent, use -body '' to force."
12 # * The compiled mhmail dropped a trailing newline from the -body argument.
13 # * Supports all post(8) (by default, without -profile) or send(1)
14 # (with -profile) options.
15 # * Optionally (with -profile) obeys the users profile, including
16 # AliasFile and send entries.
17 # * Adds -nosend option, which displays the draft but does not send it.
18 # * Drops support for undocumented -queue option.
19 #
20 # To do:
21 # * fix -resent with -profile
22 # * add -header-field name:body switch
23 # * add -attach file ... switch
24
25 usage='Usage: mhmail [addrs ... [switches]]
26 switches are:
27 -b(ody) text
28 -c(c) addrs ...
29 -f(rom) addr
30 -su(bject) text
31 -r(esent)
32 -pr(ofile)
33 -se(nd)
34 -nose(nd)
35 -v(ersion)
36 -h(elp)
37 and all post(8)/send(1) switches'
38
39 bindir=`dirname $0`
40 nmhbindir=`cd "${bindir}" && pwd`
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 from=
53 fromarg=0
54 subject=
55 subjectarg=0
56 resent=0
57 postsendargs=
58 switcharg=0
59 use_send=0
60 sendsw=1
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, -c, or -r. For the new switches that compiled mhmail
70 #### didn't have: let -p indicate mhmail -profile, not send
71 #### -port. -send masks the send(1) -send switch.
72 -b|-bo|-bod|-body) bodyarg=1 ;;
73 -c|-cc) ccarg=1 ;;
74 -f|-fr|-fro|-from) fromarg=1 ;;
75 -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
76 -nose|-nosen|-nosend) sendsw=0 ;;
77 -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
78 -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
79 exit 1 ;;
80 -r|-re|-res|-rese|-resen|-resent) resent=1 ;;
81 -se|-sen|-send) sendsw=1 ;;
82 -su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
83 -v|-ve|-ver|-vers|-versi|-versio|-version)
84 #### Cheat instead of using autoconf and make to fill in the version.
85 "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
86 -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
87 *) if [ ${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 [ ${fromarg} -eq 1 ]; then
94 from="${arg}"; fromarg=0
95 elif [ ${subjectarg} -eq 1 ]; then
96 subject="${arg}"; subjectarg=0
97 elif [ ${switcharg} -eq 1 ]; then
98 postsendargs="${postsendargs:+${postsendargs} }${arg}"
99 elif [ ${ccarg} -eq 1 ]; then
100 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
101 cclist="${cclist:+${cclist}, }${arg}"
102 else
103 #### An address.
104 tolist="${tolist:+${tolist}, }${arg}"
105 fi
106 esac
107 done
108
109 #### Check for at least one address and -from.
110 if [ "${tolist}"x = x ]; then
111 printf "mhmail: usage: mhmail addrs ... [switches]\n"
112 exit 1
113 fi
114 if [ "${from}"x = x ]; then
115 nmhlibdir=`${nmhbindir}/mhparam libdir`/
116 #### If nmhlibdir isn't right, assume that the nmh lib dir is on the PATH.
117 [ -x "${nmhlibdir}ap" ] || nmhlibdir=
118 from=`${nmhlibdir}ap -format '%(localmbox)' 0`
119 fi
120
121 #### Build header.
122 [ ${resent} -eq 0 ] && prefix= || prefix='Resent-'
123 header="${prefix}To: ${tolist}
124 "
125 [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist}
126 "
127 [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject}
128 ";
129 [ "${from}"x = x ] || header="${header}${prefix}From: ${from}
130 ";
131
132 #### Set up a file to supply as a draft to send/post. And set a
133 #### trap to remove it. send moves the file to a backup, so it will
134 #### remove that, too.
135 umask 077
136 tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
137 tmpfil="${tmpdir}/mhmail$$"
138 tmpfilbackup="${tmpdir}/[,#]mhmail$$"
139 trap 'rm -f '"${tmpfil}"' '"${tmpfilbackup}" EXIT
140
141 if [ ${resent} -eq 0 ]; then
142 #### Add blank line after header if not resending.
143 header="${header}
144 "
145 else
146 if [ ${use_send} -eq 0 ]; then
147 postsendargs="${postsendargs:+${postsendargs} }-dist"
148 else
149 mhdist=1; export mhdist
150 mhaltmsg=${tmpfil}; export mhaltmsg
151 #### This doesn't work, I'm not sure about mhaltmsg.
152 printf "mhmail: -resent not currently supported with -profile\n"
153 exit 1
154 fi
155 fi
156
157 if [ "${body}"x = x ]; then
158 #### First put message header in the file. cat >> handles blank
159 #### lines better than body=`cat`.
160 printf "%s" "${header}" > "${tmpfil}" || exit 1
161
162 tmpfile_size_before=`wc -c "${tmpfil}"`
163 #### Now grab the body from stdin.
164 cat >>"${tmpfil}"
165 tmpfile_size_after=`wc -c "${tmpfil}"`
166
167 #### Don't allow an empty body (from stdin). Use string
168 #### comparison so we don't have to strip the filename, etc.
169 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
170 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
171 exit 1
172 fi
173
174 #### Add trailing newline to body if it doesn't have one.
175 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
176 "
177 else
178 #### Put message header and body in the file.
179 printf "%s" "${header}${body}" > "${tmpfil}" || exit 1
180 fi
181
182 if [ ${sendsw} -eq 0 ]; then
183 cat "${tmpfil}"
184 else
185 if [ ${use_send} -eq 0 ]; then
186 post_or_send=`${nmhbindir}/mhparam postproc`
187 else
188 post_or_send="${nmhbindir}/send"
189 fi
190
191 if "${post_or_send}" "${tmpfil}" ${postsendargs}; then
192 exit
193 else
194 printf "Letter saved in dead.letter\n"
195 #### exec skips the trap set above.
196 exec mv "${tmpfil}" dead.letter
197 fi
198 fi
199 fi