#!/usr/bin/perl

package icecast;

use strict;
use gweb; 

1;

sub init {
  my $self={}; bless $self;
  $self->{url}="myurl.com";
  $self->{port}=8000;
  $self->{user}="admin";
  $self->{password}="h4ckm3";
  $self->{mountpoint}="/test";
  return $self
}

sub stats {
  my ($self,$mountpoint)=@_;
  if (!$mountpoint) { $mountpoint=$self->{mountpoint} }
  $self->{stats}={ found=>0, validurl=>0, login=>0 };
  my $url="http://$self->{url}\:$self->{port}/admin/stats";
  my $req = gweb::website($url,"",$self->{user}.":".$self->{password});
  if ($req->{responsecode} ne '200') { return }
  $self->{stats}{login}=1;
  $self->{stats}{validurl}=1;  
  my $stats = $req->{content};
  my @mps = split(/\<source mount=\"/,$stats);
  shift @mps;
  foreach my $mpd (@mps) {
    if ($mpd !~ /[\n\r]/) {
      $mpd =~ s/\>\</\>\n\</g;
    }
    my @lines=split(/[\r\n]/,$mpd);
    my $mp=shift @lines;
    $mp =~ s/[^\/a-zA-Z]+//;
    if ($mp eq $mountpoint) {
      $self->{stats}{found}=1;
      foreach my $line (@lines) {
        $line =~ s/^[^\<]+//;
        if ($line =~ /^\<?(.+)?\>(.*)?\<\//) {
          $self->{stats}{$1}=$2;
        }
      }
    }
  }
}

sub info {
  my ($self,$mountpoint)=@_;
  if (!$mountpoint) { $mountpoint=$self->{mountpoint} }
  $self->{info}={ found=>0, validurl=>0, login=>0 };
  my $url="http://$self->{url}\:$self->{port}/admin/listclients.xsl?mount=$mountpoint";
  my $req = gweb::website($url,"",$self->{user}.":".$self->{password});
  if ($req->{responsecode} ne '200') { return }
  $self->{info}{validurl}=1;  
  my $stats = $req->{content};
  $self->{info}{login}=1;
  $self->{info}{found}=1;
  $stats =~ s/[\r\n]//g;

  $self->{info}{users}={};
  my @chunks=split(/\<tr[^>]*\>/,$stats);
  foreach my $chunk (@chunks) {
    if ($chunk =~ /killclient/) {
      my ($dat,$rest)=split(/\<\/tr\>/,$chunk);
      my @pl=split(/\<\/td\>/,$dat);
      my @res;
      foreach my $p (@pl) {
        my ($tag,$data) = split(/\>/,$p);
        push @res,$data;
      }
      my $ip=$res[0]; my $id;
      if ($res[3] =~ /id=([0-9]+)/) { $id=$1 }
      $self->{info}{users}{$ip}={ seconds => $res[1], agent => $res[2], id => $id };
    }
  }
}

sub printinfo {
  my ($self)=@_;
  print "Listeners $self->{url}\:$self->{port}$self->{mountpoint}\n";
  my $u=$self->{info}{users};
  foreach my $ip (sort keys %{$u}) {
    print join(" ",$ip,$u->{$ip}{seconds},$u->{$ip}{agent},$u->{$ip}{id}); print "\n"
  }
}

sub killsource {
  my ($self,$mountpoint) = @_;
  if (!$mountpoint) { $mountpoint=$self->{mountpoint} }
  my $url="http://$self->{url}\:$self->{port}/admin/killsource.xsl?mount=$mountpoint";
  my $req = gweb::website($url,"",$self->{user}.":".$self->{password});
  if ($req->{responsecode} ne '200') { return 0 }
  if ($req->{content} =~ /removed/i) { return 1 }
  return 0
}

sub killclient {
  my ($self,$mountpoint,$id) = @_;
  if (!$mountpoint) { $mountpoint=$self->{mountpoint} }
  my $url="http://$self->{url}\:$self->{port}/admin/killclient.xsl?mount=$mountpoint\&id=$id";
  my $req = gweb::website($url,"",$self->{user}.":".$self->{password});
  if ($req->{responsecode} ne '200') { return 0 }
  if ($req->{content} =~ /removed/i) { return 1 }
  return 0
}

