#!/usr/bin/env perl
# itch.pl v1.0.2
# Some Mac-specific functionality for irssi
# (cc) 2008 Filipp Lepalaan <filipp@mac.com>

use strict;
use vars qw($VERSION %IRSSI);

our $away_start = 0;
our $nick_orig = "";
our $nick_suffix = "|afk";
our $tmp_root = "http://unflyingobject.com/tmp";

use Irssi;

$VERSION = '1.00';

# Grab something from iTunes
sub get_itunes
{
	my ($prop) = @_;
	my $as = 'tell application "iTunes" to return ' . $prop;
	return `echo '$as' | osascript -`;
}

# Get rating of current track
sub track_rating
{
	my ($data, $server, $witem) = @_;
	return unless $witem;
	
	# Show track rating
	my $tr = get_itunes ("rating of current track") / 20;
	my $txt = "has given this track a $tr -star rating.";
	if ($tr < 1) { $txt = "has not rated this track yet."; }
	
	$witem->command ("me $txt");
	
}

sub way
{
	my ($data, $server, $witem) = @_;
	return unless $witem;
	
	
	my $nick = $server->{nick};
	my $reason = "for no reason";
	
	if ($data ne "" ) {$reason = $data}
	
	if ($away_start > 0) # Came back
	{
		my $td = time() - $away_start;
		my $dd = int ($td / 86400);
		my $hh = int ($td / 3600) - $dd * 24;
		my $mm = int ($td / 60) - $dd * 1440 - $hh * 60;
		my $ss = int ($td) - $dd * 86400 - $hh * 3600 -$mm * 60;
		
		if ($nick ne $nick_orig) { $witem->command ("nick $nick_orig"); }
		if ($server->{usermode_away}) {$witem->command ("away")};
		
		my $duration = sprintf ("%02u:\%02u:\%02u:\%02u", $dd, $hh, $mm, $ss);
		$witem->command ("me has returned (gone for $duration)");
		$witem->print ("check your away log with /lastlog");
		$witem->command ("set autolog OFF");
		
	} else { # Went away
		$witem->command ("me has gone away ($reason)");
		$nick_orig = $server->{nick};
		if ($nick eq $nick_orig) {
			$witem->command ("nick " . $nick_orig . $nick_suffix);
		}
		
		if ($server->{usermode_away}) { $witem->command ("away $reason") };
		$witem->command ("set autolog ON");
	
	}
	
	# Start or reset the clock
	$away_start = ($away_start == 0) ? time () : 0;
	
}

sub gestalt {
  my ($server, $data, $nick, $address, $target) = @_;
  $server = $server->{tag};
  
  my $g = `system_profiler SPHardwareDataType | awk '/Model Name/ || /Processor Speed/ || /Memory/' | sed 's/[^:]*://' | awk '{printf \$0}'`;
  
  my $server = Irssi::server_find_tag ($server);
  $server->command ("NOTICE $nick Gestalt: $g RAM");
  Irssi::signal_stop ();
  
}

sub np {
  my ($data, $server, $witem) = @_;
  return unless $witem;
  
  my $as = <<END;
on run args
	tell application "iTunes"
		if (args is "send") then
			return POSIX path of (location of current track as alias)
		end if
		set ct to the current track
		set ca to artist of ct
		if (ca is not "") then
			set ca to ca & " - "
		end if
		set ctit to name of ct
		set calb to album of ct
		if (calb is not "") then
			set cy to year of ct
			if (cy is not "" and cy > 0) then
				set calb to calb & ", " & cy
			end if
			set calb to " (" & calb & ")"
		end if
		return ca & ctit & calb
	end tell
end run
END
	my $np = `echo '$as' | osascript -`;
  $witem->command ("me : " . $np);

}

sub isay {
	my ($data, $server, $witem) = @_;
	return unless $witem;
	
	my @arr = split (/ /, $data);
	my $out = "";

	foreach (@arr) {
		s/^i//i;
		$out .= 'i' . ucfirst (lc ($_)) . ' ';
	}
	
	$witem->print ($out);

}

# Be persuasive
sub msay {
  my ($data, $server, $witem) = @_;
	return unless $witem;
	
	my @a = split (/ /, $data);
	my $t = shift (@a);
	
	$witem->print (join (" ", @a) x $t);
	
}

# Show play count of current track
sub pc {
	my ($data, $server, $witem) = @_;
	return unless $witem;	

	my $pc = get_itunes ("played count of current track");
	my $td = get_itunes ("((current date) - (date added of current track)) / days as integer");
	my $txt = "has heard this track $pc times ";
	
	if ($td > 1) { $txt .= "in the last $td days."; }
	if ($pc < 1) { $txt = "is listening to this track for the first time."; }
	
	$witem->command ("me $txt");
	
}

Irssi::command_bind nt => sub {
	# Play next track
	get_itunes ("next track");
};

Irssi::command_bind pt => sub {
	# Play previous track
	get_itunes ("previous track");
};

Irssi::command_bind ('np', 'np');
Irssi::command_bind ('pc', 'pc');
Irssi::command_bind ('way', 'way');
Irssi::command_bind ('isay', 'isay');
Irssi::command_bind ('msay', 'msay');
Irssi::command_bind ('itr', 'track_rating');

Irssi::signal_add_first ('ctcp msg gestalt', 'gestalt');
