1 # Copyright © 2008, 2010 Nick Bowler
3 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 # This is free software: you are free to do what the fuck you want to.
5 # There is NO WARRANTY, to the extent permitted by law.
7 # MPDSplat helps you annoy your friends by posting your MPD status. By
8 # posting to public channels, you can piss off a wider audience.
11 use vars qw($VERSION %IRSSI);
15 use IO::Socket::INET6;
19 authors => 'Nick Bowler',
20 contact => 'nbowler@draconx.ca',
22 description => 'Annoy your friends with MPD status information.',
26 # MPD Connection Settings
27 $ENV{"MPD_HOST"} = "localhost" unless ($ENV{"MPD_HOST"});
28 $ENV{"MPD_PORT"} = 6600 unless ($ENV{"MPD_PORT"});
29 Irssi::settings_add_str("mpd", "mpd_host", $ENV{"MPD_HOST"});
30 Irssi::settings_add_int("mpd", "mpd_port", $ENV{"MPD_PORT"});
32 # Open a connection to MPD.
34 my $sock = new IO::Socket::INET6(
35 PeerAddr => Irssi::settings_get_str("mpd_host"),
36 PeerPort => Irssi::settings_get_int("mpd_port"),
39 print CLIENTERROR "failed to open MPD socket: $!.";
42 binmode($sock, ":utf8");
44 # Grab the MPD version.
45 if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
46 print CLIENTERROR "failed MPD handshake: $!.";
58 print $sock "status\n";
61 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
63 if (/^(\w+): (.*)$/) {
67 return %data if (!defined $data{"songid"});
70 print $sock "playlistid $data{songid}\n";
73 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
75 if (/^(\w+): (.*)$/) {
85 print $sock "close\n";
92 return "vorbis" if (/\.ogg$/);
93 return "flac" if (/\.flac$/);
94 return "mp3" if (/\.mp3$/);
98 my ($data, $server, $witem) = @_;
99 my ($window, $local) = (Irssi::active_win());
101 my $sock = mpd_open() or return;
102 my %info = mpd_currentsong($sock) or return;
105 if ($info{'state'} eq "stop") {
106 $window->print("MPD is currently stopped.");
110 my $text = "$info{Artist} – $info{Title}, "
111 . "track $info{Track}"
112 . ($info{Disc} > 1 ? ", disc $info{Disc} " : "")
113 . " of “$info{Album}” "
114 . "($info{bitrate}kbps " . fileformat($info{'file'}) . ")";
116 # If the active window is not a channel or query, display locally.
117 $local = 1 unless ($witem && ($witem->{type} eq "CHANNEL"
118 || $witem->{type} eq "QUERY"));
121 $window->print($text);
124 $witem->command("SAY $data: /me is listening to $text.");
126 $witem->command("SAY /me is listening to $text.");
129 Irssi::command_bind('mpd', 'cmd_current');