/var/www/www.irssi.org-old/scripts/html/wa.pl
1 $VERSION = "2.3.1";
2 %IRSSI = (
3 authors => "Matti 'qvr' Hiljanen, Piotr 'Pieta' Szymanski",
4 contact => "matti\@hiljanen.com, pieta\@osiedle.net.pl",
5 contributors => "bd\@bc-bd.org",
6 name => "wa",
7 description => "shows what WinAmp is playing with /wa command",
8 license => "Public Domain",
9 commands => "wa",
10 url => "http://matin.maapallo.org/softa/irssi",
11 );
12 #
13 # Requires httpQ >= 1.8 (http://www.kostaa.com/)
14 # Requires also WinAmp2 plugin manager if you want to use it with WinAmp3
15 # (Component ID: 118230 @ winamp.com)
16 #
17 # /SET httpq_ to configure the script
18 # /SET httpq_command sets the command which is executed on /wa
19 # the following expandos can be used with it:
20 # %T (time)
21 # %F (file name)
22 # %P (percent)
23 # %S (song name)
24 # %L (lower cased song name)
25 # httpq_command -setting was originally contributed by bd@bc-bd.org
26 #
27 # /SET httpq_trigger_ to configure the (very lame) public trigger
28 # /SET httpq_trigger_command has all the same expandos as httpq_command
29 # with some extra ones:
30 # %N (nick that triggered the script)
31 # %C (channel where the script was triggered)
32 #
33 # Known BUGS:
34 # WinAmp 2 plugin manager doesn't get the name of the song that's first in
35 # playlist, nothing that I can do about it.. But other than that, it should work
36 # just fine.
37 #
38 # Piotr Szymanski:
39 #
40 # When using httpq_v3 you'd always get error (httpq returns 0 after sending a
41 # '/getoutputtime', '/getplaylistpos', '/getplaylisttitle' and '/ getplaylistfile'
42 # because of wrong args used in previous version). I just added proper args for
43 # version 3 of httpq. Now it should work just fine.
44 #
45
46 use Socket;
47 use strict;
48
49 my($waport, $wahost, $wapass, $di);
50
51 sub getstat($$$) {
52 my ($host, $port, $data) = @_;
53 $data = "$data HTTP/1.0\r\n\r\n";
54
55 if (socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'))) {
56 if (connect(SOCK, sockaddr_in($port, inet_aton($host)))) {
57 unless (send(SOCK, $data, 0)) {
58 Irssi::print("Unable to write to the socket: $!", Irssi::MSGLEVEL_CLIENTERROR);
59 return;
60 }
61 } else {
62 Irssi::print("Unable to connect to the socket: $!", Irssi::MSGLEVEL_CLIENTERROR);
63 return;
64 }
65 } else {
66 Irssi::print("Connection to $host failed: $!", Irssi::MSGLEVEL_CLIENTERROR);
67 return;
68 }
69
70 # uncomment this for slow networks
71 # sleep(1);
72
73 my @lines = <SOCK>;
74 close(SOCK);
75 my $result = join("", @lines);
76 $result =~ s/^HTTP.*\n//g;
77 $result =~ s/^Server.*\n//g;
78 $result =~ s/^Content.*\n//g;
79 $result =~ s/^\r//g;
80 $result =~ s/[\r,\n]//g;
81 return $result;
82 }
83
84 sub check_version {
85 $wapass = Irssi::settings_get_str('httpq_password');
86 $wahost = Irssi::settings_get_str('httpq_host');
87 $waport = Irssi::settings_get_int('httpq_port');
88 my (undef,$version) = split(/x/, getstat($wahost, $waport, "GET /getversion?p=$wapass"));
89
90 if ($version >= 3000) {
91 return 1000000;
92 } else {
93 return 1;
94 }
95 }
96 sub parse_times ($$) {
97 my $emulatorbug = check_version();
98 my ($position, $length) = @_;
99 $position = sprintf("%02d", int(($position/1000/60))).":"
100 .sprintf("%02d", ($position/1000%60));
101 if ($length eq "-1") {
102 if ($di) {
103 $length = "/di.fm";
104 } else {
105 $length = "/netradio";
106 }
107 } elsif ($length eq "0") {
108 $length = "/sid";
109 } else {
110 $length = "/".sprintf("%02d", int(($length/$emulatorbug/60))).":"
111 .sprintf("%02d", ($length/$emulatorbug%60));
112 }
113 my $result = "$position$length";
114 return $result;
115 }
116
117 sub mkpercent ($$) {
118 my $emulatorbug = check_version();
119 my ($position, $length) = @_;
120 my $result;
121 my $numbers;
122 if ($length eq "-1" || $length eq "0") {
123 $result = "0";
124 } else {
125 $numbers = sprintf("%.0f",((($position/1000)/($length/$emulatorbug))*100));
126 $result = "(${numbers}%)";
127 }
128 if ($numbers>100 || $numbers<0) {
129 $result = "(wtf?)";
130 }
131 return $result;
132 }
133
134 sub vol_control($) {
135 $wapass = Irssi::settings_get_str('httpq_password');
136 $wahost = Irssi::settings_get_str('httpq_host');
137 $waport = Irssi::settings_get_int('httpq_port');
138
139 my ($direction) = @_;
140 if (!$direction) {
141 Irssi::print("Usage: /vol <up|down>");
142 return;
143 }
144
145 if ($direction eq "up") {
146 Irssi::print("Vol up..") if getstat($wahost, $waport, "GET /volumeup?p=$wapass");
147 } elsif ($direction eq "down") {
148 Irssi::print("Vol down..") if getstat($wahost, $waport, "GET /volumedown?p=$wapass");
149 }
150 }
151
152
153 # this function from bd@bc-bd.org
154 sub expand {
155 my ($string, %format) = @_;
156 my ($exp, $repl);
157 $string =~ s/%$exp/$repl/g while (($exp, $repl) = each(%format));
158 return $string;
159 }
160
161 sub main_wa {
162 $wapass = Irssi::settings_get_str('httpq_password');
163 $wahost = Irssi::settings_get_str('httpq_host');
164 $waport = Irssi::settings_get_int('httpq_port');
165 my $status = getstat($wahost, $waport, "GET /isplaying?p=$wapass");
166 if ($status eq "1") {
167 my $position = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=0"); #changed arg 'a' to 'frmt'/Pieta
168 my $length = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=1"); #changed arg 'a' to 'frmt'/Pieta
169 my $emulatorbug = check_version();
170 my $song = getstat($wahost, $waport, "GET /getplaylisttitle?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
171 .getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
172 my $file = getstat($wahost, $waport, "GET /getplaylistfile?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
173 .getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
174 if ($emulatorbug > 1 && getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass") == 0) {
175 $song = "WA2 plugin emulator error";
176 $file = "WA2 plugin emulator error";
177 }
178 # di.fm
179 if ($song =~ /\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)/i) {
180 $song =~ s/\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)//i;
181 $di = 1;
182 } else {
183 $di = 0;
184 }
185 my $lc = lc($song);
186 my $time = parse_times($position, $length);
187 my $percent = mkpercent($position, $length);
188 if (!$percent) {
189 $percent = '';
190 }
191 #my $final = "np: '$song' [$time$percent]";
192
193 my $final = expand(Irssi::settings_get_str("httpq_command"),
194 "S", $song,
195 "T", $time,
196 "P", $percent,
197 "F", $file,
198 "L", $lc);
199
200 Irssi::active_win()->command("$final");
201 } elsif ($status eq "3") {
202 Irssi::print("WinAmp is paused.");
203 } else {
204 Irssi::print("WinAmp isn't playing any song.");
205 }
206 }
207
208 sub trigger{
209 my($server,$text,$nick,$hostmask,$channel)=@_;
210 return unless Irssi::settings_get_bool('httpq_trigger_enabled');
211 my $trigger = Irssi::settings_get_str('httpq_trigger_string');
212 $trigger = "!lame" if !$trigger;
213 return unless ($text =~ /^$trigger$/);
214
215 # now just a copy and paste of most of the main_wa function
216 # this sucks, if you figure out a better way PLEASE send me a patch :)
217
218 $wapass = Irssi::settings_get_str('httpq_password');
219 $wahost = Irssi::settings_get_str('httpq_host');
220 $waport = Irssi::settings_get_int('httpq_port');
221 my $status = getstat($wahost, $waport, "GET /isplaying?p=$wapass");
222 if ($status eq "1") {
223 my $position = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=0"); #changed arg 'a' to 'frmt'/Pieta
224 my $length = getstat($wahost, $waport, "GET /getoutputtime?p=$wapass&frmt=1"); #changed arg 'a' to 'frmt'/Pieta
225 my $emulatorbug = check_version();
226 my $song = getstat($wahost, $waport, "GET /getplaylisttitle?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
227 .getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
228 my $file = getstat($wahost, $waport, "GET /getplaylistfile?p=$wapass&index=" #changed arg 'a' to 'index'/Pieta
229 .getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass"));
230 if ($emulatorbug > 1 && getstat($wahost, $waport, "GET /getplaylistpos?p=$wapass") == 0) {
231 $song = "WA2 plugin emulator error";
232 $file = "WA2 plugin emulator error";
233 }
234 # di.fm
235 if ($song =~ /\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)/i) {
236 $song =~ s/\s?\(D.?I.?G.?I.?T.?A.?L.?L.?Y.?-.?I.?M.?P.?O.?R.?T.?E.?D.*?\)//i;
237 $di = 1;
238 } else {
239 $di = 0;
240 }
241 my $lc = lc($song);
242 my $time = parse_times($position, $length);
243 my $percent = mkpercent($position, $length);
244 if (!$percent) {
245 $percent = '';
246 }
247
248 my $final = expand(Irssi::settings_get_str("httpq_trigger_command"),
249 "S", $song,
250 "T", $time,
251 "P", $percent,
252 "N", $nick,
253 "F", $file,
254 "C", $channel,
255 "L", $lc);
256
257 $server->command("$final");
258 }
259 }
260
261 #
262 # the statusbar item code used to be here, but i doubt anyone ever used it so
263 # i removed it completely. it was buggy and, imo, useless :)
264 #
265
266 Irssi::command_bind('wa', 'main_wa');
267 Irssi::command_bind('vol', 'vol_control');
268
269 Irssi::settings_add_str('misc', 'httpq_password', "password");
270 Irssi::settings_add_str('misc', 'httpq_host', "hostname");
271 Irssi::settings_add_int('misc', 'httpq_port', "4800");
272 Irssi::settings_add_str('misc', 'httpq_command', "say np: '%S' [%T %P]");
273
274 #trigger settings
275 Irssi::settings_add_bool('misc', 'httpq_trigger_enabled', 0);
276 Irssi::settings_add_str('misc', 'httpq_trigger_string', "!lame");
277 Irssi::settings_add_str('misc', 'httpq_trigger_command', "notice %N np: '%S' [%T %P]");
278 Irssi::signal_add_last("message public","trigger");
279
280 #EOF