# shamelessly copied from http://esaurito.net/blog/debian/irssi_notify.html
# Written by Philip Nelson <philip@whatsbeef.net>
# Change the following dir to where your Purple.pm is
use lib '/home/wabz/pidgin/lib/perl5/';

$MODULE_NAME = "Remote Notify";

use Purple;
use HTML::Strip;
use IO::Socket::INET;

%PLUGIN_INFO = (
	perl_api_version => 2,
	name => "Perl: $MODULE_NAME",
	version => "0.1",
	summary => "Uses libnotify to alert user of messages",
	description => "Uses libnotify to alert user of messages.",
	author => "philip\@whatsbeef.net",
	url => "http://whatsbeef.net/philip/finchnotify/",
	load => "plugin_load",
	unload => "plugin_unload"
);

my $hs = HTML::Strip->new();

# Conversations
sub conv_received_msg
{
	my ($account, $sender, $message, $conv, $flags, $data) = @_;
	$message = $hs->parse($message);
	$hs->eof;
	my $subject = 'Message from ' . $sender;
	$subject =~ s/\\/\\\\/g;
	$message =~ s/\\/\\\\/g;
	$message =~ s/\n/ /g;
	my $sock = new IO::Socket::INET(PeerAddr => '127.0.0.1', PeerPort => '12000', Proto => 'tcp');
	return if !$sock;
	print $sock "$subject\n$message\n\n";
	close $sock;
}

sub plugin_load
{
	my $plugin = shift;

	# Hook to the signals

	# Conversations
	$conv = Purple::Conversations::get_handle();
	Purple::Signal::connect($conv, "received-im-msg", $plugin,
					\&conv_received_msg, "received im message");
	Purple::Signal::connect($conv, "received-chat-msg", $plugin,
					\&conv_received_msg, "received chat message");
}

sub plugin_unload
{
	# Nothing to do here for this plugin.
}
