#!/usr/local/plan9/bin/rc

. 9.rc
. $PLAN9/lib/acme.rc

mh=/usr/bin/mh	# directory with mh binaries
path=($path $mh)

fn event {
	# $1 - c1 origin of event
	# $2 - c2 type of action
	# $3 - q0 beginning of selection
	# $4 - q1 end of selection
	# $5 - eq0 beginning of expanded selection
	# $6 - eq1 end of expanded selection
	# $7 - flag
	# $8 - nr number of runes in $7
	# $9 - text
	# $10 - chorded argument
	# $11 - origin of chorded argument

	switch($1$2){
	case E*	# write to body or tag
	case F*	# generated by ourselves; ignore
	case K*	# type away we do not care
	case Mi	# mouse: text inserted in tag
	case MI	# mouse: text inserted in body
	case Md	# mouse: text deleted from tag
	case MD	# mouse: text deleted from body

	case Mx MX	# button 2 in tag or body
		$x $*

	case Ml ML	# button 3 in tag or body
		$l $* &
	}
}

listfmt='%(msg)/	%<(mymbox{from})%<{to}To: %(decode(friendly{to}))%>%>%<(zero)%(decode(friendly{from}))%>	%(day{date}) %(month{date}) %(mday{date}) %02(hour{date}):%02(min{date})\n	%(decode{subject})'

fn boxget {
	echo -n , | winwrite addr
	$mh/scan +^$boxname -reverse -format $listfmt | winwrite data
	winctl clean
}

fn boxexec {
	echo boxexec
	switch($9){
	case Get
		boxget
	case Mail*
		postwin `{echo $9 | sed 's/Mail *//'} &
	case Delmesg Spam
		winctl 'addr=dot'
		msgs=`{winread xdata | sed -n 's/^([0-9]+)\/.*/\1/p'}
		if(~ $9 Spam)
			targ=spam
		if not
			targ=d
		thebox=`{echo $boxname | sed 's;.*/;;'}
		for(i in $msgs)
			$mh/refile -src +$thebox $i +$targ
		boxget
	case *
		winwriteevent $*
	}
}

fn boxload {
	whatis '*'
	if(~ $x boxexec && ! ~ $3 $4){
		echo -n '#'$3,'#'$4 | winwrite addr
		msgs=`{winread xdata | sed -n 's/^([0-9]+)\/.*/\1/p'}
		for(i in $msgs)
			if(test -f $boxname/$i){
				@{ msgwin $boxname $i } &
			}
	}
	if not{
		switch($9){
		case [0-9]*
			@{ msgwin $boxname $9 } &
		case */Mail/*
			f=`{echo $9 | sed 's;/+$;;'}
			if(test -d $f){
				boxwin $f &
			}
			if not if(test -f $f){
				echo reloading msg
				box=`{echo $f | sed 's;/[^/]+$;;'}
				num=`{echo $f | sed 's;.*/;;'}
				@{ msgwin $box $num } &
			}
			if not
				winwriteevent $*
		case *
			winwriteevent $*
		}
	}
}

fn cmd {
	$1 - - - - - - - - $2
}

fn boxwin {
	newwindow
	boxname=$1
	winname $boxname
	x=boxexec
	l=boxload
	cmd $x Get
	echo -n 'Get Mail Delmesg Spam ' | winwrite tag
	wineventloop
}

fn msgexec {
	echo msgexec
	switch($9){
	case Get
		echo -n , | winwrite addr
		u $mh/mhshow -nopause +^$boxname $msgnum | 9 grep -v '^part [0-9]?   +text' | 9 fmt -j | winwrite data
		winctl clean
	case Mail*
		postwin `{echo $9 | sed 's/^Mail *//'} &
	case Reply*
		postwin `{echo $9 | sed 's/^Reply *//'} &
	case Fwd*
		postwin -f `{echo $9 | sed 's/^Fwd *//'} &
	case Delmesg
		if(~ $boxname */*){
			thebox=`{echo $boxname | sed 's;.*/;;'}
			$mh/refile -src +$thebox $msgnum +d
			winctl delete
		}
	case Spam
		if(~ $boxname */*){
			thebox=`{echo $boxname | sed 's;.*/;;'}
			$mh/refile -src +$thebox $msgnum +spam
			winctl del
		}
	case *
		winwriteevent $*
	}
}

fn msgwin {
	newwindow
	boxname=$1
	msgnum=$2
	winname $boxname/$msgnum
	shift
	shift
	x=msgexec
	l=boxload
	if(~ $boxname */draft){
		x=postexec
		echo -n 'Post ' | winwrite tag
	}
	if not
		echo -n 'Reply Delmesg ' | winwrite tag
	cmd $x Get
	wineventloop
}

fn postexec {
	switch($9){
	case Get
		winctl get
	case Post
		switch($boxname){
		case */draft
			if(winctl put)
				$mh/send -attach Attach -push -draftmessage $boxname/$msgnum 
		case *
			# should copy body into draft file and post it
			# can't put - would overwrite good message
			echo cannot post from non-draft
		}
	case Del
		# check that window is clean!
		winwriteevent $*
	case *
		winwriteevent $*
	}
}

fn replyquote {
	sed '1,/^$/d' | sed 's/^/> /' 
}
fn fwdquote {
	echo '--- Forwarded Message ---'
	cat 
}

fn 
nl='
'
fn postwin {
	quote=replyquote
	if(~ $1 -f){
		quote=fwdquote
		shift
	}
	if(~ $#msgnum 1){
		# prepare a reply
		winread body | 9 grep -i '^(Reply-To|Subject|From|Message-Id):' >/tmp/mail.$pid
		winread body | $quote >/tmp/body.$pid
		oifs=$ifs
		ifs=$nl
		subject=`{9 grep -i '^Subject:' /tmp/mail.$pid | sed 1q | sed 's/^Subject: *//'}
		from=`{9 grep -i '^From:' /tmp/mail.$pid | sed 1q | sed 's/^From: *//'}
		msgid=`{9 grep -i '^Message-Id:' $boxname/$msgnum | sed 1q | sed 's/^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]: *//'}
		replyto=`{9 grep -i '^Reply-To:' $boxname/$msgnum | sed 1q | sed 's/^[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]: *//'}
		rm -f /tmp/mail.$pid
		ifs=$oifs
		if(~ $#* 0){
			if(! ~ $#replyto 0)
				*=($replyto)
			if not
				*=($from)
		}
		if(! echo $subject | 9 grep -si 're:')
			subject='Re: ' ^ $"subject
	}
	newwindow
	n=1
	while(test -f $home/Mail/draft/$n)
		n=`{echo 1+$n|hoc}
	name=$home/Mail/draft/$n
	msgnum=$n
	boxname=$home/Mail/draft
	{
		echo 'To:' $*
		if(! ~ $#subject 0)
			echo 'Subject: '^$subject
		cat $home/Mail/headers
		if(! ~ $#msgid 0)
			echo 'In-Reply-To: '^$msgid
		echo 
		if(test -f /tmp/body.$pid){
			cat /tmp/body.$pid
			rm /tmp/body.$pid
		}
	} >$name

	winname $name
	winctl get
	echo -n 'Post' | winwrite tag
	x=postexec
	l=boxload
	wineventloop
}

boxwin $home/Mail/inbox; exit

