All pastes #248042 Raw Edit

Untitled

public text v1 · immutable
#248042 ·published 2006-11-14 03:13 UTC
rendered paste body
#!/usr/bin/perl -w
use strict;
use Win32::OLE;

my $version = "0.1.1";
my $info_string = "init";
chdir("$ENV{'APPDATA'}\\X-Chat 2\\");


sub main() {
  open(my $fh, "./i2x.conf") or Xchat::print "Couldn't find i2x.conf in your "
    . "X-Chat 2 folder: $ENV{'APPDATA'}\\X-Chat 2\\ "
    . "so I'll fall back on a default info_string.";
  my @lines = <$fh>;
  close($fh);

  foreach(@lines) {
    if($_ =~ m/^info_string (.+)$/) {
      $info_string = $1;
    }
  }

  if($info_string eq "init") {
    $info_string = "I'm listening to \"%b\" by %a";
  }
}


sub xcnowplaying {
  my $iTunes = Win32::OLE->new("iTunes.Application")
    or die "Failed to connect to iTunes\n";

  my $track = $iTunes->CurrentTrack;

  my $channel = Xchat::get_info("channel");
  my $server = Xchat::get_info("server");


  # Set each variable that will replace a % value below.
  my $a = $track->{'Artist'};
  my $b = $track->{'Name'};
  my $c = $track->{'TrackNumber'};
  my $d = $track->{'Album'};
  my $e = $iTunes->{'PlayerPosition'};
  my $f = $track->{'Time'};
  my $g = $track->{'Genre'};
  my $h = $track->{'Bitrate'};
  my $i = $track->{'PlayedCount'};
  my $j = $track->{'BPM'};
  my $k = $track->{'Comments'};
  my $l = $track->{'Composer'};
  my $m = $track->{'Duration'}; # This is only a temp value.
  my $n = $track->{'KindAsString'};
  my $o = $track->{'Rating'};
  my $p = $track->{'Size'};


  # Fix the values that need sorting.
  my $percent = 100/$m*$e;
  $m = "["; # Open the progress bar.
  while($percent > 9) { # Start the progress bar with played #.
    $m .= "#";
    $percent = $percent - 10;
  }

  while(length($m) != 11) { # Finish up the progress bar with unplayed -.
    $m .= "-";
  }

  $m .= "]"; # Don't forget to close the progress bar.


  my $minutes = 0;
  while($e > 59) {
    $minutes++;
    $e = $e-60;
    if(length($e) == 1) {
      $e = "0" . $e;
    } elsif (length($e) == 0) {
      $e = "00";
    }
  }

  $e = $minutes . ":" . $e;

  $p = $p/1024/1024;
  $p =~ s/^([\d]{1,2}.[\d]{2})\d*$/$1/; # Make the size a 2dp value.


  # Sort out the message replacing all the % values.
  my $msg = $info_string;
  $msg =~ s/%a/$a/;
  $msg =~ s/%b/$b/;
  $msg =~ s/%c/$c/;
  $msg =~ s/%d/$d/;
  $msg =~ s/%e/$e/;
  $msg =~ s/%f/$f/;
  $msg =~ s/%g/$g/;
  $msg =~ s/%h/$h/;
  $msg =~ s/%i/$i/;
  $msg =~ s/%j/$j/;
  $msg =~ s/%k/$k/;
  $msg =~ s/%l/$l/;
  $msg =~ s/%m/$m/;
  $msg =~ s/%n/$n/;
  $msg =~ s/%o/$o/;
  $msg =~ s/%p/$p/;

  if($msg =~ s/^\/me //i) {
    Xchat::command("ME $msg", $channel, $server);
  } else {
    Xchat::command("MSG $channel $msg", $channel, $server);
  }

  return Xchat::EAT_ALL;
}


sub xcplay {
  my $iTunes = Win32::OLE->new("iTunes.Application")
    or die "Failed to connect to iTunes\n";

  $iTunes->Play();

  return Xchat::EAT_ALL;
}


sub xcpause {
  my $iTunes = Win32::OLE->new("iTunes.Application")
    or die "Failed to connect to iTunes\n";

  $iTunes->Pause();

  return Xchat::EAT_ALL;
}


sub xcnext {
  my $iTunes = Win32::OLE->new("iTunes.Application")
    or die "Failed to connect to iTunes\n";

  $iTunes->NextTrack();

  return Xchat::EAT_ALL;
}


sub xcprev {
  my $iTunes = Win32::OLE->new("iTunes.Application")
    or die "Failed to connect to iTunes\n";

  $iTunes->PreviousTrack();

  return Xchat::EAT_ALL;
}


Xchat::register("i2x", "$version",
  "An XChat interface for iTunes.");

Xchat::print("Plugin i2x-$version loaded.");


Xchat::hook_command("np", "xcnowplaying");
Xchat::hook_command("itunes", "xcnowplaying");
Xchat::hook_command("song", "xcnowplaying");
Xchat::hook_command("play", "xcplay");
Xchat::hook_command("pause", "xcpause");
Xchat::hook_command("next", "xcnext");
Xchat::hook_command("prev", "xcprev");

&main;