#
#   This is a lightweight alternative for iMPD, where most people only use
#   the "/mloud"-command.
#
# Bugs:
#   this is my first perl-script at all, so maybe there are some.
#
# License:
#   MIT/X Consortium License
#
#   © 2009, 2012 Moritz Wilhelmy <mw wzff de> (insert an @ and a .)
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the 
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#
# Changes:
#   2012-09-28  Update contact info
#   2009-??-??  Release of version 0.8
#
# Notes:
#   This script is currently unmaintained and has been superseded by the
#   following shell alias:
#   alias np='mpc --format '\''/me %artist% - %title% (%album%) (%time%)'\''|head -1|tr -d '\''\n'\''|xclip'
#

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

$VERSION = '0.8';
%IRSSI = (
  authors         => 'Moritz Wilhelmy',
  contact         => 'Query "ente" on Freenode',
  name            => 'mpdspam',
  description     => 'This script spams the current song played by mpd (music player daemon) into the current irssi window.',
  license         => 'MIT',
  url             => 'http://www.musicpd.org/'
);


sub mpdspam {
  my $args     = join(" ", $_[0]);

  my $format   = Irssi::settings_get_str 'mpdspam_format';
  my $host     = Irssi::settings_get_str 'mpdspam_host';
  my $port     = Irssi::settings_get_int 'mpdspam_port';
  my $mpc      = Irssi::settings_get_str 'mpdspam_mpc';

  my @mpcreply = split /\n/, `MPD_HOST='$host' MPD_PORT='$port' '$mpc' --format '$format'`;

  unless ($mpcreply[1] =~ /^\[playing\]/)
  {
    Irssi::print 'mpd is currently not playing'; 
    return;
  }

  Irssi::active_win->command($mpcreply[0] . " $args");

}

Irssi::command_bind mpdspam => \&mpdspam;

# Do not edit! Use /set mpdspam_* instead.
Irssi::settings_add_int 'misc', 'mpdspam_port', 6600;
Irssi::settings_add_str 'misc', 'mpdspam_host', 'localhost';
Irssi::settings_add_str 'misc', 'mpdspam_format', '/me is listening to %artist% - %title% (%album%) (%time%)';
Irssi::settings_add_str 'misc', 'mpdspam_mpc', '/usr/bin/mpc';

