IO::Socket::INET を使用して MPD デーモンから現在のトラックに関する情報を取得しようとしていますが、要求が何であれ、常にOK MPD versionを取得します。
#!/usr/bin/perl -w
use strict;
use feature 'switch';
use YAML qw(LoadFile Dump);
use IO::Socket::INET;
$| = 1;
my @params = ('mpd_host', 'mpd_port', 'mpd_password');
sub get_config_file
{
my @configpaths = (
"config.yml",
# "$ENV{'HOME'}/.config/mpdtweet/config.yml",
);
for (@configpaths) {
return $_ if -e $_;
}
}
sub load_conf
{
my $config = YAML::LoadFile($_[0])
or return 1;
my %configuration = %$config;
return %configuration;
}
sub check_conf
{
my @missingparams;
for ($_[0]) {
push(@missingparams, $_) unless $_[0]{$_};
}
return 0 if $#missingparams eq 0 or return @missingparams;
}
sub first_run
{
sub sure
{
print "Would you like to start the first run wizard? [y/n] ";
chomp (my $choice = <STDIN>);
given ($choice) {
when (/n/) {
exit 0;
}
when (/y/) {
print "I need some informations about how to connect to MPD.\n";
&make_sample_config(&request_mpd_informations);
print "Configuration finished!\n";
}
default {
print "$choice is not a valid answer.\n"; &sure;
}
}
}
&sure;
}
sub request_mpd_informations
{
sub request_mpd_host
{
print "Host [localhost]: ";
chomp (my $host = <STDIN>);
return "localhost" if !$host;
}
sub request_mpd_port
{
print "Port [6600]: ";
chomp (my $port = <STDIN>);
return 6600 if !$port;
}
sub request_mpd_password
{
print "Password [null]: ";
chomp(my $password = <STDIN>);
return "" if !$password;
}
return (&request_mpd_host, &request_mpd_port, &request_mpd_password);
}
sub make_sample_config
{
my $first_config = {
'mpd_host' => $_[0],
'mpd_port' => $_[1],
'mpd_password' => $_[2],
patterns => [
'#nowplaying %a - %t (%d)',
'#nowplaying %a - %t',
],
patterns_comment => [
'#nowplaying %a - %t (%d) « %c',
'#nowplaying %a - %t « %c',
],
};
open my $first_configfile, '>', 'config.yml'
or die "Can't make config file: $!";
print $first_configfile Dump($first_config);
close $first_configfile;
}
sub check_mpd_connection_handshake
{
my $data = <$_[0]>;
return 1 unless $data =~ m/OK MPD/
or return 0;
}
sub current_song_info
{ # tracktitle, artistname, albumname || 1
my $mpd_socket = shift;
my %response;
print $mpd_socket "currentsong";
while (<$mpd_socket>) {
chomp;
my ($key, $value) = split(': ', $_, 2);
$response{$key} = $value;
}
return %response;
}
my $configfilepath = &get_config_file;
open my $configfile, '<', $configfilepath
or print "I haven't found any configuration file, \
maybe this is the first time you run this software.\n"
and &first_run and exit 0;
my %config = &load_conf($configfile);
die "Malformed configuration file ($configfilepath).\n"
if &check_conf(\%config) eq 1;
close $configfile;
my $mpd_socket = new IO::Socket::INET (
PeerHost => $config{'mpd_host'},
PeerPort => $config{'mpd_port'},
Proto => 'tcp',
) or die "Error connecting to MPD socket: $!\n";
die "Error connecting to MPD daemon: maybe something else is running on $config{'mpd_port'} port"
if check_mpd_connection_handshake($mpd_socket) eq 1;
my %tags = ¤t_song_info($mpd_socket);
print $tags{artist};
$mpd_socket->close();
私は何を間違っていますか?
前もって感謝します。