# For more information, type "help zwgc"

# Punt all pings
if (upcase($opcode) == "PING") then exit endif

# initialization for message replay
set replay_sep = "\376"

# remove year from date
set dummy = rany($date, " year")

# remove cmu.edu from fromhost
set fromhost = downcase($fromhost)
if ($fromhost =~ ".*\.cmu\.edu") then
	set dummy = rany($fromhost, ".cmu.edu")
	# remove andrew
	if ($fromhost =~ ".*\.andrew") then
		set dummy = rany($fromhost, ".andrew")
	endif
endif

# remove CMU.EDU from sender
if ($sender =~ ".*@.*\.CMU\.EDU") then
	set dummy = rany($sender, ".CMU.EDU")
endif

if ($opcode == "REPLAY") then
	if ($sender != getenv("USER")) then
		print "@bold("+$sender+")@@"+$fromhost+" tried to replay your messages\n"
		put
		exit
	endif

	fields signature body
	if ($body =~ "\n$") then
		set dummy = rany($body, "\n")
	endif
	if ($body == "") then
		set body = "x"
	endif

	set replay_counter = $body
	set temp_buffer = $replay_buffer
	while ($temp_buffer != "") do
		set msg = lbreak($temp_buffer, $replay_sep)
		set dummy = lany($temp_buffer, $replay_sep)
		if (($msg != "") & ($replay_counter =~ "^.$")) then
			clearbuf
			print "@center(@bold(This is a recording...))\n"
			print $msg
			put
		endif
		set dummy = lany($replay_counter, "x")
	endwhile
	exit
endif



# Sort by class
case upcase($class)

match "WG_CTL_CLASS"
	# initialization
	if ($opcode == "WG_STARTUP") then
		set replay_buffer = zvar("replay-count")
		if ($replay_buffer == "") then
			# eight -- see below
			set replay_buffer = "\376\376\376\376\376\376\376\376"
		else
			set counter = $replay_buffer
			set replay_buffer = ""
			while ($counter != "") do
				set replay_buffer = $replay_buffer+$replay_sep
				set dummy = rany($counter, "x")
			endwhile
		endif
		# off-by-one because these are actually separators not "bodies"
		set dummy = lany($replay_buffer, "x")
	endif
	exit

match "OFFICIAL"
	fields signature body
	print "@center(@large(@bold(Official Notice\n)))"
	print "@center(@large(@bold("+$instance+"\n\n)))"
	print $time+" on "+$date+"\n\n"
	print "@("+protect($body)+")"
	print "\n@right(@italic("+protect($signature)+"))\n"
	put

match "LOGIN"
	case $opcode
	match "USER_LOGIN"
		set way = "logged in to"
	match "USER_LOGOUT"
		set way = "logged out from"
	default
		set way = "unknown opcode"
	endcase

	fields host when tty

	# remove cmu.edu from host
	if (downcase($host) =~ ".*\.cmu\.edu") then
		set dummy = rany($host, ".cmu.edu")
		# remove andrew
		if (downcase($host) =~ ".*\.andrew") then
			set dummy = rany($host, ".andrew")
		endif
	endif

	show
		@center(@bold($sender) $way @bold($host)
		@center(on $tty at $when)
	endshow
	put

default
	/* filter of unwanted users */
	set ignore_list = zvar("ignore-list")
	if ($ignore_list != "") then
		if ($ignore_list =~ "\n$") then
			set dummy = rany($ignore_list, "\n")
		endif
		set ignore_list = ":"+$ignore_list+":"
		if ($ignore_list =~ substitute(".*:$sender:.*")) then
			exit
		endif
	endif
	set unstyle_body = zvar("unstyle-body")
	set unstyle_sig  = zvar("unstyle-signature")
	set personal = ($recipient == $user)

	if ($personal) then
		set d_recipient = " {Personal}"
	else
		set dummy = lany($recipient, "*")
		# remove .CMU.EDU from realm of origin
		if ($recipient =~ "@.*\.CMU\.EDU") then
			set dummy = rany($recipient, ".CMU.EDU")
		endif
		set d_recipient = " {Broadcast"+$recipient+"}"

	endif

	if ($auth == "yes") then
		set d_auth = ""
	else
		set d_auth = "@bold(Unauthentic) "
	endif

	if ((upcase($class)) == "MESSAGE") then
		set d_class = ""
	else
		set d_class = "@bold("+$class+") Instance "
	endif

	if ((upcase($instance)) == "PERSONAL") then
		if ($personal) then
			set d_instance = "@bold(Personal)"
			set d_recipient = ""
		else
			set d_instance = "@bold(personal"+$recipient+")"
		endif
	else
		if ($personal) then
			set d_instance = "@bold("+$instance+")"
		else
			set d_instance = "@bold("+$instance+$recipient+")"
		endif
	endif

	fields signature body

	print "@center("$d_auth+$d_class+$d_instance+$d_recipient+" Message at "+$time+" on "+$date")\n\n"

	if ($unstyle_body == "") then
		print "@("+protect($body)+")"
	else
		print stylestrip(protect($body))
	endif

	if ($unstyle_sig == "") then
		print "\n@right(@italic(@("+protect($signature)+") <"+$sender+"> on "+$fromhost+"))\n"
	else
		print "\n@right(@italic("+stylestrip(protect($signature))+" <"+$sender+"> on "+$fromhost+"))\n"
	endif
	put
	
endcase

## record the message
# dispose of oldest message
set dummy = rbreak($replay_buffer, $replay_sep)
# remove final separator character
set dummy = rany($replay_buffer, $replay_sep)
# add most recent message
set replay_buffer = buffer()+$replay_sep+$replay_buffer

exit
