# Managing Music On Console from Irssi

use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
%IRSSI = (
	authors     => 'Tomasz Maciejewski',
	contact     => 'ponton616 at gmail',
	name        => 'moc',
	description => 'Managing Music On Console from Irssi',
	license     => 'GPL',
);

sub moc {

	my ($param, $server, $witem) = @_;
	my $mocpinfo = '';
	my $state = '';
	my $msg = '';

	if ($server || $server->{connected}) {

		$mocpinfo = `mocp -i`;

		#$mocpinfo =~ /.*^State: (.*)$.*/; 
		#$state = $1;
		$state = 'sd';

		if ($state eq '' || $state eq 'STOP') {
			$msg = 'MOC is stopped';
		}
		else {
			$mocpinfo =~ /.*Title: (.*).*/;
			$msg = $1;
		}


		if ($witem && ($witem->{type} eq "CHANNEL")) {
			$witem->command('SAY ' . $msg );
                }
	}

}

Irssi::command_bind ('moc', 'moc');
