#!/bin/sh
# mcom [TO] - compose mail

commajoin() {
	awk 'NR==1 {l=$0; next}
	     {l=l", "$0}
	     END {print l}'
}

notmine() {
	mine="$(maddr -a -h local-mailbox:alternate-mailboxes: $MBLAZE/profile)"
	grep -Fv -e "$mine"
}

reffmt() {
	sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\
</g' | uniq | sed 's/^/ /'
}

msgid() {
	mgenmid 2>/dev/null | sed 's/^/Message-Id: /'
}

stampdate() {
	if ! mhdr -h date "$1" >/dev/null; then
		tmp=$(mktemp -t mcom.XXXXXX)
		{
			printf 'Date: '
			mdate
			cat "$1"		
		} >"$tmp" &&
		mv "$tmp" "$1"
	fi
}

stripempty() {
	tmp=$(mktemp -t mcom.XXXXXX)
	msed 's/^[ \t]*$//d' "$1" >"$tmp"
	mv "$tmp" "$1"
}

needs_multipart() {
	mhdr -h attach "$1" >/dev/null ||
		grep -q '^#[^ ]*/[^ ]* ' "$1"
}

do_mime() {
	if needs_multipart "$draft"; then
		(
			IFS='
'
			msed '/attach/d' $draft
			for f in $(mhdr -M -h attach $draft); do
				printf '#%s %s\n' \
				       "$(file -Lbi $f | sed 's/ //g')" \
				       "$f"
			done
		) | mmime >$draftmime
	else
		mmime -r <"$draft" >"$draftmime"
	fi
}

MBLAZE=${MBLAZE:-$HOME/.mblaze}

sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} ${sendmail_args:--t}"

resume=
case "$0" in
	*mcom*)
		if [ "$1" = -r ]; then
			shift
			resume=1
			if [ "$#" -gt 0 ]; then
				echo "used draft $1"
				draft="$1"
				shift
			fi
		fi
		;;
esac

outbox=$(mhdr -h outbox "$MBLAZE/profile")
if [ -z "$outbox" ]; then
	if [ -z "$resume" ]; then
		i=0
		while [ -f "snd.$i" ]; do
			i=$((i+1))
		done
		draft="./snd.$i"
	elif [ -z "$draft" ]; then
		draft=$(ls -1t ./snd.*[0-9] | sed 1q)
	fi
	draftmime="./snd.$i.mime"
else
	if [ -z "$resume" ]; then
		draft="$(true | mdeliver -v -c -XD "$outbox")"
		if [ -z "$draft" ]; then
			printf '%s\n' "$0: failed to create draft in outbox $outbox." 1>&2
			exit 1
		fi
	elif [ -z "$draft" ]; then
		draft=$(mlist -D "$outbox" | msort -r -M | sed 1q)
	fi
	draftmime="$(printf '%s\n' "$draft" | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
fi

[ -z "$resume" ] &&
{
	case "$0" in
	*mcom*)
		printf 'To: '
		printf '%s\n' "$@" | commajoin
		printf '%s: \n' Cc Bcc Subject
		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
		[ "$from" ] && printf 'From: %s\n' "$from"
		cat "$MBLAZE/headers" 2>/dev/null
		msgid
		museragent
		printf '\n\n'
		;;
	*mfwd*)
		raw=
		[ "$1" = -r ] && raw=1 && shift
		[ "$#" -eq 0 ] && set -- .
		printf '%s: \n' To Cc Bcc
		COLUMNS=10000 mscan -f 'Subject: [%f] %s' "$@" | sed 1q
		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
		[ "$from" ] && printf 'From: %s\n' "$from"
		cat "$MBLAZE/headers" 2>/dev/null
		msgid
		museragent
		printf '\n\n'
		if [ -z "$raw" ]; then
			mseq -r "$@" | sed 's:^:#message/rfc822#inline :; s:$:>:'
		else (
			SEP=-----
			IFS='
'
			for f in $(mseq -r "$@"); do
				printf '%s Forwarded message from %s %s\n\n' \
				       $SEP "$(mhdr -d -h from "$f")" $SEP
				DISPLAY= mshow -n -N "$f" </dev/null |
					sed 's/^-/- &/'    # RFC934
				printf '\n%s %s %s\n\n' \
				       $SEP 'End forwarded message' $SEP
			done
		) fi
		;;
	*mbnc*)
		printf '%s: \n' Resent-To
		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
		[ "$from" ] && printf 'Resent-From: %s\n' "$from"
		msgid | sed 's/^/Resent-/'
		printf 'Resent-Date: %s\n' "$(mdate)"
		(
			IFS='
'
			cat $(mseq "${@:-.}")
		)
		;;
	*mrep*)
		[ "$#" -eq 0 ] && set -- .
		ng=$(mhdr -h newsgroups "$1")
		if [ "$ng" ]; then
			printf 'Newsgroups: %s\n' "$ng"
		else
			to=$(mhdr -h reply-to "$1")
			[ -z "$to" ] && to=$(mhdr -h from "$1")
			printf 'To: %s\n' "$to"
			printf 'Cc: %s\n' \
			       "$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
			printf 'Bcc: \n'
		fi
		printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")"
		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
		[ "$from" ] && printf 'From: %s\n' "$from"
		cat "$MBLAZE/headers" 2>/dev/null
		mid=$(mhdr -h message-id "$1")
		if [ "$mid" ]; then
			printf 'References:'
			{
				mhdr -h references "$1"
				printf '%s\n' "$mid"
			} | reffmt
			printf 'In-Reply-To: %s\n' "$mid"
		fi
		msgid
		museragent
		printf '\n'

		mquote "$1"
		printf '\n'
		;;
	esac

	case "$0" in
	*mbnc*) ;;
	*)
		if [ -f "$MBLAZE/signature" ]; then
			SIGNATURE="$MBLAZE/signature"
		elif [ -f ~/.signature ]; then
			SIGNATURE="$HOME/.signature"
		fi

		if [ -n "$SIGNATURE" ]; then
			printf '%s\n' '-- '
			cat "$SIGNATURE"
		fi
	esac
} >$draft

automime=
c=e
while :; do
	case "$c" in
	s|send)
		case "$(mhdr -h newsgroups "$draft")" in
			*gmane.*) sendmail="mblow -s news.gmane.org";;
			*.*) sendmail="mblow";;
		esac

		resent="$(maddr -h resent-to "$draft")"
		case "$resent" in
		?*)
			sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
			sendmail="${sendmail:-sendmail} -- $resent"
			;;
		esac

		if [ -e $draftmime ]; then
			if [ $draft -ot $draftmime ] || [ "$automime" -eq 1 ]; then
				stampdate $draftmime
				if $sendmail <$draftmime; then
					if [ "$outbox" ]; then
						mv $draftmime $draft
						mflag -d $draft
					else
						rm $draft $draftmime
					fi
					exit 0
				else
					printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				printf 'mcom: re-run mmime first.\n'
				c=
			fi
		else
			if mmime -c <$draft; then
				stampdate $draft
				if $sendmail <$draft; then
					if [ "$outbox" ]; then
						mflag -d $draft
					else
						rm $draft
					fi
					exit 0
				else
					printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				printf '%s\n' "mcom: message needs to be MIME-encoded first."
				c=
			fi
		fi
		;;
	c|cancel)
		stampdate $draft
		printf '%s\n' "mcom: cancelled draft $draft"
		exit 1
		;;
	m|mime)
		do_mime
		mshow -t $draftmime
		c=
		;;
	e|edit)
		c=
		if ! ${EDITOR:-vi} $draft; then
			c=c
		fi
		stripempty $draft
		if mmime -c <$draft; then
			automime=
		else
			automime=1
			do_mime
		fi
		;;
	d|delete)
		rm -i $draft
		if ! [ -f $draft ]; then
			rm -f $draftmime
			printf '%s\n' "mcom: deleted draft $draft"
			exit 0
		fi
		c=
		;;
	sign)
		msign $draft >$draftmime
		mshow -t $draftmime
		c=
		;;
	encrypt)
		mencrypt $draft >$draftmime
		mshow -t $draftmime
		c=
		;;
	show)
		if [ -e $draftmime ]; then
			mshow "$draftmime"
		else
			mshow "$draft"
		fi
		c=
		;;
	*)
		printf 'What now? (%s[s]end, [c]ancel, [d]elete, [e]dit, [m]ime, sign, encrypt) ' "${automime:+mime and }"
		read -r c
		;;
	esac
done
