#!/usr/bin/perl -w

# Created by Roman Tworkowski
# JID: <roman at jabber dot wroc dot pl> E-MAIL: <romano92 at gmail dot com>
# Plugin shows song MOC currently is playing in XChat.
# Usage:
# /moc
# Personalize this plugin by editing source code.
# I release this under terms of BSD license, http://www.opensource.org/licenses/bsd-license.php
#
# A little changelog
# 0.2
#	* Done cleanup. Recognizes that MOC is paused or not working.
# 0.1:
# 	* First working version, can show currently played song with artist, song title, current and total time and bitrate.

use strict;

Xchat::register("MOC in XChat", "0.2", "Shows song MOC currently is playing.");
Xchat::hook_command("moc", "show_song");

sub show_song
{
	if(`ps -e` =~ /mocp/) # Using command mocp -i => server starts working
	{
		my $moc_info = `mocp -i`;
		if ($moc_info =~ /State: PAUSE/)
		{
			Xchat::print("MOC is paused.");
		}
		else
		{
			chomp(my ($artist, $song, $current_time, $total_time, $bitrate) = ($moc_info =~ /Artist: (.+)/, $moc_info =~ /SongTitle: (.+)/, $moc_info =~ /CurrentTime: (.+)/, $moc_info =~ /TotalTime: (.+)/, $moc_info =~ /Bitrate: (.+)/));
			Xchat::command("me słucha $artist - $song [$current_time / $total_time] - $bitrate");
		}
	}
	else
	{
		Xchat::print("MOC isn't working.");
	}
}
