-
Notifications
You must be signed in to change notification settings - Fork 234
/
wa.pl
283 lines (255 loc) · 10 KB
/
wa.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "2.3.1";
%IRSSI = (
authors => "Matti 'qvr' Hiljanen, Piotr 'Pieta' Szymanski",
contact => "matti\@hiljanen.com, pieta\@osiedle.net.pl",
contributors => "bd\@bc-bd.org",
name => "wa",
description => "shows what WinAmp is playing with /wa command",
license => "Public Domain",
commands => "wa",
url => "http://matin.maapallo.org/softa/irssi",
);
#
# Requires httpQ >= 1.8 (http://www.kostaa.com/)
# Requires also WinAmp2 plugin manager if you want to use it with WinAmp3
# (Component ID: 118230 @ winamp.com)
#
# /SET httpq_ to configure the script
# /SET httpq_command sets the command which is executed on /wa
# the following expandos can be used with it:
# %T (time)
# %F (file name)
# %P (percent)
# %S (song name)
# %L (lower cased song name)
# httpq_command -setting was originally contributed by bd@bc-bd.org
#
# /SET httpq_trigger_ to configure the (very lame) public trigger
# /SET httpq_trigger_command has all the same expandos as httpq_command
# with some extra ones:
# %N (nick that triggered the script)
# %C (channel where the script was triggered)
#
# Known BUGS:
# WinAmp 2 plugin manager doesn't get the name of the song that's first in
# playlist, nothing that I can do about it.. But other than that, it should work
# just fine.
#
# Piotr Szymanski:
#
# When using httpq_v3 you'd always get error (httpq returns 0 after sending a
# '/getoutputtime', '/getplaylistpos', '/getplaylisttitle' and '/ getplaylistfile'
# because of wrong args used in previous version). I just added proper args for
# version 3 of httpq. Now it should work just fine.
#
use Socket;
my($waport, $wahost, $wapass, $di);
sub getstat($$$) {
my ($host, $port, $data) = @_;
$data = "$data HTTP/1.0\r\n\r\n";
if (socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'))) {
if (connect(SOCK, sockaddr_in($port, inet_aton($host)))) {
unless (send(SOCK, $data, 0)) {
Irssi::print("Unable to write to the socket: $!", MSGLEVEL_CLIENTERROR);
return;
}
} else {
Irssi::print("Unable to connect to the socket: $!", MSGLEVEL_CLIENTERROR);
return;
}
} else {
Irssi::print("Connection to $host failed: $!", MSGLEVEL_CLIENTERROR);
return;
}
# uncomment this for slow networks
# sleep(1);
my @lines = <SOCK>;
close(SOCK);
my $result = join("", @lines);
$result =~ s/^HTTP.*\n//g;
$result =~ s/^Server.*\n//g;
$result =~ s/^Content.*\n//g;
$result =~ s/^\r//g;
$result =~ s/[\r,\n]//g;
return $result;
}
sub check_version {
$wapass = Irssi::settings_get_str('httpq_password');
$wahost = Irssi::settings_get_str('httpq_host');
$waport = Irssi::settings_get_int('httpq_port');
my (undef,$version) = split(/x/, getstat($wahost, $waport, "GET /getversion?p=$wapass"));
if ($version >= 3000) {
return 1000000;
} else {
return 1;
}
}
sub parse_times ($$) {
my $emulatorbug = check_version();
my ($position, $length) = @_;
$position = sprintf("%02d", int(($position/1000/60))).":"
.sprintf("%02d", ($position/1000%60));
if ($length eq "-1") {
if ($di) {
$length = "/di.fm";
} else {
$length = "/netradio";
}
} elsif ($length eq "0") {
$length = "/sid";
} else {
$length = "/".sprintf("%02d", int(($length/$emulatorbug/60))).":"
.sprintf("%02d", ($length/$emulatorbug%60));
}
my $result = "$position$length";
return $result;
}
sub mkpercent ($$) {
my $emulatorbug = check_version();
my ($position, $length) = @_;
my $result;
my $numbers;
if ($length eq "-1" || $length eq "0") {
$result = "0";
} else {
$numbers = sprintf("%.0f",((($position/1000)/($length/$emulatorbug))*100));
$result = "(${numbers}%)";
}
if ($numbers>100 || $numbers<0) {
$result = "(wtf?)";
}
return $result;
}
sub vol_control($) {
$wapass = Irssi::settings_get_str('httpq_password');
$wahost = Irssi::settings_get_str('httpq_host');
$waport = Irssi::settings_get_int('httpq_port');
my ($direction) = @_;
if (!$direction) {
Irssi::print("Usage: /vol <up|down>");
return;
}
if ($direction eq "up") {
Irssi::print("Vol up..") if getstat($wahost, $waport, "GET /volumeup?p=$wapass");
} elsif ($direction eq "down") {
Irssi::print("Vol down..") if getstat($wahost, $waport, "GET /volumedown?p=$wapass");
}
}
# this function from bd@bc-bd.org
sub expand {
my ($string, %format) = @_;
my ($exp, $repl);
$string =~ s/%$exp/$repl/g while (($exp, $repl) = each(%format));
return $string;
}
sub main_wa {
$wapass = Irssi::settings_get_str('httpq_password');
$wahost = Irssi::settings_get_str('httpq_host');
$waport = Irssi::settings_get_int('httpq_port');
my $status = getstat($wahost, $waport, "GET /isplaying?p=$wapass");
if ($status eq "1") {
my $position = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=0"); #changed arg 'a' to 'frmt'/Pieta
my $length = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=1"); #changed arg 'a' to 'frmt'/Pieta
my $emulatorbug = check_version();
my $song = getstat($wahost, $waport, "GET /getplaylisttitle?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
.getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
my $file = getstat($wahost, $waport, "GET /getplaylistfile?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
.getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
if ($emulatorbug > 1 && getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass") == 0) {
$song = "WA2 plugin emulator error";
$file = "WA2 plugin emulator error";
}
# di.fm
if ($song =~ /\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)/i) {
$song =~ s/\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)//i;
$di = 1;
} else {
$di = 0;
}
my $lc = lc($song);
my $time = parse_times($position, $length);
my $percent = mkpercent($position, $length);
if (!$percent) {
$percent = '';
}
#my $final = "np: '$song' [$time$percent]";
my $final = expand(Irssi::settings_get_str("httpq_command"),
"S", $song,
"T", $time,
"P", $percent,
"F", $file,
"L", $lc);
Irssi::active_win()->command("$final");
} elsif ($status eq "3") {
Irssi::print("WinAmp is paused.");
} else {
Irssi::print("WinAmp isn't playing any song.");
}
}
sub trigger{
my($server,$text,$nick,$hostmask,$channel)=@_;
return unless Irssi::settings_get_bool('httpq_trigger_enabled');
my $trigger = Irssi::settings_get_str('httpq_trigger_string');
$trigger = "!lame" if !$trigger;
return unless ($text =~ /^$trigger$/);
# now just a copy and paste of most of the main_wa function
# this sucks, if you figure out a better way PLEASE send me a patch :)
$wapass = Irssi::settings_get_str('httpq_password');
$wahost = Irssi::settings_get_str('httpq_host');
$waport = Irssi::settings_get_int('httpq_port');
my $status = getstat($wahost, $waport, "GET /isplaying?p=$wapass");
if ($status eq "1") {
my $position = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=0"); #changed arg 'a' to 'frmt'/Pieta
my $length = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=1"); #changed arg 'a' to 'frmt'/Pieta
my $emulatorbug = check_version();
my $song = getstat($wahost, $waport, "GET /getplaylisttitle?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
.getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
my $file = getstat($wahost, $waport, "GET /getplaylistfile?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
.getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
if ($emulatorbug > 1 && getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass") == 0) {
$song = "WA2 plugin emulator error";
$file = "WA2 plugin emulator error";
}
# di.fm
if ($song =~ /\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)/i) {
$song =~ s/\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)//i;
$di = 1;
} else {
$di = 0;
}
my $lc = lc($song);
my $time = parse_times($position, $length);
my $percent = mkpercent($position, $length);
if (!$percent) {
$percent = '';
}
my $final = expand(Irssi::settings_get_str("httpq_trigger_command"),
"S", $song,
"T", $time,
"P", $percent,
"N", $nick,
"F", $file,
"C", $channel,
"L", $lc);
$server->command("$final");
}
}
#
# the statusbar item code used to be here, but i doubt anyone ever used it so
# i removed it completely. it was buggy and, imo, useless :)
#
Irssi::command_bind('wa', 'main_wa');
Irssi::command_bind('vol', 'vol_control');
Irssi::settings_add_str('misc', 'httpq_password', "password");
Irssi::settings_add_str('misc', 'httpq_host', "hostname");
Irssi::settings_add_int('misc', 'httpq_port', "4800");
Irssi::settings_add_str('misc', 'httpq_command', "say np: '%S' [%T %P]");
#trigger settings
Irssi::settings_add_bool('misc', 'httpq_trigger_enabled', 0);
Irssi::settings_add_str('misc', 'httpq_trigger_string', "!lame");
Irssi::settings_add_str('misc', 'httpq_trigger_command', "notice %N np: '%S' [%T %P]");
Irssi::signal_add_last("message public","trigger");
#EOF