All pastes #807494 Raw Edit

Untitled

public text v1 · immutable
·published 2007-12-06 19:23 UTC
rendered paste body
#!/usr/bin/perl -w

##################################################
# itunes.pl by igmir
# 


$script = "iTunes - Igmir";
$sversion=1.1;
$sdate = "04-22-2006";


IRC::register("iTunes -Igmir", "v. $sversion", "","");
IRC::print ("$script $sversion-($sdate) Help: /itunes help\n\n");


IRC::add_command_handler ("itunes", "itunes");
Xchat::hook_print("Channel Message", "notice_handler");



## USEFULL VARS
$active = 0;
$old_song="";
$auto_share=0;
$home = qx(osascript -e 'return system attribute "HOME"');
chomp($home);
	if(!(-e "$home/.xchat2/angel_script_itunes.conf")){
		open(FILE,">$home/.xchat2/angel_script_itunes.conf");
		print FILE "## config file for AnGeL & DeViL X-Chat script\n";
		print FILE "itunes_chans=";
		print FILE "\n";
		close FILE;
	}
		
		
# share song suboutines
sub share_it {
	my ($nick) = @_;
my $get_path = "tell application \"iTunes\" 
		set j to the location of current track
		set j to posix path of j
		end tell
        set j to \"".'\"'."\" & j & \"".'\"'."\" as string
        return j";
	my $file = `osascript -e \'$get_path\'`;
                chomp($file);
		$file = $nick ."\t". $file; 
		&auto_get_file($file);
	
}

sub auto_share
{
	if ($auto_share==0) 
	{
		$auto_share=1;
		IRC::print("Auto Share \002ON\017");
	}
	else 
	{
	$auto_share=0;
	IRC::print("Auto Share \002OFF\017");		
	}
sub notice_handler() {
		$col_nicks=Xchat::get_prefs("text_color_nicks");
		$m_nick = $_[0][0];
		if ($col_nicks==1){
		$m_nick = substr $m_nick, 3;
		}
		$m_line = $_[0][1];

	if($m_line =~ "!GETSONG")
	{
		if ($auto_share==1){
		&share_it($m_nick);
	}
	}
    } 	
    return 1;
}

sub auto_get_file()
{
 my ($file) = @_;
 @args=split('\t',$file);
 IRC::command("/DCC SEND ".$args[0]." $args[1]");
 return 1;
	
}
#iTunes manage display chan list
sub itunes_add_spam_chan {

	my $channel = shift;
	if (!($channel =~ m/^#/)) {
		$channel = "#".$channel;
	}
		open(FILE_OLD,"<$home/.xchat2/angel_script_itunes.conf");
		@filearray=<FILE_OLD>;
		close FILE_OLD;
		open(FILE_NEW,"+>$home/.xchat2/angel_script_itunes.conf");
		@filearray_new=<FILE_NEW>;
	for ($i=0;$i<=@filearray;$i++)
	{
		if ($filearray[$i] =~ m/^itunes_chans/)	{
			if (!($filearray[$i]=~ m/$channel/)) {
			
			$filearray[$i]=~ s/\n//i;
			$filearray_new[$i]=$filearray[$i] . $channel . ";";
		}
		else { $filearray_new[$i]=$filearray[$i]; }
		}
		else 
		{ $filearray_new[$i]=$filearray[$i];}

		print FILE_NEW $filearray_new[$i];
	}
	close FILE_NEW;
	IRC::print("\037Added\017 channel \002\00304$channel\017 to iTunes Spam List!");
	return 1;
	
}

sub itunes_del_spam_chan() {
	($data) = @_;
	IRC::print("Usage: /itunes del [#]<chan>\n"), return -1 if $data =~ /^\s*$/;
	{
	$channel = shift;
	if (!($channel =~ m/^#/)) {
		$channel = "#".$channel;
	}
		open(FILE_OLD,"<$home/.xchat2/angel_script_itunes.conf");
		@filearray=<FILE_OLD>;
		close FILE_OLD;
		IRC::print("\037Removed\017 channel \002\00304$channel\017 from iTunes Spam List!");
		open(FILE_NEW,"+>$home/.xchat2/angel_script_itunes.conf");
		@filearray_new=<FILE_NEW>;
	for ($i=0;$i<=@filearray;$i++)
	{
		if ($filearray[$i] =~ m/^itunes_chans/)	{
			$filearray[$i]=~ s/\n//i;
			$filearray[$i]=~ s/$channel;//i;
		}
		$filearray_new[$i]=$filearray[$i];

		print FILE_NEW $filearray_new[$i];
	}
	close FILE_NEW;
	return 1;
	}
}

sub itunes_list_spam_chan {
		open(FILE,"<$home/.xchat2/angel_script_itunes.conf");
		@filearray=<FILE>;
		close FILE;
	for ($i=0;$i<=@filearray;$i++)
	{
		if ($filearray[$i] =~ m/^itunes_chans/)	{
			$filearray[$i]=~ s/\n//i;
			$chanlist_tmp = substr($filearray[$i],13);
			@chanlist=split(';',$chanlist_tmp);
		}
		
	}
	$listchans=join(',',@chanlist);
		IRC::print("List of channels on which you will spam iTunes song:\n\n \002\00304$listchans\n\n");
	return 1;

}

#iTunes display

sub check_itunes_song {
	my $get_track = qq(tell application "iTunes"
        set this_name to the name of current track
        set this_artist to the artist of current track
	set this_length to the time of current track
	set this_album to the album of current track
end tell
return this_name & tab & this_artist & tab & this_length & tab & this_album);




$get_track = join ("' -e '",$get_track);


	$temp = qx(osascript -e '$get_track');
	$temp =~ s/\n//g;


	$temp =~ s/\tmissing value\t/\t\t/g;	
	$temp =~ s/\tmissing value$/\t/g;
	($track,$player,$duration,$album) = split(/\t/,$temp);

	if($player eq ""){
		
		$player = "Unknown Artist";
	}
	
	if($track =~ /^\s*$/){
		
			$track = "is listening to nothing at all";
	
	}
	if($album ne "") {
		$album = "$album";	
	
	}
		
			
			$line  = $player." - ".$track;
			#$line  = $player." - ".$track." - ".$album." ( ".$duration." )";
			$track = $line;
		
	
	&output($track);
}


sub output(){
	($current_song) = @_;
	
	$current_song =~ s/\t/ \- /g;
		open(FILE,"<$home/.xchat2/angel_script_itunes.conf");
		@filearray=<FILE>;
		close FILE;
	for ($i=0;$i<=@filearray;$i++)
	{
		if ($filearray[$i] =~ m/^itunes_chans/)	{
			$filearray[$i]=~ s/\n//i;
			$chanlist_tmp = substr($filearray[$i],13);
			@chanlist=split(';',$chanlist_tmp);
		}
		
	}

	if ($current_song ne $old_song) 
	{
		$old_song=$current_song;
		if($auto_share>0){
			$getit="type \002!GETSONG\017 to get current song!!";
		}
		if (@chanlist <= 0) {
		IRC::command("/ME  Playing: $current_song");
		}
		else
		{
	for ($i=0;$i<@chanlist;$i++){
		$channel=$chanlist[$i];
		 IRC::command_with_channel("/MSG $channel $current_song",$channel);
		}
	}
	}
	return 1;
}



sub itunes_show() {
	($play)=@_;
	if ($play =~ m/play/) {
		if ($active==0){
			$active++;
		$myhook =Xchat::hook_timer(5000, "check_itunes_song");
	} 
	}
	elsif ($play =~ m/stop/) {
		if ($active==1){
		Xchat::unhook($myhook);
		$old_song="";
	}
	}
	elsif ($play =~ m/show/) {
		$old_song="";
		&check_itunes_song;
	}
return 1;
}

sub update_itunes {
	system "curl http://www.vipsnet.net/xchat/mac/ad_iTunes.vers -o Plugins/ad_iTunes.vers";
	open(TEMP,"Plugins/ad_iTunes.vers");
	@temp_file=<TEMP>;
	close (TEMP);
	system "rm -f Plugins/ad_iTunes.vers";
	$getvers=$temp_file[0];
	if ($getvers > $sversion){
		IRC::print("There is a *\002\00304new\017* version of this script --> \00312auto upgrading\017... [ \00304$sversion\017 -> \00303$getvers\017 ]");
		system "curl http://www.vipsnet.net/xchat/mac/ad_iTunes.pl -o Plugins/ad_iTunes.pl";
		IRC::print("\n\n\00312RELOADING Script....\017");
		IRC::command("/unload Plugins/ad_iTunes.pl");
		IRC::command("/load Plugins/ad_iTunes.pl");
	}
	else
	{
		IRC::print("This script is *\00303up-to-date\017*\n\n");
	}
	return 1;
}

sub itunes
{
	$arg = shift(@_);
	for ($arg) {
	if (/help/) {
	IRC::print ("\n\n
		    \00304_______________________________________________________________________________________\017\n\n
		    /itunes help\00304..............\017to show this help
		    /itunes play/stop\00304.........\017to turn on/off itunes current song display
		    /itunes show\00304..............\017to display itunes current song like on play, but manually
		    /itunes send <nick>\00304.......\017to send current song to <nick>
		    /itunes autosend\00304..........\017to activate public !GETSONG on/off
		    /itunes reload\00304............\017to reload iTunes-Xchat plugin
		    /itunes update\00304............\017to update iTunes-Xchat plugin
		    /itunes add [#]<channel>\00304..\017to add a  channel to spam list
		    /itunes del [#]<channel>\00304..\017to del a chan from spam list
		    /itunes list\00304..............\017to show active spam chan list
		    \00304_______________________________________________________________________________________\017\n\n");		
		return 1;
	}
	elsif (/play/) {
		&itunes_show("play");
	return 1;
	}
	elsif (/stop/) {
		&itunes_show("stop");
	return 1;
	}	
	elsif (/show/) {
		&itunes_show("show");
	return 1;
	}
	elsif (/add/) {
		@args=split(' ',$arg);
		my ($data) = shift(@args);
		IRC::print("Usage: /itunes add [#]\00304\002<chan>\017\n"), return -1 if $data =~ /^\s*$/;
			{
				&itunes_add_spam_chan(@args);
			}
	return 1;
	}	
	elsif (/del/) {
		@args=split(' ',$arg);
		my ($data) = shift(@args);
		IRC::print("Usage: /itunes del [#]\00304\002<chan>\017\n"), return -1 if $data =~ /^\s*$/;
			{
				&itunes_del_spam_chan(@args);
			}
	return 1;
	}
	elsif (/list/) {
		&itunes_list_spam_chan;
	return 1;
	}
	elsif (/update/) {
		&update_itunes;
	return 1;
	}
	elsif (/autosend/) {
		&auto_share;
	return 1;
	}	
	elsif (/send/) {
		@args=split(' ',$arg);
		my ($data) = shift(@args);
		IRC::print("Usage: /itunes add [#]<chan>\n"), return -1 if $data =~ /^\s*$/;
			{
		&share_it(@args);
			}
	return 1;
	}
	elsif (/reload/) {
		IRC::print("\n\n\00312RELOADING Script....\017");
		IRC::command("/unload Plugins/ad_iTunes.pl");
		IRC::command("/load Plugins/ad_iTunes.pl");
	return 1;
	}	
	}
	return 1;
}
&update_itunes;