-
Notifications
You must be signed in to change notification settings - Fork 235
/
mpd.pl
225 lines (191 loc) · 7.13 KB
/
mpd.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# MPD Now-Playing Script for irssi
# Copyright (C) 2005 Erik Scharwaechter
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# The full version of the license can be found at
# http://www.gnu.org/copyleft/gpl.html.
#
#
#######################################################################
# I'd like to thank Bumby <[email protected]> for his impc script, #
# which helped me a lot with making this script. #
#######################################################################
# Type "/np help" for a help page! #
#######################################################################
# TODO: #
#######################################################################
# CHANGELOG: #
# 0.4: First official release #
# 0.5: Info message if no song is playing #
# Display alternative text if artist and title are not set #
# Some minor changes #
# 0.6: Added some more format directives(time, album) #
# Added support for password authentication #
# 0.7: Added format directives for bitrate and volume #
# Fixed socket not timing out at specified interval #
# 0.8: unescaped left brace in regex becomes an error in perl5 v26 #
#######################################################################
use strict;
use IO::Socket;
use Irssi;
use vars qw{$VERSION %IRSSI %MPD};
$VERSION = "0.8";
%IRSSI = (
name => 'mpd',
authors => 'Erik Scharwaechter, Tobias Böhm, Mikkel Kroman',
license => 'GPLv2',
description => 'print the song you are listening to',
);
sub my_status_print {
my($msg,$witem) = @_;
if ($witem) {
$witem->print($msg);
} else {
Irssi::print($msg);
}
}
sub np {
my($data,$server,$witem) = @_;
if ($data =~ /^help/) {
help();
return;
}
$MPD{'port'} = Irssi::settings_get_str('mpd_port');
$MPD{'host'} = Irssi::settings_get_str('mpd_host');
$MPD{'password'} = Irssi::settings_get_str('mpd_password');
$MPD{'timeout'} = Irssi::settings_get_str('mpd_timeout');
$MPD{'format'} = Irssi::settings_get_str('mpd_format');
$MPD{'alt_text'} = Irssi::settings_get_str('mpd_alt_text');
my $socket = IO::Socket::INET->new(
Proto => 'tcp',
PeerPort => $MPD{'port'},
PeerAddr => $MPD{'host'},
Timeout => $MPD{'timeout'}
);
if (not $socket) {
my_status_print('No MPD listening at '.$MPD{'host'}.':'.$MPD{'port'}.'.', $witem);
return;
}
my $ans = "";
if ($MPD{'password'} =~ /^.+$/) {
print $socket 'password ' . $MPD{'password'} . "\n";
while (not $ans =~ /^(OK$|ACK)/) {
$ans = <$socket>;
}
if ($ans =~ /^ACK \[...\] \{.*?\} (.*)$/){
my_status_print('Auth Error: '.$1, $witem);
close $socket;
return;
}
}
$MPD{'status'} = "";
$MPD{'artist'} = "";
$MPD{'album'} = "";
$MPD{'title'} = "";
$MPD{'filename'} = "";
$MPD{'elapsed'} = "";
$MPD{'total'} = "";
$MPD{'volume'} = "";
$MPD{'bitrate'} = "";
my $ans = "";
my $str = "";
print $socket "status\n";
while (not $ans =~ /^(OK$|ACK)/) {
$ans = <$socket>;
if ($ans =~ /^ACK \[...\] \{.*?\} (.*)$/){
my_status_print($1, $witem);
close $socket;
return;
} elsif ($ans =~ /^state: (.+)$/) {
$MPD{'status'} = $1;
} elsif ($ans =~ /^time: (\d+):(\d+)$/) {
$MPD{'elapsed'} = sprintf("%01d:%02d", $1/60,$1%60);
$MPD{'total'} = sprintf("%01d:%02d", $2/60,$2%60);
} elsif ($ans =~ /^volume: (\d+)$/) {
$MPD{'volume'} = $1
} elsif ($ans =~ /^bitrate: (\d+)$/) {
$MPD{'bitrate'} = $1
}
}
if ($MPD{'status'} eq "stop") {
my_status_print("No song playing in MPD.", $witem);
close $socket;
return;
}
print $socket "currentsong\n";
$ans = "";
while (not $ans =~ /^(OK$|ACK)/) {
$ans = <$socket>;
if ($ans =~ /file: (.+)$/) {
my $filename = $1;
$filename =~ s/.*\///;
$MPD{'filename'} = $filename;
} elsif ($ans =~ /^Artist: (.+)$/) {
$MPD{'artist'} = $1;
} elsif ($ans =~ /^Album: (.+)$/) {
$MPD{'album'} = $1;
} elsif ($ans =~ /^Title: (.+)$/) {
$MPD{'title'} = $1;
}
}
close $socket;
if ($MPD{'artist'} eq "" and $MPD{'title'} eq "") {
$str = $MPD{'alt_text'};
} else {
$str = $MPD{'format'};
}
$str =~ s/\%ARTIST/$MPD{'artist'}/g;
$str =~ s/\%ALBUM/$MPD{'album'}/g;
$str =~ s/\%TITLE/$MPD{'title'}/g;
$str =~ s/\%FILENAME/$MPD{'filename'}/g;
$str =~ s/\%ELAPSED/$MPD{'elapsed'}/g;
$str =~ s/\%TOTAL/$MPD{'total'}/g;
$str =~ s/\%BITRATE/$MPD{'bitrate'}/g;
$str =~ s/\%VOLUME/$MPD{'volume'}/g;
if ($witem && ($witem->{type} eq "CHANNEL" ||
$witem->{type} eq "QUERY")) {
if($MPD{'format'} =~ /^\/me /) {
$witem->command($str);
} else {
$witem->command("MSG ".$witem->{name}." $str");
}
} else {
Irssi::print("You're not in a channel.");
}
}
sub help {
print '
MPD Now-Playing Script
========================
by Erik Scharwaechter ([email protected])
extended by Tobias Böhm ([email protected])
VARIABLES
mpd_host The host that runs MPD (localhost)
mpd_port The port MPD is bound to (6600)
mpd_password A password for a profile with at least read permissions - optional ()
mpd_timeout Connection timeout in seconds (5)
mpd_format The text to display (np: %%ARTIST - %%TITLE)
mpd_alt_text The Text to display, if %%ARTIST and %%TITLE are empty (np: %%FILENAME)
USAGE
/np Print the song you are listening to
/np help Print this text
';
}
Irssi::settings_add_str('mpd', 'mpd_host', 'localhost');
Irssi::settings_add_str('mpd', 'mpd_port', '6600');
Irssi::settings_add_str('mpd', 'mpd_password', '');
Irssi::settings_add_str('mpd', 'mpd_timeout', '5');
Irssi::settings_add_str('mpd', 'mpd_format', 'np: %ARTIST - %TITLE');
Irssi::settings_add_str('mpd', 'mpd_alt_text', 'np: %FILENAME');
Irssi::command_bind np => \&np;
Irssi::command_bind 'np help' => \&help;