#!/usr/ug/bin/perl5
#			vim:fo=croql:cin:com=\:#
#
# the special box format (Heil)
# now with html-esque tags for increased irritation


use strict;
use POSIX qw(strftime);

use vars qw($UON $UOFF $BLON $BLOFF $BON $BOFF $CEOL $STOMP $BEEP);



my $COLUMNS;
if (defined($ENV{'COLUMNS'})) {
	$COLUMNS= $ENV{'COLUMNS'};
}
else {
	$COLUMNS= 80;
}

exit if $ENV{'GALE_CATEGORY'} =~ m-/ping$|/ping:-;

if (-t 1) {
	$BON= `tput md 2>/dev/null`;
	if ($? == 0) {
		$BOFF= `tput me`;
		$UON= `tput us`;
		$UOFF= $BOFF;
		$BLON= `tput so`;
		$BLOFF= $BOFF;
	}
	else {
		$BON= `tput bold`;
		$BOFF= `tput sgr0`;
		$UON= `tput smul`;
		$UOFF= `tput rmul`;
		$BLON= `tput blink`;
		$BLOFF= $BOFF;
	}

	$CEOL= `tput el`;
	$STOMP= "\r";
	$BEEP= "\a";
}


if ($ENV{'GALE_CATEGORY'} =~ m-/receipt$|/receipt:-) {
	&printReceipt;
}
else {
	&printMessage;
}

sub printReceipt {
#* <time> Received by <jtr@ugcs.caltech.edu> (John Reese) 12/26 16:29
	print "* Received by ";
	if (defined($ENV{'GALE_SIGNED'})) {
		print "<$BON", $ENV{'GALE_SIGNED'}, "$BOFF>";
	}
	else {
		print "*${BON}unverified${BOFF}*";
	}
	print " (", $ENV{'HEADER_FROM'}, ")" if defined($ENV{'HEADER_FROM'});
	print strftime(" %m/%d %H:%M", localtime($ENV{'HEADER_TIME'}))
		if defined($ENV{'HEADER_TIME'});
	print "\n";
}

sub printMessage {
	my $hatemail= 0;
	print "${STOMP}${BON}";
	my $y= "";
	$y .= "+" if defined($ENV{'HEADER_RECEIPT_TO'});
	$y .= "[" . $ENV{'GALE_CATEGORY'} . "] ";

	if (defined($ENV{'GALE_TEXT_MESSAGE_SENDER'}) ||
		defined($ENV{'GALE_SIGNED'})) {
		$y .= $ENV{'GALE_TEXT_MESSAGE_SENDER'},
			if defined($ENV{'GALE_TEXT_MESSAGE_SENDER'});

		if (defined($ENV{'GALE_SIGNED'})) {
			$y .= " <" . $ENV{'GALE_SIGNED'} . ">";
		}
		else {
			$y .= " *unverified*";
		}
	}
	else {
		$y .= "*anonymous jackass*";
	}
	$y .= ' ';
	my $fullw= ($COLUMNS-2) + $COLUMNS * int((length($y) + 2) / $COLUMNS);
	$y .= '=' x ($fullw - length($y)) . "\\${BOFF}\n";
	print $y;
	while (<stdin>) {
		chop;
		my $width= $COLUMNS - 5;
		s/\t/        /g;
		if (/<HR\/>/i) {
			my @lines= split /<HR\/>/i, $_;
			printf("${BON}|${BOFF} %-*s ${BON}|${BOFF}\n", $width, $lines[0])
				if (length($lines[0]) > 0);
			$hatemail++ if (length($lines[0]) > $width);
			printf("${BON}|${BOFF}%*s${BON}|${BOFF}\n", $width+2,
				   "-" x ($width+2));
			printf("${BON}|${BOFF} %-*s ${BON}|${BOFF}\n", $width, $lines[1])
				if (length($lines[1]) > 0);
			$hatemail++ if (length($lines[1]) > $width);
		}
		else {
			$width += &fmtTag('<B>', $BON);
			$width += &fmtTag('</B>', $BOFF);
			$width += &fmtTag('<U>', $UON);
			$width += &fmtTag('</U>', $UOFF);
			if (! -f $ENV{'HOME'} . "/.no-heil-blink" &&
				! -f $ENV{'HOME'} . "/.no-heil-blink-" . $ENV{'TERM'}) {
				$width += &fmtTag('<[^/>]*>', $BLON);
				$width += &fmtTag('</[^/>]*>', $BLOFF);
				$width += &fmtWrapTag('<[^/>]*/>');
			}
			s/\&lt;/</gi;
			s/\&gt;/>/gi;

			printf("${BON}|${BOFF} %-*s ${BON}|${BOFF}\n", $width, $_);
			$hatemail++ if (length($_) > $width);
			$hatemail= 0 if (/Heil/);
		}
	}

	my $x= "\\ ";
	if (defined($ENV{'GALE_ENCRYPTED'})) {
		$x .= "Private message decrypted with " . $ENV{'GALE_ENCRYPTED'};
	}
	else {
		$x .= "Public message";
	}

	$x .= " (overrun)" if ($hatemail);
	
	$x .= ", " . $ENV{'GALE_TIME_ID_TIME'} . " ";
	$x .= "_" x ($COLUMNS - 2 - length($x)) . "/";
	print "${BON}${x}${BOFF}\n";
	print "${BON} \\${BOFF}\n${BON}  \\ Addressed to: ",
		$ENV{'GALE_TEXT_MESSAGE_RECIPIENT'}, "${BOFF}\n"
		if (defined($ENV{'GALE_TEXT_MESSAGE_RECIPIENT'}));
	print "${BEEP}" if defined($ENV{'GALE_ENCRYPTED'});
	print "\n";
	if ($hatemail && (-f $ENV{'HOME'} . "/.i-am-an-asshole")) {
		if (defined($ENV{'GALE_SIGNED'})) {
 			# evil marginbot omitted
		}
		else {
 			# evil marginbot omitted
		}
	}
	
		

}


sub fmtTag {
	my($tag, $code)= @_;

	if (/$tag/i) {
		my $r= s/$tag/$code/gi;
		return $r * length($code);
	}
	return 0;
}

sub fmtWrapTag {
	my($tag)= @_;

	if (/$tag/i) {
		my $r= s/($tag)/${BLON}\1${BLOFF}/gi;
		return $r * (length($BLON.$BLOFF));
	}
	return 0;
}
