#!/usr/bin/perl

# propagate.pl
# - rewrites web pages with .down.tmpl in case of downtime
# - should be in crontab and rn as frequently as possible
# Copyright (c) 1999 Arne Claassen <shoutcast@claassen.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use IO::Socket;

$, = "\n";

# ____ Define Globals _________________________________________________________
use vars qw($SOCK $PIDFILE $LOGFILE $MP3SPYSERVER $MP3SPYURL @SONGINFO @WEEK %CONF);

print "Content-type: text/html\n\n";

# ____ Set up Constants _______________________________________________________
$MP3SPYSERVER = "update.mp3spy.com";
$MP3SPYURL = "/mp3spy/song.php3";
$SLEEPTIME = '60';
$LOGFILE = '/home/arne/web/propagate.log';


# _____ Get the Input ________________________________
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/\t/ /g;
	$in{$name} = $value;
	}

if ($ENV{'QUERY_STRING'}) {
	@query_vals = split(/&/, $ENV{'QUERY_STRING'});
	foreach $couple (@query_vals) {
		($name, $value) = split(/=/, $couple);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$value =~ s/\t/ /g;
		$in{$name} = $value;
		}
	}

my($result) = updateMP3spy(%in);
if(!$result) {
	print "OK\n";
	}
else {
	print "KO\n$result";
	}

exit;

# ____ updateMP3spy ____________________________________________________________
sub updateMP3spy {
	my(%in) = @_;
	my($trackinfo) = $in{'title'};
	if(!$trackinfo) { return("no trackinfo provided"); }
	my($key,$good,$val);
	$trackinfo =~ s/([\W])/"%".unpack('H', $1).unpack('h', $1)/eg;
	$trackinfo =~ tr/ /+/;
	my($mp3spyquery) = "?ip=$in{'ip'}&title=$trackinfo";

	my $sock = IO::Socket::INET->new(	Proto => "tcp",
										PeerAddr => "$MP3SPYSERVER",
										PeerPort => 80,
										Timeout => 30
										);
	if(!$sock) { return("Unable to connect to $MP3SPYSERVER"); }
	$sock->autoflush(1);

	print $sock "GET ${MP3SPYURL}${mp3spyquery} HTTP\1.0\n\n";
#	print "GET ${MP3SPYURL}${mp3spyquery} HTTP\1.0\n\n";

	while($val = <$sock>) {
#		print "$val<BR>\n";
		if ($val =~ /^OK$/) { $good = 1; }
		elsif($val =~ /^NO$/) { $good = 0; }
		}
	close $sock;
	if(!$good) { return("MP3Spy did not return a good code"); }
	return;
	}