html/mpd.pl
1 # MPD Now-Playing Script for irssi
2 # Copyright (C) 2005 Erik Scharwaechter
3 # <diozaka@gmx.de>
4 #
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.
8 #
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.
13 #
14 # The full version of the license can be found at
15 # http://www.gnu.org/copyleft/gpl.html.
16 #
17 #
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 #######################################################################
24 # TODO: #
25 # - add more format directives #
26 #######################################################################
27 # CHANGELOG: #
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 #######################################################################
33
34 use strict;
35 use IO::Socket;
36 use Irssi;
37
38 use vars qw{$VERSION %IRSSI %MPD};
39
40 $VERSION = "0.5";
41 %IRSSI = (
42 name => 'mpd',
43 authors => 'Erik Scharwaechter',
44 contact => 'diozaka@gmx.de',
45 license => 'GPLv2',
46 description => 'print the song you are listening to',
47 );
48
49 sub my_status_print {
50 my($msg,$witem) = @_;
51
52 if ($witem) {
53 $witem->print($msg);
54 } else {
55 Irssi::print($msg);
56 }
57 }
58
59 sub np {
60 my($data,$server,$witem) = @_;
61
62 if ($data =~ /^help/) {
63 help();
64 return;
65 }
66
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');
72
73 my $socket = IO::Socket::INET->new(
74 Proto => 'tcp',
75 PeerPort => $MPD{'port'},
76 PeerAddr => $MPD{'host'},
77 timeout => $MPD{'timeout'}
78 );
79
80 if (not $socket) {
81 my_status_print('No MPD listening at '.$MPD{'host'}.':'.$MPD{'port'}.'.', $witem);
82 return;
83 }
84
85 $MPD{'status'} = "";
86 $MPD{'artist'} = "";
87 $MPD{'title'} = "";
88 $MPD{'filename'} = "";
89
90 my $ans = "";
91 my $str = "";
92
93 print $socket "status\n";
94 while (not $ans =~ /^(OK$|ACK)/) {
95 $ans = <$socket>;
96 if ($ans =~ /state: (.+)$/) {
97 $MPD{'status'} = $1;
98 }
99 }
100
101 if ($MPD{'status'} eq "stop") {
102 my_status_print("No song playing in MPD.", $witem);
103 close $socket;
104 return;
105 }
106
107 print $socket "currentsong\n";
108 $ans = "";
109 while (not $ans =~ /^(OK$|ACK)/) {
110 $ans = <$socket>;
111 if ($ans =~ /file: (.+)$/) {
112 my $filename = $1;
113 $filename =~ s/.*\///;
114 $MPD{'filename'} = $filename;
115 } elsif ($ans =~ /Artist: (.+)$/) {
116 $MPD{'artist'} = $1;
117 } elsif ($ans =~ /Title: (.+)$/) {
118 $MPD{'title'} = $1;
119 }
120 }
121
122 close $socket;
123
124 if ($MPD{'artist'} eq "" and $MPD{'title'} eq "") {
125 $str = $MPD{'alt_text'};
126 } else {
127 $str = $MPD{'format'};
128 }
129
130 $str =~ s/\%ARTIST/$MPD{'artist'}/g;
131 $str =~ s/\%TITLE/$MPD{'title'}/g;
132 $str =~ s/\%FILENAME/$MPD{'filename'}/g;
133
134 if ($witem && ($witem->{type} eq "CHANNEL" ||
135 $witem->{type} eq "QUERY")) {
136 if($MPD{'format'} =~ /^\/me /) {
137 $witem->command($str);
138 } else {
139 $witem->command("MSG ".$witem->{name}." $str");
140 }
141 } else {
142 Irssi::print("You're not in a channel.");
143 }
144 }
145
146
147 sub help {
148 print '
149 MPD Now-Playing Script
150 ========================
151
152 by Erik Scharwaechter (diozaka@gmx.de)
153
154 VARIABLES
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)
160
161 USAGE
162 /np Print the song you are listening to
163 /np help Print this text
164 ';
165 }
166
167
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');
173
174 Irssi::command_bind np => \&np;
175 Irssi::command_bind 'np help' => \&help;
176