#!/usr/bin/perl -w
use strict;

# xmms plugin for [x]Chat - (C) 2004, Guido S. Nickels

# config options - feel free to edit

my $command    = "xmms";      # the command handler for xchat
my $cmd_ctrl   = "ctrl";      # control command
my $cmd_help   = "help";      # help command
my $cmd_info   = "info";      # track info command
my $info_say   = "/me is playing";  # output prefix for public track info
my $gethere    = "finger music\@intranet.nickels-it.de - ";

# some more globs - please leave them alone ;o)

my $myname = "[X]Chat::MMS";
my $version = "1.6";
my $author = "Guido S. Nickels";
my $desc = "control xmms from within xchat";

# don't touch anything here unless you REALLY know what you're doing

# help text

my $help =
"-------------------------------------------------------------
$myname $version - usage:

  /$command <command> [option {arg}]

Commands
--------

  $cmd_ctrl <play|pause|stop|prev|next|shf|rpt|vol {0-100}>
  
    Options:  play         -- play current playlist position
              pause        -- pause xmms
              stop         -- stop playing
              prev         -- jump to previous track
              next         -- jump to next track
              shf          -- toggle shuffle mode
              rpt          -- toggle repeat mode
              vol {0-100}  -- raise/lower main volume
	      
    Example:  /$command $cmd_ctrl vol 80
              set main volume level to 80%

  $cmd_info <short|long> {pub}
  
    Options:  short        -- short track info
              long         -- long track info
              (pub == announce in current query/channel)
	      
  $cmd_help

    print this help screen
-------------------------------------------------------------
";

# include Xmms::Remote

use Xmms::Remote ();
my $remote = Xmms::Remote->new();

# register plugin and command handler to irc client

IRC::register ($myname,$version,$author,$desc);
IRC::print("Loading $myname $version\n");
IRC::print("  (C) 2004 G. Nickels <g.nickels\@nickels-it.de>\n");
IRC::print("  :: type /$command $cmd_help for command overview ::\n");
IRC::add_command_handler($command, "cmd_xmms");

# command handler functions

# scan for commands

sub cmd_xmms {
  my $input = shift(@_);
  my @args = split (/\s+/,$input);
  my $cmd = shift(@args);
  unless (xmmsrun()) {
    return 1;
  }
  if ($cmd =~ /^$cmd_info$/i) {
    s_info(@args);
  }
  elsif ($cmd =~ /^$cmd_ctrl$/i) {
    s_ctrl(@args);
  }
  elsif ($cmd =~ /^$cmd_help$/i) {
    s_help(@args);
  }
  else {
    s_help();
  }
  return 1;
}

# track info

sub s_info {
  my $sub = shift @_;
  my @opt = @_;

  my $title = $remote->get_playlist_title;
  $title =~ s/_[^_]*_[^_]*_/ - /;
  while($title =~ /-  -/) {
    $title =~ s/-  -/-/;
  }

  my $time = $remote->get_playlist_timestr;
  $time = "[$time\m] ";
  if($time eq '[0:00m] ') {
    $time = '';
  }
  
  my($rate, $freq, $nch) = $remote->get_info;
  my $mode;
  $rate /= 1000;
  $freq /= 1000;
  if($nch>1) {
    $mode = "stereo";
  }
  else {
    $mode = "mono";
  }
  $rate = "$rate kbit/s, ";
  $freq = "$freq kHz/";
  $mode = "$mode ($nch channels) ";

  my $file = $remote->get_playlist_file;
  my $media = '';
  my $download = '';
  if($file =~ /^*\.mp3$/i) {
    $media = 'MPEG 1.0 (layer 3), ';
    $download = "$gethere";
  }
  elsif($file =~ /^*\.ogg$/i) {
    $media = 'OGG/Vorbis, ';
    $download = "$gethere";
  }
  elsif($file =~ /^*\.mod$/i) {
    $media = 'Amiga MOD, ';
    $rate = '';
    $freq = '';
    $download = '';
  }
  elsif($file =~ /^*\.wav$/i) {
    $media = 'Wave Audio, ';
    $rate = '';
    $download = "$gethere";
  }
  elsif($media =~ /^*\.sid$/i) {
    $media = 'C64 SID, ';
    $rate = '';
    $freq = '';
    $download = '';
  }
  elsif($media =~ /^*\.mid$/i) {
    $media = 'General MIDI, ';
    $rate = '';
    $download = "$gethere";
  }
  elsif($media =~ /^*\:*$/i) {
    $media = "Stream: $file, ";
    $download = '';
  }
  else {
    $media = '';
    $rate = '';
    $freq = '';
    $download = '';
  }

  if($sub =~ /^long$/i) {
    if ($opt[0] eq 'pub') {
      IRC::command("$info_say $title $time- $media$rate$freq$mode- $download$myname");
    }
    else {
      IRC::print("xmms: $title $time- $media$rate$freq$mode");
    }
    return 1;
  }
  elsif($sub =~ /^short$/i) {
    if ($opt[0] eq 'pub') {
      IRC::command("$info_say $title $time- $download$myname");
    }
    else {
      IRC::print("xmms: $title $time");
    }
  }
  else {
    IRC::print("xmms: ERROR - no such command/option (try /$command $cmd_help)");
  }
}

# control functions

sub s_ctrl {
  my $sub = shift @_;
  my @opt = @_;
  my $status;

  if($sub =~ /^play$/i) {
    $remote->play;
    IRC::print("xmms: playing");
  }
  elsif($sub =~ /^pause$/i) {
    $remote->pause;
    IRC::print("xmms: paused");
  }
  elsif($sub =~ /^stop$/i) {
    $remote->stop;
    IRC::print("xmms: stopped");
  }
  elsif($sub =~ /^next$/i) {
    $remote->playlist_next;
    IRC::print("xmms: jumped to next track");
  }
  elsif($sub =~ /^prev$/i) {
    $remote->playlist_prev;
    IRC::print("xmms: jumped to previous track");
  }
  elsif($sub =~ /^shf$/i) {
    $remote->toggle_shuffle;
    $status = $remote->is_shuffle;
    if ($status eq '0') {
      IRC::print("xmms: shuffle turned ON");
    }
    else {
      IRC::print("xmms: shuffle turned OFF");
    }
  }
  elsif($sub =~ /^rpt$/i) {
    $remote->toggle_repeat;
    $status = $remote->is_repeat;
    if ($status eq '0') {
      IRC::print("xmms: repeat turned ON");
    }
    else {
      IRC::print("xmms: repeat turned OFF");
    }
  }
  elsif($sub =~ /^vol$/i) {
    $remote->set_main_volume($opt[0]);
    sleep 1;
    $status = $remote->get_main_volume;
    IRC::print("xmms: volume level changed [$status%]");
  }
  else {
    IRC::print("xmms: ERROR - no such command/option (try /$command $cmd_help)");
  }
}

sub s_help {
  my @rows = split (/\n/, $help);
  foreach my $row (@rows) {
    IRC::print($row);
  }
}

sub xmmsrun {
  unless ($remote->is_running) {
    IRC::print("xmms: ERROR - xmms not running");
    return undef;
  }
  return 1;
}

