| 1 | package Xmms::Streamcast; | ||
| 2 | |||
| 3 | use Term::ReadKey; | ||
| 4 | use Socket; | ||
| 5 | use POSIX; | ||
| 6 | use strict; | ||
| 7 | use File::Basename; | ||
| 8 | use MP3::Info; | ||
| 9 | use Symbol (); | ||
| 10 | use IO::Handle; | ||
| 11 | use Xmms::Config (); | ||
| 12 | |||
| 13 | { | ||
| 14 | no strict; | ||
| 15 | $VERSION = "0.09"; | ||
| 16 | } | ||
| 17 | |||
| 18 | if (caller eq 'Xmms') { | ||
| 19 | #Xmms::shell has pulled us in, so alias the commands | ||
| 20 | *Xmms::Cmd::skip = \&skip; | ||
| 21 | *Xmms::Cmd::request = \&request; | ||
| 22 | *Xmms::Cmd::requests = \&requests; | ||
| 23 | *Xmms::Cmd::requested = \&requests; | ||
| 24 | *Xmms::Cmd::stream = \&streaming; | ||
| 25 | *Xmms::Cmd::songs = \&songs; | ||
| 26 | *Xmms::Cmd::dug = \&dug; | ||
| 27 | *Xmms::Cmd::search = \&search; | ||
| 28 | *Xmms::Cmd::shost = \&shost; | ||
| 29 | *Xmms::Cmd::sport = \&sport; | ||
| 30 | *Xmms::Cmd::sserver = \&sserver; | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | my $pconfig = Xmms::Config->new(Xmms::Config->perlfile); | ||
| 35 | my $server = $pconfig->read(streamcast => 'streamhost'); | ||
| 36 | my $port = $pconfig->read(streamcast => 'streamport'); | ||
| 37 | |||
| 38 | my $VERSION = "0.09"; | ||
| 39 | print "Streamcast support $VERSION...available\n"; | ||
| 40 | print "Stream server.............$server:$port\n"; | ||
| 41 | |||
| 42 | my $DEBUG = 0; | ||
| 43 | sub shost ($;$$) { | ||
| 44 | |||
| 45 | my ($self, $args) = @_; | ||
| 46 | my $host = $args; | ||
| 47 | $pconfig->write(streamcast => 'streamhost', "$host"); | ||
| 48 | $pconfig->write_file(Xmms::Config->perlfile); | ||
| 49 | $server = $host; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub sport{ | ||
| 53 | |||
| 54 | my ($self, $args) = @_; | ||
| 55 | my $stream_port = $args; | ||
| 56 | $pconfig->write(streamcast => 'streamport', "$stream_port"); | ||
| 57 | $pconfig->write_file(Xmms::Config->perlfile); | ||
| 58 | $port = $stream_port; | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | sub sserver{ | ||
| 63 | |||
| 64 | print "$server:$port\n"; | ||
| 65 | |||
| 66 | } | ||
| 67 | sub skip { | ||
| 68 | |||
| 69 | my $fh = open_connection(); | ||
| 70 | my $string = send_commands($fh, 'skip current'); | ||
| 71 | close_connection($fh); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub streaming { | ||
| 75 | my $title; | ||
| 76 | my $mins; | ||
| 77 | my $secs; | ||
| 78 | my $song_id; | ||
| 79 | my $file; | ||
| 80 | my $base; | ||
| 81 | my $artist; | ||
| 82 | my $fh = open_connection(); | ||
| 83 | my $string = send_commands($fh, 'show current'); | ||
| 84 | close_connection($fh); | ||
| 85 | |||
| 86 | my @line = split(/\n/, $string); | ||
| 87 | foreach my $s (@line) { | ||
| 88 | if ($s =~ m/ID\: (\d+)/g){ | ||
| 89 | $song_id = $1; | ||
| 90 | # print "$song_id "; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ($s =~ m/FILENAME\: (.*)$/g){ | ||
| 94 | $file = $1; | ||
| 95 | $file = basename($file); | ||
| 96 | chop($file); | ||
| 97 | # print "$file"; | ||
| 98 | } | ||
| 99 | if ($s =~ m/MM\: (\d+)/g){ | ||
| 100 | $mins = $1; | ||
| 101 | } | ||
| 102 | |||
| 103 | if ($s =~ m/SS\: (\d+)/g){ | ||
| 104 | $secs= $1; | ||
| 105 | if ($secs < 10) { | ||
| 106 | $secs = "0$secs"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | if ($s =~ m/TITLE\: (.*)/g){ | ||
| 110 | $title = $1; | ||
| 111 | chop($title); | ||
| 112 | # print "TITLE: $title\n" | ||
| 113 | } | ||
| 114 | if ($s =~ m/ARTIST\: (.*)/g){ | ||
| 115 | $artist = $1; | ||
| 116 | chop($artist); | ||
| 117 | # print "ARTIST: $artist<\n"; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | if ($title) { | ||
| 121 | print "$song_id - $artist - $title ($mins:$secs)\n"; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | print "$song_id $file ($mins:$secs)\n"; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | sub dug ($;$$) { | ||
| 128 | |||
| 129 | my ($self, $args) = @_; | ||
| 130 | my $grep = $args; | ||
| 131 | my $all = 0; | ||
| 132 | my $fh = open_connection(); | ||
| 133 | my $string = send_commands($fh, 'show songs'); | ||
| 134 | close_connection($fh); | ||
| 135 | |||
| 136 | my @songs = split(/\n/, $string); | ||
| 137 | |||
| 138 | foreach my $s (@songs) { | ||
| 139 | my $in = ''; | ||
| 140 | if ($s =~ m/$grep/){ | ||
| 141 | if ($s =~ m/(\d+)/g) { | ||
| 142 | my $song_id = $1; | ||
| 143 | my $output = $s; | ||
| 144 | chop($output); | ||
| 145 | if($all == 0){ | ||
| 146 | print "$output (y/n/a/b)? "; | ||
| 147 | $in = ReadLine(); | ||
| 148 | chomp($in); | ||
| 149 | } | ||
| 150 | if ($in eq "y") { | ||
| 151 | \&request(" ",$song_id); | ||
| 152 | } | ||
| 153 | elsif ($in eq "b"){ | ||
| 154 | last; | ||
| 155 | } | ||
| 156 | elsif ($in eq "a" || $all != 0){ | ||
| 157 | $all = 1; | ||
| 158 | \&request(" ",$song_id); | ||
| 159 | } | ||
| 160 | |||
| 161 | else { | ||
| 162 | print "*not requested*\n"; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub songs { | ||
| 170 | |||
| 171 | my $fh = open_connection(); | ||
| 172 | my $string = send_commands($fh, 'show songs'); | ||
| 173 | close_connection($fh); | ||
| 174 | |||
| 175 | print {pager()} "$string"; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub request { | ||
| 179 | |||
| 180 | my ($self, $args) = @_; | ||
| 181 | my @nums = split(/\s/, $args); | ||
| 182 | foreach my $num (@nums) { | ||
| 183 | if($num == ' ') { | ||
| 184 | next; | ||
| 185 | } | ||
| 186 | my $fh = open_connection(); | ||
| 187 | my $string = send_commands($fh, "add request $num"); | ||
| 188 | print "song $num requested\n"; | ||
| 189 | close_connection($fh); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | sub requests { | ||
| 194 | |||
| 195 | my $nothing = 0; | ||
| 196 | my $fh = open_connection(); | ||
| 197 | my $string = send_commands($fh, 'show requests'); | ||
| 198 | close_connection($fh); | ||
| 199 | |||
| 200 | my @songs = split(/\n/, $string); | ||
| 201 | |||
| 202 | foreach my $s (@songs) { | ||
| 203 | if ($s =~ /.*\r$/) { | ||
| 204 | chop($s); | ||
| 205 | } | ||
| 206 | my ($id, $file); | ||
| 207 | |||
| 208 | if ($s =~ /^(\d+) (.*)$/) { | ||
| 209 | $id = $1; | ||
| 210 | $file = $2; | ||
| 211 | $file = basename($file); | ||
| 212 | print "$id $file\n"; | ||
| 213 | $nothing++; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | if (!$nothing){ | ||
| 218 | print "Zero requested songs. \n"; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | sub search ($;$$) { | ||
| 222 | |||
| 223 | my ($self, $args) = @_; | ||
| 224 | my $grep = $args; | ||
| 225 | my $all = 0; | ||
| 226 | my $fh = open_connection(); | ||
| 227 | my $string = send_commands($fh, 'show songs'); | ||
| 228 | close_connection($fh); | ||
| 229 | |||
| 230 | my @songs = split(/\n/, $string); | ||
| 231 | |||
| 232 | foreach my $s (@songs) { | ||
| 233 | my $in = ''; | ||
| 234 | if ($s =~ m/$grep/){ | ||
| 235 | if ($s =~ m/(\d+) (.*)$/g) { | ||
| 236 | my $song_id = $1; | ||
| 237 | my $string = $2; | ||
| 238 | my $output = $s; | ||
| 239 | chop($output); | ||
| 240 | print "$song_id $string\n"; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | sub open_connection { | ||
| 246 | |||
| 247 | my $localport = $port; | ||
| 248 | |||
| 249 | local *SOCK; | ||
| 250 | socket(SOCK, PF_INET, SOCK_STREAM, 0) || die server_down(); | ||
| 251 | my $ipaddr = inet_aton($server); | ||
| 252 | connect(SOCK, sockaddr_in($localport, $ipaddr)) || die server_down(); | ||
| 253 | print "Connection open\n" if $DEBUG; | ||
| 254 | select(SOCK); | ||
| 255 | $| = 1; | ||
| 256 | select(STDOUT); | ||
| 257 | |||
| 258 | return *SOCK; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | sub close_connection { | ||
| 264 | |||
| 265 | my $fh = shift; | ||
| 266 | |||
| 267 | my $string = ''; | ||
| 268 | print "quit sent\n" if $DEBUG; | ||
| 269 | print $fh "quit\r\n"; | ||
| 270 | while(defined (my $line = <$fh>)) { | ||
| 271 | $string .= $line; | ||
| 272 | } | ||
| 273 | |||
| 274 | print "Closing connection\n" if $DEBUG; | ||
| 275 | close($fh); | ||
| 276 | |||
| 277 | return $string; | ||
| 278 | } | ||
| 279 | sub send_commands { | ||
| 280 | |||
| 281 | my $fh = shift; | ||
| 282 | my $string = ''; | ||
| 283 | |||
| 284 | $string = read_to_prompt($fh); | ||
| 285 | if (@_) { | ||
| 286 | foreach my $cmd (@_) { | ||
| 287 | print $fh "$cmd\r\n"; | ||
| 288 | $string .= read_to_prompt($fh); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | return $string; | ||
| 293 | } | ||
| 294 | sub read_to_prompt { | ||
| 295 | |||
| 296 | my $fh = shift; | ||
| 297 | |||
| 298 | my $buff = ''; | ||
| 299 | my $line = ''; | ||
| 300 | my $string = ''; | ||
| 301 | my $read = 1; | ||
| 302 | while ($read) { | ||
| 303 | $buff = ''; | ||
| 304 | sysread($fh, $buff, 1); | ||
| 305 | print "Buff: ->$buff<-\n" if $DEBUG > 1; | ||
| 306 | if ($buff ne "\n") { | ||
| 307 | $line .= $buff; | ||
| 308 | if ($line =~ /^\>\s.*/) { | ||
| 309 | print "Prompt Found\n" if $DEBUG; | ||
| 310 | $read = 0; | ||
| 311 | } | ||
| 312 | } else { | ||
| 313 | $string .= $line; | ||
| 314 | $string .= "\n"; | ||
| 315 | print "Line: ->$line<-\n" if $DEBUG; | ||
| 316 | $line = ''; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | print "read_to_prompt: Returning string ->$string<-\n" if $DEBUG; | ||
| 321 | return $string; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub server_down { | ||
| 325 | |||
| 326 | print " radio network is down for the moment\n"; | ||
| 327 | return 1; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub pager { | ||
| 331 | my $fh = Symbol::gensym(); | ||
| 332 | |||
| 333 | if ($ENV{PAGER}) { | ||
| 334 | open $fh, "|$ENV{PAGER}"; | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | $fh = \*STDOUT; | ||
| 338 | } | ||
| 339 | |||
| 340 | return $fh; | ||
| 341 | } | ||
| 342 | |||
| 343 | 1; |