#!usr/bin/perl

#########################################################################
#	Author	Daniel Chokola						#
#	Title	Milkbot							#
#	Date	4/16/2003						#
#	Desc	Controls Winamp or XMMS over Milkbone via Milkbot	#
#########################################################################

package MilkbotMusic;

use strict;
no strict 'refs';
use warnings;

use Milkbone;


if($^O =~ /linux/) {
	eval "use Xmms::Remote";
}
elsif($^O =~ /Win32/) {
	eval "use Winamp::Control";
}

my ($amp, $os, %commands);

register_hook("milkbot_get_commands", \&init);

sub init {
	my $cmd = '';

	if($^O =~ /Win32/i) {
		$os = 'win32';
	}
	elsif($^O =~ /linux/) {
		$os = 'linux';
	}

	if($^O =~ /Win32/) {
		$amp = Winamp::Control->new(host => 'localhost', port => 4800, passwd => 'pass');
	}
	elsif($^O =~ /linux/) {
		$amp = new Xmms::Remote ();
	}

        $commands{'.list'}[0]   = "list_$os";
        $commands{'.list'}[1]   = '.list [num]--makes a list of songs num long based on the song currently playing, 5 by default';
        $commands{'.next'}[0]   = "next_$os";
        $commands{'.next'}[1]   = '.next--jumps ahead one track';
        $commands{'.paus'}[0]   = "paus_$os";
        $commands{'.paus'}[1]   = '.paus--pauses the current song';
        $commands{'.play'}[0]   = "play_$os";
        $commands{'.play'}[1]   = '.play--plays the current song';
        $commands{'.prev'}[0]   = "prev_$os";
        $commands{'.prev'}[1]   = '.prev--jumps back one track';
        $commands{'.rand'}[0]   = "rand_$os";
        $commands{'.rand'}[1]   = '.rand--randomly selects a song';
        $commands{'.rpt'}[0]    = "rpt_$os";
        $commands{'.rpt'}[1]    = '.rpt [on|off]--toggles the repeat status (on or off)';
        $commands{'.shuf'}[0]   = "shuf_$os";
        $commands{'.shuf'}[1]   = '.shuf [on|off]--toggles the shuffle status (on or off)';
        $commands{'.song'}[0]   = "song_$os";
        $commands{'.song'}[1]   = '.song--displays the current song';
        $commands{'.stop'}[0]   = "stop_$os";
        $commands{'.stop'}[1]   = '.stop--stops playback';
        $commands{'.vis'}[0]    = "vis_$os";
        $commands{'.vis'}[1]    = '.vis--turns on the visualization';

	for(keys(%commands)) {
		hook("milkbot_set_command", -name => $_, -desc => $commands{$_}[1]);
	}
	register_hook("milkbot_command", sub { $cmd = $ARGS{-cmd};
		register_hook("milkbot_command_$cmd", sub {
			$commands{$cmd}[0]->($ARGS{-user}, $ARGS{-msg});
			deregister_hook("milkbot_command_$cmd") if $cmd;
		});
	});
}

sub send_im {
	my ($to, $msg, $away) = @_;
	$away = 0;
	print $msg, $to;
	hook("protocol_send_im", -dest => $to, -msg => $msg, -away => $away);
}

sub list_linux {
	my ($from, $msg) = @_;
	my @list;

	return if($amp->get_playlist_length < 1);
	if($msg) {
		for(my $i = 0; $i < $msg; $i++) {
			$list[$i] = $amp->get_playlist_title($amp->get_playlist_pos + $i - (($msg - 1) / 2));
		}
	}
	if(@list) {
		$list[$msg / 2] = '<b>' . $list[$msg / 2] . '</b>';
		send_im($_[0], join("\n", @list), 0);
	}
	else {
		for(my $i = 0; $i < 5; $i++) {
			$list[$i] = $amp->get_playlist_title($amp->get_playlist_pos + $i - 2);
		}
		$list[2] = '<b>' . $list[2] . '</b>';
		send_im($_[0], join("\n", @list), 0);
	}
}

sub next_linux {
	$amp->playlist_next;
	sleep(1.1);
	song_linux(@_);
}

sub paus_linux {
	$amp->pause;
	send_im($_[0], 'Pause <b>toggled</b>.', 0);
}

sub play_linux {
	$amp->play;
	sleep(1.1);
	song_linux(@_);
}

sub prev_linux {
	$amp->playlist_prev;
	sleep(1.1);
	song_linux(@_);
}

sub rand_linux {
	$amp->playlist_next;	# a simple, but inelegant solution to the
	$amp->toggle_shuffle;	# problem of randomizing tracks in xmms:
	$amp->playlist_next;	# do it twice!
	$amp->toggle_shuffle;
	sleep(1.1);
	song_linux(@_);
}

sub rpt_linux {
	$amp->toggle_repeat();
	send_im($_[0], 'Repeat <b>toggled</b>.', 0);
}

sub shuf_linux {
	$amp->toggle_shuffle();
	send_im($_[0], 'Shuffle <b>toggled</b>.', 0);
}

sub stop_linux {
	$amp->stop;
	send_im($_[0], '<b>Stopped</b>', 0);
}

sub song_linux {
	my $song = $amp->get_playlist_title($amp->get_playlist_pos());
	if($song) {
		send_im($_[0], "Now playing:\n<b>$song</b>", 0);
	}
	else {
		send_im($_[0], '<b>Stopped</b>.', 0);
	}
}

sub vis_linux {
	send_im($_[0], 'Command <b>failed</b>.', 0);
}

sub list_win32 {
	my ($from, $msg) = @_;
	my @list;

	return if($amp->get_playlist_length < 1);
	if($msg) {
		for(my $i = 0; $i < $msg; $i++) {
			$list[$i] = $amp->getplaylisttitle($amp->getlistpos + $i - (($msg - 1) / 2));
		}
	}
	if(@list) {
		$list[$msg / 2] = '<b>' . $list[$msg / 2] . '</b>';
		send_im($_[0], join("\n", @list), 0);
	}
	else {
		for(my $i = 0; $i < 5; $i++) {
			$list[$i] = $amp->getplaylisttitle($amp->getplaylistpos + $i - 2);
		}
		$list[2] = '<b>' . $list[2] . '</b>';
		send_im($_[0], join("\n", @list), 0);
	}
}

sub next_win32 {
	$amp->next;
	song_win32(@_);
}

sub paus_win32 {
	$amp->pause;
	song_win32(@_) if $amp->isplaying;
}

sub play_win32 {
	$amp->play;
	song_win32(@_);
}

sub prev_win32 {
	$amp->prev;
	sleep(1.1);
	song_win32(@_);
}

sub rand_win32 {
	my $status;
	$status = $amp->shuffle_status;
	$amp->shuffle(a => 1);
	$amp->next;
	$amp->shuffle(a => 0) if(!$status);
	sleep(1.1);
	song_win32(@_);
}

sub rpt_win32 {
	my ($from, $msg) = @_;

	$msg =~ tr/A-Z/a-z/ if $msg;
	if($msg =~ /on/) {
		$amp->repeat(1);
		send_im($_[0], 'Repeat <b>on</b>.', 0);
	}
	elsif($msg =~ /off/) {
		$amp->repeat(0);
		send_im($_[0], 'Repeat <b>off</b>.', 0);
	}
	else {
		send_im($_[0], $commands{'.rpt'}[1], 0);
	}
}

sub shuf_win32 {
	my $msg = $_[1];

	$msg =~ tr/A-Z/a-z/ if $msg;
	if($msg =~ /on/) {
		$amp->shuffle(a => 1);
		send_im($_[0], 'Shuffle <b>on</b>.', 0);
	}
	elsif($msg =~ /off/) {
		$amp->shuffle(a => 0);
		send_im($_[0], 'Shuffle <b>off</b>.', 0);
	}
	else {
		send_im($_[0], $commands{'.shuf'}[1], 0);
	}
}

sub stop_win32 {
	$amp->fadeoutandstop;
	send_im($_[0], '<b>Stopped</b>.', 0);
}

sub song_win32 {
	my $song;

	if($amp->isplaying) {
		$song = $amp->getcurrenttitle;
		send_im($_[0], "Now playing:\n<b>$song</b>", 0)
	}
	else {
		send_im($_[0], '<b>Stopped</b>.', 0);
	}
}

sub vis_win32 {
	$amp->exec_visual();
	send_im($_[0], 'Visualization <b>toggled</b>.', 0);
}
1;