use strict;
use IO::Handle;
use vars qw($VERSION %IRSSI);

use Irssi;
$VERSION = '0.0.1';
%IRSSI = (
	authors     => 'Jeroen Coekaerts, Koenraad Heijlen, Wulf Coulmann',
	contact     => 'grenouille@c-base.org',
	name        => 'm2l',
	description => 'write who is talking to you, on what IRC Network to a logfiel, so you can use this for xmpp -> osd messeges.',
	license     => 'BSD',
	url         => 'http://gpl.coulmann.de/remote-osd.html',
	changed     => '2008-11-09'
);

# based on osd.pl  
# url         => 'http://scripts.irssi.org/#osd.pl',



#--------------------------------------------------------------------
# Public Variables
#--------------------------------------------------------------------
my %myHELP = ();

#--------------------------------------------------------------------
# Settings
#--------------------------------------------------------------------

my $send_file = '/var/log/osd_msg';  # where we are logging.
my $regex = qr/(foo|bar)/;           # change foo bar to terms you whant to be notifyed about (beside messages with your nic as recipient)


#--------------------------------------------------------------------
# Help function
#--------------------------------------------------------------------
sub cmd_help { 
	my ($about) = @_;

	%myHELP = (
		osd_test => "
osd_test

Displays a small test message on screen
",
		m2l => "
M2L 

* your notification will be written to ".$send_file."
* you get info on ".$regex." (beside messages with your nic as recipient)
change this to your need in the script 

* osd_showactivechannel	(default: yes)
Currently the setting is: " . Irssi::settings_get_str('m2l_showactivechannel') . "


",


);

	if ( $about =~ /(osd_test|m2l)/i ) { 
		Irssi::print($myHELP{lc($1)});
	} 
}

#--------------------------------------------------------------------
# Irssi::Settings
#--------------------------------------------------------------------

Irssi::settings_add_str('M2L', 'm2l_showactivechannel', "yes");


#--------------------------------------------------------------------
# initialize the pipe, test it.
#--------------------------------------------------------------------

sub init {
	osdprint("M2L Loaded.");
}


#--------------------------------------------------------------------
# Private message parsing
#--------------------------------------------------------------------

sub priv_msg {
	my ($server,$msg,$nick,$address,$target) = @_;
        print LOG "privat";
	if ((Irssi::settings_get_str('m2l_showactivechannel') =~ /yes/) or
	   not (Irssi::active_win()->get_active_name() eq "$nick") ) {
                        $nick =~ s/\@.*//;
			osdprint($server->{chatnet}.":$nick -- $msg");
	}
}

#--------------------------------------------------------------------
# Public message parsing
#--------------------------------------------------------------------

sub pub_msg {
	my ($server,$msg,$nick,$address, $channel) = @_;
	my $show;
        print LOG "public";

	if (Irssi::settings_get_str('m2l_showactivechannel') =~ /yes/) {
		$show = 1;
	} elsif(uc(Irssi::active_win()->get_active_name()) eq uc($channel)) {
		$show = 0;
	}

	if ($show) {
	#if (0 == 0) {
		my $onick= quotemeta "$server->{nick}";
		my $pat ='(\:|\,|\s)'; # option...
		if($msg =~ /^$onick\s*$pat/i){
			osdprint("$channel".":$nick -- $msg");
		}elsif($msg =~ /$regex/i && not $channel =~ /bitlbee/ ){
			osdprint("you mentiond by $channel".":$nick -- $msg");
		}
	}
}

#--------------------------------------------------------------------
# The actual printing
#--------------------------------------------------------------------

sub osdprint {
	my ($text) = @_;
        $text =~ s/public#//;
        $text =~ s/\n/##/;
        $text =~ s/##.*$//;
        $text =~ s/'/`/g;
 
	open( MSG, '>>'.$send_file ) 
		or print "unable to open $send_file\n ";
        print MSG $text."\n"; 
        close MSG; 
}

#--------------------------------------------------------------------
# A test command.
#--------------------------------------------------------------------

sub cmd_osd_test {
	osdprint("Testing OSD");
}


#--------------------------------------------------------------------
# Irssi::signal_add_last / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::signal_add_last("message public", "pub_msg");
Irssi::signal_add_last("message private", "priv_msg");

Irssi::command_bind("osd_test","cmd_osd_test", "M2L");
Irssi::command_bind("help","cmd_help", "Irssi commands");

#--------------------------------------------------------------------
# The command that's executed at load time.
#--------------------------------------------------------------------

init();

#--------------------------------------------------------------------
# This text is printed at Load time.
#--------------------------------------------------------------------

Irssi::print("Use /help m2l for more information."); 


#- end

