All pastes #468108 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-02 22:00 UTC
rendered paste body
#!/usr/bin/perl
use DCOP::Amarok::Player;

Xchat::register('amaroK','0.0.1','Perl based X-Chat interface to amaroK');

# We only hook one /command, then run all our sub commands off that
# It's just cleaner this way
Xchat::hook_command('amarok',\&amarok_root,{help_text => 'amaroK command root, type /amarok help for more information'});

# We define our commands here
my %cmds = (
	'help' => {
		desc => 'Display help',
		hook => \&help
	},
	'np' => {
		desc => 'Performs the now playing action on the current window',
		hook => \&now_playing
	}
);

# The root sub, this is our router
sub amarok_root {
	my $cmd = lc($_[0][1]);

	if (exists($cmds{$cmd})) {
		use Data::Dumper;
		return &{$cmds{$cmd}{hook}}($_);
	} else {
		Xchat::print("Huh?");
	}
}

# Help target, list commands with their help message
sub help {
	Xchat::printf('%-8s - %s','Command','Help');
	foreach my $key (keys(%cmds)) {
		Xchat::printf('%-8s - %s',$key,$cmds{$key}{desc});
	}
}

# Now playing target - Well guess what this does
sub now_playing {
	my $player = DCOP::Amarok::Player->new();

	my %info = ();
	foreach my $var (qw(album artist title status track totalTime currentTime isPlaying sampleRate bitrate status)) {
		$info{$var} = $player->$var();
	}
	$info{sampleRate} = int ($info{sampleRate}/1000);
	if ($info{title} =~ /^(.+)\ \/\ (.+)$/) {
		$info{title} = $2;
		$info{artist} = $1;
	}
	
	my $command = 'action ';
	if ($info{status} >= 1) {
		if ($info{status} == 1) {
			$command .= "has paused while playing: \cC1,12 [%title%] by \cC1,8 [%artist%] ";
		} else {
			$command .= "\cB \cC1,12 Gentoo feat Amarok sound \cC1,8 : \cC1,12 [%title%] \cC1,13 by \cC1,12 [%artist%] \cC1,13";
		}
		if ($info{album}) {
			$command .= "from \cC1,8 [%album%] ";
		}
		$command .= '[%currentTime%/%totalTime%]-';
		$command .= '[%bitrate%kbps]-';
		$command .= '[%sampleRate%Khz]';
	} else {
		$command .= 'isn\'t playing anything...';
	}

	$command =~ s/%([^%]+)%/$info{$1}/gie;

	Xchat::command($command);
	Xchat::EAT_ALL;
}