1 # Copyright (C) 2008, 2010 Nick Bowler
3 # Irssi script for annoying your friends with MPD status information.
5 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 # This is free software: you are free to do what the fuck you want to.
7 # There is NO WARRANTY, to the extent permitted by law.
10 use vars qw($VERSION %IRSSI);
14 use IO::Socket::INET6;
18 settings_get_bool settings_add_bool
19 settings_get_int settings_add_int
20 settings_get_str settings_add_str
25 authors => 'Nick Bowler',
26 contact => 'nbowler@draconx.ca',
28 description => 'Irssi client for the Music Player Daemon.',
32 # MPD Connection Settings
33 $ENV{"MPD_HOST"} = "localhost" unless ($ENV{"MPD_HOST"});
34 $ENV{"MPD_PORT"} = 6600 unless ($ENV{"MPD_PORT"});
35 settings_add_str("mpd", "mpd_host", $ENV{"MPD_HOST"});
36 settings_add_int("mpd", "mpd_port", $ENV{"MPD_PORT"});
38 # Open a connection to MPD.
40 my $sock = new IO::Socket::INET6(
41 PeerAddr => settings_get_str("mpd_host"),
42 PeerPort => settings_get_int("mpd_port"),
45 print CLIENTERROR "failed to open MPD socket: $!.";
48 binmode($sock, ":utf8");
50 # Grab the MPD version.
51 if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
52 print CLIENTERROR "failed MPD handshake: $!.";
64 print $sock "status\n";
67 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
69 if (/^(\w+): (.*)$/) {
73 return %data if (!defined $data{"songid"});
76 print $sock "playlistid $data{songid}\n";
79 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
81 if (/^(\w+): (.*)$/) {
91 print $sock "close\n";
98 return "vorbis" if (/\.ogg$/);
99 return "flac" if (/\.flac$/);
100 return "mp3" if (/\.mp3$/);
104 my ($data, $server, $witem) = @_;
105 my ($window, $local) = (Irssi::active_win());
107 my $sock = mpd_open() or return;
108 my %info = mpd_currentsong($sock) or return;
111 if ($info{'state'} eq "stop") {
112 $window->print("MPD is currently stopped.");
116 my $text = "$info{Artist} – $info{Title}, "
117 . "track $info{Track} of “$info{Album}” "
118 . "($info{bitrate}kbps " . fileformat($info{'file'}) . ")";
120 # If the active window is not a channel or query, display locally.
121 $local = 1 unless ($witem && ($witem->{type} eq "CHANNEL"
122 || $witem->{type} eq "QUERY"));
125 $window->print($text);
128 $witem->command("SAY $data: /me is listening to $text.");
130 $witem->command("SAY /me is listening to $text.");
134 command_bind('mpd', 'cmd_current');