about summary refs log tree commit diff
path: root/mcom
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-08 16:01:06 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-08 16:01:06 +0200
commitd2d687478d055fa0acf2a6452cac324cc6bb54fb (patch)
tree3df33f515b20d38d0dc3fecdee89873603215a84 /mcom
parentd453a5ac58e7e197e22a73ab9fc7ed24c213e383 (diff)
downloadmblaze-d2d687478d055fa0acf2a6452cac324cc6bb54fb.tar.gz
mblaze-d2d687478d055fa0acf2a6452cac324cc6bb54fb.tar.xz
mblaze-d2d687478d055fa0acf2a6452cac324cc6bb54fb.zip
rename mcomp -> mcom, mrepl -> mrep
mcomp is in mtools already.

mfwd will be added in the future.
Diffstat (limited to 'mcom')
-rwxr-xr-xmcom167
1 files changed, 167 insertions, 0 deletions
diff --git a/mcom b/mcom
new file mode 100755
index 0000000..72269a8
--- /dev/null
+++ b/mcom
@@ -0,0 +1,167 @@
+#!/bin/sh
+# mcom [TO] - compose mail
+
+commajoin() {
+	sed ':a;N;s/\n/, /;$!b a'
+}
+
+reffmt() {
+	sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
+}
+
+msgid() {
+	mgenmid 2>/dev/null | sed 's/^/Message-Id: /'
+}
+
+msgdate() {
+	echo -n "Date: "
+	mdate
+}
+
+outbox=$(mhdr -h outbox ~/.mblaze/profile)
+if [ -z "$outbox" ]; then
+	i=0
+	while [ -f "snd.$i" ]; do
+		i=$((i+1))
+	done
+	draft="snd.$i"
+	draftmime="./snd.$i.mime"
+else
+	draft="$(true | mdeliver -v -XD "$outbox")"
+	if [ -z "$draft" ]; then
+		echo "$0: failed to create draft in outbox $outbox." 1>&2
+		exit 1
+	fi
+	draftmime="$(echo $draft | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
+fi
+
+{
+	case "$0" in
+	*mcom*)
+		echo -n "To: $1"
+		[ "$#" -ge 1 ] && shift
+		for rcpt; do
+			echo -n ", $rcpt"
+		done
+		echo
+		echo "Cc: "
+		echo "Bcc: "
+		echo "Subject: "
+		from=$(mhdr -h local-mailbox ~/.mblaze/profile)
+		[ "$from" ] && echo "From: $from"
+		cat ~/.mblaze/headers 2>/dev/null
+		msgid
+		msgdate
+		echo
+		echo
+		;;
+	*mrep*)
+		[ "$#" -eq 0 ] && set -- .
+		to=$(mhdr -h reply-to "$1")
+		[ -z "$to" ] && to=$(mhdr -h from "$1")
+		echo "To: $to"
+		echo "Cc: $(mhdr -d -A -h to:cc: "$1" | commajoin)"
+		echo "Bcc: "
+		s=$(mhdr -d -h subject "$1")
+		os=
+		while [ "$os" != "$s" ]; do
+			os=$s
+			s=${s# }
+			s=${s#[Rr][Ee]:}
+			s=${s#[Aa][Ww]:}
+			s=${s#[Ff][Ww][Dd]:}
+		done
+		echo "Subject: Re: $s"
+		cat ~/.mblaze/headers 2>/dev/null
+		mid=$(mhdr -h message-id "$1")
+		if [ "$mid" ]; then
+			echo -n "References:"
+			{
+				mhdr -h references "$1"
+				printf "%s\n" "$mid"
+			} | reffmt
+			echo "In-Reply-To: $mid"
+		fi
+		msgid
+		msgdate
+		echo
+
+		mquote "$1"
+		echo
+	esac
+
+	if [ -f ~/.signature ]; then
+		printf '%s\n' '-- '
+		cat ~/.signature
+	fi
+} >$draft
+
+c=e
+while :; do
+	case "$c" in
+	s|send)
+		if [ -e $draftmime ]; then
+			if [ $draft -ot $draftmime ]; then
+				if sendmail -t <$draftmime; then
+					if [ "$outbox" ]; then
+						mv $draftmime $draft
+						mflag -d $draft
+					else
+						rm $draft $draftmime
+					fi
+					exit 0
+				else
+					echo "mcom: sendmail failed, kept draft $draft"
+					exit 2
+				fi
+			else
+				echo "mcom: re-run mmime first."
+				c=
+			fi
+		else
+			if mmime -c <$draft; then
+				if sendmail -t <$draft; then
+					if [ "$outbox" ]; then
+						mflag -d $draft
+					else
+						rm $draft
+					fi
+					exit 0
+				else
+					echo "mcom: sendmail failed, kept draft $draft"
+					exit 2
+				fi
+			else
+				echo "mcom: message needs to be MIME-encoded first."
+				c=
+			fi
+		fi
+		;;
+	c|cancel)
+		echo "mcom: cancelled draft $draft"
+		exit 1
+		;;
+	m|mime)
+		(
+			IFS='
+'
+			msed '/attach/d' $draft
+			for f in $(mhdr -M -h attach $draft); do
+				echo "#$(file -Lbi $f | sed 's/ //g') $f"
+			done
+		) | tee /tmp/v | mmime >$draftmime
+		mshow -t $draftmime
+		c=
+		;;
+	e|edit)
+		c=
+		if ! ${EDITOR:-vi} $draft; then
+			c=c
+		fi
+		;;
+	*)
+		echo -n "What now? ([s]end, [c]ancel, [e]dit, [m]ime) "
+		read -r c
+		;;
+	esac
+done