1 # MPD Now-Playing Script for irssi
2 # Copyright (C) 2005 Erik Scharwaechter
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License version 2
7 # as published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # The full version of the license can be found at
15 # http://www.gnu.org/copyleft/gpl.html.
18 #######################################################################
19 # I'd like to thank Bumby <bumby@evilninja.org> for his impc script, #
20 # which helped me a lot with making this script. #
21 #######################################################################
22 # Type "/np help" for a help page! #
23 #######################################################################
25 # - add more format directives #
26 #######################################################################
28 # 0.4: First official release #
29 # 0.5: Info message if no song is playing #
30 # Display alternative text if artist and title are not set #
31 # Some minor changes #
32 #######################################################################
38 use vars
qw{$VERSION %IRSSI %MPD};
43 authors
=> 'Erik Scharwaechter',
44 contact
=> 'diozaka@gmx.de',
46 description
=> 'print the song you are listening to',
60 my($data,$server,$witem) = @_;
62 if ($data =~ /^help/) {
67 $MPD{'port'} = Irssi
::settings_get_str
('mpd_port');
68 $MPD{'host'} = Irssi
::settings_get_str
('mpd_host');
69 $MPD{'timeout'} = Irssi
::settings_get_str
('mpd_timeout');
70 $MPD{'format'} = Irssi
::settings_get_str
('mpd_format');
71 $MPD{'alt_text'} = Irssi
::settings_get_str
('mpd_alt_text');
73 my $socket = IO
::Socket
::INET
->new(
75 PeerPort
=> $MPD{'port'},
76 PeerAddr
=> $MPD{'host'},
77 timeout
=> $MPD{'timeout'}
81 my_status_print
('No MPD listening at '.$MPD{'host'}.':'.$MPD{'port'}.'.', $witem);
88 $MPD{'filename'} = "";
93 print $socket "status\n";
94 while (not $ans =~ /^(OK$|ACK)/) {
96 if ($ans =~ /state: (.+)$/) {
101 if ($MPD{'status'} eq "stop") {
102 my_status_print
("No song playing in MPD.", $witem);
107 print $socket "currentsong\n";
109 while (not $ans =~ /^(OK$|ACK)/) {
111 if ($ans =~ /file: (.+)$/) {
113 $filename =~ s/.*\///;
114 $MPD{'filename'} = $filename;
115 } elsif ($ans =~ /Artist: (.+)$/) {
117 } elsif ($ans =~ /Title: (.+)$/) {
124 if ($MPD{'artist'} eq "" and $MPD{'title'} eq "") {
125 $str = $MPD{'alt_text'};
127 $str = $MPD{'format'};
130 $str =~ s/\%ARTIST/$MPD{'artist'}/g;
131 $str =~ s/\%TITLE/$MPD{'title'}/g;
132 $str =~ s/\%FILENAME/$MPD{'filename'}/g;
134 if ($witem && ($witem->{type
} eq "CHANNEL" ||
135 $witem->{type
} eq "QUERY")) {
136 if($MPD{'format'} =~ /^\/me
/) {
137 $witem->command($str);
139 $witem->command("MSG ".$witem->{name
}." $str");
142 Irssi
::print("You're not in a channel.");
149 MPD Now-Playing Script
150 ========================
152 by Erik Scharwaechter (diozaka@gmx.de)
155 mpd_host The host that runs MPD (localhost)
156 mpd_port The port MPD is bound to (6600)
157 mpd_timeout Connection timeout in seconds (5)
158 mpd_format The text to display (np: %%ARTIST - %%TITLE)
159 mpd_alt_text The Text to display, if %%ARTIST and %%TITLE are empty (np: %%FILENAME)
162 /np Print the song you are listening to
163 /np help Print this text
168 Irssi
::settings_add_str
('mpd', 'mpd_host', 'localhost');
169 Irssi
::settings_add_str
('mpd', 'mpd_port', '6600');
170 Irssi
::settings_add_str
('mpd', 'mpd_timeout', '5');
171 Irssi
::settings_add_str
('mpd', 'mpd_format', 'np: %ARTIST - %TITLE');
172 Irssi
::settings_add_str
('mpd', 'mpd_alt_text', 'np: %FILENAME');
174 Irssi
::command_bind np
=> \
&np
;
175 Irssi
::command_bind
'np help' => \
&help
;