use Purple;
use IO::Handle;
%PLUGIN_INFO=(perl_api_version => 2,
	      name => "Mythtv OSD Plugin",
	      version => "0.1",
	      summary => "MythTV OSD plugin for the Perl interpreter based on Festival Plugin.",
	      description => "Shows chat and signon info within MythTV.",
	      author => "Koen Erens <koen.erens\@gmail.com>",
	      url => "http://www.luckies.nl/",
	      load => "plugin_load",
	      unload => "plugin_unload");

sub plugin_init {
  Purple::Debug::info("plugin_init()", "MythTV OSD Plugin Initing.");
  printArray(@_);
  Purple::Debug::info("plugin_init()", "MythTV OSD Plugin Inited.");
  return %PLUGIN_INFO;
}

sub plugin_unload {
  Purple::Debug::info("plugin_unload()", "MythTV OSD Plugin Unloading.");
  printArray(@_);
  my $plugin = shift;
  Purple::Debug::info("plugin_unload()", "MythTV OSD Plugin Unloaded.");
}

sub speak_message {
  Purple::Debug::info("speak_message()", "MythTV OSD Plugin Speaking.");
  printArray(@_);
  # The handle and the user data come in as arguments
  my ($account, $buddy, $message,$conversation,$flags,$blank) = @_;
  printHash(%{$account});
  $buddy=Purple::Find::buddy($account,$buddy);
  $contact=Purple::BuddyList::Buddy::get_contact($buddy);
  $buddy=Purple::BuddyList::Contact::get_alias($contact);
  $message=stripHtml($message);
  $lastbuddy=$buddy;
  system("mythtvosd mythtvosd --template=alert --alert_text=\"Message from MSN: $buddy says: $message \"" );
  Purple::Debug::info("speak_message()", "MythTV OSD Plugin Spoke.");
}

sub sign_on {
 Purple::Debug::info("signon_message()", "MythTV OSD Plugin Speaking.");
 $account = $_[0];#useless
	$sender = $_[1];
	$text = $_[2];
	$flag = $_[3]; #not sure what this is
	#Various debug prints to show what has been passed in
	my $ref = shift;
	my $buddy = $ref->get_alias();
	my $status = shift;
system("mythtvosd mythtvosd --template=alert --alert_text=\"$buddy nu online \"" );
Purple::Debug::info("signon_message()", "MythTV OSD Plugin Spoke.");
}


sub plugin_load {
  Purple::Debug::info("plugin_load()", "MythTV OSD Plugin Loading.");
  printArray(@_);
  my $plugin = shift;
  printHash(%{$plugin});
  # User data to be given as an argument to the callback perl subroutine.
  $data = "";

  # A pointer to the actual plugin handle needed by the callback function
  $plugin_handle = Purple::Conversations::get_handle();
  $buddylist_handle = Purple::BuddyList::get_handle ();
  Purple::Signal::connect ($buddylist_handle, "buddy-signed-on",$plugin, \&sign_on, $data);
  Purple::Signal::connect($plugin_handle, "received-im-msg", $plugin, \&speak_message, $data);
  Purple::Signal::connect($plugin_handle, "received-chat-msg", $plugin, \&speak_message, $data);
  Purple::Debug::info("plugin_load()", "MythTV OSD Plugin Loaded.");
}
sub printArray{
  my $i=0;
  foreach $value(@_){
    Purple::Debug::info("array","$i: $value\n");
    $i++;
  }
}
sub printHash{
  %hash=@_;;
  foreach$key(keys %hash){
    $value=$hash{$key};
    Purple::Debug::info("hash","$key: $value\n");
  }
}
sub stripHtml{
  Purple::Debug::info("stripHtml()", "MythTV OSD Plugin Stripping.");
  $retval=$_[0];
  $retval=~s/<.*?>//g;
  Purple::Debug::info("stripHtml()", "MythTV OSD Plugin Stripped.");
  return $retval;
}
