#!/usr/local/bin/perl

# ShoutcastpWIT[o`FbJ
# laser@s7.xrea.com
# socketgȂT[oł͓܂
# ǂg悤܂܂񂪐ӔC͂Ƃ܂

# gp@
# ڌĂяoSSI[h(ڑ҃JEgt)
# radio.cgi?mode=redirect&imagedir=http://\摜̃fBNgURL
# CGI[h(_CNg)

#--------------------------------------------------------------------

# ڑURL
$get_url = "http://nyama.sytes.net:8000/";

# Ot@C
$logfile = "./radio8000log.txt";

# XVԊu 5Ȃ500 30bȂ30
# ӁFԊu蒷ĂANZXƍXV܂
$updatetime_temp = 100;

# \摜 t@Ĉ
$radio_pic1 = "radio_onair3.gif"; # ON AIR
$radio_pic2 = "radio_off4.gif";   # OFF

# ̉摜fBNg Ō / Kv
$noquery_imgurl = "http://laser.s7.xrea.com/";

# SSI[h̉摜\ev[g
# $reprint ON AIR\摜URL
sub outimage{
	print "<a href=\"http://nyama.sytes.net:8000/listen.pls\"><img src=\"$reprint\" alt=\"60bƂɍXV\" border=\"0\"></a>";
}

# SSI[h̐ڑҐ\ev[g
# $onair_ip ڑҐ
sub outconnect{
	print " <font size=\"-1\">݂̐ڑ : $onair_ip (60bȓ)</font>";
}

#--------------------------------------------------------------------

# Ԏ擾
$ENV{TZ} = 'JST-9';
$times = time;
($sec,$min,$hour,$mday,$month,$year,$wday,undef,undef) = localtime($times);
$month++;
$year = 1900 + $year;
$actime = sprintf("%d%02d%02d%02d%02d%02d", $year, $month, $mday, $hour, $min, $sec);

# mF
@PAIRS = split(/&/,$ENV{'QUERY_STRING'});
foreach(@PAIRS){
	($key, $value) = split(/=/, $_);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$QUERY{$key} = $value;
}

# OmF $onairip+1000Ă
open(IN,"<$logfile");
($onaircheck,$lastupdatetime,$onairip) = split(/<>/, <IN>);
close(IN);

# \Jn
if($actime > $lastupdatetime + $updatetime_temp){
	$onairflag = &httpget; # OFFȂ0 Ȃڑ+1000Ԃ
	&printout($onairflag,$onairflag);
}else{
	&printout($onaircheck,$onairip);
}

exit;

#--------------------------------------------------------------------

# Ԃ
sub printout{
	local($onair_c,$onair_ip) = @_;
	# 摜\֌W
	$QUERY{'imagedir'} = $noquery_imgurl if(!($QUERY{'imagedir'} =~ /http\:\/\//));
	if(!$onair_c){
		$reprint = "$QUERY{'imagedir'}$radio_pic2";
	}else{
		$reprint = "$QUERY{'imagedir'}$radio_pic1";
	}

	# o
	if($QUERY{'mode'} eq "redirect"){
		print "Location: $reprint\n\n";
	}else{
		print "Content-type: text/html\n\n";
		&outimage;
		if($onair_ip){
			$onair_ip = $onair_ip - 1000;
			&outconnect;
		}
	}
}

# socketget
sub httpget{
	# ߂2000(www.amezo2000.com)̃XNvg
	$get_url =~ /http:\/\/([^:\/]*)(:([0-9]+))?(\/.*)?/;
	$host = $1;
	$port = ($3 ne "") ? $3 : 80;
	$path = ($4 ne "") ? $4 : "/";
	$addr = (gethostbyname($host))[4];
	$name = pack("S n a4 x8", 2, $port, $addr);
	socket(S, 2, 1, 0);
	connect(S, $name);
	binmode(S);
	select(S); $| = 1; select(stdout);
	print S "GET $path HTTP/1.0\r\n"
		. "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)\r\n"
		. "Host: $host\r\n"
		. "Referer: $get_url\r\n\r\n";
	while(<S>){
		if ($_ eq "\n" || $_ eq "\r\n" || $_ eq "\r") { last; }
	}
	while(<S>){
		$data2 .= $_;
	}
	close(S);

	# ON AIR KȂ̂ŏ
	if($data2 =~ /Server is currently up and/){
		$onair = 1;
		if($data2 =~ /listeners \(([0-9]*) unique\)/){
			$ipprint = $1;
			$ipprint = $ipprint + 1000;
		}else{
			$ipprint = 1000;
		}
	}else{
		$onair = 0;
		$ipprint = 0;
	}

	# O
	open(OUT,"+< $logfile");
	flock(OUT,2);
	truncate(OUT,0);
	seek(OUT,0,0);
	print OUT "$onair<>$actime<>$ipprint<>\n";
	close(OUT);
	return $ipprint;
}

